blob: 2aaf9babde3016ee0fdc4692b215c6a4cc9c9802 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020025#include <import/ebmbtree.h>
26
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010027#include <haproxy/buf-t.h>
28#include <haproxy/compat.h>
29#include <haproxy/api.h>
30#include <haproxy/debug.h>
31#include <haproxy/tools.h>
32#include <haproxy/ticks.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010033
34#include <haproxy/connection.h>
35#include <haproxy/fd.h>
36#include <haproxy/freq_ctr.h>
37#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010038#include <haproxy/h3.h>
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010039#include <haproxy/hq_interop.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010041#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010042#include <haproxy/pipe.h>
43#include <haproxy/proxy.h>
44#include <haproxy/quic_cc.h>
45#include <haproxy/quic_frame.h>
46#include <haproxy/quic_loss.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020047#include <haproxy/cbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010048#include <haproxy/quic_tls.h>
49#include <haproxy/ssl_sock.h>
50#include <haproxy/stream_interface.h>
51#include <haproxy/task.h>
52#include <haproxy/trace.h>
53#include <haproxy/xprt_quic.h>
54
Amaury Denoyellea22d8602021-11-10 15:17:56 +010055/* list of supported QUIC versions by this implementation */
56static int quic_supported_version[] = {
57 0x00000001,
58
59 /* placeholder, do not add entry after this */
60 0x0
61};
62
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020063/* This is the values of some QUIC transport parameters when absent.
64 * Should be used to initialize any transport parameters (local or remote)
65 * before updating them with customized values.
66 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010067struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020068 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020069 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
70 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020071 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010072};
73
74/* trace source and events */
75static void quic_trace(enum trace_level level, uint64_t mask, \
76 const struct trace_source *src,
77 const struct ist where, const struct ist func,
78 const void *a1, const void *a2, const void *a3, const void *a4);
79
80static const struct trace_event quic_trace_events[] = {
81 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
82 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
83 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
84 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
85 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
86 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
87 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050088 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010089 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
90 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
91 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
92 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
93 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
94 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
95 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
96 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
97 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
98 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
99 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
100 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
101 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
102 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
103 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
104 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
105 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
106 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
107 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100108 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
109 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
110 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
111 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
112 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
113 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100114 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200115 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
116 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100117 { /* end */ }
118};
119
120static const struct name_desc quic_trace_lockon_args[4] = {
121 /* arg1 */ { /* already used by the connection */ },
122 /* arg2 */ { .name="quic", .desc="QUIC transport" },
123 /* arg3 */ { },
124 /* arg4 */ { }
125};
126
127static const struct name_desc quic_trace_decoding[] = {
128#define QUIC_VERB_CLEAN 1
129 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
130 { /* end */ }
131};
132
133
134struct trace_source trace_quic = {
135 .name = IST("quic"),
136 .desc = "QUIC xprt",
137 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
138 .default_cb = quic_trace,
139 .known_events = quic_trace_events,
140 .lockon_args = quic_trace_lockon_args,
141 .decoding = quic_trace_decoding,
142 .report_events = ~0, /* report everything by default */
143};
144
145#define TRACE_SOURCE &trace_quic
146INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
147
148static BIO_METHOD *ha_quic_meth;
149
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200150DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200151DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100152DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100153DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200154 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100155DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100156DECLARE_POOL(pool_head_quic_connection_id,
157 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100158DECLARE_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 +0100159DECLARE_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 +0100160DECLARE_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 +0100161DECLARE_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 +0100162DECLARE_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 +0200163DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100164DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100165
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200166static 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 +0200167 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200168 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200169 int ack, int nb_pto_dgrams, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100170
171/* Add traces to <buf> depending on <frm> TX frame type. */
172static inline void chunk_tx_frm_appendf(struct buffer *buf,
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200173 const struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100174{
175 switch (frm->type) {
176 case QUIC_FT_CRYPTO:
177 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
178 (unsigned long long)frm->crypto.offset,
179 (unsigned long long)frm->crypto.len);
180 break;
181 default:
182 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
183 }
184}
185
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100186/* Only for debug purpose */
187struct enc_debug_info {
188 unsigned char *payload;
189 size_t payload_len;
190 unsigned char *aad;
191 size_t aad_len;
192 uint64_t pn;
193};
194
195/* Initializes a enc_debug_info struct (only for debug purpose) */
196static inline void enc_debug_info_init(struct enc_debug_info *edi,
197 unsigned char *payload, size_t payload_len,
198 unsigned char *aad, size_t aad_len, uint64_t pn)
199{
200 edi->payload = payload;
201 edi->payload_len = payload_len;
202 edi->aad = aad;
203 edi->aad_len = aad_len;
204 edi->pn = pn;
205}
206
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100207/* Trace callback for QUIC.
208 * These traces always expect that arg1, if non-null, is of type connection.
209 */
210static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
211 const struct ist where, const struct ist func,
212 const void *a1, const void *a2, const void *a3, const void *a4)
213{
214 const struct connection *conn = a1;
215
216 if (conn) {
217 struct quic_tls_secrets *secs;
218 struct quic_conn *qc;
219
220 qc = conn->qc;
221 chunk_appendf(&trace_buf, " : conn@%p", conn);
222 if ((mask & QUIC_EV_CONN_INIT) && qc) {
223 chunk_appendf(&trace_buf, "\n odcid");
224 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100225 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100226 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100227 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100228 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100229 }
230
231 if (mask & QUIC_EV_CONN_ADDDATA) {
232 const enum ssl_encryption_level_t *level = a2;
233 const size_t *len = a3;
234
235 if (level) {
236 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
237
238 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
239 }
240 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100241 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100242 }
243 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
244 /* Initial read & write secrets. */
245 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
246 const unsigned char *rx_sec = a2;
247 const unsigned char *tx_sec = a3;
248
249 secs = &qc->els[level].tls_ctx.rx;
250 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
251 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
252 if (rx_sec)
253 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
254 quic_tls_keys_hexdump(&trace_buf, secs);
255 }
256 secs = &qc->els[level].tls_ctx.tx;
257 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
258 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
259 if (tx_sec)
260 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
261 quic_tls_keys_hexdump(&trace_buf, secs);
262 }
263 }
264 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
265 const enum ssl_encryption_level_t *level = a2;
266 const unsigned char *secret = a3;
267 const size_t *secret_len = a4;
268
269 if (level) {
270 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
271
272 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
273 if (secret && secret_len)
274 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
275 secs = &qc->els[lvl].tls_ctx.rx;
276 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
277 quic_tls_keys_hexdump(&trace_buf, secs);
278 }
279 }
280
281 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
282 const enum ssl_encryption_level_t *level = a2;
283 const unsigned char *secret = a3;
284 const size_t *secret_len = a4;
285
286 if (level) {
287 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
288
289 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
290 if (secret && secret_len)
291 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
292 secs = &qc->els[lvl].tls_ctx.tx;
293 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
294 quic_tls_keys_hexdump(&trace_buf, secs);
295 }
296
297 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100298
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100299 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300 const struct quic_tx_packet *pkt = a2;
301 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100302 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100303
304 if (qel) {
305 struct quic_pktns *pktns;
306
307 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100308 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
309 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100310 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100311 (unsigned long long)qc->path->cwnd,
312 (unsigned long long)qc->path->prep_in_flight,
313 (unsigned long long)qc->path->in_flight,
314 (unsigned long long)pktns->tx.in_flight,
315 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100316 }
317 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200318 const struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100319 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
320 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
321 list_for_each_entry(frm, &pkt->frms, list)
322 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100323 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
324 }
325
326 if (room) {
327 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
328 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
329 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100330 }
331 }
332
333 if (mask & QUIC_EV_CONN_HDSHK) {
334 const enum quic_handshake_state *state = a2;
335 const int *err = a3;
336
337 if (state)
338 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
339 if (err)
340 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
341 }
342
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100343 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100344 const struct quic_rx_packet *pkt = a2;
345 const unsigned long *pktlen = a3;
346 const SSL *ssl = a4;
347
348 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100349 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100350 pkt, quic_packet_type_enc_level_char(pkt->type));
351 if (pkt->pnl)
352 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
353 (unsigned long long)pkt->pn);
354 if (pkt->token_len)
355 chunk_appendf(&trace_buf, " toklen=%llu",
356 (unsigned long long)pkt->token_len);
357 if (pkt->aad_len)
358 chunk_appendf(&trace_buf, " aadlen=%llu",
359 (unsigned long long)pkt->aad_len);
360 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
361 pkt->flags, (unsigned long long)pkt->len);
362 }
363 if (pktlen)
364 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
365 if (ssl) {
366 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
367 chunk_appendf(&trace_buf, " el=%c",
368 quic_enc_level_char(ssl_to_quic_enc_level(level)));
369 }
370 }
371
372 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
373 const struct quic_rx_packet *pkt = a2;
374 const struct quic_rx_crypto_frm *cf = a3;
375 const SSL *ssl = a4;
376
377 if (pkt)
378 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
379 quic_packet_type_enc_level_char(pkt->type),
380 (unsigned long long)pkt->pn);
381 if (cf)
382 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
383 (unsigned long long)cf->offset_node.key,
384 (unsigned long long)cf->len);
385 if (ssl) {
386 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200387 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100388 quic_enc_level_char(ssl_to_quic_enc_level(level)));
389 }
390 }
391
392 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
393 const struct quic_frame *frm = a2;
394
395 if (frm)
396 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
397 }
398
399 if (mask & QUIC_EV_CONN_PHPKTS) {
400 const struct quic_enc_level *qel = a2;
401
402 if (qel) {
403 struct quic_pktns *pktns;
404
405 pktns = qc->pktns;
406 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200407 " 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 +0100408 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200409 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille2766e782021-08-30 17:16:07 +0200410 !!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100411 (unsigned long long)qc->path->cwnd,
412 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100413 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100414 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
415 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100416 }
417 }
418
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100419 if (mask & QUIC_EV_CONN_ENCPKT) {
420 const struct enc_debug_info *edi = a2;
421
422 if (edi)
423 chunk_appendf(&trace_buf,
424 " payload=@%p payload_len=%llu"
425 " aad=@%p aad_len=%llu pn=%llu",
426 edi->payload, (unsigned long long)edi->payload_len,
427 edi->aad, (unsigned long long)edi->aad_len,
428 (unsigned long long)edi->pn);
429 }
430
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100431 if (mask & QUIC_EV_CONN_RMHP) {
432 const struct quic_rx_packet *pkt = a2;
433
434 if (pkt) {
435 const int *ret = a3;
436
437 chunk_appendf(&trace_buf, " pkt@%p", pkt);
438 if (ret && *ret)
439 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
440 pkt->pnl, (unsigned long long)pkt->pn);
441 }
442 }
443
444 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200445 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100446 const unsigned long *val1 = a3;
447 const unsigned long *val2 = a4;
448
449 if (frm)
450 chunk_tx_frm_appendf(&trace_buf, frm);
451 if (val1)
452 chunk_appendf(&trace_buf, " %lu", *val1);
453 if (val2)
454 chunk_appendf(&trace_buf, "..%lu", *val2);
455 }
456
457 if (mask & QUIC_EV_CONN_RTTUPDT) {
458 const unsigned int *rtt_sample = a2;
459 const unsigned int *ack_delay = a3;
460 const struct quic_loss *ql = a4;
461
462 if (rtt_sample)
463 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
464 if (ack_delay)
465 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
466 if (ql)
467 chunk_appendf(&trace_buf,
468 " srtt=%ums rttvar=%ums min_rtt=%ums",
469 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
470 }
471 if (mask & QUIC_EV_CONN_CC) {
472 const struct quic_cc_event *ev = a2;
473 const struct quic_cc *cc = a3;
474
475 if (a2)
476 quic_cc_event_trace(&trace_buf, ev);
477 if (a3)
478 quic_cc_state_trace(&trace_buf, cc);
479 }
480
481 if (mask & QUIC_EV_CONN_PKTLOSS) {
482 const struct quic_pktns *pktns = a2;
483 const struct list *lost_pkts = a3;
484 struct quic_conn *qc = conn->qc;
485
486 if (pktns) {
487 chunk_appendf(&trace_buf, " pktns=%s",
488 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
489 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
490 if (pktns->tx.loss_time)
491 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100492 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100493 }
494 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
495 struct quic_tx_packet *pkt;
496
497 chunk_appendf(&trace_buf, " lost_pkts:");
498 list_for_each_entry(pkt, lost_pkts, list)
499 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
500 }
501 }
502
503 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
504 struct quic_conn *qc = conn->qc;
505 const struct quic_pktns *pktns = a2;
506 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100507 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100508
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100509 if (ifae_pkts)
510 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
511 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100512 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100513 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100514 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100515 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
516 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100517 if (mask & QUIC_EV_CONN_STIMER) {
518 if (pktns->tx.loss_time)
519 chunk_appendf(&trace_buf, " loss_time=%dms",
520 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
521 }
522 if (mask & QUIC_EV_CONN_SPTO) {
523 if (pktns->tx.time_of_last_eliciting)
524 chunk_appendf(&trace_buf, " tole=%dms",
525 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
526 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100527 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100528 }
529 }
530
531 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
532 chunk_appendf(&trace_buf,
533 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
534 }
535 }
536
537 if (mask & QUIC_EV_CONN_SPPKTS) {
538 const struct quic_tx_packet *pkt = a2;
539
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100540 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
541 (unsigned long long)qc->path->cwnd,
542 (unsigned long long)qc->path->prep_in_flight,
543 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100544 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100545 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100546 (unsigned long)pkt->pn_node.key,
547 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
548 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100549 (unsigned long long)pkt->in_flight_len,
550 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100551 }
552 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100553
554 if (mask & QUIC_EV_CONN_SSLALERT) {
555 const uint8_t *alert = a2;
556 const enum ssl_encryption_level_t *level = a3;
557
558 if (alert)
559 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
560 if (level)
561 chunk_appendf(&trace_buf, " el=%c",
562 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
563 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100564
565 if (mask & QUIC_EV_CONN_BCFRMS) {
566 const size_t *sz1 = a2;
567 const size_t *sz2 = a3;
568 const size_t *sz3 = a4;
569
570 if (sz1)
571 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
572 if (sz2)
573 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
574 if (sz3)
575 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
576 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100577
578 if (mask & QUIC_EV_CONN_PSTRM) {
579 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100580
581 if (a2) {
582 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100583
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100584 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
585 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
586 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
587 (unsigned long long)s->id,
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200588 (unsigned long long)s->offset.key,
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100589 (unsigned long long)s->len);
590 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100591 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100592 }
593 if (mask & QUIC_EV_CONN_LPKT) {
594 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200595 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100596
597 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200598 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100599 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200600 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
601 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200602 if (len)
603 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100604 }
605
606}
607
608/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200609static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100610{
611 struct quic_conn *qc;
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200612 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613
614 qc = ctx->conn->qc;
615 if (objt_server(qc->conn->target))
616 return 1;
617
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200618 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
619 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
620 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
621 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200622 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100623 return 1;
624
625 return 0;
626}
627
628/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
629 * both loss detection and PTO and schedule the task assiated to this timer if needed.
630 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200631static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100632{
633 struct quic_conn *qc;
634 struct quic_pktns *pktns;
635 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200636 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100637
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100638 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
639 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100640 qc = ctx->conn->qc;
641 pktns = quic_loss_pktns(qc);
642 if (tick_isset(pktns->tx.loss_time)) {
643 qc->timer = pktns->tx.loss_time;
644 goto out;
645 }
646
647 /* XXX TODO: anti-amplification: the timer must be
648 * cancelled for a server which reached the anti-amplification limit.
649 */
650
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100651 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
652 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100653 /* Timer cancellation. */
654 qc->timer = TICK_ETERNITY;
655 goto out;
656 }
657
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200658 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
659 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100660 if (tick_isset(pto))
661 qc->timer = pto;
662 out:
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200663 if (qc->timer != TICK_ETERNITY)
664 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100665 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
666}
667
668#ifndef OPENSSL_IS_BORINGSSL
669int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
670 const uint8_t *read_secret,
671 const uint8_t *write_secret, size_t secret_len)
672{
673 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
674 struct quic_tls_ctx *tls_ctx =
675 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
676 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
677
678 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
679 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
680 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
681 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
682
683 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
684 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
685 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
686 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
687 read_secret, secret_len)) {
688 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
689 return 0;
690 }
691
692 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
693 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
694 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
695 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
696 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
697 write_secret, secret_len)) {
698 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
699 return 0;
700 }
701
702 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
703 if (objt_server(conn->target) && level == ssl_encryption_application) {
704 const unsigned char *buf;
705 size_t buflen;
706
707 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
708 if (!buflen)
709 return 0;
710
711 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
712 return 0;
713 }
714 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
715
716 return 1;
717}
718#else
719/* ->set_read_secret callback to derive the RX secrets at <level> encryption
720 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500721 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100722 */
723int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
724 const SSL_CIPHER *cipher,
725 const uint8_t *secret, size_t secret_len)
726{
727 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
728 struct quic_tls_ctx *tls_ctx =
729 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
730
731 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
732 tls_ctx->rx.aead = tls_aead(cipher);
733 tls_ctx->rx.md = tls_md(cipher);
734 tls_ctx->rx.hp = tls_hp(cipher);
735
736 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
737 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
738 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
739 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
740 secret, secret_len)) {
741 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
742 goto err;
743 }
744
745 if (objt_server(conn->target) && level == ssl_encryption_application) {
746 const unsigned char *buf;
747 size_t buflen;
748
749 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
750 if (!buflen)
751 goto err;
752
753 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
754 goto err;
755 }
756
757 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
758 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
759
760 return 1;
761
762 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100763 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100764 return 0;
765}
766
767/* ->set_write_secret callback to derive the TX secrets at <level>
768 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500769 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100770 */
771int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
772 const SSL_CIPHER *cipher,
773 const uint8_t *secret, size_t secret_len)
774{
775 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
776 struct quic_tls_ctx *tls_ctx =
777 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
778
779 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
780 tls_ctx->tx.aead = tls_aead(cipher);
781 tls_ctx->tx.md = tls_md(cipher);
782 tls_ctx->tx.hp = tls_hp(cipher);
783
784 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
785 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
786 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
787 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
788 secret, secret_len)) {
789 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
790 goto err;
791 }
792
793 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
794 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
795
796 return 1;
797
798 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100799 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100800 return 0;
801}
802#endif
803
804/* This function copies the CRYPTO data provided by the TLS stack found at <data>
805 * with <len> as size in CRYPTO buffers dedicated to store the information about
806 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
807 * It fails only if it could not managed to allocate enough CRYPTO buffers to
808 * store all the data.
809 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
810 */
811static int quic_crypto_data_cpy(struct quic_enc_level *qel,
812 const unsigned char *data, size_t len)
813{
814 struct quic_crypto_buf **qcb;
815 /* The remaining byte to store in CRYPTO buffers. */
816 size_t cf_offset, cf_len, *nb_buf;
817 unsigned char *pos;
818
819 nb_buf = &qel->tx.crypto.nb_buf;
820 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
821 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
822 cf_len = len;
823
824 while (len) {
825 size_t to_copy, room;
826
827 pos = (*qcb)->data + (*qcb)->sz;
828 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
829 to_copy = len > room ? room : len;
830 if (to_copy) {
831 memcpy(pos, data, to_copy);
832 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
833 qel->tx.crypto.sz += to_copy;
834 (*qcb)->sz += to_copy;
835 pos += to_copy;
836 len -= to_copy;
837 data += to_copy;
838 }
839 else {
840 struct quic_crypto_buf **tmp;
841
842 tmp = realloc(qel->tx.crypto.bufs,
843 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
844 if (tmp) {
845 qel->tx.crypto.bufs = tmp;
846 qcb = &qel->tx.crypto.bufs[*nb_buf];
847 *qcb = pool_alloc(pool_head_quic_crypto_buf);
848 if (!*qcb)
849 return 0;
850
851 (*qcb)->sz = 0;
852 ++*nb_buf;
853 }
854 else {
855 break;
856 }
857 }
858 }
859
860 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
861 * have been buffered.
862 */
863 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200864 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100865
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200866 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100867 if (!frm)
868 return 0;
869
870 frm->type = QUIC_FT_CRYPTO;
871 frm->crypto.offset = cf_offset;
872 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +0200873 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +0200874 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100875 }
876
877 return len == 0;
878}
879
880
881/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
882 * wants to provide the QUIC layer with CRYPTO data.
883 * Returns 1 if succeeded, 0 if not.
884 */
885int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
886 const uint8_t *data, size_t len)
887{
888 struct connection *conn;
889 enum quic_tls_enc_level tel;
890 struct quic_enc_level *qel;
891
892 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
893 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
894 tel = ssl_to_quic_enc_level(level);
895 qel = &conn->qc->els[tel];
896
897 if (tel == -1) {
898 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
899 goto err;
900 }
901
902 if (!quic_crypto_data_cpy(qel, data, len)) {
903 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
904 goto err;
905 }
906
907 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
908 conn, &level, &len);
909
910 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
911 return 1;
912
913 err:
914 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
915 return 0;
916}
917
918int ha_quic_flush_flight(SSL *ssl)
919{
920 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
921
922 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
923 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
924
925 return 1;
926}
927
928int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
929{
930 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
931
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100932 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100933 return 1;
934}
935
936/* QUIC TLS methods */
937static SSL_QUIC_METHOD ha_quic_method = {
938#ifdef OPENSSL_IS_BORINGSSL
939 .set_read_secret = ha_set_rsec,
940 .set_write_secret = ha_set_wsec,
941#else
942 .set_encryption_secrets = ha_quic_set_encryption_secrets,
943#endif
944 .add_handshake_data = ha_quic_add_handshake_data,
945 .flush_flight = ha_quic_flush_flight,
946 .send_alert = ha_quic_send_alert,
947};
948
949/* Initialize the TLS context of a listener with <bind_conf> as configuration.
950 * Returns an error count.
951 */
952int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
953{
954 struct proxy *curproxy = bind_conf->frontend;
955 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
956 int cfgerr = 0;
957
958#if 0
959 /* XXX Did not manage to use this. */
960 const char *ciphers =
961 "TLS_AES_128_GCM_SHA256:"
962 "TLS_AES_256_GCM_SHA384:"
963 "TLS_CHACHA20_POLY1305_SHA256:"
964 "TLS_AES_128_CCM_SHA256";
965#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200966 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100967 long options =
968 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
969 SSL_OP_SINGLE_ECDH_USE |
970 SSL_OP_CIPHER_SERVER_PREFERENCE;
971 SSL_CTX *ctx;
972
973 ctx = SSL_CTX_new(TLS_server_method());
974 bind_conf->initial_ctx = ctx;
975
976 SSL_CTX_set_options(ctx, options);
977#if 0
978 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
979 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
980 "for bind '%s' at [%s:%d].\n",
981 curproxy->id, ciphers,
982 bind_conf->arg, bind_conf->file, bind_conf->line);
983 cfgerr++;
984 }
985#endif
986
987 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
988 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
989 "for bind '%s' at [%s:%d].\n",
990 curproxy->id, groups,
991 bind_conf->arg, bind_conf->file, bind_conf->line);
992 cfgerr++;
993 }
994
995 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
996 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
997 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
998 SSL_CTX_set_default_verify_paths(ctx);
999
1000#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1001#ifdef OPENSSL_IS_BORINGSSL
1002 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1003 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1004#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1005 if (bind_conf->ssl_conf.early_data) {
1006 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
1007 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
1008 }
1009 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1010 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1011#else
1012 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1013#endif
1014 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1015#endif
1016 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1017
1018 return cfgerr;
1019}
1020
1021/* Decode an expected packet number from <truncated_on> its truncated value,
1022 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1023 * the number of bits used to encode this packet number (its length in bytes * 8).
1024 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1025 */
1026static uint64_t decode_packet_number(uint64_t largest_pn,
1027 uint32_t truncated_pn, unsigned int pn_nbits)
1028{
1029 uint64_t expected_pn = largest_pn + 1;
1030 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1031 uint64_t pn_hwin = pn_win / 2;
1032 uint64_t pn_mask = pn_win - 1;
1033 uint64_t candidate_pn;
1034
1035
1036 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1037 /* Note that <pn_win> > <pn_hwin>. */
1038 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1039 candidate_pn + pn_hwin <= expected_pn)
1040 return candidate_pn + pn_win;
1041
1042 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1043 return candidate_pn - pn_win;
1044
1045 return candidate_pn;
1046}
1047
1048/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1049 * cryptographic context.
1050 * <largest_pn> is the largest received packet number and <pn> the address of
1051 * the packet number field for this packet with <byte0> address of its first byte.
1052 * <end> points to one byte past the end of this packet.
1053 * Returns 1 if succeeded, 0 if not.
1054 */
1055static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1056 int64_t largest_pn, unsigned char *pn,
1057 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001058 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001059{
1060 int ret, outlen, i, pnlen;
1061 uint64_t packet_number;
1062 uint32_t truncated_pn = 0;
1063 unsigned char mask[5] = {0};
1064 unsigned char *sample;
1065 EVP_CIPHER_CTX *cctx;
1066 unsigned char *hp_key;
1067
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001068 /* Check there is enough data in this packet. */
1069 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1070 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1071 return 0;
1072 }
1073
1074 cctx = EVP_CIPHER_CTX_new();
1075 if (!cctx) {
1076 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1077 return 0;
1078 }
1079
1080 ret = 0;
1081 sample = pn + QUIC_PACKET_PN_MAXLEN;
1082
1083 hp_key = tls_ctx->rx.hp_key;
1084 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1085 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1086 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1087 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1088 goto out;
1089 }
1090
1091 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1092 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1093 for (i = 0; i < pnlen; i++) {
1094 pn[i] ^= mask[i + 1];
1095 truncated_pn = (truncated_pn << 8) | pn[i];
1096 }
1097
1098 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1099 /* Store remaining information for this unprotected header */
1100 pkt->pn = packet_number;
1101 pkt->pnl = pnlen;
1102
1103 ret = 1;
1104
1105 out:
1106 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001107
1108 return ret;
1109}
1110
1111/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1112 * address, with <payload_len> as payload length, <aad> as address of
1113 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1114 * context.
1115 * Returns 1 if succeeded, 0 if not.
1116 */
1117static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1118 unsigned char *aad, size_t aad_len, uint64_t pn,
1119 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1120{
1121 unsigned char iv[12];
1122 unsigned char *tx_iv = tls_ctx->tx.iv;
1123 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001124 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001125
1126 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1127 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001128 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001129 }
1130
1131 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1132 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1133 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001134 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001135 }
1136
1137 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001138
1139 err:
1140 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1141 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1142 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001143}
1144
1145/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1146 * Returns 1 if succeeded, 0 if not.
1147 */
1148static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1149{
1150 int ret;
1151 unsigned char iv[12];
1152 unsigned char *rx_iv = tls_ctx->rx.iv;
1153 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1154
1155 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1156 return 0;
1157
1158 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1159 pkt->data, pkt->aad_len,
1160 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1161 if (!ret)
1162 return 0;
1163
1164 /* Update the packet length (required to parse the frames). */
1165 pkt->len = pkt->aad_len + ret;
1166
1167 return 1;
1168}
1169
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001170/* Remove from <qcs> stream the acknowledged frames.
1171 * Never fails.
1172 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001173static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001174{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001175 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001176 struct eb64_node *frm_node;
1177
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001178 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001179 frm_node = eb64_first(&qcs->tx.acked_frms);
1180 while (frm_node) {
1181 struct quic_stream *strm;
1182
1183 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1184 if (strm->offset.key != qcs->tx.ack_offset)
1185 break;
1186
1187 b_del(strm->buf, strm->len);
1188 qcs->tx.ack_offset += strm->len;
1189 frm_node = eb64_next(frm_node);
1190 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001191 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001192 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001193
1194 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001195}
1196
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001197/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001198static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001199 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001200{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001201 int stream_acked;
1202 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001203
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001204 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001205 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001206 switch (frm->type) {
1207 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1208 {
1209 struct qcs *qcs = frm->stream.qcs;
1210 struct quic_stream *strm = &frm->stream;
1211
1212 if (qcs->tx.ack_offset == strm->offset.key) {
1213 b_del(strm->buf, strm->len);
1214 qcs->tx.ack_offset += strm->len;
1215 LIST_DELETE(&frm->list);
1216 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001217 qc->qcc->flags &= ~QC_CF_MUX_MFULL;
1218 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001219 }
1220 else {
1221 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1222 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001223 stream_acked |= qcs_try_to_consume(qcs);
Amaury Denoyellecae07912021-10-08 17:57:41 +02001224
1225 if (qcs->flags & QC_SF_DETACH)
1226 tasklet_wakeup(qcs->shut_tl);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001227 }
1228 break;
1229 default:
1230 LIST_DELETE(&frm->list);
1231 pool_free(pool_head_quic_frame, frm);
1232 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001233
1234 if (stream_acked) {
1235 struct qcc *qcc = qc->qcc;
1236
1237 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1238 tasklet_wakeup(qcc->subs->tasklet);
1239 qcc->subs->events &= ~SUB_RETRY_SEND;
1240 if (!qcc->subs->events)
1241 qcc->subs = NULL;
1242 }
1243 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001244}
1245
1246/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1247 * deallocating them, and their TX frames.
1248 * Returns the last node reached to be used for the next range.
1249 * May be NULL if <largest> node could not be found.
1250 */
1251static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1252 struct list *newly_acked_pkts,
1253 struct eb64_node *largest_node,
1254 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001255 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001256{
1257 struct eb64_node *node;
1258 struct quic_tx_packet *pkt;
1259
1260 if (largest_node)
1261 node = largest_node;
1262 else {
1263 node = eb64_lookup(pkts, largest);
1264 while (!node && largest > smallest) {
1265 node = eb64_lookup(pkts, --largest);
1266 }
1267 }
1268
1269 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001270 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001271
1272 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1273 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001274 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001275 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1276 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1277 qc_treat_acked_tx_frm(frm, ctx);
1278 node = eb64_prev(node);
1279 eb64_delete(&pkt->pn_node);
1280 }
1281
1282 return node;
1283}
1284
1285/* Treat <frm> frame whose packet it is attached to has just been detected as non
1286 * acknowledged.
1287 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001288static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001289 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001290 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001291{
1292 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001293 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001294 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001295}
1296
1297
1298/* Free the TX packets of <pkts> list */
1299static inline void free_quic_tx_pkts(struct list *pkts)
1300{
1301 struct quic_tx_packet *pkt, *tmp;
1302
1303 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001304 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001305 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001306 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001307 }
1308}
1309
1310/* Send a packet loss event nofification to the congestion controller
1311 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1312 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1313 * at <now_us> current time.
1314 * Always succeeds.
1315 */
1316static inline void qc_cc_loss_event(struct quic_conn *qc,
1317 unsigned int lost_bytes,
1318 unsigned int newest_time_sent,
1319 unsigned int period,
1320 unsigned int now_us)
1321{
1322 struct quic_cc_event ev = {
1323 .type = QUIC_CC_EVT_LOSS,
1324 .loss.now_ms = now_ms,
1325 .loss.max_ack_delay = qc->max_ack_delay,
1326 .loss.lost_bytes = lost_bytes,
1327 .loss.newest_time_sent = newest_time_sent,
1328 .loss.period = period,
1329 };
1330
1331 quic_cc_event(&qc->path->cc, &ev);
1332}
1333
1334/* Send a packet ack event nofication for each newly acked packet of
1335 * <newly_acked_pkts> list and free them.
1336 * Always succeeds.
1337 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001338static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001339 struct list *newly_acked_pkts)
1340{
1341 struct quic_conn *qc = ctx->conn->qc;
1342 struct quic_tx_packet *pkt, *tmp;
1343 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1344
1345 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1346 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001347 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001348 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001349 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001350 ev.ack.acked = pkt->in_flight_len;
1351 ev.ack.time_sent = pkt->time_sent;
1352 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001353 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001354 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001355 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001356 }
1357
1358}
1359
1360/* Handle <pkts> list of lost packets detected at <now_us> handling
1361 * their TX frames.
1362 * Send a packet loss event to the congestion controller if
1363 * in flight packet have been lost.
1364 * Also frees the packet in <pkts> list.
1365 * Never fails.
1366 */
1367static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001368 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001369 struct list *pkts,
1370 uint64_t now_us)
1371{
1372 struct quic_conn *qc = ctx->conn->qc;
1373 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001374 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001375 uint64_t lost_bytes;
1376
1377 lost_bytes = 0;
1378 oldest_lost = newest_lost = NULL;
1379 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1380 lost_bytes += pkt->in_flight_len;
1381 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001382 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001383 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001384 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001385 /* Treat the frames of this lost packet. */
1386 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1387 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001388 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001389 if (!oldest_lost) {
1390 oldest_lost = newest_lost = pkt;
1391 }
1392 else {
1393 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001394 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001395 newest_lost = pkt;
1396 }
1397 }
1398
1399 if (lost_bytes) {
1400 /* Sent a packet loss event to the congestion controller. */
1401 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1402 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001403 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001404 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001405 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001406 }
1407}
1408
1409/* Look for packet loss from sent packets for <qel> encryption level of a
1410 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1411 * their tree if deemed as lost or set the <loss_time> value the packet number
1412 * space if any not deemed lost.
1413 * Should be called after having received an ACK frame with newly acknowledged
1414 * packets or when the the loss detection timer has expired.
1415 * Always succeeds.
1416 */
1417static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1418 struct quic_conn *qc,
1419 struct list *lost_pkts)
1420{
1421 struct eb_root *pkts;
1422 struct eb64_node *node;
1423 struct quic_loss *ql;
1424 unsigned int loss_delay;
1425
1426 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1427 pkts = &pktns->tx.pkts;
1428 pktns->tx.loss_time = TICK_ETERNITY;
1429 if (eb_is_empty(pkts))
1430 goto out;
1431
1432 ql = &qc->path->loss;
1433 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1434 loss_delay += loss_delay >> 3;
1435 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1436
1437 node = eb64_first(pkts);
1438 while (node) {
1439 struct quic_tx_packet *pkt;
1440 int64_t largest_acked_pn;
1441 unsigned int loss_time_limit, time_sent;
1442
1443 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001444 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001445 node = eb64_next(node);
1446 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1447 break;
1448
1449 time_sent = pkt->time_sent;
1450 loss_time_limit = tick_add(time_sent, loss_delay);
1451 if (tick_is_le(time_sent, now_ms) ||
1452 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1453 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001454 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001455 }
1456 else {
1457 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1458 }
1459 }
1460
1461 out:
1462 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1463}
1464
1465/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1466 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001467 * 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 +01001468 * acked ack-eliciting packet.
1469 * Return 1, if succeeded, 0 if not.
1470 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001471static 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 +01001472 struct quic_enc_level *qel,
1473 unsigned int *rtt_sample,
1474 const unsigned char **pos, const unsigned char *end)
1475{
1476 struct quic_ack *ack = &frm->ack;
1477 uint64_t smallest, largest;
1478 struct eb_root *pkts;
1479 struct eb64_node *largest_node;
1480 unsigned int time_sent, pkt_flags;
1481 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1482 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1483
1484 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1485 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1486 ctx->conn,, &ack->largest_ack);
1487 goto err;
1488 }
1489
1490 if (ack->first_ack_range > ack->largest_ack) {
1491 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1492 ctx->conn,, &ack->first_ack_range);
1493 goto err;
1494 }
1495
1496 largest = ack->largest_ack;
1497 smallest = largest - ack->first_ack_range;
1498 pkts = &qel->pktns->tx.pkts;
1499 pkt_flags = 0;
1500 largest_node = NULL;
1501 time_sent = 0;
1502
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001503 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 +01001504 largest_node = eb64_lookup(pkts, largest);
1505 if (!largest_node) {
1506 TRACE_DEVEL("Largest acked packet not found",
1507 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1508 goto err;
1509 }
1510
1511 time_sent = eb64_entry(&largest_node->node,
1512 struct quic_tx_packet, pn_node)->time_sent;
1513 }
1514
1515 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1516 ctx->conn,, &largest, &smallest);
1517 do {
1518 uint64_t gap, ack_range;
1519
1520 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1521 largest_node, largest, smallest, ctx);
1522 if (!ack->ack_range_num--)
1523 break;
1524
1525 if (!quic_dec_int(&gap, pos, end))
1526 goto err;
1527
1528 if (smallest < gap + 2) {
1529 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1530 ctx->conn,, &gap, &smallest);
1531 goto err;
1532 }
1533
1534 largest = smallest - gap - 2;
1535 if (!quic_dec_int(&ack_range, pos, end))
1536 goto err;
1537
1538 if (largest < ack_range) {
1539 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1540 ctx->conn,, &largest, &ack_range);
1541 goto err;
1542 }
1543
1544 /* Do not use this node anymore. */
1545 largest_node = NULL;
1546 /* Next range */
1547 smallest = largest - ack_range;
1548
1549 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1550 ctx->conn,, &largest, &smallest);
1551 } while (1);
1552
1553 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001554 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001555
1556 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1557 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001558 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001559 }
1560
1561 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1562 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1563 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1564 if (!LIST_ISEMPTY(&lost_pkts))
1565 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1566 }
1567 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1568 if (quic_peer_validated_addr(ctx))
1569 ctx->conn->qc->path->loss.pto_count = 0;
1570 qc_set_timer(ctx);
1571 }
1572
1573
1574 return 1;
1575
1576 err:
1577 free_quic_tx_pkts(&newly_acked_pkts);
1578 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1579 return 0;
1580}
1581
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001582/* This function gives the detail of the SSL error. It is used only
1583 * if the debug mode and the verbose mode are activated. It dump all
1584 * the SSL error until the stack was empty.
1585 */
1586static forceinline void qc_ssl_dump_errors(struct connection *conn)
1587{
1588 if (unlikely(global.mode & MODE_DEBUG)) {
1589 while (1) {
1590 unsigned long ret;
1591
1592 ret = ERR_get_error();
1593 if (!ret)
1594 return;
1595
1596 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1597 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1598 }
1599 }
1600}
1601
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001602int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1603 const char **str, int *len);
1604
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001605/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1606 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001607 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001608 * Return 1 if succeeded, 0 if not.
1609 */
1610static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001611 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001612 const unsigned char *data, size_t len,
1613 struct quic_rx_packet *pkt,
1614 struct quic_rx_crypto_frm *cf)
1615{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001616 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001617 struct quic_conn *qc;
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001618 const char *alpn;
1619 int alpn_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001620
1621 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1622 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001623 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001624 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1625 TRACE_PROTO("SSL_provide_quic_data() error",
1626 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1627 goto err;
1628 }
1629
1630 el->rx.crypto.offset += len;
1631 TRACE_PROTO("in order CRYPTO data",
1632 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1633
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001634 state = HA_ATOMIC_LOAD(&qc->state);
1635 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001636 ssl_err = SSL_do_handshake(ctx->ssl);
1637 if (ssl_err != 1) {
1638 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1639 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1640 TRACE_PROTO("SSL handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001641 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001642 goto out;
1643 }
1644
1645 TRACE_DEVEL("SSL handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001646 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001647 qc_ssl_dump_errors(ctx->conn);
1648 BUG_ON(1);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001649 goto err;
1650 }
1651
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001652 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001653 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001654 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001655 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001656 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001657 } else {
1658 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1659 if (ssl_err != 1) {
1660 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1661 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1662 TRACE_DEVEL("SSL post handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001663 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001664 goto out;
1665 }
1666
1667 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001668 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001669 goto err;
1670 }
1671
1672 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001673 QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001674 }
1675
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001676 conn_get_alpn(ctx->conn, &alpn, &alpn_len);
1677 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) {
1678 qc->qcc->app_ops = &h3_ops;
1679 if (!qc->qcc->app_ops->init(qc->qcc))
1680 goto err;
1681 }
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +01001682 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) {
1683 qc->qcc->app_ops = &hq_interop_ops;
1684 }
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001685 else {
1686 /* TODO RFC9001 8.1. Protocol Negotiation
1687 * must return no_application_protocol TLS alert
1688 */
1689 ABORT_NOW();
1690 }
1691
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001692 out:
1693 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1694 return 1;
1695
1696 err:
1697 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1698 return 0;
1699}
1700
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001701/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1702 * <pkt> RX packet.
1703 * Return it if succeeded, NULL if not.
1704 */
1705static inline
1706struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1707 struct quic_rx_packet *pkt)
1708{
1709 struct quic_rx_strm_frm *frm;
1710
1711 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1712 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001713 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001714 frm->len = stream_frm->len;
1715 frm->data = stream_frm->data;
1716 frm->pkt = pkt;
1717 }
1718
1719 return frm;
1720}
1721
1722/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05001723 * several streams, depending on the already open ones.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001724 * Return this node if succeeded, NULL if not.
1725 */
1726static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1727{
1728 unsigned int strm_type;
1729 int64_t sub_id;
1730 struct eb64_node *strm_node;
1731
1732 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1733
1734 strm_type = id & QCS_ID_TYPE_MASK;
1735 sub_id = id >> QCS_ID_TYPE_SHIFT;
1736 strm_node = NULL;
1737 if (qc_local_stream_id(qcc, id)) {
1738 /* Local streams: this stream must be already opened. */
1739 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1740 if (!strm_node) {
1741 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1742 goto out;
1743 }
1744 }
1745 else {
1746 /* Remote streams. */
1747 struct eb_root *strms;
1748 uint64_t largest_id;
1749 enum qcs_type qcs_type;
1750
1751 strms = &qcc->streams_by_id;
1752 qcs_type = qcs_id_type(id);
1753 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1754 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1755 goto out;
1756 }
1757
1758 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1759 * correct value.
1760 */
1761 largest_id = qcc->strms[qcs_type].largest_id;
1762 if (sub_id > (int64_t)largest_id) {
1763 /* RFC: "A stream ID that is used out of order results in all streams
1764 * of that type with lower-numbered stream IDs also being opened".
1765 * So, let's "open" these streams.
1766 */
1767 int64_t i;
1768 struct qcs *qcs;
1769
1770 qcs = NULL;
1771 for (i = largest_id + 1; i <= sub_id; i++) {
1772 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1773 if (!qcs) {
1774 TRACE_PROTO("Could not allocate a new stream",
1775 QUIC_EV_CONN_PSTRM, qcc->conn);
1776 goto out;
1777 }
1778
1779 qcc->strms[qcs_type].largest_id = i;
1780 }
1781 if (qcs)
1782 strm_node = &qcs->by_id;
1783 }
1784 else {
1785 strm_node = eb64_lookup(strms, id);
1786 }
1787 }
1788
1789 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1790 return strm_node;
1791
1792 out:
1793 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1794 return NULL;
1795}
1796
1797/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1798 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1799 * to reflect the data which have been consumed.
1800 */
1801static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1802{
1803 size_t ret;
1804
1805 ret = 0;
1806 while (strm_frm->len) {
1807 size_t try;
1808
1809 try = b_contig_space(buf);
1810 if (!try)
1811 break;
1812
1813 if (try > strm_frm->len)
1814 try = strm_frm->len;
1815 memcpy(b_tail(buf), strm_frm->data, try);
1816 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001817 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001818 b_add(buf, try);
1819 ret += try;
1820 }
1821
1822 return ret;
1823}
1824
1825/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1826 * streams may be open. The data are copied to the stream RX buffer if possible.
1827 * If not, the STREAM frame is stored to be treated again later.
1828 * We rely on the flow control so that not to store too much STREAM frames.
1829 * Return 1 if succeeded, 0 if not.
1830 */
1831static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1832 struct quic_stream *strm_frm,
1833 struct quic_conn *qc)
1834{
1835 struct qcs *strm;
1836 struct eb64_node *strm_node, *frm_node;
1837 struct quic_rx_strm_frm *frm;
1838
1839 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1840 if (!strm_node) {
1841 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1842 return 0;
1843 }
1844
1845 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001846 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001847 /* FIXME: handle the case where this frame overlap others */
1848 if (frm_node) {
1849 TRACE_PROTO("Already existing stream data",
1850 QUIC_EV_CONN_PSTRM, qc->conn);
1851 goto out;
1852 }
1853
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001854 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001855 int ret;
1856
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02001857 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001858 goto store_frm;
1859
1860 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +01001861 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) < 0) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001862 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1863 return 0;
1864 }
1865
1866 strm->rx.offset += ret;
1867 }
1868
1869 if (!strm_frm->len)
1870 goto out;
1871
1872 store_frm:
1873 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1874 if (!frm) {
1875 TRACE_PROTO("Could not alloc RX STREAM frame",
1876 QUIC_EV_CONN_PSTRM, qc->conn);
1877 return 0;
1878 }
1879
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001880 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001881 quic_rx_packet_refinc(pkt);
1882
1883 out:
1884 return 1;
1885}
1886
1887/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1888 * streams may be open. The data are copied to the stream RX buffer if possible.
1889 * If not, the STREAM frame is stored to be treated again later.
1890 * We rely on the flow control so that not to store too much STREAM frames.
1891 * Return 1 if succeeded, 0 if not.
1892 */
1893static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1894 struct quic_stream *strm_frm,
1895 struct quic_conn *qc)
1896{
1897 struct qcs *strm;
1898 struct eb64_node *strm_node, *frm_node;
1899 struct quic_rx_strm_frm *frm;
1900 size_t strm_frm_len;
1901
1902 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1903 if (!strm_node) {
1904 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1905 return 0;
1906 }
1907
1908 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001909 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001910 /* FIXME: handle the case where this frame overlap others */
1911 if (frm_node) {
1912 TRACE_PROTO("Already existing stream data",
1913 QUIC_EV_CONN_PSTRM, qc->conn);
1914 goto out;
1915 }
1916
1917 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001918 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001919 int ret;
1920
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02001921 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001922 goto store_frm;
1923
1924 /* qc_strm_cpy() will modify the offset, depending on the number
1925 * of bytes copied.
1926 */
1927 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1928 /* Inform the application of the arrival of this new stream */
1929 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1930 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1931 return 0;
1932 }
1933
1934 if (ret)
1935 ruqs_notify_recv(strm);
1936
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001937 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001938 }
1939 /* Take this frame into an account for the stream flow control */
1940 strm->rx.offset += strm_frm_len;
1941 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05001942 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001943 */
1944 if (!strm_frm->len)
1945 goto out;
1946
1947 store_frm:
1948 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1949 if (!frm) {
1950 TRACE_PROTO("Could not alloc RX STREAM frame",
1951 QUIC_EV_CONN_PSTRM, qc->conn);
1952 return 0;
1953 }
1954
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001955 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001956 quic_rx_packet_refinc(pkt);
1957
1958 out:
1959 return 1;
1960}
1961
1962static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1963 struct quic_stream *strm_frm,
1964 struct quic_conn *qc)
1965{
1966 if (strm_frm->id & QCS_ID_DIR_BIT)
1967 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1968 else
1969 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1970}
1971
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001972/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1973 * as I/O handler context and <qel> as encryption level.
1974 * Returns 1 if succeeded, 0 if failed.
1975 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001976static 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 +01001977 struct quic_enc_level *qel)
1978{
1979 struct quic_frame frm;
1980 const unsigned char *pos, *end;
1981 struct quic_conn *conn = ctx->conn->qc;
1982
1983 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1984 /* Skip the AAD */
1985 pos = pkt->data + pkt->aad_len;
1986 end = pkt->data + pkt->len;
1987
1988 while (pos < end) {
1989 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1990 goto err;
1991
1992 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001993 case QUIC_FT_PADDING:
1994 if (pos != end) {
1995 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1996 goto err;
1997 }
1998 break;
1999 case QUIC_FT_PING:
2000 break;
2001 case QUIC_FT_ACK:
2002 {
2003 unsigned int rtt_sample;
2004
2005 rtt_sample = 0;
2006 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
2007 goto err;
2008
2009 if (rtt_sample) {
2010 unsigned int ack_delay;
2011
2012 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
2013 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
2014 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
2015 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002016 break;
2017 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002018 case QUIC_FT_CRYPTO:
2019 if (frm.crypto.offset != qel->rx.crypto.offset) {
2020 struct quic_rx_crypto_frm *cf;
2021
2022 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2023 if (!cf) {
2024 TRACE_DEVEL("CRYPTO frame allocation failed",
2025 QUIC_EV_CONN_PRSHPKT, ctx->conn);
2026 goto err;
2027 }
2028
2029 cf->offset_node.key = frm.crypto.offset;
2030 cf->len = frm.crypto.len;
2031 cf->data = frm.crypto.data;
2032 cf->pkt = pkt;
2033 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2034 quic_rx_packet_refinc(pkt);
2035 }
2036 else {
2037 /* XXX TO DO: <cf> is used only for the traces. */
2038 struct quic_rx_crypto_frm cf = { };
2039
2040 cf.offset_node.key = frm.crypto.offset;
2041 cf.len = frm.crypto.len;
2042 if (!qc_provide_cdata(qel, ctx,
2043 frm.crypto.data, frm.crypto.len,
2044 pkt, &cf))
2045 goto err;
2046 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002047 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002048 case QUIC_FT_STREAM_8:
2049 case QUIC_FT_STREAM_9:
2050 case QUIC_FT_STREAM_A:
2051 case QUIC_FT_STREAM_B:
2052 case QUIC_FT_STREAM_C:
2053 case QUIC_FT_STREAM_D:
2054 case QUIC_FT_STREAM_E:
2055 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002056 {
2057 struct quic_stream *stream = &frm.stream;
2058
2059 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
2060 if (objt_listener(ctx->conn->target)) {
2061 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2062 goto err;
2063 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2064 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002065
2066 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
2067 goto err;
2068
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002069 break;
2070 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002071 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002072 break;
2073 case QUIC_FT_CONNECTION_CLOSE:
2074 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle493bb1d2021-10-13 16:34:49 +02002075 conn->qcc->flags |= QC_CF_CC_RECV;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002076 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002077 case QUIC_FT_HANDSHAKE_DONE:
2078 if (objt_listener(ctx->conn->target))
2079 goto err;
2080
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002081 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002082 break;
2083 default:
2084 goto err;
2085 }
2086 }
2087
2088 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2089 * has successfully parse a Handshake packet. The Initial encryption must also
2090 * be discarded.
2091 */
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002092 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) {
2093 int state = HA_ATOMIC_LOAD(&conn->state);
2094
2095 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
2096 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2097 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2098 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
2099 qc_set_timer(ctx);
2100 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
2101 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_SERVER_HANDSHAKE);
2102 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002103 }
2104
2105 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
2106 return 1;
2107
2108 err:
2109 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2110 return 0;
2111}
2112
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002113/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002114 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002115 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2116 * This function must be called only after having built a correct datagram.
2117 * Always succeeds.
2118 */
2119static inline void qc_set_dg(struct cbuf *cbuf,
2120 uint16_t dglen, struct quic_tx_packet *pkt)
2121{
2122 write_u16(cb_wr(cbuf), dglen);
2123 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2124 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2125}
2126
2127/* Prepare as much as possible handshake packets into <qr> ring buffer for
2128 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2129 * several packets in the same datagram. A header made of two fields is added
2130 * to each datagram: the datagram length followed by the address of the first
2131 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002132 * Returns 1 if succeeded, or 0 if something wrong happened.
2133 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002134static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002135{
2136 struct quic_conn *qc;
2137 enum quic_tls_enc_level tel, next_tel;
2138 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002139 struct cbuf *cbuf;
2140 unsigned char *end_buf, *end, *pos, *spos;
2141 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2142 /* length of datagrams */
2143 uint16_t dglen;
2144 size_t total;
2145 /* Each datagram is prepended with its length followed by the
2146 * address of the first packet in the datagram.
2147 */
2148 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002149
2150 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2151 qc = ctx->conn->qc;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002152 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state))) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002153 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002154 goto err;
2155 }
2156
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002157 start:
2158 total = 0;
2159 dglen = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002160 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002161 cbuf = qr->cbuf;
2162 spos = pos = cb_wr(cbuf);
2163 /* Leave at least <dglen> bytes at the end of this buffer
2164 * to ensure there is enough room to mark the end of prepared
2165 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002166 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002167 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2168 first_pkt = prv_pkt = NULL;
2169 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002170 int err, nb_ptos, ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002171 enum quic_pkt_type pkt_type;
2172
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002173 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002174 nb_ptos = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002175 if (!prv_pkt) {
2176 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2177 do {
2178 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2179 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2180 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002181 ack = HA_ATOMIC_BTR(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002182 /* Do not build any more packet if the TX secrets are not available or
2183 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002184 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002185 * and if there is no more CRYPTO data available or in flight
2186 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002187 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002188 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002189 (!ack && !nb_ptos &&
2190 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2191 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002192 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002193 /* Set the current datagram as prepared into <cbuf> if
2194 * the was already a correct packet which was previously written.
2195 */
2196 if (prv_pkt)
2197 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002198 break;
2199 }
2200
2201 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002202 if (!prv_pkt) {
2203 /* Leave room for the datagram header */
2204 pos += dg_headlen;
2205 end = pos + qc->path->mtu;
2206 }
2207
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002208 cur_pkt = qc_build_pkt(&pos, end, qel, qc, pkt_type, ack, nb_ptos, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002209 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002210 if (err < 0) {
2211 if (!prv_pkt && nb_ptos)
2212 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002213 if (ack)
2214 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002215 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002216 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002217 case -2:
2218 goto err;
2219 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002220 /* If there was already a correct packet present, set the
2221 * current datagram as prepared into <cbuf>.
2222 */
2223 if (prv_pkt) {
2224 qc_set_dg(cbuf, dglen, first_pkt);
2225 goto stop_build;
2226 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002227 goto out;
2228 default:
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002229 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2230 if (!cur_pkt)
2231 goto err;
2232
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002233 total += cur_pkt->len;
2234 /* keep trace of the first packet in the datagram */
2235 if (!first_pkt)
2236 first_pkt = cur_pkt;
2237 /* Attach the current one to the previous one */
2238 if (prv_pkt)
2239 prv_pkt->next = cur_pkt;
2240 /* Let's say we have to build a new dgram */
2241 prv_pkt = NULL;
2242 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002243 /* Discard the Initial encryption keys as soon as
2244 * a handshake packet could be built.
2245 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002246 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002247 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2248 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002249 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002250 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2251 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002252 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002253 }
2254 /* Special case for Initial packets: when they have all
2255 * been sent, select the next level.
2256 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002257 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002258 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2259 (next_tel != QUIC_TLS_ENC_LEVEL_NONE && qc->els[next_tel].pktns->tx.in_flight))) {
Frédéric Lécaille4bade772021-08-23 08:54:28 +02002260 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2261 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2262 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002263 tel = next_tel;
2264 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002265 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002266 /* If there is data for the next level, do not
2267 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002268 * which sends only one Initial packet, then wait
2269 * for additional CRYPTO data from the server to enter the
2270 * next level.
2271 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002272 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002273 }
2274 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002275 }
2276
2277 /* If we have to build a new datagram, set the current datagram as
2278 * prepared into <cbuf>.
2279 */
2280 if (!prv_pkt) {
2281 qc_set_dg(cbuf, dglen, first_pkt);
2282 first_pkt = NULL;
2283 dglen = 0;
2284 }
2285 }
2286
2287 stop_build:
2288 /* Reset <wr> writer index if in front of <rd> index */
2289 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2290 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2291
2292 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2293 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2294 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2295 /* Mark the end of contiguous data for the reader */
2296 write_u16(cb_wr(cbuf), 0);
2297 cb_add(cbuf, sizeof(uint16_t));
2298 }
2299 }
2300
2301 if (rd && rd <= cbuf->wr) {
2302 cb_wr_reset(cbuf);
2303 if (pos == spos) {
2304 /* Reuse the same buffer if nothing was built. */
2305 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002306 }
2307 }
2308 }
2309
2310 out:
2311 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002312 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002313
2314 err:
2315 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002316 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002317}
2318
2319/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002320 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002321 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002322int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002323{
2324 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002325 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002326
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002327 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002328 cbuf = qr->cbuf;
2329 while (cb_contig_data(cbuf)) {
2330 unsigned char *pos;
2331 struct buffer tmpbuf = { };
2332 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2333 uint16_t dglen;
2334 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002335 unsigned int time_sent;
2336
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002337 pos = cb_rd(cbuf);
2338 dglen = read_u16(pos);
2339 /* End of prepared datagrams.
2340 * Reset the reader index only if in front of the writer index.
2341 */
2342 if (!dglen) {
2343 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2344
2345 if (wr && wr < cbuf->rd) {
2346 cb_rd_reset(cbuf);
2347 continue;
2348 }
2349 break;
2350 }
2351
2352 pos += sizeof dglen;
2353 first_pkt = read_ptr(pos);
2354 pos += sizeof first_pkt;
2355 tmpbuf.area = (char *)pos;
2356 tmpbuf.size = tmpbuf.data = dglen;
2357
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002358 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002359 for (pkt = first_pkt; pkt; pkt = pkt->next)
2360 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002361 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002362 &tmpbuf, tmpbuf.data, 0) <= 0) {
2363 for (pkt = first_pkt; pkt; pkt = pkt->next)
2364 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002365 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002366 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002367
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002368 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002369 qc->tx.bytes += tmpbuf.data;
2370 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002371
2372 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2373 pkt->time_sent = time_sent;
2374 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2375 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002376 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002377 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002378 qc->path->in_flight += pkt->in_flight_len;
2379 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2380 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002381 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002382 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2383 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002384 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002385 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002386 }
2387 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002388
2389 return 1;
2390}
2391
2392/* Build all the frames which must be sent just after the handshake have succeeded.
2393 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2394 * a HANDSHAKE_DONE frame.
2395 * Return 1 if succeeded, 0 if not.
2396 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002397static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002398{
2399 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002400 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002401 struct quic_frame *frm;
2402
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002403 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002404 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002405 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002406 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002407 if (!frm)
2408 return 0;
2409
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002410 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002411 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002412 }
2413
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002414 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002415 struct quic_connection_id *cid;
2416
2417 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002418 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002419 if (!frm || !cid)
2420 goto err;
2421
2422 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002423 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002424 }
2425
2426 return 1;
2427
2428 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002429 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002430 return 0;
2431}
2432
2433/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002434void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002435{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002436 struct eb64_node *n;
2437 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002438
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002439 n = eb64_first(&arngs->root);
2440 while (n) {
2441 struct eb64_node *next;
2442
2443 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2444 next = eb64_next(n);
2445 eb64_delete(n);
2446 free(ar);
2447 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002448 }
2449}
2450
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002451/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2452 * descending order.
2453 */
2454static inline size_t sack_gap(struct quic_arng_node *p,
2455 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002456{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002457 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002458}
2459
2460
2461/* Remove the last elements of <ack_ranges> list of ack range updating its
2462 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002463 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002464 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002465static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002466{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002467 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002468
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002469 last = eb64_last(&arngs->root);
2470 while (last && arngs->enc_sz > limit) {
2471 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002472
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002473 prev = eb64_prev(last);
2474 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002475 return 0;
2476
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002477 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2478 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2479 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2480 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2481 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2482 --arngs->sz;
2483 eb64_delete(last);
2484 pool_free(pool_head_quic_arng, last);
2485 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002486 }
2487
2488 return 1;
2489}
2490
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002491/* Set the encoded size of <arngs> QUIC ack ranges. */
2492static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2493{
2494 struct eb64_node *node, *next;
2495 struct quic_arng_node *ar, *ar_next;
2496
2497 node = eb64_last(&arngs->root);
2498 if (!node)
2499 return;
2500
2501 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2502 arngs->enc_sz = quic_int_getsize(ar->last) +
2503 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2504
2505 while ((next = eb64_prev(node))) {
2506 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2507 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2508 quic_int_getsize(ar_next->last - ar_next->first.key);
2509 node = next;
2510 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2511 }
2512}
2513
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002514/* Insert <ar> ack range into <argns> tree of ack ranges.
2515 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002516 */
2517static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002518struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2519 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002520{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002521 struct quic_arng_node *new_ar;
2522
2523 new_ar = pool_alloc(pool_head_quic_arng);
2524 if (new_ar) {
2525 new_ar->first.key = ar->first;
2526 new_ar->last = ar->last;
2527 eb64_insert(&arngs->root, &new_ar->first);
2528 arngs->sz++;
2529 }
2530
2531 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002532}
2533
2534/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002535 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002536 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002537 *
2538 * Descending order
2539 * ------------->
2540 * range1 range2
2541 * ..........|--------|..............|--------|
2542 * ^ ^ ^ ^
2543 * | | | |
2544 * last1 first1 last2 first2
2545 * ..........+--------+--------------+--------+......
2546 * diff1 gap12 diff2
2547 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002548 * To encode the previous list of ranges we must encode integers as follows in
2549 * descending order:
2550 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002551 * with diff1 = last1 - first1
2552 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002553 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002554 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002555 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002556int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2557 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002558{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002559 struct eb64_node *le;
2560 struct quic_arng_node *new_node;
2561 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002562
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002563 new = NULL;
2564 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002565 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002566 if (!new_node)
2567 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002568
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002569 goto out;
2570 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002571
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002572 le = eb64_lookup_le(&arngs->root, ar->first);
2573 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002574 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002575 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002576 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002577 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002578 else {
2579 struct quic_arng_node *le_ar =
2580 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002581
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002582 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002583 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002584 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002585
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002586 if (le_ar->last + 1 >= ar->first) {
2587 le_ar->last = ar->last;
2588 new = le;
2589 new_node = le_ar;
2590 }
2591 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002592 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002593 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002594 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002595
2596 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002597 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002598 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002599
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002600 /* Verify that the new inserted node does not overlap the nodes
2601 * which follow it.
2602 */
2603 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002604 struct eb64_node *next;
2605 struct quic_arng_node *next_node;
2606
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002607 while ((next = eb64_next(new))) {
2608 next_node =
2609 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002610 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002611 break;
2612
2613 if (next_node->last > new_node->last)
2614 new_node->last = next_node->last;
2615 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002616 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002617 /* Decrement the size of these ranges. */
2618 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002619 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002620 }
2621
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002622 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002623 quic_arngs_set_enc_sz(arngs);
2624
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002625 return 1;
2626}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002627/* Remove the header protection of packets at <el> encryption level.
2628 * Always succeeds.
2629 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002630static 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 +01002631{
2632 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002633 struct quic_rx_packet *pqpkt;
2634 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002635 struct quic_enc_level *app_qel;
2636
2637 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2638 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2639 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002640 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002641 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002642 TRACE_PROTO("hp not removed (handshake not completed)",
2643 QUIC_EV_CONN_ELRMHP, ctx->conn);
2644 goto out;
2645 }
2646 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002647 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002648 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2649 pqpkt->data + pqpkt->pn_offset,
2650 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2651 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2652 /* XXX TO DO XXX */
2653 }
2654 else {
2655 /* The AAD includes the packet number field */
2656 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2657 /* Store the packet into the tree of packets to decrypt. */
2658 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002659 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002660 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2661 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002662 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002663 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2664 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002665 MT_LIST_DELETE_SAFE(pkttmp1);
2666 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002667 }
2668
2669 out:
2670 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2671}
2672
2673/* Process all the CRYPTO frame at <el> encryption level.
2674 * Return 1 if succeeded, 0 if not.
2675 */
2676static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002677 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002678{
2679 struct eb64_node *node;
2680
2681 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
2682 node = eb64_first(&el->rx.crypto.frms);
2683 while (node) {
2684 struct quic_rx_crypto_frm *cf;
2685
2686 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2687 if (cf->offset_node.key != el->rx.crypto.offset)
2688 break;
2689
2690 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2691 goto err;
2692
2693 node = eb64_next(node);
2694 quic_rx_packet_refdec(cf->pkt);
2695 eb64_delete(&cf->offset_node);
2696 pool_free(pool_head_quic_rx_crypto_frm, cf);
2697 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002698 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2699 return 1;
2700
2701 err:
2702 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2703 return 0;
2704}
2705
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002706/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002707 * This is the caller responsibility to check that <cur_el> is different of <next_el>
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002708 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002709 * Return 1 if succeeded, 0 if not.
2710 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002711int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2712 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002713{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002714 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002715 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002716 struct quic_conn *qc = ctx->conn->qc;
2717 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002718
2719 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002720 qel = cur_el;
2721 next_tel:
2722 if (!qel)
2723 goto out;
2724
2725 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
2726 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002727 while (node) {
2728 struct quic_rx_packet *pkt;
2729
2730 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002731 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
2732 ctx->conn, pkt, NULL, ctx->ssl);
2733 if (!qc_pkt_decrypt(pkt, &qel->tls_ctx)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002734 /* Drop the packet */
2735 TRACE_PROTO("packet decryption failed -> dropped",
2736 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2737 }
2738 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002739 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002740 /* Drop the packet */
2741 TRACE_PROTO("packet parsing failed -> dropped",
2742 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2743 }
2744 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002745 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2746
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002747 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002748 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
2749 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002750 if (pkt->pn > largest_pn)
2751 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002752 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002753 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002754 TRACE_DEVEL("Could not update ack range list",
2755 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002756 }
2757 }
2758 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002759 eb64_delete(&pkt->pn_node);
2760 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002761 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002762 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002763
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002764 /* Update the largest packet number. */
2765 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002766 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
2767 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002768 goto err;
2769
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002770 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002771 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002772 qel = next_el;
2773 goto next_tel;
2774 }
2775
2776 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002777 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2778 return 1;
2779
2780 err:
2781 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2782 return 0;
2783}
2784
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002785/* QUIC connection packet handler task. */
2786struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002787{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002788 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002789 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002790 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002791 enum quic_tls_enc_level tel, next_tel;
2792 struct quic_enc_level *qel, *next_qel;
2793 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002794 struct qring *qr; // Tx ring
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002795 int prev_st, st, force_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002796
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002797 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002798 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002799 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002800 st = HA_ATOMIC_LOAD(&qc->state);
2801 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002802 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002803 start:
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002804 if (!quic_get_tls_enc_levels(&tel, &next_tel, st))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002805 goto err;
2806
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002807 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002808 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002809
2810 next_level:
2811 tls_ctx = &qel->tls_ctx;
2812
2813 /* If the header protection key for this level has been derived,
2814 * remove the packet header protections.
2815 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002816 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002817 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2818 qc_rm_hp_pkts(qel, ctx);
2819
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002820 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002821 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
2822 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2823 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002824 goto err;
2825
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002826 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02002827 if (st >= QUIC_HS_ST_COMPLETE &&
2828 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002829 /* Discard the Handshake keys. */
2830 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002831 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002832 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2833 qc_set_timer(ctx);
2834 if (!quic_build_post_handshake_frames(qc))
2835 goto err;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002836 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002837 }
2838
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002839 if (!qr)
2840 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2841 ret = qc_prep_hdshk_pkts(qr, ctx);
2842 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002843 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002844 else if (ret == 0)
2845 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002846
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002847 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002848 goto err;
2849
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002850 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002851 /* Check if there is something to do for the next level.
2852 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002853 if (next_qel && next_qel != qel &&
2854 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002855 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002856 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002857 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002858 goto next_level;
2859 }
2860
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002861 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002862 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002863 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002864
2865 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002866 if (qr)
2867 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002868 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002869 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002870}
2871
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002872/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002873static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2874{
2875 int i;
2876
2877 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2878 if (qel->tx.crypto.bufs[i]) {
2879 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2880 qel->tx.crypto.bufs[i] = NULL;
2881 }
2882 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002883 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002884}
2885
2886/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002887 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002888 * Returns 1 if succeeded, 0 if not.
2889 */
2890static int quic_conn_enc_level_init(struct quic_conn *qc,
2891 enum quic_tls_enc_level level)
2892{
2893 struct quic_enc_level *qel;
2894
2895 qel = &qc->els[level];
2896 qel->level = quic_to_ssl_enc_level(level);
2897 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2898 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2899 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2900 qel->tls_ctx.rx.flags = 0;
2901 qel->tls_ctx.tx.flags = 0;
2902
2903 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002904 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002905 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002906 qel->rx.crypto.offset = 0;
2907 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002908
2909 /* Allocate only one buffer. */
2910 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2911 if (!qel->tx.crypto.bufs)
2912 goto err;
2913
2914 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2915 if (!qel->tx.crypto.bufs[0])
2916 goto err;
2917
2918 qel->tx.crypto.bufs[0]->sz = 0;
2919 qel->tx.crypto.nb_buf = 1;
2920
2921 qel->tx.crypto.sz = 0;
2922 qel->tx.crypto.offset = 0;
2923
2924 return 1;
2925
2926 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002927 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002928 return 0;
2929}
2930
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002931/* Release all the memory allocated for <conn> QUIC connection. */
2932static void quic_conn_free(struct quic_conn *conn)
2933{
2934 int i;
2935
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002936 if (!conn)
2937 return;
2938
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002939 free_quic_conn_cids(conn);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02002940
2941 /* remove the connection from receiver cids trees */
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01002942 HA_RWLOCK_WRLOCK(QUIC_LOCK, &conn->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02002943 ebmb_delete(&conn->odcid_node);
2944 ebmb_delete(&conn->scid_node);
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01002945 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &conn->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02002946
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002947 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2948 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002949 if (conn->timer_task)
2950 task_destroy(conn->timer_task);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01002951 pool_free(pool_head_quic_conn_rxbuf, conn->rx.buf.area);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002952 pool_free(pool_head_quic_conn, conn);
2953}
2954
Amaury Denoyelle414cac52021-09-22 11:14:37 +02002955void quic_close(struct connection *conn, void *xprt_ctx)
2956{
2957 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
2958 struct quic_conn *qc = conn_ctx->conn->qc;
2959 quic_conn_free(qc);
2960}
2961
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002962/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002963static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002964{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002965 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002966 struct quic_conn *qc;
2967 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002968 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002969
2970 conn_ctx = task->context;
2971 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002972 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2973 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002974 task->expire = TICK_ETERNITY;
2975 pktns = quic_loss_pktns(qc);
2976 if (tick_isset(pktns->tx.loss_time)) {
2977 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2978
2979 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2980 if (!LIST_ISEMPTY(&lost_pkts))
2981 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2982 qc_set_timer(conn_ctx);
2983 goto out;
2984 }
2985
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002986 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002987 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002988 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002989 pktns->tx.pto_probe = 1;
2990 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002991 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002992 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2993 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2994
2995 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2996 hel->pktns->tx.pto_probe = 1;
2997 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2998 iel->pktns->tx.pto_probe = 1;
2999 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003000 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003001 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3002 qc->path->loss.pto_count++;
3003
3004 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003005 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003006
3007 return task;
3008}
3009
3010/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3011 * connections used to identify the first Initial packets of client connecting
3012 * to listeners. This parameter must be NULL for QUIC connections attached
3013 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3014 * <scid> is the source connection ID with <scid_len> as length.
3015 * Returns 1 if succeeded, 0 if not.
3016 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003017static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
3018 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003019 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003020{
3021 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003022 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003023 /* Initial CID. */
3024 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003025 char *buf_area;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003026
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003027 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003028 qc = pool_zalloc(pool_head_quic_conn);
3029 if (!qc) {
3030 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3031 goto err;
3032 }
3033
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003034 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3035 if (!buf_area) {
3036 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT);
3037 goto err;
3038 }
3039
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003040 qc->cids = EB_ROOT;
3041 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003042 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003043 struct listener *l = owner;
3044
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003045 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046 /* Copy the initial DCID. */
3047 qc->odcid.len = dcid_len;
3048 if (qc->odcid.len)
3049 memcpy(qc->odcid.data, dcid, dcid_len);
3050
3051 /* Copy the SCID as our DCID for this connection. */
3052 if (scid_len)
3053 memcpy(qc->dcid.data, scid, scid_len);
3054 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003055 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003056 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003057 }
3058 /* QUIC Client (outgoing connection to servers) */
3059 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003060 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003061 if (dcid_len)
3062 memcpy(qc->dcid.data, dcid, dcid_len);
3063 qc->dcid.len = dcid_len;
3064 }
3065
3066 /* Initialize the output buffer */
3067 qc->obuf.pos = qc->obuf.data;
3068
3069 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003070 if (!icid) {
3071 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
3072 goto err;
3073 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003074
3075 /* Select our SCID which is the first CID with 0 as sequence number. */
3076 qc->scid = icid->cid;
3077
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003078 /* Packet number spaces initialization. */
3079 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3080 quic_pktns_init(&qc->pktns[i]);
3081 /* QUIC encryption level context initialization. */
3082 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003083 if (!quic_conn_enc_level_init(qc, i)) {
3084 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003085 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003086 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003087 /* Initialize the packet number space. */
3088 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3089 }
3090
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003091 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003092 /* TX part. */
3093 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003094 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3095 qc->tx.wbuf = qc->tx.rbuf = 0;
3096 qc->tx.bytes = 0;
3097 qc->tx.nb_pto_dgrams = 0;
3098 /* RX part. */
3099 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003100 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003101 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3102 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3103 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003104
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003105 /* XXX TO DO: Only one path at this time. */
3106 qc->path = &qc->paths[0];
3107 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3108
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003109 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003110
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003111 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003112
3113 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003114 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003115 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003116 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003117}
3118
3119/* Initialize the timer task of <qc> QUIC connection.
3120 * Returns 1 if succeeded, 0 if not.
3121 */
3122static int quic_conn_init_timer(struct quic_conn *qc)
3123{
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003124 qc->timer_task = task_new_anywhere();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003125 if (!qc->timer_task)
3126 return 0;
3127
3128 qc->timer = TICK_ETERNITY;
3129 qc->timer_task->process = process_timer;
3130 qc->timer_task->context = qc->conn->xprt_ctx;
3131
3132 return 1;
3133}
3134
3135/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3136 * past one byte of this buffer.
3137 */
3138static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3139 struct quic_rx_packet *pkt)
3140{
3141 unsigned char dcid_len, scid_len;
3142
3143 /* Version */
3144 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3145 return 0;
3146
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003147 /* Destination Connection ID Length */
3148 dcid_len = *(*buf)++;
3149 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3150 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3151 /* XXX MUST BE DROPPED */
3152 return 0;
3153
3154 if (dcid_len) {
3155 /* Check that the length of this received DCID matches the CID lengths
3156 * of our implementation for non Initials packets only.
3157 */
3158 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
3159 return 0;
3160
3161 memcpy(pkt->dcid.data, *buf, dcid_len);
3162 }
3163
3164 pkt->dcid.len = dcid_len;
3165 *buf += dcid_len;
3166
3167 /* Source Connection ID Length */
3168 scid_len = *(*buf)++;
3169 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3170 /* XXX MUST BE DROPPED */
3171 return 0;
3172
3173 if (scid_len)
3174 memcpy(pkt->scid.data, *buf, scid_len);
3175 pkt->scid.len = scid_len;
3176 *buf += scid_len;
3177
3178 return 1;
3179}
3180
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003181/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3182 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3183 * encryption level matching with the packet type. <*qel> may be null if not found.
3184 * Note that <ctx> may be null (for Initial packets).
3185 */
3186static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003187 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003188 struct quic_enc_level **qel)
3189{
3190 enum quic_tls_enc_level tel;
3191
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003192 /* Special case without connection context (first Initial packets) */
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003193 if (!ctx) {
3194 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3195 return 1;
3196 }
3197
3198 tel = quic_packet_type_enc_level(pkt->type);
3199 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3200 *qel = NULL;
3201 return 0;
3202 }
3203
3204 *qel = &qc->els[tel];
3205 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3206 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3207 return 0;
3208 }
3209
3210 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003211 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3212 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003213 return 1;
3214
3215 return 0;
3216}
3217
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003218/* Insert <pkt> RX packet in its <qel> RX packets tree */
3219static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3220{
3221 pkt->pn_node.key = pkt->pn;
3222 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3223 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3224 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3225 quic_rx_packet_refinc(pkt);
3226}
3227
3228/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003229 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3230 * byte past the end of the buffer containing this packet and <beg> the address of
3231 * the packet first byte.
3232 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3233 * Returns 1 if succeeded, 0 if not.
3234 */
3235static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3236 unsigned char **buf, unsigned char *beg,
3237 const unsigned char *end,
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003238 struct quic_conn *qc, struct quic_enc_level **el,
3239 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003240{
3241 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003242 struct quic_enc_level *qel;
3243 /* Only for traces. */
3244 struct quic_rx_packet *qpkt_trace;
3245
3246 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003247 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003248 /* The packet number is here. This is also the start minus
3249 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3250 * protection.
3251 */
3252 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003253 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003254 /* Note that the following function enables us to unprotect the packet
3255 * number and its length subsequently used to decrypt the entire
3256 * packets.
3257 */
3258 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3259 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003260 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003261 goto err;
3262 }
3263
3264 /* The AAD includes the packet number field found at <pn>. */
3265 pkt->aad_len = pn - beg + pkt->pnl;
3266 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003267 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003268 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003269 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3270 /* If the packet number space has been discarded, this packet
3271 * will be not parsed.
3272 */
3273 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
3274 goto out;
3275 }
3276
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003277 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003278 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003279 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3280 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003281 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003282 else {
3283 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
3284 goto err;
3285 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003286
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003287 *el = qel;
3288 /* No reference counter incrementation here!!! */
3289 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3290 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3291 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3292 b_add(&qc->rx.buf, pkt->len);
3293 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003294 /* Updtate the offset of <*buf> for the next QUIC packet. */
3295 *buf = beg + pkt->len;
3296
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003297 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003298 return 1;
3299
3300 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003301 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 +01003302 return 0;
3303}
3304
3305/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3306 * Also set <*long_header> to 1 if this form is long, 0 if not.
3307 */
3308static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3309 unsigned char byte0, int *long_header)
3310{
3311 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3312 pkt->type =
3313 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3314 *long_header = 1;
3315 }
3316 else {
3317 pkt->type = QUIC_PACKET_TYPE_SHORT;
3318 *long_header = 0;
3319 }
3320}
3321
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003322/*
3323 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3324 */
3325static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3326{
3327 int j = 0, version;
3328
3329 do {
3330 version = quic_supported_version[j];
3331 if (version == pkt->version)
3332 return 1;
3333
3334 version = quic_supported_version[++j];
3335 } while(version);
3336
3337 return 0;
3338}
3339
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003340__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003341static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3342 struct quic_rx_packet *pkt,
3343 struct quic_dgram_ctx *dgram_ctx,
3344 struct sockaddr_storage *saddr)
3345{
3346 unsigned char *beg;
3347 uint64_t len;
3348 struct quic_conn *qc;
3349 struct eb_root *cids;
3350 struct ebmb_node *node;
3351 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003352 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003353 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003354 size_t b_cspace;
3355 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003356
3357 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003358 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003359 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3360 if (end <= *buf)
3361 goto err;
3362
3363 /* Fixed bit */
3364 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3365 /* XXX TO BE DISCARDED */
3366 goto err;
3367
3368 srv_conn = dgram_ctx->owner;
3369 beg = *buf;
3370 /* Header form */
3371 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3372 if (long_header) {
3373 size_t cid_lookup_len;
3374
3375 if (!quic_packet_read_long_header(buf, end, pkt))
3376 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003377
3378 /* unsupported QUIC version */
3379 if (!qc_pkt_is_supported_version(pkt)) {
3380 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3381 goto err;
3382 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003383
3384 /* For Initial packets, and for servers (QUIC clients connections),
3385 * there is no Initial connection IDs storage.
3386 */
3387 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3388 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3389 cid_lookup_len = pkt->dcid.len;
3390 }
3391 else {
3392 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3393 cid_lookup_len = QUIC_CID_LEN;
3394 }
3395
3396 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3397 if (!node)
3398 goto err;
3399
3400 qc = ebmb_entry(node, struct quic_conn, scid_node);
3401
3402 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3403 qc->dcid.len = pkt->scid.len;
3404 if (pkt->scid.len)
3405 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3406 }
3407
3408 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3409 uint64_t token_len;
3410
3411 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3412 goto err;
3413
3414 /* XXX TO DO XXX 0 value means "the token is not present".
3415 * A server which sends an Initial packet must not set the token.
3416 * So, a client which receives an Initial packet with a token
3417 * MUST discard the packet or generate a connection error with
3418 * PROTOCOL_VIOLATION as type.
3419 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3420 */
3421 pkt->token_len = token_len;
3422 }
3423 }
3424 else {
3425 /* XXX TO DO: Short header XXX */
3426 if (end - *buf < QUIC_CID_LEN)
3427 goto err;
3428
3429 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3430 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3431 if (!node)
3432 goto err;
3433
3434 qc = ebmb_entry(node, struct quic_conn, scid_node);
3435 *buf += QUIC_CID_LEN;
3436 }
3437 /* Store the DCID used for this packet to check the packet which
3438 * come in this UDP datagram match with it.
3439 */
3440 if (!dgram_ctx->dcid_node)
3441 dgram_ctx->dcid_node = node;
3442 /* Only packets packets with long headers and not RETRY or VERSION as type
3443 * have a length field.
3444 */
3445 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3446 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3447 goto err;
3448
3449 pkt->len = len;
3450 }
3451 else if (!long_header) {
3452 /* A short packet is the last one of an UDP datagram. */
3453 pkt->len = end - *buf;
3454 }
3455
3456 conn_ctx = qc->conn->xprt_ctx;
3457
3458 /* Increase the total length of this packet by the header length. */
3459 pkt->len += *buf - beg;
3460 /* Do not check the DCID node before the length. */
3461 if (dgram_ctx->dcid_node != node) {
3462 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3463 goto err;
3464 }
3465
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003466 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3467 b_cspace = b_contig_space(&qc->rx.buf);
3468 if (b_cspace < pkt->len) {
3469 /* Let us consume the remaining contiguous space. */
3470 b_add(&qc->rx.buf, b_cspace);
3471 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3472 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3473 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3474 goto err;
3475 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003476 }
3477
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003478 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) {
3479 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3480 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003481 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003482 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003483
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003484 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3485 if (pkt->aad_len)
3486 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003487 /* Wake the tasklet of the QUIC connection packet handler. */
3488 if (conn_ctx)
3489 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3490
3491 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3492
3493 return pkt->len;
3494
3495 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003496 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003497 return -1;
3498}
3499
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003500/*
3501 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3502 * address <addr>.
3503 * Implementation of RFC9000 6. Version Negotiation
3504 *
3505 * TODO implement a rate-limiting sending of Version Negotiation packets
3506 *
3507 * Returns 0 on success else non-zero
3508 */
3509static int qc_send_version_negotiation(int fd, struct sockaddr_storage *addr,
3510 struct quic_rx_packet *pkt)
3511{
3512 char buf[256];
3513 int i = 0, j, version;
3514 const socklen_t addrlen = get_addr_len(addr);
3515
3516 /*
3517 * header form
3518 * long header, fixed bit to 0 for Version Negotiation
3519 */
3520 buf[i++] = '\x80';
3521
3522 /* null version for Version Negotiation */
3523 buf[i++] = '\x00';
3524 buf[i++] = '\x00';
3525 buf[i++] = '\x00';
3526 buf[i++] = '\x00';
3527
3528 /* source connection id */
3529 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003530 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003531 i += pkt->scid.len;
3532
3533 /* destination connection id */
3534 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003535 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003536 i += pkt->dcid.len;
3537
3538 /* supported version */
3539 j = 0;
3540 do {
3541 version = htonl(quic_supported_version[j]);
3542 memcpy(&buf[i], &version, sizeof(version));
3543 i += sizeof(version);
3544
3545 version = quic_supported_version[++j];
3546 } while (version);
3547
3548 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3549 return 1;
3550
3551 return 0;
3552}
3553
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003554static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3555 struct quic_rx_packet *pkt,
3556 struct quic_dgram_ctx *dgram_ctx,
3557 struct sockaddr_storage *saddr)
3558{
3559 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003560 struct quic_conn *qc;
3561 struct eb_root *cids;
3562 struct ebmb_node *node;
3563 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003564 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003565 int long_header = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003566 size_t b_cspace;
3567 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003568
3569 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003570 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003571 qel = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003572 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003573 if (end <= *buf)
3574 goto err;
3575
3576 /* Fixed bit */
3577 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3578 /* XXX TO BE DISCARDED */
3579 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3580 goto err;
3581 }
3582
3583 l = dgram_ctx->owner;
3584 beg = *buf;
3585 /* Header form */
3586 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3587 if (long_header) {
3588 unsigned char dcid_len;
3589
3590 if (!quic_packet_read_long_header(buf, end, pkt)) {
3591 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3592 goto err;
3593 }
3594
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003595 /* RFC9000 6. Version Negotiation */
3596 if (!qc_pkt_is_supported_version(pkt)) {
3597 /* do not send Version Negotiation in response to a
3598 * Version Negotiation packet.
3599 */
3600 if (!pkt->version) {
3601 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3602 goto err;
3603 }
3604
3605 /* unsupported version, send Negotiation packet */
3606 if (qc_send_version_negotiation(l->rx.fd, saddr, pkt)) {
3607 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
3608 goto err;
3609 }
3610
3611 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
3612 goto out;
3613 }
3614
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003615 dcid_len = pkt->dcid.len;
3616 /* For Initial packets, and for servers (QUIC clients connections),
3617 * there is no Initial connection IDs storage.
3618 */
3619 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003620 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003621 /* DCIDs of first packets coming from clients may have the same values.
3622 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3623 */
3624 quic_cid_saddr_cat(&pkt->dcid, saddr);
3625 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003626
3627 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3628 end - *buf < token_len) {
3629 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3630 goto err;
3631 }
3632
3633 /* XXX TO DO XXX 0 value means "the token is not present".
3634 * A server which sends an Initial packet must not set the token.
3635 * So, a client which receives an Initial packet with a token
3636 * MUST discard the packet or generate a connection error with
3637 * PROTOCOL_VIOLATION as type.
3638 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3639 */
3640 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003641 }
3642 else {
3643 if (pkt->dcid.len != QUIC_CID_LEN) {
3644 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3645 goto err;
3646 }
3647
3648 cids = &l->rx.cids;
3649 }
3650
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003651 /* Only packets packets with long headers and not RETRY or VERSION as type
3652 * have a length field.
3653 */
3654 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3655 uint64_t len;
3656
3657 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3658 end - *buf < len) {
3659 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3660 goto err;
3661 }
3662
3663 pkt->len = len;
3664 }
3665
3666
3667 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003668 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3669 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3670 cids == &l->rx.odcids) {
3671 /* Switch to the definitive tree ->cids containing the final CIDs. */
3672 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3673 if (node) {
3674 /* If found, signal this with NULL as special value for <cids>. */
3675 pkt->dcid.len = dcid_len;
3676 cids = NULL;
3677 }
3678 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003679 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003680
3681 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003682 int ipv4;
3683 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003684 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003685 const unsigned char *salt = initial_salt_v1;
3686 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003687
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003688 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3689 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3690 goto err;
3691 }
3692
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003693 pkt->saddr = *saddr;
3694 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3695 * of <saddr>.
3696 */
3697 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003698 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003699 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003700 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003701 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003702 goto err;
3703
3704 odcid = &qc->rx.params.original_destination_connection_id;
3705 /* Copy the transport parameters. */
3706 qc->rx.params = l->bind_conf->quic_params;
3707 /* Copy original_destination_connection_id transport parameter. */
3708 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3709 odcid->len = pkt->odcid_len;
3710 /* Copy the initial source connection ID. */
3711 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3712 qc->enc_params_len =
3713 quic_transport_params_encode(qc->enc_params,
3714 qc->enc_params + sizeof qc->enc_params,
3715 &qc->rx.params, 1);
3716 if (!qc->enc_params_len)
3717 goto err;
3718
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003719 /* NOTE: the socket address has been concatenated to the destination ID
3720 * chosen by the client for Initial packets.
3721 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003722 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
3723 salt = initial_salt_draft_29;
3724 salt_len = sizeof initial_salt_draft_29;
3725 }
3726 if (!qc_new_isecs(qc, salt, salt_len,
3727 pkt->dcid.data, pkt->odcid_len, 1)) {
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003728 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3729 goto err;
3730 }
3731
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003732 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003733 /* This is the DCID node sent in this packet by the client. */
3734 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003735 /* Enqueue this packet. */
3736 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3737 /* Try to accept a new connection. */
3738 listener_accept(l);
3739
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01003740 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003741 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3742 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3743 /* Insert our SCID, the connection ID for the QUIC client. */
3744 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01003745 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003746 if (n != &qc->scid_node) {
3747 quic_conn_free(qc);
3748 qc = ebmb_entry(n, struct quic_conn, scid_node);
3749 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003750 }
3751 else {
3752 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3753 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3754 else
3755 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003756 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003757 }
3758 }
3759 else {
3760 if (end - *buf < QUIC_CID_LEN) {
3761 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3762 goto err;
3763 }
3764
3765 cids = &l->rx.cids;
3766 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3767 if (!node) {
3768 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3769 goto err;
3770 }
3771
3772 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003773 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003774 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003775 /* A short packet is the last one of an UDP datagram. */
3776 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003777 }
3778
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003779 pkt->qc = qc;
3780
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003781 /* Store the DCID used for this packet to check the packet which
3782 * come in this UDP datagram match with it.
3783 */
3784 if (!dgram_ctx->dcid_node) {
3785 dgram_ctx->dcid_node = node;
3786 dgram_ctx->qc = qc;
3787 }
3788
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003789 /* Increase the total length of this packet by the header length. */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003790 pkt->raw_len = pkt->len += *buf - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003791 /* Do not check the DCID node before the length. */
3792 if (dgram_ctx->dcid_node != node) {
3793 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3794 goto err;
3795 }
3796
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003797 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3798 b_cspace = b_contig_space(&qc->rx.buf);
3799 if (b_cspace < pkt->len) {
3800 /* Let us consume the remaining contiguous space. */
3801 b_add(&qc->rx.buf, b_cspace);
3802 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3803 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3804 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3805 goto err;
3806 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003807 }
3808
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003809 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) {
3810 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003811 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3812 goto err;
3813 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003814
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003815 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003816 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003817 if (pkt->aad_len)
3818 qc_pkt_insert(pkt, qel);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003819 /* Wake up the connection packet handler task from here only if all
3820 * the contexts have been initialized, especially the mux context
3821 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3822 * will start it if these contexts for the connection are not already
3823 * initialized.
3824 */
3825 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003826 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003827
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003828 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003829 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3830
3831 return pkt->len;
3832
3833 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003834 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003835 qc ? qc->conn : NULL, pkt);
3836 return -1;
3837}
3838
3839/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3840 * in advance. This is the reponsability of the caller to check there is enough room in this
3841 * buffer to build a long header.
3842 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3843 */
3844static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3845 int type, size_t pn_len, struct quic_conn *conn)
3846{
3847 if (type > QUIC_PACKET_TYPE_RETRY)
3848 return 0;
3849
3850 /* #0 byte flags */
3851 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3852 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3853 /* Version */
3854 quic_write_uint32(buf, end, conn->version);
3855 *(*buf)++ = conn->dcid.len;
3856 /* Destination connection ID */
3857 if (conn->dcid.len) {
3858 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3859 *buf += conn->dcid.len;
3860 }
3861 /* Source connection ID */
3862 *(*buf)++ = conn->scid.len;
3863 if (conn->scid.len) {
3864 memcpy(*buf, conn->scid.data, conn->scid.len);
3865 *buf += conn->scid.len;
3866 }
3867
3868 return 1;
3869}
3870
3871/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3872 * in advance. This is the reponsability of the caller to check there is enough room in this
3873 * buffer to build a long header.
3874 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3875 */
3876static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3877 size_t pn_len, struct quic_conn *conn)
3878{
3879 /* #0 byte flags */
3880 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3881 /* Destination connection ID */
3882 if (conn->dcid.len) {
3883 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3884 *buf += conn->dcid.len;
3885 }
3886
3887 return 1;
3888}
3889
3890/* Apply QUIC header protection to the packet with <buf> as first byte address,
3891 * <pn> as address of the Packet number field, <pnlen> being this field length
3892 * with <aead> as AEAD cipher and <key> as secret key.
3893 * Returns 1 if succeeded or 0 if failed.
3894 */
3895static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3896 const EVP_CIPHER *aead, const unsigned char *key)
3897{
3898 int i, ret, outlen;
3899 EVP_CIPHER_CTX *ctx;
3900 /* We need an IV of at least 5 bytes: one byte for bytes #0
3901 * and at most 4 bytes for the packet number
3902 */
3903 unsigned char mask[5] = {0};
3904
3905 ret = 0;
3906 ctx = EVP_CIPHER_CTX_new();
3907 if (!ctx)
3908 return 0;
3909
3910 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3911 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3912 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3913 goto out;
3914
3915 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3916 for (i = 0; i < pnlen; i++)
3917 pn[i] ^= mask[i + 1];
3918
3919 ret = 1;
3920
3921 out:
3922 EVP_CIPHER_CTX_free(ctx);
3923
3924 return ret;
3925}
3926
3927/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3928 * ACK ranges if needed to a value below <limit> in bytes.
3929 * Return 1 if succeeded, 0 if not.
3930 */
3931static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3932{
3933 size_t room, ack_delay_sz;
3934
3935 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3936 /* A frame is made of 1 byte for the frame type. */
3937 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003938 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003939 return 0;
3940
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003941 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003942}
3943
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003944/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3945 * 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 +01003946 * and <*len> the packet Length field initialized with the number of bytes already present
3947 * 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 +02003948 * <headlen> is the number of bytes already present in this packet before building frames.
3949 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003950 * Update consequently <*len> to reflect the size of these frames built
3951 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003952 * Return 1 if succeeded, 0 if not.
3953 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003954static inline int qc_build_frms(struct quic_tx_packet *pkt,
3955 size_t room, size_t *len, size_t headlen,
3956 struct quic_enc_level *qel,
3957 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003958{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003959 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003960 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003961 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003962 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003963
Frédéric Lécailleea604992020-12-24 13:01:37 +01003964 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003965 if (*len > room || headlen > remain)
3966 return 0;
3967
Frédéric Lécailleea604992020-12-24 13:01:37 +01003968 /* If we are not probing we must take into an account the congestion
3969 * control window.
3970 */
3971 if (!conn->tx.nb_pto_dgrams)
3972 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003973 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003974 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003975 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 +01003976 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003977 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003978
Frédéric Lécailleea604992020-12-24 13:01:37 +01003979 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003980 break;
3981
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003982 switch (cf->type) {
3983 case QUIC_FT_CRYPTO:
3984 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3985 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3986 /* Compute the length of this CRYPTO frame header */
3987 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3988 /* Compute the data length of this CRyPTO frame. */
3989 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3990 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3991 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3992 if (!dlen)
3993 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003994
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003995 pkt->cdata_len += dlen;
3996 /* CRYPTO frame length. */
3997 flen = hlen + quic_int_getsize(dlen) + dlen;
3998 TRACE_PROTO(" CRYPTO frame length (flen)",
3999 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
4000 /* Add the CRYPTO data length and its encoded length to the packet
4001 * length and the length of this length.
4002 */
4003 *len += flen;
4004 room -= flen;
4005 if (dlen == cf->crypto.len) {
4006 /* <cf> CRYPTO data have been consumed. */
4007 MT_LIST_DELETE_SAFE(tmp1);
4008 LIST_APPEND(&pkt->frms, &cf->list);
4009 }
4010 else {
4011 struct quic_frame *new_cf;
4012
4013 new_cf = pool_alloc(pool_head_quic_frame);
4014 if (!new_cf) {
4015 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
4016 return 0;
4017 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004018
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004019 new_cf->type = QUIC_FT_CRYPTO;
4020 new_cf->crypto.len = dlen;
4021 new_cf->crypto.offset = cf->crypto.offset;
4022 new_cf->crypto.qel = qel;
4023 LIST_APPEND(&pkt->frms, &new_cf->list);
4024 /* Consume <dlen> bytes of the current frame. */
4025 cf->crypto.len -= dlen;
4026 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004027 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004028 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004029
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004030 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004031 /* Note that these frames are accepted in short packets only without
4032 * "Length" packet field. Here, <*len> is used only to compute the
4033 * sum of the lengths of the already built frames for this packet.
4034 */
4035 TRACE_PROTO(" New STREAM frame build (room, len)",
4036 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
4037 /* Compute the length of this STREAM frame "header" made a all the field
4038 * excepting the variable ones. Note that +1 is for the type of this frame.
4039 */
4040 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004041 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004042 /* Compute the data length of this STREAM frame. */
4043 avail_room = room - hlen - *len;
4044 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004045 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004046
4047 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4048 dlen = max_available_room(avail_room, &dlen_sz);
4049 if (dlen > cf->stream.len) {
4050 dlen = cf->stream.len;
4051 }
4052 dlen_sz = quic_int_getsize(dlen);
4053 flen = hlen + dlen_sz + dlen;
4054 }
4055 else {
4056 dlen = QUIC_MIN(avail_room, cf->stream.len);
4057 flen = hlen + dlen;
4058 }
4059 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
4060 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen);
4061 TRACE_PROTO(" STREAM frame length (flen)",
4062 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
4063 /* Add the STREAM data length and its encoded length to the packet
4064 * length and the length of this length.
4065 */
4066 *len += flen;
4067 room -= flen;
4068 if (dlen == cf->stream.len) {
4069 /* <cf> STREAM data have been consumed. */
4070 MT_LIST_DELETE_SAFE(tmp1);
4071 LIST_APPEND(&pkt->frms, &cf->list);
4072 }
4073 else {
4074 struct quic_frame *new_cf;
4075
4076 new_cf = pool_zalloc(pool_head_quic_frame);
4077 if (!new_cf) {
4078 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn);
4079 return 0;
4080 }
4081
4082 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004083 new_cf->stream.qcs = cf->stream.qcs;
4084 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004085 new_cf->stream.id = cf->stream.id;
4086 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4087 new_cf->stream.offset = cf->stream.offset;
4088 new_cf->stream.len = dlen;
4089 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4090 /* FIN bit reset */
4091 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4092 new_cf->stream.data = cf->stream.data;
4093 LIST_APPEND(&pkt->frms, &new_cf->list);
4094 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4095 /* Consume <dlen> bytes of the current frame. */
4096 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004097 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004098 cf->stream.data += dlen;
4099 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004100 break;
4101
4102 default:
4103 flen = qc_frm_len(cf);
4104 BUG_ON(!flen);
4105 if (flen > room)
4106 continue;
4107
4108 *len += flen;
4109 room -= flen;
4110 MT_LIST_DELETE_SAFE(tmp1);
4111 LIST_APPEND(&pkt->frms, &cf->list);
4112 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004113 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004114 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004115 }
4116
Frédéric Lécailleea604992020-12-24 13:01:37 +01004117 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004118}
4119
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004120/* This function evaluates if <pkt> packet may be built into a buffer with
4121 * <room> as available room. A valid packet should at least contain a valid
4122 * header and at least a frame.
4123 * To estimate the minimal space to build a packet, we consider the worst case:
4124 - there is not enough space to build ack-eliciting frames from
4125 qel->pktns->tx.frms. This is safe to consider this because when we build
4126 a packet we first build the ACK frames, then the ack-eliciting frames
4127 from qel->pktns->tx.frms only if there is enough space for these
4128 ack-eliciting frames, finally PING and PADDING frames if needed,
4129 - we have to ensure there is enough space to build an ACK frame if required,
4130 and a PING frame, even if we do not have to probe,
4131 - we also have to verify there is enough space to build a PADDING frame
4132 if needed, especially if there is no need to send an ACK frame.
4133 * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build
4134 * a valid packet).
4135 */
4136static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt,
4137 int ack, int nb_pto_dgrams,
4138 struct quic_enc_level *qel, struct quic_conn *conn)
4139{
4140 size_t minlen, token_fields_len;
4141 /* XXX FIXME XXX : ack delay not supported */
4142 uint64_t ack_delay = 0;
4143 size_t ack_frm_len = 0;
4144
4145 TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT,
4146 conn->conn, NULL, NULL, &room);
4147 /* When we do not have to probe nor send acks either, we must take into
4148 * an account the data which have already been prepared and limit
4149 * the size of this packet. We will potentially build an ack-eliciting
4150 * packet.
4151 */
4152 if (!nb_pto_dgrams && !ack) {
4153 size_t path_room;
4154
4155 path_room = quic_path_prep_data(conn->path);
4156 if (room > path_room)
4157 room = path_room;
4158 }
4159
4160 if (ack)
4161 /* A frame is made of 1 byte for the frame type. */
4162 ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz;
4163
4164 /* XXX FIXME XXX : token not supported */
4165 token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
4166 /* Check there is enough room to build the header followed by a token,
4167 * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes
4168 * encryption tag is also taken into an account. Note that we have no
4169 * knowledge of the packet number for this packet. It must be atomically
4170 * incremented each time a packet is built. But before building a packet
4171 * we must estimate if it may be built if we do not want to consume a packet
4172 * number for nothing! Note that we add 1 byte more to
4173 * <minlen> to be able to build an ack-eliciting packet when probing without
4174 * ack-eliciting frames to send. In this case we need to add a 1-byte length
4175 * PING frame.
4176 */
4177 minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1;
4178 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
4179 minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len
4180 + token_fields_len;
4181 else
4182 minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len;
4183
4184 /* Consider any PADDING frame to add */
4185 if (objt_server(conn->conn->target) &&
4186 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
4187 minlen < QUIC_INITIAL_PACKET_MINLEN) {
4188 /* Pad too short client Initial packet */
4189 minlen += QUIC_INITIAL_PACKET_MINLEN - minlen;
4190 }
4191 else if (!ack) {
4192 /* Consider we will have to add the longest short PADDING frame to
4193 * protect a 1-byte length packet number.
4194 */
4195 minlen += QUIC_PACKET_PN_MAXLEN - 1;
4196 }
4197
4198 if (room < minlen) {
4199 TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT,
4200 conn->conn, NULL, NULL, &room);
4201 return 0;
4202 }
4203
4204 return 1;
4205}
4206
4207/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004208 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4209 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4210 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004211 * 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 +02004212 * 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 +02004213 * having returned from this function.
4214 * 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 +01004215 * number field in this packet. <pn_len> will also have the packet number
4216 * length as value.
4217 *
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05004218 * Always succeeds: this is the responsibility of the caller to ensure there is
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004219 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004220 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004221static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004222 struct quic_tx_packet *pkt, int ack, int nb_pto_dgrams,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004223 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
4224 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004225{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004226 unsigned char *beg;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004227 size_t len, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004228 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4229 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004230 size_t ack_frm_len;
4231 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004232 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004233
Frédéric Lécailleea604992020-12-24 13:01:37 +01004234 /* Length field value with CRYPTO frames if present. */
4235 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004236 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004237 /* When not probing and not acking, reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004238 * the congestion controller window. So, we do not limit the size of this
4239 * packet if we have an ACK frame to send because an ACK frame is not
4240 * ack-eliciting. This size will be limited if we have ack-eliciting
4241 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004242 */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004243 if (!nb_pto_dgrams && !ack) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004244 size_t path_room;
4245
4246 path_room = quic_path_prep_data(conn->path);
4247 if (end - beg > path_room)
4248 end = beg + path_room;
4249 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004250
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004251 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004252 /* packet number length */
4253 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004254 /* Build the header */
4255 if (pkt->type == QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004256 quic_build_packet_short_header(&pos, end, *pn_len, conn);
4257 else
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004258 quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn);
4259 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4260 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004261 *pos++ = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004262 /* Ensure there is enough room for the TLS encryption tag */
4263 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004264 /* Build an ACK frame if required. */
4265 ack_frm_len = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004266 if (ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004267 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004268 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004269 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4270 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4271 * that from here, we do not know if we will have to send a PING frame.
4272 * This will be decided after having computed the ack-eliciting frames
4273 * to be added to this packet.
4274 */
4275 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 +01004276 if (!ack_frm_len) {
4277 ssize_t room = end - pos;
4278 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4279 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004280 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004281 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004282 }
4283
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004284 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004285 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004286 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004287 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004288
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004289 /* Initialize the length of the frames built below to <len>.
4290 * If any frame could be successfully built by qc_build_frms(),
4291 * we will have len_frms > len.
4292 */
4293 len_frms = len;
4294 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01004295 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4296 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004297 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004298
4299 add_ping_frm = 0;
4300 padding_len = 0;
4301 if (objt_server(conn->conn->target) &&
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004302 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004303 len < QUIC_INITIAL_PACKET_MINLEN) {
4304 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
4305 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004306 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004307 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004308 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004309 add_ping_frm = 1;
4310 len += 1;
4311 }
4312 /* If there is no frame at all to follow, add at least a PADDING frame. */
4313 if (!ack_frm_len)
4314 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4315 }
4316
4317 /* Length (of the remaining data). Must not fail because, the buffer size
4318 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004319 * for the encryption tag. It must be taken into an account for the length
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004320 * of this packet.
4321 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01004322 if (len_frms)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004323 len = len_frms + QUIC_TLS_TAG_LEN;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004324 else
4325 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004326 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004327 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004328
4329 /* Packet number field address. */
4330 *buf_pn = pos;
4331
4332 /* Packet number encoding. */
4333 quic_packet_number_encode(&pos, end, pn, *pn_len);
4334
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004335 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4336 ssize_t room = end - pos;
4337 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4338 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004339 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004340 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004341
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004342 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004343 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004344 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004345
4346 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02004347 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004348 ssize_t room = end - pos;
4349 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4350 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004351 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004352 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004353 }
4354 }
4355
4356 /* Build a PING frame if needed. */
4357 if (add_ping_frm) {
4358 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004359 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4360 ssize_t room = end - pos;
4361 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4362 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004363 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004364 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004365 }
4366
4367 /* Build a PADDING frame if needed. */
4368 if (padding_len) {
4369 frm.type = QUIC_FT_PADDING;
4370 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004371 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4372 ssize_t room = end - pos;
4373 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4374 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004375 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004376 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004377 }
4378
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004379 /* Always reset this variable as this function has no idea
4380 * if it was set. It is handle by the loss detection timer.
4381 */
4382 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004383 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004384}
4385
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004386static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004387{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004388 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004389 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004390 pkt->cdata_len = 0;
4391 pkt->in_flight_len = 0;
4392 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004393 pkt->next = NULL;
4394 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004395}
4396
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004397/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004398static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4399{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004400 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004401
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004402 if (!pkt)
4403 return;
4404
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004405 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004406 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004407 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004408 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004409 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004410}
4411
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004412/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4413 * type for <qc> QUIC connection from <qel> encryption level.
4414 * Return -2 if the packet could not be allocated or encrypted for any reason,
4415 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004416 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004417static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4418 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004419 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004420 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02004421 int ack, int nb_pto_dgrams, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004422{
4423 /* The pointer to the packet number field. */
4424 unsigned char *buf_pn;
4425 unsigned char *beg, *end, *payload;
4426 int64_t pn;
4427 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004428 struct quic_tls_ctx *tls_ctx;
4429 struct quic_tx_packet *pkt;
4430
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004431 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004432 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004433 pkt = pool_alloc(pool_head_quic_tx_packet);
4434 if (!pkt) {
4435 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 +02004436 *err = -2;
4437 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004438 }
4439
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004440 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004441 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004442 pn_len = 0;
4443 buf_pn = NULL;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004444 if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004445 *err = -1;
4446 goto err;
4447 }
4448
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004449 /* Consume a packet number. */
4450 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004451 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 +01004452
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004453 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004454 payload = buf_pn + pn_len;
4455 payload_len = end - payload;
4456 aad_len = payload - beg;
4457
4458 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004459 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4460 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004461 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004462 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004463
4464 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004465 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004466 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4467 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4468 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004469 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004470 goto err;
4471 }
4472
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004473 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4474 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004475 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004476 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004477 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004478 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004479 pkt->in_flight_len = pkt->len;
4480 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004481 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004482 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004483 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4484
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004485 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004486
4487 err:
4488 free_quic_tx_packet(pkt);
4489 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004490 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004491}
4492
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004493/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4494 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004495 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004496static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4497 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004498{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004499 size_t try, done = 0;
4500
4501 if (!conn_ctrl_ready(conn))
4502 return 0;
4503
4504 if (!fd_recv_ready(conn->handle.fd))
4505 return 0;
4506
4507 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004508
4509 /* read the largest possible block. For this, we perform only one call
4510 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004511 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004512 */
4513 while (count > 0) {
4514 try = b_contig_space(buf);
4515 if (!try)
4516 break;
4517
4518 if (try > count)
4519 try = count;
4520
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004521 b_add(buf, try);
4522 done += try;
4523 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004524 }
4525
4526 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4527 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4528
4529 leave:
4530 return done;
4531
4532 read0:
4533 conn_sock_read0(conn);
4534 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4535
4536 /* Now a final check for a possible asynchronous low-level error
4537 * report. This can happen when a connection receives a reset
4538 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4539 * we might have come from there by just checking POLL_HUP instead
4540 * of recv()'s return value 0, so we have no way to tell there was
4541 * an error without checking.
4542 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004543 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004544 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4545 goto leave;
4546}
4547
4548
4549/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4550 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4551 * other pending data for example, but this flag is ignored at the moment.
4552 * Only one call to send() is performed, unless the buffer wraps, in which case
4553 * a second call may be performed. The connection's flags are updated with
4554 * whatever special event is detected (error, empty). The caller is responsible
4555 * for taking care of those events and avoiding the call if inappropriate. The
4556 * function does not call the connection's polling update function, so the caller
4557 * is responsible for this. It's up to the caller to update the buffer's contents
4558 * based on the return value.
4559 */
4560static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4561{
4562 ssize_t ret;
4563 size_t try, done;
4564 int send_flag;
4565
4566 if (!conn_ctrl_ready(conn))
4567 return 0;
4568
4569 if (!fd_send_ready(conn->handle.fd))
4570 return 0;
4571
4572 done = 0;
4573 /* send the largest possible block. For this we perform only one call
4574 * to send() unless the buffer wraps and we exactly fill the first hunk,
4575 * in which case we accept to do it once again.
4576 */
4577 while (count) {
4578 try = b_contig_data(buf, done);
4579 if (try > count)
4580 try = count;
4581
4582 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4583 if (try < count || flags & CO_SFL_MSG_MORE)
4584 send_flag |= MSG_MORE;
4585
4586 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4587 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4588 if (ret > 0) {
4589 count -= ret;
4590 done += ret;
4591
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004592 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004593 conn->flags |= CO_FL_WAIT_L4L6;
4594 /* if the system buffer is full, don't insist */
4595 if (ret < try)
4596 break;
4597 }
4598 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4599 /* nothing written, we need to poll for write first */
4600 fd_cant_send(conn->handle.fd);
4601 break;
4602 }
4603 else if (errno != EINTR) {
4604 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4605 break;
4606 }
4607 }
4608 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4609 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4610
4611 if (done > 0) {
4612 /* we count the total bytes sent, and the send rate for 32-byte
4613 * blocks. The reason for the latter is that freq_ctr are
4614 * limited to 4GB and that it's not enough per second.
4615 */
4616 _HA_ATOMIC_ADD(&global.out_bytes, done);
4617 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4618 }
4619 return done;
4620}
4621
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004622/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4623 * event subscriber <es> is not allowed to change from a previous call as long
4624 * as at least one event is still subscribed. The <event_type> must only be a
4625 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4626 */
4627static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4628{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004629 struct qcc *qcc = conn->qc->qcc;
4630
4631 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4632 BUG_ON(qcc->subs && qcc->subs != es);
4633
4634 es->events |= event_type;
4635 qcc->subs = es;
4636
4637 if (event_type & SUB_RETRY_RECV)
4638 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn, qcc);
4639
4640 if (event_type & SUB_RETRY_SEND)
4641 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn, qcc);
4642
4643 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004644}
4645
4646/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4647 * The <es> pointer is not allowed to differ from the one passed to the
4648 * subscribe() call. It always returns zero.
4649 */
4650static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4651{
4652 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4653}
4654
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004655/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4656 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4657 * parameters of this session.
4658 * This is the responsibility of the caller to check the validity of all the
4659 * pointers passed as parameter to this function.
4660 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4661 * CO_ER_SSL_NO_MEM.
4662 */
4663static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4664 unsigned char *params, size_t params_len)
4665{
4666 int retry;
4667
4668 retry = 1;
4669 retry:
4670 *ssl = SSL_new(ssl_ctx);
4671 if (!*ssl) {
4672 if (!retry--)
4673 goto err;
4674
4675 pool_gc(NULL);
4676 goto retry;
4677 }
4678
4679 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4680 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
4681 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
4682 goto err;
4683
4684 SSL_free(*ssl);
4685 *ssl = NULL;
4686 if (!retry--)
4687 goto err;
4688
4689 pool_gc(NULL);
4690 goto retry;
4691 }
4692
4693 return 0;
4694
4695 err:
4696 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4697 return -1;
4698}
4699
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004700/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4701 * connection with <xprt_ctx> as address of the xprt context.
4702 * Returns 1 if succeeded, 0 if not.
4703 */
4704static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4705{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004706 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004707
4708 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4709
4710 if (*xprt_ctx)
4711 goto out;
4712
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004713 ctx = pool_alloc(pool_head_quic_conn_ctx);
4714 if (!ctx) {
4715 conn->err_code = CO_ER_SYS_MEMLIM;
4716 goto err;
4717 }
4718
4719 ctx->wait_event.tasklet = tasklet_new();
4720 if (!ctx->wait_event.tasklet) {
4721 conn->err_code = CO_ER_SYS_MEMLIM;
4722 goto err;
4723 }
4724
4725 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4726 ctx->wait_event.tasklet->context = ctx;
4727 ctx->wait_event.events = 0;
4728 ctx->conn = conn;
4729 ctx->subs = NULL;
4730 ctx->xprt_ctx = NULL;
4731
4732 ctx->xprt = xprt_get(XPRT_QUIC);
4733 if (objt_server(conn->target)) {
4734 /* Server */
4735 struct server *srv = __objt_server(conn->target);
4736 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004737 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004738 int ssl_err, ipv4;
4739
4740 ssl_err = SSL_ERROR_NONE;
4741 if (RAND_bytes(dcid, sizeof dcid) != 1)
4742 goto err;
4743
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004744 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004745 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004746 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004747 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004748 goto err;
4749
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004750 /* Insert our SCID, the connection ID for the QUIC client. */
4751 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4752
4753 conn->qc = qc;
4754 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004755 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
4756 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004757 goto err;
4758
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004759 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004760 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004761 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4762 qc->enc_params_len =
4763 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4764 &qc->rx.params, 0);
4765 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004766 goto err;
4767
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004768 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
4769 qc->enc_params, qc->enc_params_len) == -1)
4770 goto err;
4771
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004772 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004773 SSL_set_connect_state(ctx->ssl);
4774 ssl_err = SSL_do_handshake(ctx->ssl);
4775 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004776 int st;
4777
4778 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004779 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4780 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004781 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004782 }
4783 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004784 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004785 goto err;
4786 }
4787 }
4788 }
4789 else if (objt_listener(conn->target)) {
4790 /* Listener */
4791 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004792 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004793
Frédéric Lécailled4d6aa72021-09-03 15:56:18 +02004794 ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004795 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4796 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004797 goto err;
4798
4799 SSL_set_accept_state(ctx->ssl);
4800 }
4801
4802 *xprt_ctx = ctx;
4803
4804 /* Leave init state and start handshake */
4805 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004806
4807 out:
4808 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4809
4810 return 0;
4811
4812 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004813 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004814 tasklet_free(ctx->wait_event.tasklet);
4815 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004816 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004817 return -1;
4818}
4819
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004820/* Start the QUIC transport layer */
4821static int qc_xprt_start(struct connection *conn, void *ctx)
4822{
4823 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004824 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004825
4826 qc = conn->qc;
4827 if (!quic_conn_init_timer(qc)) {
4828 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4829 return 0;
4830 }
4831
4832 tasklet_wakeup(qctx->wait_event.tasklet);
4833 return 1;
4834}
4835
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004836/* transport-layer operations for QUIC connections. */
4837static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004838 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004839 .snd_buf = quic_conn_from_buf,
4840 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004841 .subscribe = quic_conn_subscribe,
4842 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004843 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004844 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004845 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4846 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01004847 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004848 .name = "QUIC",
4849};
4850
4851__attribute__((constructor))
4852static void __quic_conn_init(void)
4853{
4854 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4855 xprt_register(XPRT_QUIC, &ssl_quic);
4856}
4857
4858__attribute__((destructor))
4859static void __quic_conn_deinit(void)
4860{
4861 BIO_meth_free(ha_quic_meth);
4862}
4863
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004864/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
4865 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004866 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004867 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004868static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004869 struct sockaddr_storage *saddr, qpkt_read_func *func)
4870{
4871 unsigned char *pos;
4872 const unsigned char *end;
4873 struct quic_dgram_ctx dgram_ctx = {
4874 .dcid_node = NULL,
4875 .owner = owner,
4876 };
4877
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004878 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004879 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004880 do {
4881 int ret;
4882 struct quic_rx_packet *pkt;
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004883 size_t pkt_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004884
Willy Tarreaue4498932021-03-22 21:13:05 +01004885 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004886 if (!pkt)
4887 goto err;
4888
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004889 quic_rx_packet_refinc(pkt);
4890 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004891 pkt_len = pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02004892 quic_rx_packet_refdec(pkt);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004893 if (ret == -1 && !pkt_len)
4894 /* If the packet length could not be found, we cannot continue. */
4895 break;
4896
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004897 } while (pos < end);
4898
4899 /* Increasing the received bytes counter by the UDP datagram length
4900 * if this datagram could be associated to a connection.
4901 */
4902 if (dgram_ctx.qc)
4903 dgram_ctx.qc->rx.bytes += len;
4904
4905 return pos - (unsigned char *)buf;
4906
4907 err:
4908 return -1;
4909}
4910
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004911ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004912 struct sockaddr_storage *saddr)
4913{
4914 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4915}
4916
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004917/*
4918 * Local variables:
4919 * c-indent-level: 8
4920 * c-basic-offset: 8
4921 * End:
4922 */