blob: 40346f9bfa34607cad4a6834ec71b7956ef0fee9 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
25#include <haproxy/buf-t.h>
26#include <haproxy/compat.h>
27#include <haproxy/api.h>
28#include <haproxy/debug.h>
29#include <haproxy/tools.h>
30#include <haproxy/ticks.h>
31#include <haproxy/time.h>
32
33#include <haproxy/connection.h>
34#include <haproxy/fd.h>
35#include <haproxy/freq_ctr.h>
36#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037#include <haproxy/h3.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010038#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010039#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/pipe.h>
41#include <haproxy/proxy.h>
42#include <haproxy/quic_cc.h>
43#include <haproxy/quic_frame.h>
44#include <haproxy/quic_loss.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020045#include <haproxy/cbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010046#include <haproxy/quic_tls.h>
47#include <haproxy/ssl_sock.h>
48#include <haproxy/stream_interface.h>
49#include <haproxy/task.h>
50#include <haproxy/trace.h>
51#include <haproxy/xprt_quic.h>
52
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020053/* This is the values of some QUIC transport parameters when absent.
54 * Should be used to initialize any transport parameters (local or remote)
55 * before updating them with customized values.
56 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010057struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020058 .max_udp_payload_size = QUIC_DFLT_MAX_UDP_PAYLOAD_SIZE,
59 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
60 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020061 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010062};
63
64/* trace source and events */
65static void quic_trace(enum trace_level level, uint64_t mask, \
66 const struct trace_source *src,
67 const struct ist where, const struct ist func,
68 const void *a1, const void *a2, const void *a3, const void *a4);
69
70static const struct trace_event quic_trace_events[] = {
71 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
72 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
73 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
74 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
75 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
76 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
77 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050078 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010079 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
80 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
81 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
82 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
83 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
84 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
85 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
86 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
87 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
88 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
89 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
90 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
91 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
92 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
93 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
94 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
95 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
96 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
97 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010098 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
99 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
100 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
101 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
102 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
103 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100104 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100105 { /* end */ }
106};
107
108static const struct name_desc quic_trace_lockon_args[4] = {
109 /* arg1 */ { /* already used by the connection */ },
110 /* arg2 */ { .name="quic", .desc="QUIC transport" },
111 /* arg3 */ { },
112 /* arg4 */ { }
113};
114
115static const struct name_desc quic_trace_decoding[] = {
116#define QUIC_VERB_CLEAN 1
117 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
118 { /* end */ }
119};
120
121
122struct trace_source trace_quic = {
123 .name = IST("quic"),
124 .desc = "QUIC xprt",
125 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
126 .default_cb = quic_trace,
127 .known_events = quic_trace_events,
128 .lockon_args = quic_trace_lockon_args,
129 .decoding = quic_trace_decoding,
130 .report_events = ~0, /* report everything by default */
131};
132
133#define TRACE_SOURCE &trace_quic
134INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
135
136static BIO_METHOD *ha_quic_meth;
137
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200138DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100139DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200140 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100141DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100142DECLARE_POOL(pool_head_quic_connection_id,
143 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100144DECLARE_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 +0100145DECLARE_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 +0100146DECLARE_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 +0100147DECLARE_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 +0100148DECLARE_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 +0200149DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100150DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100151
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200152static 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 +0200153 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200154 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200155 int ack, int nb_pto_dgrams, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100156
157/* Add traces to <buf> depending on <frm> TX frame type. */
158static inline void chunk_tx_frm_appendf(struct buffer *buf,
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200159 const struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160{
161 switch (frm->type) {
162 case QUIC_FT_CRYPTO:
163 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
164 (unsigned long long)frm->crypto.offset,
165 (unsigned long long)frm->crypto.len);
166 break;
167 default:
168 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
169 }
170}
171
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100172/* Only for debug purpose */
173struct enc_debug_info {
174 unsigned char *payload;
175 size_t payload_len;
176 unsigned char *aad;
177 size_t aad_len;
178 uint64_t pn;
179};
180
181/* Initializes a enc_debug_info struct (only for debug purpose) */
182static inline void enc_debug_info_init(struct enc_debug_info *edi,
183 unsigned char *payload, size_t payload_len,
184 unsigned char *aad, size_t aad_len, uint64_t pn)
185{
186 edi->payload = payload;
187 edi->payload_len = payload_len;
188 edi->aad = aad;
189 edi->aad_len = aad_len;
190 edi->pn = pn;
191}
192
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100193/* Trace callback for QUIC.
194 * These traces always expect that arg1, if non-null, is of type connection.
195 */
196static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
197 const struct ist where, const struct ist func,
198 const void *a1, const void *a2, const void *a3, const void *a4)
199{
200 const struct connection *conn = a1;
201
202 if (conn) {
203 struct quic_tls_secrets *secs;
204 struct quic_conn *qc;
205
206 qc = conn->qc;
207 chunk_appendf(&trace_buf, " : conn@%p", conn);
208 if ((mask & QUIC_EV_CONN_INIT) && qc) {
209 chunk_appendf(&trace_buf, "\n odcid");
210 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100211 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100212 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100213 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100214 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215 }
216
217 if (mask & QUIC_EV_CONN_ADDDATA) {
218 const enum ssl_encryption_level_t *level = a2;
219 const size_t *len = a3;
220
221 if (level) {
222 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
223
224 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
225 }
226 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100227 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100228 }
229 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
230 /* Initial read & write secrets. */
231 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
232 const unsigned char *rx_sec = a2;
233 const unsigned char *tx_sec = a3;
234
235 secs = &qc->els[level].tls_ctx.rx;
236 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
237 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
238 if (rx_sec)
239 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
240 quic_tls_keys_hexdump(&trace_buf, secs);
241 }
242 secs = &qc->els[level].tls_ctx.tx;
243 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
244 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
245 if (tx_sec)
246 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
247 quic_tls_keys_hexdump(&trace_buf, secs);
248 }
249 }
250 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
251 const enum ssl_encryption_level_t *level = a2;
252 const unsigned char *secret = a3;
253 const size_t *secret_len = a4;
254
255 if (level) {
256 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
257
258 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
259 if (secret && secret_len)
260 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
261 secs = &qc->els[lvl].tls_ctx.rx;
262 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
263 quic_tls_keys_hexdump(&trace_buf, secs);
264 }
265 }
266
267 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
268 const enum ssl_encryption_level_t *level = a2;
269 const unsigned char *secret = a3;
270 const size_t *secret_len = a4;
271
272 if (level) {
273 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
274
275 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
276 if (secret && secret_len)
277 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
278 secs = &qc->els[lvl].tls_ctx.tx;
279 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
280 quic_tls_keys_hexdump(&trace_buf, secs);
281 }
282
283 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100284
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100285 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100286 const struct quic_tx_packet *pkt = a2;
287 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100288 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100289
290 if (qel) {
291 struct quic_pktns *pktns;
292
293 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100294 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
295 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100296 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100297 (unsigned long long)qc->path->cwnd,
298 (unsigned long long)qc->path->prep_in_flight,
299 (unsigned long long)qc->path->in_flight,
300 (unsigned long long)pktns->tx.in_flight,
301 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100302 }
303 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200304 const struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100305 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
306 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
307 list_for_each_entry(frm, &pkt->frms, list)
308 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100309 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
310 }
311
312 if (room) {
313 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
314 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
315 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100316 }
317 }
318
319 if (mask & QUIC_EV_CONN_HDSHK) {
320 const enum quic_handshake_state *state = a2;
321 const int *err = a3;
322
323 if (state)
324 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
325 if (err)
326 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
327 }
328
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100329 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100330 const struct quic_rx_packet *pkt = a2;
331 const unsigned long *pktlen = a3;
332 const SSL *ssl = a4;
333
334 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100335 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100336 pkt, quic_packet_type_enc_level_char(pkt->type));
337 if (pkt->pnl)
338 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
339 (unsigned long long)pkt->pn);
340 if (pkt->token_len)
341 chunk_appendf(&trace_buf, " toklen=%llu",
342 (unsigned long long)pkt->token_len);
343 if (pkt->aad_len)
344 chunk_appendf(&trace_buf, " aadlen=%llu",
345 (unsigned long long)pkt->aad_len);
346 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
347 pkt->flags, (unsigned long long)pkt->len);
348 }
349 if (pktlen)
350 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
351 if (ssl) {
352 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
353 chunk_appendf(&trace_buf, " el=%c",
354 quic_enc_level_char(ssl_to_quic_enc_level(level)));
355 }
356 }
357
358 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
359 const struct quic_rx_packet *pkt = a2;
360 const struct quic_rx_crypto_frm *cf = a3;
361 const SSL *ssl = a4;
362
363 if (pkt)
364 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
365 quic_packet_type_enc_level_char(pkt->type),
366 (unsigned long long)pkt->pn);
367 if (cf)
368 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
369 (unsigned long long)cf->offset_node.key,
370 (unsigned long long)cf->len);
371 if (ssl) {
372 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
373 chunk_appendf(&trace_buf, " el=%c",
374 quic_enc_level_char(ssl_to_quic_enc_level(level)));
375 }
376 }
377
378 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
379 const struct quic_frame *frm = a2;
380
381 if (frm)
382 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
383 }
384
385 if (mask & QUIC_EV_CONN_PHPKTS) {
386 const struct quic_enc_level *qel = a2;
387
388 if (qel) {
389 struct quic_pktns *pktns;
390
391 pktns = qc->pktns;
392 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200393 " 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 +0100394 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200395 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille2766e782021-08-30 17:16:07 +0200396 !!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100397 (unsigned long long)qc->path->cwnd,
398 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100399 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100400 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
401 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100402 }
403 }
404
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100405 if (mask & QUIC_EV_CONN_ENCPKT) {
406 const struct enc_debug_info *edi = a2;
407
408 if (edi)
409 chunk_appendf(&trace_buf,
410 " payload=@%p payload_len=%llu"
411 " aad=@%p aad_len=%llu pn=%llu",
412 edi->payload, (unsigned long long)edi->payload_len,
413 edi->aad, (unsigned long long)edi->aad_len,
414 (unsigned long long)edi->pn);
415 }
416
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100417 if (mask & QUIC_EV_CONN_RMHP) {
418 const struct quic_rx_packet *pkt = a2;
419
420 if (pkt) {
421 const int *ret = a3;
422
423 chunk_appendf(&trace_buf, " pkt@%p", pkt);
424 if (ret && *ret)
425 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
426 pkt->pnl, (unsigned long long)pkt->pn);
427 }
428 }
429
430 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200431 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100432 const unsigned long *val1 = a3;
433 const unsigned long *val2 = a4;
434
435 if (frm)
436 chunk_tx_frm_appendf(&trace_buf, frm);
437 if (val1)
438 chunk_appendf(&trace_buf, " %lu", *val1);
439 if (val2)
440 chunk_appendf(&trace_buf, "..%lu", *val2);
441 }
442
443 if (mask & QUIC_EV_CONN_RTTUPDT) {
444 const unsigned int *rtt_sample = a2;
445 const unsigned int *ack_delay = a3;
446 const struct quic_loss *ql = a4;
447
448 if (rtt_sample)
449 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
450 if (ack_delay)
451 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
452 if (ql)
453 chunk_appendf(&trace_buf,
454 " srtt=%ums rttvar=%ums min_rtt=%ums",
455 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
456 }
457 if (mask & QUIC_EV_CONN_CC) {
458 const struct quic_cc_event *ev = a2;
459 const struct quic_cc *cc = a3;
460
461 if (a2)
462 quic_cc_event_trace(&trace_buf, ev);
463 if (a3)
464 quic_cc_state_trace(&trace_buf, cc);
465 }
466
467 if (mask & QUIC_EV_CONN_PKTLOSS) {
468 const struct quic_pktns *pktns = a2;
469 const struct list *lost_pkts = a3;
470 struct quic_conn *qc = conn->qc;
471
472 if (pktns) {
473 chunk_appendf(&trace_buf, " pktns=%s",
474 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
475 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
476 if (pktns->tx.loss_time)
477 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100478 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100479 }
480 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
481 struct quic_tx_packet *pkt;
482
483 chunk_appendf(&trace_buf, " lost_pkts:");
484 list_for_each_entry(pkt, lost_pkts, list)
485 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
486 }
487 }
488
489 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
490 struct quic_conn *qc = conn->qc;
491 const struct quic_pktns *pktns = a2;
492 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100493 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100495 if (ifae_pkts)
496 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
497 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100498 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100499 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100501 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
502 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100503 if (mask & QUIC_EV_CONN_STIMER) {
504 if (pktns->tx.loss_time)
505 chunk_appendf(&trace_buf, " loss_time=%dms",
506 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
507 }
508 if (mask & QUIC_EV_CONN_SPTO) {
509 if (pktns->tx.time_of_last_eliciting)
510 chunk_appendf(&trace_buf, " tole=%dms",
511 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
512 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100513 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100514 }
515 }
516
517 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
518 chunk_appendf(&trace_buf,
519 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
520 }
521 }
522
523 if (mask & QUIC_EV_CONN_SPPKTS) {
524 const struct quic_tx_packet *pkt = a2;
525
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100526 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
527 (unsigned long long)qc->path->cwnd,
528 (unsigned long long)qc->path->prep_in_flight,
529 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100530 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100531 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100532 (unsigned long)pkt->pn_node.key,
533 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
534 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100535 (unsigned long long)pkt->in_flight_len,
536 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100537 }
538 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100539
540 if (mask & QUIC_EV_CONN_SSLALERT) {
541 const uint8_t *alert = a2;
542 const enum ssl_encryption_level_t *level = a3;
543
544 if (alert)
545 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
546 if (level)
547 chunk_appendf(&trace_buf, " el=%c",
548 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
549 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100550
551 if (mask & QUIC_EV_CONN_BCFRMS) {
552 const size_t *sz1 = a2;
553 const size_t *sz2 = a3;
554 const size_t *sz3 = a4;
555
556 if (sz1)
557 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
558 if (sz2)
559 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
560 if (sz3)
561 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
562 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100563
564 if (mask & QUIC_EV_CONN_PSTRM) {
565 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100566
567 if (a2) {
568 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100569
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100570 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
571 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
572 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
573 (unsigned long long)s->id,
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200574 (unsigned long long)s->offset.key,
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100575 (unsigned long long)s->len);
576 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100577 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100578 }
579 if (mask & QUIC_EV_CONN_LPKT) {
580 const struct quic_rx_packet *pkt = a2;
581
582 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200583 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100584 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200585 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
586 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587 }
588
589}
590
591/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200592static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100593{
594 struct quic_conn *qc;
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200595 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100596
597 qc = ctx->conn->qc;
598 if (objt_server(qc->conn->target))
599 return 1;
600
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200601 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
602 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
603 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
604 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200605 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100606 return 1;
607
608 return 0;
609}
610
611/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
612 * both loss detection and PTO and schedule the task assiated to this timer if needed.
613 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200614static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615{
616 struct quic_conn *qc;
617 struct quic_pktns *pktns;
618 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200619 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100620
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100621 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
622 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100623 qc = ctx->conn->qc;
624 pktns = quic_loss_pktns(qc);
625 if (tick_isset(pktns->tx.loss_time)) {
626 qc->timer = pktns->tx.loss_time;
627 goto out;
628 }
629
630 /* XXX TODO: anti-amplification: the timer must be
631 * cancelled for a server which reached the anti-amplification limit.
632 */
633
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100634 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
635 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100636 /* Timer cancellation. */
637 qc->timer = TICK_ETERNITY;
638 goto out;
639 }
640
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200641 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
642 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100643 if (tick_isset(pto))
644 qc->timer = pto;
645 out:
646 task_schedule(qc->timer_task, qc->timer);
647 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
648}
649
650#ifndef OPENSSL_IS_BORINGSSL
651int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
652 const uint8_t *read_secret,
653 const uint8_t *write_secret, size_t secret_len)
654{
655 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
656 struct quic_tls_ctx *tls_ctx =
657 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
658 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
659
660 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
661 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
662 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
663 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
664
665 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
666 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
667 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
668 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
669 read_secret, secret_len)) {
670 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
671 return 0;
672 }
673
674 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
675 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
676 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
677 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
678 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
679 write_secret, secret_len)) {
680 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
681 return 0;
682 }
683
684 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
685 if (objt_server(conn->target) && level == ssl_encryption_application) {
686 const unsigned char *buf;
687 size_t buflen;
688
689 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
690 if (!buflen)
691 return 0;
692
693 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
694 return 0;
695 }
696 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
697
698 return 1;
699}
700#else
701/* ->set_read_secret callback to derive the RX secrets at <level> encryption
702 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500703 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100704 */
705int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
706 const SSL_CIPHER *cipher,
707 const uint8_t *secret, size_t secret_len)
708{
709 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
710 struct quic_tls_ctx *tls_ctx =
711 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
712
713 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
714 tls_ctx->rx.aead = tls_aead(cipher);
715 tls_ctx->rx.md = tls_md(cipher);
716 tls_ctx->rx.hp = tls_hp(cipher);
717
718 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
719 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
720 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
721 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
722 secret, secret_len)) {
723 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
724 goto err;
725 }
726
727 if (objt_server(conn->target) && level == ssl_encryption_application) {
728 const unsigned char *buf;
729 size_t buflen;
730
731 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
732 if (!buflen)
733 goto err;
734
735 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
736 goto err;
737 }
738
739 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
740 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
741
742 return 1;
743
744 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100745 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100746 return 0;
747}
748
749/* ->set_write_secret callback to derive the TX secrets at <level>
750 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500751 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100752 */
753int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
754 const SSL_CIPHER *cipher,
755 const uint8_t *secret, size_t secret_len)
756{
757 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
758 struct quic_tls_ctx *tls_ctx =
759 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
760
761 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
762 tls_ctx->tx.aead = tls_aead(cipher);
763 tls_ctx->tx.md = tls_md(cipher);
764 tls_ctx->tx.hp = tls_hp(cipher);
765
766 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
767 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
768 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
769 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
770 secret, secret_len)) {
771 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
772 goto err;
773 }
774
775 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
776 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
777
778 return 1;
779
780 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100781 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100782 return 0;
783}
784#endif
785
786/* This function copies the CRYPTO data provided by the TLS stack found at <data>
787 * with <len> as size in CRYPTO buffers dedicated to store the information about
788 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
789 * It fails only if it could not managed to allocate enough CRYPTO buffers to
790 * store all the data.
791 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
792 */
793static int quic_crypto_data_cpy(struct quic_enc_level *qel,
794 const unsigned char *data, size_t len)
795{
796 struct quic_crypto_buf **qcb;
797 /* The remaining byte to store in CRYPTO buffers. */
798 size_t cf_offset, cf_len, *nb_buf;
799 unsigned char *pos;
800
801 nb_buf = &qel->tx.crypto.nb_buf;
802 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
803 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
804 cf_len = len;
805
806 while (len) {
807 size_t to_copy, room;
808
809 pos = (*qcb)->data + (*qcb)->sz;
810 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
811 to_copy = len > room ? room : len;
812 if (to_copy) {
813 memcpy(pos, data, to_copy);
814 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
815 qel->tx.crypto.sz += to_copy;
816 (*qcb)->sz += to_copy;
817 pos += to_copy;
818 len -= to_copy;
819 data += to_copy;
820 }
821 else {
822 struct quic_crypto_buf **tmp;
823
824 tmp = realloc(qel->tx.crypto.bufs,
825 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
826 if (tmp) {
827 qel->tx.crypto.bufs = tmp;
828 qcb = &qel->tx.crypto.bufs[*nb_buf];
829 *qcb = pool_alloc(pool_head_quic_crypto_buf);
830 if (!*qcb)
831 return 0;
832
833 (*qcb)->sz = 0;
834 ++*nb_buf;
835 }
836 else {
837 break;
838 }
839 }
840 }
841
842 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
843 * have been buffered.
844 */
845 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200846 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100847
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200848 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100849 if (!frm)
850 return 0;
851
852 frm->type = QUIC_FT_CRYPTO;
853 frm->crypto.offset = cf_offset;
854 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +0200855 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +0200856 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100857 }
858
859 return len == 0;
860}
861
862
863/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
864 * wants to provide the QUIC layer with CRYPTO data.
865 * Returns 1 if succeeded, 0 if not.
866 */
867int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
868 const uint8_t *data, size_t len)
869{
870 struct connection *conn;
871 enum quic_tls_enc_level tel;
872 struct quic_enc_level *qel;
873
874 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
875 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
876 tel = ssl_to_quic_enc_level(level);
877 qel = &conn->qc->els[tel];
878
879 if (tel == -1) {
880 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
881 goto err;
882 }
883
884 if (!quic_crypto_data_cpy(qel, data, len)) {
885 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
886 goto err;
887 }
888
889 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
890 conn, &level, &len);
891
892 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
893 return 1;
894
895 err:
896 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
897 return 0;
898}
899
900int ha_quic_flush_flight(SSL *ssl)
901{
902 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
903
904 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
905 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
906
907 return 1;
908}
909
910int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
911{
912 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
913
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100914 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100915 return 1;
916}
917
918/* QUIC TLS methods */
919static SSL_QUIC_METHOD ha_quic_method = {
920#ifdef OPENSSL_IS_BORINGSSL
921 .set_read_secret = ha_set_rsec,
922 .set_write_secret = ha_set_wsec,
923#else
924 .set_encryption_secrets = ha_quic_set_encryption_secrets,
925#endif
926 .add_handshake_data = ha_quic_add_handshake_data,
927 .flush_flight = ha_quic_flush_flight,
928 .send_alert = ha_quic_send_alert,
929};
930
931/* Initialize the TLS context of a listener with <bind_conf> as configuration.
932 * Returns an error count.
933 */
934int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
935{
936 struct proxy *curproxy = bind_conf->frontend;
937 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
938 int cfgerr = 0;
939
940#if 0
941 /* XXX Did not manage to use this. */
942 const char *ciphers =
943 "TLS_AES_128_GCM_SHA256:"
944 "TLS_AES_256_GCM_SHA384:"
945 "TLS_CHACHA20_POLY1305_SHA256:"
946 "TLS_AES_128_CCM_SHA256";
947#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200948 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100949 long options =
950 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
951 SSL_OP_SINGLE_ECDH_USE |
952 SSL_OP_CIPHER_SERVER_PREFERENCE;
953 SSL_CTX *ctx;
954
955 ctx = SSL_CTX_new(TLS_server_method());
956 bind_conf->initial_ctx = ctx;
957
958 SSL_CTX_set_options(ctx, options);
959#if 0
960 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
961 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
962 "for bind '%s' at [%s:%d].\n",
963 curproxy->id, ciphers,
964 bind_conf->arg, bind_conf->file, bind_conf->line);
965 cfgerr++;
966 }
967#endif
968
969 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
970 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
971 "for bind '%s' at [%s:%d].\n",
972 curproxy->id, groups,
973 bind_conf->arg, bind_conf->file, bind_conf->line);
974 cfgerr++;
975 }
976
977 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
978 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
979 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
980 SSL_CTX_set_default_verify_paths(ctx);
981
982#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
983#ifdef OPENSSL_IS_BORINGSSL
984 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
985 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
986#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
987 if (bind_conf->ssl_conf.early_data) {
988 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
989 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
990 }
991 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
992 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
993#else
994 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
995#endif
996 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
997#endif
998 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
999
1000 return cfgerr;
1001}
1002
1003/* Decode an expected packet number from <truncated_on> its truncated value,
1004 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1005 * the number of bits used to encode this packet number (its length in bytes * 8).
1006 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1007 */
1008static uint64_t decode_packet_number(uint64_t largest_pn,
1009 uint32_t truncated_pn, unsigned int pn_nbits)
1010{
1011 uint64_t expected_pn = largest_pn + 1;
1012 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1013 uint64_t pn_hwin = pn_win / 2;
1014 uint64_t pn_mask = pn_win - 1;
1015 uint64_t candidate_pn;
1016
1017
1018 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1019 /* Note that <pn_win> > <pn_hwin>. */
1020 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1021 candidate_pn + pn_hwin <= expected_pn)
1022 return candidate_pn + pn_win;
1023
1024 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1025 return candidate_pn - pn_win;
1026
1027 return candidate_pn;
1028}
1029
1030/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1031 * cryptographic context.
1032 * <largest_pn> is the largest received packet number and <pn> the address of
1033 * the packet number field for this packet with <byte0> address of its first byte.
1034 * <end> points to one byte past the end of this packet.
1035 * Returns 1 if succeeded, 0 if not.
1036 */
1037static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1038 int64_t largest_pn, unsigned char *pn,
1039 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001040 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001041{
1042 int ret, outlen, i, pnlen;
1043 uint64_t packet_number;
1044 uint32_t truncated_pn = 0;
1045 unsigned char mask[5] = {0};
1046 unsigned char *sample;
1047 EVP_CIPHER_CTX *cctx;
1048 unsigned char *hp_key;
1049
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001050 /* Check there is enough data in this packet. */
1051 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1052 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1053 return 0;
1054 }
1055
1056 cctx = EVP_CIPHER_CTX_new();
1057 if (!cctx) {
1058 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1059 return 0;
1060 }
1061
1062 ret = 0;
1063 sample = pn + QUIC_PACKET_PN_MAXLEN;
1064
1065 hp_key = tls_ctx->rx.hp_key;
1066 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1067 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1068 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1069 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1070 goto out;
1071 }
1072
1073 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1074 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1075 for (i = 0; i < pnlen; i++) {
1076 pn[i] ^= mask[i + 1];
1077 truncated_pn = (truncated_pn << 8) | pn[i];
1078 }
1079
1080 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1081 /* Store remaining information for this unprotected header */
1082 pkt->pn = packet_number;
1083 pkt->pnl = pnlen;
1084
1085 ret = 1;
1086
1087 out:
1088 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001089
1090 return ret;
1091}
1092
1093/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1094 * address, with <payload_len> as payload length, <aad> as address of
1095 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1096 * context.
1097 * Returns 1 if succeeded, 0 if not.
1098 */
1099static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1100 unsigned char *aad, size_t aad_len, uint64_t pn,
1101 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1102{
1103 unsigned char iv[12];
1104 unsigned char *tx_iv = tls_ctx->tx.iv;
1105 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001106 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001107
1108 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1109 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001110 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001111 }
1112
1113 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1114 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1115 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001116 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001117 }
1118
1119 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001120
1121 err:
1122 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1123 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1124 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001125}
1126
1127/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1128 * Returns 1 if succeeded, 0 if not.
1129 */
1130static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1131{
1132 int ret;
1133 unsigned char iv[12];
1134 unsigned char *rx_iv = tls_ctx->rx.iv;
1135 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1136
1137 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1138 return 0;
1139
1140 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1141 pkt->data, pkt->aad_len,
1142 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1143 if (!ret)
1144 return 0;
1145
1146 /* Update the packet length (required to parse the frames). */
1147 pkt->len = pkt->aad_len + ret;
1148
1149 return 1;
1150}
1151
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001152/* Remove from <qcs> stream the acknowledged frames.
1153 * Never fails.
1154 */
1155static void qcs_try_to_consume(struct qcs *qcs)
1156{
1157 struct eb64_node *frm_node;
1158
1159 frm_node = eb64_first(&qcs->tx.acked_frms);
1160 while (frm_node) {
1161 struct quic_stream *strm;
1162
1163 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1164 if (strm->offset.key != qcs->tx.ack_offset)
1165 break;
1166
1167 b_del(strm->buf, strm->len);
1168 qcs->tx.ack_offset += strm->len;
1169 frm_node = eb64_next(frm_node);
1170 eb64_delete(&strm->offset);
1171 }
1172}
1173
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001174/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001175static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001176 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001177{
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001178
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001179 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001180 switch (frm->type) {
1181 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1182 {
1183 struct qcs *qcs = frm->stream.qcs;
1184 struct quic_stream *strm = &frm->stream;
1185
1186 if (qcs->tx.ack_offset == strm->offset.key) {
1187 b_del(strm->buf, strm->len);
1188 qcs->tx.ack_offset += strm->len;
1189 LIST_DELETE(&frm->list);
1190 pool_free(pool_head_quic_frame, frm);
1191 }
1192 else {
1193 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1194 }
1195 qcs_try_to_consume(qcs);
1196 }
1197 break;
1198 default:
1199 LIST_DELETE(&frm->list);
1200 pool_free(pool_head_quic_frame, frm);
1201 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001202}
1203
1204/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1205 * deallocating them, and their TX frames.
1206 * Returns the last node reached to be used for the next range.
1207 * May be NULL if <largest> node could not be found.
1208 */
1209static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1210 struct list *newly_acked_pkts,
1211 struct eb64_node *largest_node,
1212 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001213 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001214{
1215 struct eb64_node *node;
1216 struct quic_tx_packet *pkt;
1217
1218 if (largest_node)
1219 node = largest_node;
1220 else {
1221 node = eb64_lookup(pkts, largest);
1222 while (!node && largest > smallest) {
1223 node = eb64_lookup(pkts, --largest);
1224 }
1225 }
1226
1227 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001228 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001229
1230 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1231 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001232 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001233 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1234 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1235 qc_treat_acked_tx_frm(frm, ctx);
1236 node = eb64_prev(node);
1237 eb64_delete(&pkt->pn_node);
1238 }
1239
1240 return node;
1241}
1242
1243/* Treat <frm> frame whose packet it is attached to has just been detected as non
1244 * acknowledged.
1245 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001246static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001247 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001248 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001249{
1250 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001251 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001252 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001253}
1254
1255
1256/* Free the TX packets of <pkts> list */
1257static inline void free_quic_tx_pkts(struct list *pkts)
1258{
1259 struct quic_tx_packet *pkt, *tmp;
1260
1261 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001262 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001263 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001264 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001265 }
1266}
1267
1268/* Send a packet loss event nofification to the congestion controller
1269 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1270 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1271 * at <now_us> current time.
1272 * Always succeeds.
1273 */
1274static inline void qc_cc_loss_event(struct quic_conn *qc,
1275 unsigned int lost_bytes,
1276 unsigned int newest_time_sent,
1277 unsigned int period,
1278 unsigned int now_us)
1279{
1280 struct quic_cc_event ev = {
1281 .type = QUIC_CC_EVT_LOSS,
1282 .loss.now_ms = now_ms,
1283 .loss.max_ack_delay = qc->max_ack_delay,
1284 .loss.lost_bytes = lost_bytes,
1285 .loss.newest_time_sent = newest_time_sent,
1286 .loss.period = period,
1287 };
1288
1289 quic_cc_event(&qc->path->cc, &ev);
1290}
1291
1292/* Send a packet ack event nofication for each newly acked packet of
1293 * <newly_acked_pkts> list and free them.
1294 * Always succeeds.
1295 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001296static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001297 struct list *newly_acked_pkts)
1298{
1299 struct quic_conn *qc = ctx->conn->qc;
1300 struct quic_tx_packet *pkt, *tmp;
1301 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1302
1303 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1304 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001305 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001306 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001307 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001308 ev.ack.acked = pkt->in_flight_len;
1309 ev.ack.time_sent = pkt->time_sent;
1310 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001311 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001312 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001313 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001314 }
1315
1316}
1317
1318/* Handle <pkts> list of lost packets detected at <now_us> handling
1319 * their TX frames.
1320 * Send a packet loss event to the congestion controller if
1321 * in flight packet have been lost.
1322 * Also frees the packet in <pkts> list.
1323 * Never fails.
1324 */
1325static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001326 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001327 struct list *pkts,
1328 uint64_t now_us)
1329{
1330 struct quic_conn *qc = ctx->conn->qc;
1331 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001332 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001333 uint64_t lost_bytes;
1334
1335 lost_bytes = 0;
1336 oldest_lost = newest_lost = NULL;
1337 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1338 lost_bytes += pkt->in_flight_len;
1339 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001340 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001341 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001342 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001343 /* Treat the frames of this lost packet. */
1344 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1345 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001346 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001347 if (!oldest_lost) {
1348 oldest_lost = newest_lost = pkt;
1349 }
1350 else {
1351 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001352 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001353 newest_lost = pkt;
1354 }
1355 }
1356
1357 if (lost_bytes) {
1358 /* Sent a packet loss event to the congestion controller. */
1359 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1360 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001361 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001362 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001363 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001364 }
1365}
1366
1367/* Look for packet loss from sent packets for <qel> encryption level of a
1368 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1369 * their tree if deemed as lost or set the <loss_time> value the packet number
1370 * space if any not deemed lost.
1371 * Should be called after having received an ACK frame with newly acknowledged
1372 * packets or when the the loss detection timer has expired.
1373 * Always succeeds.
1374 */
1375static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1376 struct quic_conn *qc,
1377 struct list *lost_pkts)
1378{
1379 struct eb_root *pkts;
1380 struct eb64_node *node;
1381 struct quic_loss *ql;
1382 unsigned int loss_delay;
1383
1384 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1385 pkts = &pktns->tx.pkts;
1386 pktns->tx.loss_time = TICK_ETERNITY;
1387 if (eb_is_empty(pkts))
1388 goto out;
1389
1390 ql = &qc->path->loss;
1391 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1392 loss_delay += loss_delay >> 3;
1393 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1394
1395 node = eb64_first(pkts);
1396 while (node) {
1397 struct quic_tx_packet *pkt;
1398 int64_t largest_acked_pn;
1399 unsigned int loss_time_limit, time_sent;
1400
1401 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001402 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001403 node = eb64_next(node);
1404 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1405 break;
1406
1407 time_sent = pkt->time_sent;
1408 loss_time_limit = tick_add(time_sent, loss_delay);
1409 if (tick_is_le(time_sent, now_ms) ||
1410 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1411 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001412 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001413 }
1414 else {
1415 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1416 }
1417 }
1418
1419 out:
1420 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1421}
1422
1423/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1424 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001425 * 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 +01001426 * acked ack-eliciting packet.
1427 * Return 1, if succeeded, 0 if not.
1428 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001429static 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 +01001430 struct quic_enc_level *qel,
1431 unsigned int *rtt_sample,
1432 const unsigned char **pos, const unsigned char *end)
1433{
1434 struct quic_ack *ack = &frm->ack;
1435 uint64_t smallest, largest;
1436 struct eb_root *pkts;
1437 struct eb64_node *largest_node;
1438 unsigned int time_sent, pkt_flags;
1439 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1440 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1441
1442 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1443 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1444 ctx->conn,, &ack->largest_ack);
1445 goto err;
1446 }
1447
1448 if (ack->first_ack_range > ack->largest_ack) {
1449 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1450 ctx->conn,, &ack->first_ack_range);
1451 goto err;
1452 }
1453
1454 largest = ack->largest_ack;
1455 smallest = largest - ack->first_ack_range;
1456 pkts = &qel->pktns->tx.pkts;
1457 pkt_flags = 0;
1458 largest_node = NULL;
1459 time_sent = 0;
1460
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001461 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 +01001462 largest_node = eb64_lookup(pkts, largest);
1463 if (!largest_node) {
1464 TRACE_DEVEL("Largest acked packet not found",
1465 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1466 goto err;
1467 }
1468
1469 time_sent = eb64_entry(&largest_node->node,
1470 struct quic_tx_packet, pn_node)->time_sent;
1471 }
1472
1473 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1474 ctx->conn,, &largest, &smallest);
1475 do {
1476 uint64_t gap, ack_range;
1477
1478 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1479 largest_node, largest, smallest, ctx);
1480 if (!ack->ack_range_num--)
1481 break;
1482
1483 if (!quic_dec_int(&gap, pos, end))
1484 goto err;
1485
1486 if (smallest < gap + 2) {
1487 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1488 ctx->conn,, &gap, &smallest);
1489 goto err;
1490 }
1491
1492 largest = smallest - gap - 2;
1493 if (!quic_dec_int(&ack_range, pos, end))
1494 goto err;
1495
1496 if (largest < ack_range) {
1497 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1498 ctx->conn,, &largest, &ack_range);
1499 goto err;
1500 }
1501
1502 /* Do not use this node anymore. */
1503 largest_node = NULL;
1504 /* Next range */
1505 smallest = largest - ack_range;
1506
1507 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1508 ctx->conn,, &largest, &smallest);
1509 } while (1);
1510
1511 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001512 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001513
1514 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1515 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001516 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001517 }
1518
1519 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1520 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1521 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1522 if (!LIST_ISEMPTY(&lost_pkts))
1523 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1524 }
1525 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1526 if (quic_peer_validated_addr(ctx))
1527 ctx->conn->qc->path->loss.pto_count = 0;
1528 qc_set_timer(ctx);
1529 }
1530
1531
1532 return 1;
1533
1534 err:
1535 free_quic_tx_pkts(&newly_acked_pkts);
1536 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1537 return 0;
1538}
1539
1540/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1541 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001542 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001543 * Return 1 if succeeded, 0 if not.
1544 */
1545static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001546 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001547 const unsigned char *data, size_t len,
1548 struct quic_rx_packet *pkt,
1549 struct quic_rx_crypto_frm *cf)
1550{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001551 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001552 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001553
1554 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1555 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001556 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001557 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1558 TRACE_PROTO("SSL_provide_quic_data() error",
1559 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1560 goto err;
1561 }
1562
1563 el->rx.crypto.offset += len;
1564 TRACE_PROTO("in order CRYPTO data",
1565 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1566
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001567 state = HA_ATOMIC_LOAD(&qc->state);
1568 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001569 ssl_err = SSL_do_handshake(ctx->ssl);
1570 if (ssl_err != 1) {
1571 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1572 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1573 TRACE_PROTO("SSL handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001574 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001575 goto out;
1576 }
1577
1578 TRACE_DEVEL("SSL handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001579 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001580 goto err;
1581 }
1582
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001583 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001584 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001585 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001586 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001587 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001588 } else {
1589 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1590 if (ssl_err != 1) {
1591 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1592 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1593 TRACE_DEVEL("SSL post handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001594 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001595 goto out;
1596 }
1597
1598 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001599 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001600 goto err;
1601 }
1602
1603 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001604 QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001605 }
1606
1607 out:
1608 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1609 return 1;
1610
1611 err:
1612 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1613 return 0;
1614}
1615
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001616/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1617 * <pkt> RX packet.
1618 * Return it if succeeded, NULL if not.
1619 */
1620static inline
1621struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1622 struct quic_rx_packet *pkt)
1623{
1624 struct quic_rx_strm_frm *frm;
1625
1626 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1627 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001628 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001629 frm->len = stream_frm->len;
1630 frm->data = stream_frm->data;
1631 frm->pkt = pkt;
1632 }
1633
1634 return frm;
1635}
1636
1637/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1638 * several streams, depending on the already open onces.
1639 * Return this node if succeeded, NULL if not.
1640 */
1641static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1642{
1643 unsigned int strm_type;
1644 int64_t sub_id;
1645 struct eb64_node *strm_node;
1646
1647 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1648
1649 strm_type = id & QCS_ID_TYPE_MASK;
1650 sub_id = id >> QCS_ID_TYPE_SHIFT;
1651 strm_node = NULL;
1652 if (qc_local_stream_id(qcc, id)) {
1653 /* Local streams: this stream must be already opened. */
1654 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1655 if (!strm_node) {
1656 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1657 goto out;
1658 }
1659 }
1660 else {
1661 /* Remote streams. */
1662 struct eb_root *strms;
1663 uint64_t largest_id;
1664 enum qcs_type qcs_type;
1665
1666 strms = &qcc->streams_by_id;
1667 qcs_type = qcs_id_type(id);
1668 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1669 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1670 goto out;
1671 }
1672
1673 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1674 * correct value.
1675 */
1676 largest_id = qcc->strms[qcs_type].largest_id;
1677 if (sub_id > (int64_t)largest_id) {
1678 /* RFC: "A stream ID that is used out of order results in all streams
1679 * of that type with lower-numbered stream IDs also being opened".
1680 * So, let's "open" these streams.
1681 */
1682 int64_t i;
1683 struct qcs *qcs;
1684
1685 qcs = NULL;
1686 for (i = largest_id + 1; i <= sub_id; i++) {
1687 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1688 if (!qcs) {
1689 TRACE_PROTO("Could not allocate a new stream",
1690 QUIC_EV_CONN_PSTRM, qcc->conn);
1691 goto out;
1692 }
1693
1694 qcc->strms[qcs_type].largest_id = i;
1695 }
1696 if (qcs)
1697 strm_node = &qcs->by_id;
1698 }
1699 else {
1700 strm_node = eb64_lookup(strms, id);
1701 }
1702 }
1703
1704 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1705 return strm_node;
1706
1707 out:
1708 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1709 return NULL;
1710}
1711
1712/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1713 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1714 * to reflect the data which have been consumed.
1715 */
1716static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1717{
1718 size_t ret;
1719
1720 ret = 0;
1721 while (strm_frm->len) {
1722 size_t try;
1723
1724 try = b_contig_space(buf);
1725 if (!try)
1726 break;
1727
1728 if (try > strm_frm->len)
1729 try = strm_frm->len;
1730 memcpy(b_tail(buf), strm_frm->data, try);
1731 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001732 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001733 b_add(buf, try);
1734 ret += try;
1735 }
1736
1737 return ret;
1738}
1739
1740/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1741 * streams may be open. The data are copied to the stream RX buffer if possible.
1742 * If not, the STREAM frame is stored to be treated again later.
1743 * We rely on the flow control so that not to store too much STREAM frames.
1744 * Return 1 if succeeded, 0 if not.
1745 */
1746static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1747 struct quic_stream *strm_frm,
1748 struct quic_conn *qc)
1749{
1750 struct qcs *strm;
1751 struct eb64_node *strm_node, *frm_node;
1752 struct quic_rx_strm_frm *frm;
1753
1754 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1755 if (!strm_node) {
1756 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1757 return 0;
1758 }
1759
1760 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001761 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001762 /* FIXME: handle the case where this frame overlap others */
1763 if (frm_node) {
1764 TRACE_PROTO("Already existing stream data",
1765 QUIC_EV_CONN_PSTRM, qc->conn);
1766 goto out;
1767 }
1768
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001769 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001770 int ret;
1771
1772 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1773 goto store_frm;
1774
1775 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1776 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1777 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1778 return 0;
1779 }
1780
1781 strm->rx.offset += ret;
1782 }
1783
1784 if (!strm_frm->len)
1785 goto out;
1786
1787 store_frm:
1788 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1789 if (!frm) {
1790 TRACE_PROTO("Could not alloc RX STREAM frame",
1791 QUIC_EV_CONN_PSTRM, qc->conn);
1792 return 0;
1793 }
1794
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001795 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001796 quic_rx_packet_refinc(pkt);
1797
1798 out:
1799 return 1;
1800}
1801
1802/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1803 * streams may be open. The data are copied to the stream RX buffer if possible.
1804 * If not, the STREAM frame is stored to be treated again later.
1805 * We rely on the flow control so that not to store too much STREAM frames.
1806 * Return 1 if succeeded, 0 if not.
1807 */
1808static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1809 struct quic_stream *strm_frm,
1810 struct quic_conn *qc)
1811{
1812 struct qcs *strm;
1813 struct eb64_node *strm_node, *frm_node;
1814 struct quic_rx_strm_frm *frm;
1815 size_t strm_frm_len;
1816
1817 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1818 if (!strm_node) {
1819 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1820 return 0;
1821 }
1822
1823 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001824 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001825 /* FIXME: handle the case where this frame overlap others */
1826 if (frm_node) {
1827 TRACE_PROTO("Already existing stream data",
1828 QUIC_EV_CONN_PSTRM, qc->conn);
1829 goto out;
1830 }
1831
1832 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001833 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001834 int ret;
1835
1836 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1837 goto store_frm;
1838
1839 /* qc_strm_cpy() will modify the offset, depending on the number
1840 * of bytes copied.
1841 */
1842 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1843 /* Inform the application of the arrival of this new stream */
1844 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1845 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1846 return 0;
1847 }
1848
1849 if (ret)
1850 ruqs_notify_recv(strm);
1851
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001852 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001853 }
1854 /* Take this frame into an account for the stream flow control */
1855 strm->rx.offset += strm_frm_len;
1856 /* It all the data were provided to the application, there is no need to
1857 * store any more inforamtion for it.
1858 */
1859 if (!strm_frm->len)
1860 goto out;
1861
1862 store_frm:
1863 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1864 if (!frm) {
1865 TRACE_PROTO("Could not alloc RX STREAM frame",
1866 QUIC_EV_CONN_PSTRM, qc->conn);
1867 return 0;
1868 }
1869
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001870 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001871 quic_rx_packet_refinc(pkt);
1872
1873 out:
1874 return 1;
1875}
1876
1877static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1878 struct quic_stream *strm_frm,
1879 struct quic_conn *qc)
1880{
1881 if (strm_frm->id & QCS_ID_DIR_BIT)
1882 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1883 else
1884 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1885}
1886
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001887/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1888 * as I/O handler context and <qel> as encryption level.
1889 * Returns 1 if succeeded, 0 if failed.
1890 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001891static 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 +01001892 struct quic_enc_level *qel)
1893{
1894 struct quic_frame frm;
1895 const unsigned char *pos, *end;
1896 struct quic_conn *conn = ctx->conn->qc;
1897
1898 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1899 /* Skip the AAD */
1900 pos = pkt->data + pkt->aad_len;
1901 end = pkt->data + pkt->len;
1902
1903 while (pos < end) {
1904 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1905 goto err;
1906
1907 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001908 case QUIC_FT_PADDING:
1909 if (pos != end) {
1910 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1911 goto err;
1912 }
1913 break;
1914 case QUIC_FT_PING:
1915 break;
1916 case QUIC_FT_ACK:
1917 {
1918 unsigned int rtt_sample;
1919
1920 rtt_sample = 0;
1921 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1922 goto err;
1923
1924 if (rtt_sample) {
1925 unsigned int ack_delay;
1926
1927 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1928 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1929 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1930 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001931 break;
1932 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001933 case QUIC_FT_CRYPTO:
1934 if (frm.crypto.offset != qel->rx.crypto.offset) {
1935 struct quic_rx_crypto_frm *cf;
1936
1937 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1938 if (!cf) {
1939 TRACE_DEVEL("CRYPTO frame allocation failed",
1940 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1941 goto err;
1942 }
1943
1944 cf->offset_node.key = frm.crypto.offset;
1945 cf->len = frm.crypto.len;
1946 cf->data = frm.crypto.data;
1947 cf->pkt = pkt;
1948 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1949 quic_rx_packet_refinc(pkt);
1950 }
1951 else {
1952 /* XXX TO DO: <cf> is used only for the traces. */
1953 struct quic_rx_crypto_frm cf = { };
1954
1955 cf.offset_node.key = frm.crypto.offset;
1956 cf.len = frm.crypto.len;
1957 if (!qc_provide_cdata(qel, ctx,
1958 frm.crypto.data, frm.crypto.len,
1959 pkt, &cf))
1960 goto err;
1961 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001962 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001963 case QUIC_FT_STREAM_8:
1964 case QUIC_FT_STREAM_9:
1965 case QUIC_FT_STREAM_A:
1966 case QUIC_FT_STREAM_B:
1967 case QUIC_FT_STREAM_C:
1968 case QUIC_FT_STREAM_D:
1969 case QUIC_FT_STREAM_E:
1970 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001971 {
1972 struct quic_stream *stream = &frm.stream;
1973
1974 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
1975 if (objt_listener(ctx->conn->target)) {
1976 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
1977 goto err;
1978 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
1979 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001980
1981 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
1982 goto err;
1983
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001984 break;
1985 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001986 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001987 break;
1988 case QUIC_FT_CONNECTION_CLOSE:
1989 case QUIC_FT_CONNECTION_CLOSE_APP:
1990 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001991 case QUIC_FT_HANDSHAKE_DONE:
1992 if (objt_listener(ctx->conn->target))
1993 goto err;
1994
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001995 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001996 break;
1997 default:
1998 goto err;
1999 }
2000 }
2001
2002 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2003 * has successfully parse a Handshake packet. The Initial encryption must also
2004 * be discarded.
2005 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002006 if (HA_ATOMIC_LOAD(&conn->state) == QUIC_HS_ST_SERVER_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002007 pkt->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2008 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2009 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
2010 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002011 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002012 }
2013
2014 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
2015 return 1;
2016
2017 err:
2018 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2019 return 0;
2020}
2021
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002022/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
2023 * buffer. This is the responsability of the caller to check there is enough
2024 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2025 * This function must be called only after having built a correct datagram.
2026 * Always succeeds.
2027 */
2028static inline void qc_set_dg(struct cbuf *cbuf,
2029 uint16_t dglen, struct quic_tx_packet *pkt)
2030{
2031 write_u16(cb_wr(cbuf), dglen);
2032 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2033 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2034}
2035
2036/* Prepare as much as possible handshake packets into <qr> ring buffer for
2037 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2038 * several packets in the same datagram. A header made of two fields is added
2039 * to each datagram: the datagram length followed by the address of the first
2040 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002041 * Returns 1 if succeeded, or 0 if something wrong happened.
2042 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002043static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002044{
2045 struct quic_conn *qc;
2046 enum quic_tls_enc_level tel, next_tel;
2047 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002048 struct cbuf *cbuf;
2049 unsigned char *end_buf, *end, *pos, *spos;
2050 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2051 /* length of datagrams */
2052 uint16_t dglen;
2053 size_t total;
2054 /* Each datagram is prepended with its length followed by the
2055 * address of the first packet in the datagram.
2056 */
2057 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002058
2059 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2060 qc = ctx->conn->qc;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002061 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state))) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002062 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002063 goto err;
2064 }
2065
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002066 start:
2067 total = 0;
2068 dglen = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002069 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002070 cbuf = qr->cbuf;
2071 spos = pos = cb_wr(cbuf);
2072 /* Leave at least <dglen> bytes at the end of this buffer
2073 * to ensure there is enough room to mark the end of prepared
2074 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002075 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002076 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2077 first_pkt = prv_pkt = NULL;
2078 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002079 int err, nb_ptos, ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002080 enum quic_pkt_type pkt_type;
2081
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002082 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002083 nb_ptos = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002084 if (!prv_pkt) {
2085 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2086 do {
2087 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2088 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2089 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002090 ack = HA_ATOMIC_BTR(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002091 /* Do not build any more packet if the TX secrets are not available or
2092 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002093 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002094 * and if there is no more CRYPTO data available or in flight
2095 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002096 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002097 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002098 (!ack && !nb_ptos &&
2099 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2100 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002101 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002102 /* Set the current datagram as prepared into <cbuf> if
2103 * the was already a correct packet which was previously written.
2104 */
2105 if (prv_pkt)
2106 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002107 break;
2108 }
2109
2110 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002111 if (!prv_pkt) {
2112 /* Leave room for the datagram header */
2113 pos += dg_headlen;
2114 end = pos + qc->path->mtu;
2115 }
2116
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002117 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 +02002118 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002119 if (err < 0) {
2120 if (!prv_pkt && nb_ptos)
2121 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002122 if (ack)
2123 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002124 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002125 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002126 case -2:
2127 goto err;
2128 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002129 /* If there was already a correct packet present, set the
2130 * current datagram as prepared into <cbuf>.
2131 */
2132 if (prv_pkt) {
2133 qc_set_dg(cbuf, dglen, first_pkt);
2134 goto stop_build;
2135 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002136 goto out;
2137 default:
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002138 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2139 if (!cur_pkt)
2140 goto err;
2141
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002142 total += cur_pkt->len;
2143 /* keep trace of the first packet in the datagram */
2144 if (!first_pkt)
2145 first_pkt = cur_pkt;
2146 /* Attach the current one to the previous one */
2147 if (prv_pkt)
2148 prv_pkt->next = cur_pkt;
2149 /* Let's say we have to build a new dgram */
2150 prv_pkt = NULL;
2151 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002152 /* Discard the Initial encryption keys as soon as
2153 * a handshake packet could be built.
2154 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002155 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002156 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2157 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2158 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2159 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002160 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002161 }
2162 /* Special case for Initial packets: when they have all
2163 * been sent, select the next level.
2164 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002165 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002166 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2167 (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 +02002168 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2169 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2170 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002171 tel = next_tel;
2172 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002173 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002174 /* If there is data for the next level, do not
2175 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002176 * which sends only one Initial packet, then wait
2177 * for additional CRYPTO data from the server to enter the
2178 * next level.
2179 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002180 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002181 }
2182 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002183 }
2184
2185 /* If we have to build a new datagram, set the current datagram as
2186 * prepared into <cbuf>.
2187 */
2188 if (!prv_pkt) {
2189 qc_set_dg(cbuf, dglen, first_pkt);
2190 first_pkt = NULL;
2191 dglen = 0;
2192 }
2193 }
2194
2195 stop_build:
2196 /* Reset <wr> writer index if in front of <rd> index */
2197 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2198 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2199
2200 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2201 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2202 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2203 /* Mark the end of contiguous data for the reader */
2204 write_u16(cb_wr(cbuf), 0);
2205 cb_add(cbuf, sizeof(uint16_t));
2206 }
2207 }
2208
2209 if (rd && rd <= cbuf->wr) {
2210 cb_wr_reset(cbuf);
2211 if (pos == spos) {
2212 /* Reuse the same buffer if nothing was built. */
2213 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002214 }
2215 }
2216 }
2217
2218 out:
2219 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002220 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002221
2222 err:
2223 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002224 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002225}
2226
2227/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002228 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002229 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002230int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002231{
2232 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002233 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002234
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002235 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002236 cbuf = qr->cbuf;
2237 while (cb_contig_data(cbuf)) {
2238 unsigned char *pos;
2239 struct buffer tmpbuf = { };
2240 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2241 uint16_t dglen;
2242 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002243 unsigned int time_sent;
2244
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002245 pos = cb_rd(cbuf);
2246 dglen = read_u16(pos);
2247 /* End of prepared datagrams.
2248 * Reset the reader index only if in front of the writer index.
2249 */
2250 if (!dglen) {
2251 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2252
2253 if (wr && wr < cbuf->rd) {
2254 cb_rd_reset(cbuf);
2255 continue;
2256 }
2257 break;
2258 }
2259
2260 pos += sizeof dglen;
2261 first_pkt = read_ptr(pos);
2262 pos += sizeof first_pkt;
2263 tmpbuf.area = (char *)pos;
2264 tmpbuf.size = tmpbuf.data = dglen;
2265
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002266 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002267 for (pkt = first_pkt; pkt; pkt = pkt->next)
2268 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002269 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002270 &tmpbuf, tmpbuf.data, 0) <= 0) {
2271 for (pkt = first_pkt; pkt; pkt = pkt->next)
2272 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002273 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002274 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002275
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002276 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002277 qc->tx.bytes += tmpbuf.data;
2278 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002279
2280 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2281 pkt->time_sent = time_sent;
2282 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2283 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002284 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002285 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002286 qc->path->in_flight += pkt->in_flight_len;
2287 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2288 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002289 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002290 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2291 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002292 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002293 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002294 }
2295 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002296
2297 return 1;
2298}
2299
2300/* Build all the frames which must be sent just after the handshake have succeeded.
2301 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2302 * a HANDSHAKE_DONE frame.
2303 * Return 1 if succeeded, 0 if not.
2304 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002305static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002306{
2307 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002308 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002309 struct quic_frame *frm;
2310
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002311 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002312 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002313 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002314 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002315 if (!frm)
2316 return 0;
2317
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002318 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002319 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002320 }
2321
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002322 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002323 struct quic_connection_id *cid;
2324
2325 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002326 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002327 if (!frm || !cid)
2328 goto err;
2329
2330 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002331 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002332 }
2333
2334 return 1;
2335
2336 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002337 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002338 return 0;
2339}
2340
2341/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002342void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002343{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002344 struct eb64_node *n;
2345 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002346
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002347 n = eb64_first(&arngs->root);
2348 while (n) {
2349 struct eb64_node *next;
2350
2351 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2352 next = eb64_next(n);
2353 eb64_delete(n);
2354 free(ar);
2355 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002356 }
2357}
2358
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002359/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2360 * descending order.
2361 */
2362static inline size_t sack_gap(struct quic_arng_node *p,
2363 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002364{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002365 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002366}
2367
2368
2369/* Remove the last elements of <ack_ranges> list of ack range updating its
2370 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002371 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002372 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002373static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002374{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002375 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002376
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002377 last = eb64_last(&arngs->root);
2378 while (last && arngs->enc_sz > limit) {
2379 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002380
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002381 prev = eb64_prev(last);
2382 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002383 return 0;
2384
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002385 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2386 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2387 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2388 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2389 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2390 --arngs->sz;
2391 eb64_delete(last);
2392 pool_free(pool_head_quic_arng, last);
2393 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002394 }
2395
2396 return 1;
2397}
2398
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002399/* Set the encoded size of <arngs> QUIC ack ranges. */
2400static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2401{
2402 struct eb64_node *node, *next;
2403 struct quic_arng_node *ar, *ar_next;
2404
2405 node = eb64_last(&arngs->root);
2406 if (!node)
2407 return;
2408
2409 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2410 arngs->enc_sz = quic_int_getsize(ar->last) +
2411 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2412
2413 while ((next = eb64_prev(node))) {
2414 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2415 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2416 quic_int_getsize(ar_next->last - ar_next->first.key);
2417 node = next;
2418 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2419 }
2420}
2421
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002422/* Insert <ar> ack range into <argns> tree of ack ranges.
2423 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002424 */
2425static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002426struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2427 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002428{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002429 struct quic_arng_node *new_ar;
2430
2431 new_ar = pool_alloc(pool_head_quic_arng);
2432 if (new_ar) {
2433 new_ar->first.key = ar->first;
2434 new_ar->last = ar->last;
2435 eb64_insert(&arngs->root, &new_ar->first);
2436 arngs->sz++;
2437 }
2438
2439 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002440}
2441
2442/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002443 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002444 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002445 *
2446 * Descending order
2447 * ------------->
2448 * range1 range2
2449 * ..........|--------|..............|--------|
2450 * ^ ^ ^ ^
2451 * | | | |
2452 * last1 first1 last2 first2
2453 * ..........+--------+--------------+--------+......
2454 * diff1 gap12 diff2
2455 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002456 * To encode the previous list of ranges we must encode integers as follows in
2457 * descending order:
2458 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002459 * with diff1 = last1 - first1
2460 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002461 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002462 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002463 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002464int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2465 struct quic_arng *ar)
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 *le;
2468 struct quic_arng_node *new_node;
2469 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002470
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002471 new = NULL;
2472 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002473 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002474 if (!new_node)
2475 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002476
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002477 goto out;
2478 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002479
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002480 le = eb64_lookup_le(&arngs->root, ar->first);
2481 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002482 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002483 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002484 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002485 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002486 else {
2487 struct quic_arng_node *le_ar =
2488 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002489
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002490 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002491 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002492 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002493
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002494 if (le_ar->last + 1 >= ar->first) {
2495 le_ar->last = ar->last;
2496 new = le;
2497 new_node = le_ar;
2498 }
2499 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002500 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002501 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002502 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002503
2504 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002505 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002506 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002507
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002508 /* Verify that the new inserted node does not overlap the nodes
2509 * which follow it.
2510 */
2511 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002512 struct eb64_node *next;
2513 struct quic_arng_node *next_node;
2514
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002515 while ((next = eb64_next(new))) {
2516 next_node =
2517 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002518 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002519 break;
2520
2521 if (next_node->last > new_node->last)
2522 new_node->last = next_node->last;
2523 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002524 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002525 /* Decrement the size of these ranges. */
2526 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002527 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002528 }
2529
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002530 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002531 quic_arngs_set_enc_sz(arngs);
2532
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002533 return 1;
2534}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002535/* Remove the header protection of packets at <el> encryption level.
2536 * Always succeeds.
2537 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002538static 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 +01002539{
2540 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002541 struct quic_rx_packet *pqpkt;
2542 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002543 struct quic_enc_level *app_qel;
2544
2545 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2546 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2547 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002548 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002549 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002550 TRACE_PROTO("hp not removed (handshake not completed)",
2551 QUIC_EV_CONN_ELRMHP, ctx->conn);
2552 goto out;
2553 }
2554 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002555 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002556 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2557 pqpkt->data + pqpkt->pn_offset,
2558 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2559 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2560 /* XXX TO DO XXX */
2561 }
2562 else {
2563 /* The AAD includes the packet number field */
2564 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2565 /* Store the packet into the tree of packets to decrypt. */
2566 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002567 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002568 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002569 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002570 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2571 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002572 MT_LIST_DELETE_SAFE(pkttmp1);
2573 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002574 }
2575
2576 out:
2577 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2578}
2579
2580/* Process all the CRYPTO frame at <el> encryption level.
2581 * Return 1 if succeeded, 0 if not.
2582 */
2583static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002584 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002585{
2586 struct eb64_node *node;
2587
2588 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002589 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002590 node = eb64_first(&el->rx.crypto.frms);
2591 while (node) {
2592 struct quic_rx_crypto_frm *cf;
2593
2594 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2595 if (cf->offset_node.key != el->rx.crypto.offset)
2596 break;
2597
2598 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2599 goto err;
2600
2601 node = eb64_next(node);
2602 quic_rx_packet_refdec(cf->pkt);
2603 eb64_delete(&cf->offset_node);
2604 pool_free(pool_head_quic_rx_crypto_frm, cf);
2605 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002606 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002607 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2608 return 1;
2609
2610 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002611 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002612 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2613 return 0;
2614}
2615
2616/* Process all the packets at <el> encryption level.
2617 * Return 1 if succeeded, 0 if not.
2618 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002619int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2620 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002621{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002622 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002623 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002624 struct quic_conn *qc = ctx->conn->qc;
2625 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002626
2627 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002628 qel = cur_el;
2629 next_tel:
2630 if (!qel)
2631 goto out;
2632
2633 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
2634 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002635 while (node) {
2636 struct quic_rx_packet *pkt;
2637
2638 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002639 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
2640 ctx->conn, pkt, NULL, ctx->ssl);
2641 if (!qc_pkt_decrypt(pkt, &qel->tls_ctx)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002642 /* Drop the packet */
2643 TRACE_PROTO("packet decryption failed -> dropped",
2644 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2645 }
2646 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002647 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002648 /* Drop the packet */
2649 TRACE_PROTO("packet parsing failed -> dropped",
2650 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2651 }
2652 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002653 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2654
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002655 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002656 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
2657 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002658 if (pkt->pn > largest_pn)
2659 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002660 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002661 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002662 TRACE_DEVEL("Could not update ack range list",
2663 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002664 }
2665 }
2666 node = eb64_next(node);
2667 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002669 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002670
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002671 /* Update the largest packet number. */
2672 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002673 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
2674 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002675 goto err;
2676
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002677 if (qel == cur_el) {
2678 qel = next_el;
2679 goto next_tel;
2680 }
2681
2682 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002683 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2684 return 1;
2685
2686 err:
2687 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2688 return 0;
2689}
2690
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002691/* QUIC connection packet handler task. */
2692struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002693{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002694 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002695 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002696 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002697 enum quic_tls_enc_level tel, next_tel;
2698 struct quic_enc_level *qel, *next_qel;
2699 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002700 struct qring *qr; // Tx ring
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002701 int prev_st, st, force_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002702
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002703 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002704 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002705 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002706 st = HA_ATOMIC_LOAD(&qc->state);
2707 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002708 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002709 start:
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002710 if (!quic_get_tls_enc_levels(&tel, &next_tel, st))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002711 goto err;
2712
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002713 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002714 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002715
2716 next_level:
2717 tls_ctx = &qel->tls_ctx;
2718
2719 /* If the header protection key for this level has been derived,
2720 * remove the packet header protections.
2721 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002722 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002723 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2724 qc_rm_hp_pkts(qel, ctx);
2725
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002726 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002727 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
2728 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2729 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002730 goto err;
2731
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002732 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02002733 if (st >= QUIC_HS_ST_COMPLETE &&
2734 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002735 /* Discard the Handshake keys. */
2736 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2737 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2738 qc_set_timer(ctx);
2739 if (!quic_build_post_handshake_frames(qc))
2740 goto err;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002741 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002742 }
2743
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002744 if (!qr)
2745 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2746 ret = qc_prep_hdshk_pkts(qr, ctx);
2747 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002748 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002749 else if (ret == 0)
2750 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002751
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002752 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002753 goto err;
2754
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002755 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002756 /* Check if there is something to do for the next level.
2757 */
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002758 if (next_qel && (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002759 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760 qel = next_qel;
2761 goto next_level;
2762 }
2763
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002764 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002765 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002766 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002767
2768 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002769 if (qr)
2770 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002771 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002772 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002773}
2774
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002775/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002776static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2777{
2778 int i;
2779
2780 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2781 if (qel->tx.crypto.bufs[i]) {
2782 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2783 qel->tx.crypto.bufs[i] = NULL;
2784 }
2785 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002786 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002787}
2788
2789/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002790 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002791 * Returns 1 if succeeded, 0 if not.
2792 */
2793static int quic_conn_enc_level_init(struct quic_conn *qc,
2794 enum quic_tls_enc_level level)
2795{
2796 struct quic_enc_level *qel;
2797
2798 qel = &qc->els[level];
2799 qel->level = quic_to_ssl_enc_level(level);
2800 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2801 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2802 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2803 qel->tls_ctx.rx.flags = 0;
2804 qel->tls_ctx.tx.flags = 0;
2805
2806 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002807 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002808 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002809 qel->rx.crypto.offset = 0;
2810 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2811 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002812
2813 /* Allocate only one buffer. */
2814 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2815 if (!qel->tx.crypto.bufs)
2816 goto err;
2817
2818 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2819 if (!qel->tx.crypto.bufs[0])
2820 goto err;
2821
2822 qel->tx.crypto.bufs[0]->sz = 0;
2823 qel->tx.crypto.nb_buf = 1;
2824
2825 qel->tx.crypto.sz = 0;
2826 qel->tx.crypto.offset = 0;
2827
2828 return 1;
2829
2830 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002831 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002832 return 0;
2833}
2834
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002835/* Release all the memory allocated for <conn> QUIC connection. */
2836static void quic_conn_free(struct quic_conn *conn)
2837{
2838 int i;
2839
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002840 if (!conn)
2841 return;
2842
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002843 free_quic_conn_cids(conn);
2844 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2845 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002846 if (conn->timer_task)
2847 task_destroy(conn->timer_task);
2848 pool_free(pool_head_quic_conn, conn);
2849}
2850
2851/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002852static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002853{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002854 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002855 struct quic_conn *qc;
2856 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002857 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002858
2859 conn_ctx = task->context;
2860 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002861 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2862 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002863 task->expire = TICK_ETERNITY;
2864 pktns = quic_loss_pktns(qc);
2865 if (tick_isset(pktns->tx.loss_time)) {
2866 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2867
2868 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2869 if (!LIST_ISEMPTY(&lost_pkts))
2870 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2871 qc_set_timer(conn_ctx);
2872 goto out;
2873 }
2874
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002875 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002876 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002877 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002878 pktns->tx.pto_probe = 1;
2879 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002880 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002881 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2882 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2883
2884 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2885 hel->pktns->tx.pto_probe = 1;
2886 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2887 iel->pktns->tx.pto_probe = 1;
2888 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002889 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002890 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2891 qc->path->loss.pto_count++;
2892
2893 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002894 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002895
2896 return task;
2897}
2898
2899/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2900 * connections used to identify the first Initial packets of client connecting
2901 * to listeners. This parameter must be NULL for QUIC connections attached
2902 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2903 * <scid> is the source connection ID with <scid_len> as length.
2904 * Returns 1 if succeeded, 0 if not.
2905 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002906static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2907 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002908 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002909{
2910 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002911 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002912 /* Initial CID. */
2913 struct quic_connection_id *icid;
2914
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002915 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002916 qc = pool_zalloc(pool_head_quic_conn);
2917 if (!qc) {
2918 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2919 goto err;
2920 }
2921
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002922 qc->cids = EB_ROOT;
2923 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002924 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002925 struct listener *l = owner;
2926
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002927 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002928 /* Copy the initial DCID. */
2929 qc->odcid.len = dcid_len;
2930 if (qc->odcid.len)
2931 memcpy(qc->odcid.data, dcid, dcid_len);
2932
2933 /* Copy the SCID as our DCID for this connection. */
2934 if (scid_len)
2935 memcpy(qc->dcid.data, scid, scid_len);
2936 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002937 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002938 }
2939 /* QUIC Client (outgoing connection to servers) */
2940 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002941 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002942 if (dcid_len)
2943 memcpy(qc->dcid.data, dcid, dcid_len);
2944 qc->dcid.len = dcid_len;
2945 }
2946
2947 /* Initialize the output buffer */
2948 qc->obuf.pos = qc->obuf.data;
2949
2950 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002951 if (!icid) {
2952 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
2953 goto err;
2954 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002955
2956 /* Select our SCID which is the first CID with 0 as sequence number. */
2957 qc->scid = icid->cid;
2958
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002959 /* Packet number spaces initialization. */
2960 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2961 quic_pktns_init(&qc->pktns[i]);
2962 /* QUIC encryption level context initialization. */
2963 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002964 if (!quic_conn_enc_level_init(qc, i)) {
2965 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002966 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002967 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002968 /* Initialize the packet number space. */
2969 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2970 }
2971
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02002972 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002973 /* TX part. */
2974 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002975 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2976 qc->tx.wbuf = qc->tx.rbuf = 0;
2977 qc->tx.bytes = 0;
2978 qc->tx.nb_pto_dgrams = 0;
2979 /* RX part. */
2980 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002981 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002982
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002983 /* XXX TO DO: Only one path at this time. */
2984 qc->path = &qc->paths[0];
2985 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2986
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002987 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002988
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002989 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002990
2991 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002992 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002993 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002994 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002995}
2996
2997/* Initialize the timer task of <qc> QUIC connection.
2998 * Returns 1 if succeeded, 0 if not.
2999 */
3000static int quic_conn_init_timer(struct quic_conn *qc)
3001{
3002 qc->timer_task = task_new(MAX_THREADS_MASK);
3003 if (!qc->timer_task)
3004 return 0;
3005
3006 qc->timer = TICK_ETERNITY;
3007 qc->timer_task->process = process_timer;
3008 qc->timer_task->context = qc->conn->xprt_ctx;
3009
3010 return 1;
3011}
3012
3013/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3014 * past one byte of this buffer.
3015 */
3016static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3017 struct quic_rx_packet *pkt)
3018{
3019 unsigned char dcid_len, scid_len;
3020
3021 /* Version */
3022 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3023 return 0;
3024
3025 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
3026
3027 /* Destination Connection ID Length */
3028 dcid_len = *(*buf)++;
3029 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3030 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3031 /* XXX MUST BE DROPPED */
3032 return 0;
3033
3034 if (dcid_len) {
3035 /* Check that the length of this received DCID matches the CID lengths
3036 * of our implementation for non Initials packets only.
3037 */
3038 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
3039 return 0;
3040
3041 memcpy(pkt->dcid.data, *buf, dcid_len);
3042 }
3043
3044 pkt->dcid.len = dcid_len;
3045 *buf += dcid_len;
3046
3047 /* Source Connection ID Length */
3048 scid_len = *(*buf)++;
3049 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3050 /* XXX MUST BE DROPPED */
3051 return 0;
3052
3053 if (scid_len)
3054 memcpy(pkt->scid.data, *buf, scid_len);
3055 pkt->scid.len = scid_len;
3056 *buf += scid_len;
3057
3058 return 1;
3059}
3060
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003061/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3062 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3063 * encryption level matching with the packet type. <*qel> may be null if not found.
3064 * Note that <ctx> may be null (for Initial packets).
3065 */
3066static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003067 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003068 struct quic_enc_level **qel)
3069{
3070 enum quic_tls_enc_level tel;
3071
3072 /* Special case without connection context (firt Initial packets) */
3073 if (!ctx) {
3074 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3075 return 1;
3076 }
3077
3078 tel = quic_packet_type_enc_level(pkt->type);
3079 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3080 *qel = NULL;
3081 return 0;
3082 }
3083
3084 *qel = &qc->els[tel];
3085 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3086 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3087 return 0;
3088 }
3089
3090 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003091 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3092 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003093 return 1;
3094
3095 return 0;
3096}
3097
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003098/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3099 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3100 * byte past the end of the buffer containing this packet and <beg> the address of
3101 * the packet first byte.
3102 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3103 * Returns 1 if succeeded, 0 if not.
3104 */
3105static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3106 unsigned char **buf, unsigned char *beg,
3107 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003108 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003109{
3110 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003111 struct quic_enc_level *qel;
3112 /* Only for traces. */
3113 struct quic_rx_packet *qpkt_trace;
3114
3115 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003116 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003117 /* The packet number is here. This is also the start minus
3118 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3119 * protection.
3120 */
3121 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003122 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003123 /* Note that the following function enables us to unprotect the packet
3124 * number and its length subsequently used to decrypt the entire
3125 * packets.
3126 */
3127 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3128 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003129 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003130 goto err;
3131 }
3132
3133 /* The AAD includes the packet number field found at <pn>. */
3134 pkt->aad_len = pn - beg + pkt->pnl;
3135 qpkt_trace = pkt;
3136 /* Store the packet */
3137 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003138 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003139 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003140 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003141 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003142 else if (qel) {
3143 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003144 pkt->pn_offset = pn - beg;
3145 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3146 }
3147
3148 memcpy(pkt->data, beg, pkt->len);
3149 /* Updtate the offset of <*buf> for the next QUIC packet. */
3150 *buf = beg + pkt->len;
3151
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003152 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003153 return 1;
3154
3155 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003156 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 +01003157 return 0;
3158}
3159
3160/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3161 * Also set <*long_header> to 1 if this form is long, 0 if not.
3162 */
3163static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3164 unsigned char byte0, int *long_header)
3165{
3166 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3167 pkt->type =
3168 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3169 *long_header = 1;
3170 }
3171 else {
3172 pkt->type = QUIC_PACKET_TYPE_SHORT;
3173 *long_header = 0;
3174 }
3175}
3176
3177static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3178 struct quic_rx_packet *pkt,
3179 struct quic_dgram_ctx *dgram_ctx,
3180 struct sockaddr_storage *saddr)
3181{
3182 unsigned char *beg;
3183 uint64_t len;
3184 struct quic_conn *qc;
3185 struct eb_root *cids;
3186 struct ebmb_node *node;
3187 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003188 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003189 int long_header;
3190
3191 qc = NULL;
3192 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3193 if (end <= *buf)
3194 goto err;
3195
3196 /* Fixed bit */
3197 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3198 /* XXX TO BE DISCARDED */
3199 goto err;
3200
3201 srv_conn = dgram_ctx->owner;
3202 beg = *buf;
3203 /* Header form */
3204 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3205 if (long_header) {
3206 size_t cid_lookup_len;
3207
3208 if (!quic_packet_read_long_header(buf, end, pkt))
3209 goto err;
3210
3211 /* For Initial packets, and for servers (QUIC clients connections),
3212 * there is no Initial connection IDs storage.
3213 */
3214 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3215 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3216 cid_lookup_len = pkt->dcid.len;
3217 }
3218 else {
3219 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3220 cid_lookup_len = QUIC_CID_LEN;
3221 }
3222
3223 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3224 if (!node)
3225 goto err;
3226
3227 qc = ebmb_entry(node, struct quic_conn, scid_node);
3228
3229 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3230 qc->dcid.len = pkt->scid.len;
3231 if (pkt->scid.len)
3232 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3233 }
3234
3235 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3236 uint64_t token_len;
3237
3238 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3239 goto err;
3240
3241 /* XXX TO DO XXX 0 value means "the token is not present".
3242 * A server which sends an Initial packet must not set the token.
3243 * So, a client which receives an Initial packet with a token
3244 * MUST discard the packet or generate a connection error with
3245 * PROTOCOL_VIOLATION as type.
3246 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3247 */
3248 pkt->token_len = token_len;
3249 }
3250 }
3251 else {
3252 /* XXX TO DO: Short header XXX */
3253 if (end - *buf < QUIC_CID_LEN)
3254 goto err;
3255
3256 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3257 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3258 if (!node)
3259 goto err;
3260
3261 qc = ebmb_entry(node, struct quic_conn, scid_node);
3262 *buf += QUIC_CID_LEN;
3263 }
3264 /* Store the DCID used for this packet to check the packet which
3265 * come in this UDP datagram match with it.
3266 */
3267 if (!dgram_ctx->dcid_node)
3268 dgram_ctx->dcid_node = node;
3269 /* Only packets packets with long headers and not RETRY or VERSION as type
3270 * have a length field.
3271 */
3272 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3273 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3274 goto err;
3275
3276 pkt->len = len;
3277 }
3278 else if (!long_header) {
3279 /* A short packet is the last one of an UDP datagram. */
3280 pkt->len = end - *buf;
3281 }
3282
3283 conn_ctx = qc->conn->xprt_ctx;
3284
3285 /* Increase the total length of this packet by the header length. */
3286 pkt->len += *buf - beg;
3287 /* Do not check the DCID node before the length. */
3288 if (dgram_ctx->dcid_node != node) {
3289 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3290 goto err;
3291 }
3292
3293 if (pkt->len > sizeof pkt->data) {
3294 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3295 goto err;
3296 }
3297
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003298 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003299 goto err;
3300
3301 /* Wake the tasklet of the QUIC connection packet handler. */
3302 if (conn_ctx)
3303 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3304
3305 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3306
3307 return pkt->len;
3308
3309 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003310 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003311 return -1;
3312}
3313
3314static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3315 struct quic_rx_packet *pkt,
3316 struct quic_dgram_ctx *dgram_ctx,
3317 struct sockaddr_storage *saddr)
3318{
3319 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003320 struct quic_conn *qc;
3321 struct eb_root *cids;
3322 struct ebmb_node *node;
3323 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003324 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003325 int long_header = 0;
3326
3327 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003328 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003329 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003330 if (end <= *buf)
3331 goto err;
3332
3333 /* Fixed bit */
3334 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3335 /* XXX TO BE DISCARDED */
3336 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3337 goto err;
3338 }
3339
3340 l = dgram_ctx->owner;
3341 beg = *buf;
3342 /* Header form */
3343 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3344 if (long_header) {
3345 unsigned char dcid_len;
3346
3347 if (!quic_packet_read_long_header(buf, end, pkt)) {
3348 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3349 goto err;
3350 }
3351
3352 dcid_len = pkt->dcid.len;
3353 /* For Initial packets, and for servers (QUIC clients connections),
3354 * there is no Initial connection IDs storage.
3355 */
3356 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003357 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003358 /* DCIDs of first packets coming from clients may have the same values.
3359 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3360 */
3361 quic_cid_saddr_cat(&pkt->dcid, saddr);
3362 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003363
3364 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3365 end - *buf < token_len) {
3366 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3367 goto err;
3368 }
3369
3370 /* XXX TO DO XXX 0 value means "the token is not present".
3371 * A server which sends an Initial packet must not set the token.
3372 * So, a client which receives an Initial packet with a token
3373 * MUST discard the packet or generate a connection error with
3374 * PROTOCOL_VIOLATION as type.
3375 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3376 */
3377 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003378 }
3379 else {
3380 if (pkt->dcid.len != QUIC_CID_LEN) {
3381 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3382 goto err;
3383 }
3384
3385 cids = &l->rx.cids;
3386 }
3387
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003388 /* Only packets packets with long headers and not RETRY or VERSION as type
3389 * have a length field.
3390 */
3391 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3392 uint64_t len;
3393
3394 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3395 end - *buf < len) {
3396 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3397 goto err;
3398 }
3399
3400 pkt->len = len;
3401 }
3402
3403
3404 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003405 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3406 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3407 cids == &l->rx.odcids) {
3408 /* Switch to the definitive tree ->cids containing the final CIDs. */
3409 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3410 if (node) {
3411 /* If found, signal this with NULL as special value for <cids>. */
3412 pkt->dcid.len = dcid_len;
3413 cids = NULL;
3414 }
3415 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003416 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003417
3418 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003419 int ipv4;
3420 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003421 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003422 const unsigned char *salt = initial_salt_v1;
3423 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003424
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003425 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3426 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3427 goto err;
3428 }
3429
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003430 pkt->saddr = *saddr;
3431 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3432 * of <saddr>.
3433 */
3434 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003435 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003436 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003437 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003438 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003439 goto err;
3440
3441 odcid = &qc->rx.params.original_destination_connection_id;
3442 /* Copy the transport parameters. */
3443 qc->rx.params = l->bind_conf->quic_params;
3444 /* Copy original_destination_connection_id transport parameter. */
3445 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3446 odcid->len = pkt->odcid_len;
3447 /* Copy the initial source connection ID. */
3448 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3449 qc->enc_params_len =
3450 quic_transport_params_encode(qc->enc_params,
3451 qc->enc_params + sizeof qc->enc_params,
3452 &qc->rx.params, 1);
3453 if (!qc->enc_params_len)
3454 goto err;
3455
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003456 /* NOTE: the socket address has been concatenated to the destination ID
3457 * chosen by the client for Initial packets.
3458 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003459 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
3460 salt = initial_salt_draft_29;
3461 salt_len = sizeof initial_salt_draft_29;
3462 }
3463 if (!qc_new_isecs(qc, salt, salt_len,
3464 pkt->dcid.data, pkt->odcid_len, 1)) {
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003465 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3466 goto err;
3467 }
3468
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003469 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003470 /* This is the DCID node sent in this packet by the client. */
3471 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003472 /* Enqueue this packet. */
3473 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3474 /* Try to accept a new connection. */
3475 listener_accept(l);
3476
3477 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3478 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3479 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3480 /* Insert our SCID, the connection ID for the QUIC client. */
3481 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3482 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3483 if (n != &qc->scid_node) {
3484 quic_conn_free(qc);
3485 qc = ebmb_entry(n, struct quic_conn, scid_node);
3486 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003487 }
3488 else {
3489 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3490 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3491 else
3492 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003493 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003494 }
3495 }
3496 else {
3497 if (end - *buf < QUIC_CID_LEN) {
3498 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3499 goto err;
3500 }
3501
3502 cids = &l->rx.cids;
3503 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3504 if (!node) {
3505 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3506 goto err;
3507 }
3508
3509 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003510 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003511 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003512 /* A short packet is the last one of an UDP datagram. */
3513 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003514 }
3515
3516 /* Store the DCID used for this packet to check the packet which
3517 * come in this UDP datagram match with it.
3518 */
3519 if (!dgram_ctx->dcid_node) {
3520 dgram_ctx->dcid_node = node;
3521 dgram_ctx->qc = qc;
3522 }
3523
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003524 /* Increase the total length of this packet by the header length. */
3525 pkt->len += *buf - beg;
3526 /* Do not check the DCID node before the length. */
3527 if (dgram_ctx->dcid_node != node) {
3528 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3529 goto err;
3530 }
3531
3532 if (pkt->len > sizeof pkt->data) {
3533 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3534 goto err;
3535 }
3536
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003537 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3538 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3539 goto err;
3540 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003541
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003542
3543 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003544 /* Wake up the connection packet handler task from here only if all
3545 * the contexts have been initialized, especially the mux context
3546 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3547 * will start it if these contexts for the connection are not already
3548 * initialized.
3549 */
3550 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003551 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003552
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003553 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3554
3555 return pkt->len;
3556
3557 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003558 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003559 qc ? qc->conn : NULL, pkt);
3560 return -1;
3561}
3562
3563/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3564 * in advance. This is the reponsability of the caller to check there is enough room in this
3565 * buffer to build a long header.
3566 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3567 */
3568static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3569 int type, size_t pn_len, struct quic_conn *conn)
3570{
3571 if (type > QUIC_PACKET_TYPE_RETRY)
3572 return 0;
3573
3574 /* #0 byte flags */
3575 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3576 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3577 /* Version */
3578 quic_write_uint32(buf, end, conn->version);
3579 *(*buf)++ = conn->dcid.len;
3580 /* Destination connection ID */
3581 if (conn->dcid.len) {
3582 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3583 *buf += conn->dcid.len;
3584 }
3585 /* Source connection ID */
3586 *(*buf)++ = conn->scid.len;
3587 if (conn->scid.len) {
3588 memcpy(*buf, conn->scid.data, conn->scid.len);
3589 *buf += conn->scid.len;
3590 }
3591
3592 return 1;
3593}
3594
3595/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3596 * in advance. This is the reponsability of the caller to check there is enough room in this
3597 * buffer to build a long header.
3598 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3599 */
3600static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3601 size_t pn_len, struct quic_conn *conn)
3602{
3603 /* #0 byte flags */
3604 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3605 /* Destination connection ID */
3606 if (conn->dcid.len) {
3607 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3608 *buf += conn->dcid.len;
3609 }
3610
3611 return 1;
3612}
3613
3614/* Apply QUIC header protection to the packet with <buf> as first byte address,
3615 * <pn> as address of the Packet number field, <pnlen> being this field length
3616 * with <aead> as AEAD cipher and <key> as secret key.
3617 * Returns 1 if succeeded or 0 if failed.
3618 */
3619static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3620 const EVP_CIPHER *aead, const unsigned char *key)
3621{
3622 int i, ret, outlen;
3623 EVP_CIPHER_CTX *ctx;
3624 /* We need an IV of at least 5 bytes: one byte for bytes #0
3625 * and at most 4 bytes for the packet number
3626 */
3627 unsigned char mask[5] = {0};
3628
3629 ret = 0;
3630 ctx = EVP_CIPHER_CTX_new();
3631 if (!ctx)
3632 return 0;
3633
3634 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3635 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3636 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3637 goto out;
3638
3639 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3640 for (i = 0; i < pnlen; i++)
3641 pn[i] ^= mask[i + 1];
3642
3643 ret = 1;
3644
3645 out:
3646 EVP_CIPHER_CTX_free(ctx);
3647
3648 return ret;
3649}
3650
3651/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3652 * ACK ranges if needed to a value below <limit> in bytes.
3653 * Return 1 if succeeded, 0 if not.
3654 */
3655static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3656{
3657 size_t room, ack_delay_sz;
3658
3659 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3660 /* A frame is made of 1 byte for the frame type. */
3661 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003662 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003663 return 0;
3664
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003665 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003666}
3667
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003668/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3669 * 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 +01003670 * and <*len> the packet Length field initialized with the number of bytes already present
3671 * 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 +02003672 * <headlen> is the number of bytes already present in this packet before building frames.
3673 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003674 * Update consequently <*len> to reflect the size of these frames built
3675 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003676 * Return 1 if succeeded, 0 if not.
3677 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003678static inline int qc_build_frms(struct quic_tx_packet *pkt,
3679 size_t room, size_t *len, size_t headlen,
3680 struct quic_enc_level *qel,
3681 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003682{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003683 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003684 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003685 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003686 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003687
Frédéric Lécailleea604992020-12-24 13:01:37 +01003688 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003689 if (*len > room || headlen > remain)
3690 return 0;
3691
Frédéric Lécailleea604992020-12-24 13:01:37 +01003692 /* If we are not probing we must take into an account the congestion
3693 * control window.
3694 */
3695 if (!conn->tx.nb_pto_dgrams)
3696 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003697 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003698 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003699 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 +01003700 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003701 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003702
Frédéric Lécailleea604992020-12-24 13:01:37 +01003703 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003704 break;
3705
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003706 switch (cf->type) {
3707 case QUIC_FT_CRYPTO:
3708 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3709 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3710 /* Compute the length of this CRYPTO frame header */
3711 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3712 /* Compute the data length of this CRyPTO frame. */
3713 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3714 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3715 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3716 if (!dlen)
3717 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003718
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003719 pkt->cdata_len += dlen;
3720 /* CRYPTO frame length. */
3721 flen = hlen + quic_int_getsize(dlen) + dlen;
3722 TRACE_PROTO(" CRYPTO frame length (flen)",
3723 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3724 /* Add the CRYPTO data length and its encoded length to the packet
3725 * length and the length of this length.
3726 */
3727 *len += flen;
3728 room -= flen;
3729 if (dlen == cf->crypto.len) {
3730 /* <cf> CRYPTO data have been consumed. */
3731 MT_LIST_DELETE_SAFE(tmp1);
3732 LIST_APPEND(&pkt->frms, &cf->list);
3733 }
3734 else {
3735 struct quic_frame *new_cf;
3736
3737 new_cf = pool_alloc(pool_head_quic_frame);
3738 if (!new_cf) {
3739 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3740 return 0;
3741 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003742
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003743 new_cf->type = QUIC_FT_CRYPTO;
3744 new_cf->crypto.len = dlen;
3745 new_cf->crypto.offset = cf->crypto.offset;
3746 new_cf->crypto.qel = qel;
3747 LIST_APPEND(&pkt->frms, &new_cf->list);
3748 /* Consume <dlen> bytes of the current frame. */
3749 cf->crypto.len -= dlen;
3750 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003751 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003752 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003753
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003754 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003755 /* Note that these frames are accepted in short packets only without
3756 * "Length" packet field. Here, <*len> is used only to compute the
3757 * sum of the lengths of the already built frames for this packet.
3758 */
3759 TRACE_PROTO(" New STREAM frame build (room, len)",
3760 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3761 /* Compute the length of this STREAM frame "header" made a all the field
3762 * excepting the variable ones. Note that +1 is for the type of this frame.
3763 */
3764 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003765 ((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 +02003766 /* Compute the data length of this STREAM frame. */
3767 avail_room = room - hlen - *len;
3768 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003769 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003770
3771 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
3772 dlen = max_available_room(avail_room, &dlen_sz);
3773 if (dlen > cf->stream.len) {
3774 dlen = cf->stream.len;
3775 }
3776 dlen_sz = quic_int_getsize(dlen);
3777 flen = hlen + dlen_sz + dlen;
3778 }
3779 else {
3780 dlen = QUIC_MIN(avail_room, cf->stream.len);
3781 flen = hlen + dlen;
3782 }
3783 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
3784 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen);
3785 TRACE_PROTO(" STREAM frame length (flen)",
3786 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3787 /* Add the STREAM data length and its encoded length to the packet
3788 * length and the length of this length.
3789 */
3790 *len += flen;
3791 room -= flen;
3792 if (dlen == cf->stream.len) {
3793 /* <cf> STREAM data have been consumed. */
3794 MT_LIST_DELETE_SAFE(tmp1);
3795 LIST_APPEND(&pkt->frms, &cf->list);
3796 }
3797 else {
3798 struct quic_frame *new_cf;
3799
3800 new_cf = pool_zalloc(pool_head_quic_frame);
3801 if (!new_cf) {
3802 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3803 return 0;
3804 }
3805
3806 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003807 new_cf->stream.qcs = cf->stream.qcs;
3808 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003809 new_cf->stream.id = cf->stream.id;
3810 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
3811 new_cf->stream.offset = cf->stream.offset;
3812 new_cf->stream.len = dlen;
3813 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
3814 /* FIN bit reset */
3815 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
3816 new_cf->stream.data = cf->stream.data;
3817 LIST_APPEND(&pkt->frms, &new_cf->list);
3818 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
3819 /* Consume <dlen> bytes of the current frame. */
3820 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003821 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003822 cf->stream.data += dlen;
3823 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003824 break;
3825
3826 default:
3827 flen = qc_frm_len(cf);
3828 BUG_ON(!flen);
3829 if (flen > room)
3830 continue;
3831
3832 *len += flen;
3833 room -= flen;
3834 MT_LIST_DELETE_SAFE(tmp1);
3835 LIST_APPEND(&pkt->frms, &cf->list);
3836 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003837 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003838 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003839 }
3840
Frédéric Lécailleea604992020-12-24 13:01:37 +01003841 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003842}
3843
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003844/* This function evaluates if <pkt> packet may be built into a buffer with
3845 * <room> as available room. A valid packet should at least contain a valid
3846 * header and at least a frame.
3847 * To estimate the minimal space to build a packet, we consider the worst case:
3848 - there is not enough space to build ack-eliciting frames from
3849 qel->pktns->tx.frms. This is safe to consider this because when we build
3850 a packet we first build the ACK frames, then the ack-eliciting frames
3851 from qel->pktns->tx.frms only if there is enough space for these
3852 ack-eliciting frames, finally PING and PADDING frames if needed,
3853 - we have to ensure there is enough space to build an ACK frame if required,
3854 and a PING frame, even if we do not have to probe,
3855 - we also have to verify there is enough space to build a PADDING frame
3856 if needed, especially if there is no need to send an ACK frame.
3857 * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build
3858 * a valid packet).
3859 */
3860static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt,
3861 int ack, int nb_pto_dgrams,
3862 struct quic_enc_level *qel, struct quic_conn *conn)
3863{
3864 size_t minlen, token_fields_len;
3865 /* XXX FIXME XXX : ack delay not supported */
3866 uint64_t ack_delay = 0;
3867 size_t ack_frm_len = 0;
3868
3869 TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT,
3870 conn->conn, NULL, NULL, &room);
3871 /* When we do not have to probe nor send acks either, we must take into
3872 * an account the data which have already been prepared and limit
3873 * the size of this packet. We will potentially build an ack-eliciting
3874 * packet.
3875 */
3876 if (!nb_pto_dgrams && !ack) {
3877 size_t path_room;
3878
3879 path_room = quic_path_prep_data(conn->path);
3880 if (room > path_room)
3881 room = path_room;
3882 }
3883
3884 if (ack)
3885 /* A frame is made of 1 byte for the frame type. */
3886 ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz;
3887
3888 /* XXX FIXME XXX : token not supported */
3889 token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3890 /* Check there is enough room to build the header followed by a token,
3891 * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes
3892 * encryption tag is also taken into an account. Note that we have no
3893 * knowledge of the packet number for this packet. It must be atomically
3894 * incremented each time a packet is built. But before building a packet
3895 * we must estimate if it may be built if we do not want to consume a packet
3896 * number for nothing! Note that we add 1 byte more to
3897 * <minlen> to be able to build an ack-eliciting packet when probing without
3898 * ack-eliciting frames to send. In this case we need to add a 1-byte length
3899 * PING frame.
3900 */
3901 minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1;
3902 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
3903 minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len
3904 + token_fields_len;
3905 else
3906 minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len;
3907
3908 /* Consider any PADDING frame to add */
3909 if (objt_server(conn->conn->target) &&
3910 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3911 minlen < QUIC_INITIAL_PACKET_MINLEN) {
3912 /* Pad too short client Initial packet */
3913 minlen += QUIC_INITIAL_PACKET_MINLEN - minlen;
3914 }
3915 else if (!ack) {
3916 /* Consider we will have to add the longest short PADDING frame to
3917 * protect a 1-byte length packet number.
3918 */
3919 minlen += QUIC_PACKET_PN_MAXLEN - 1;
3920 }
3921
3922 if (room < minlen) {
3923 TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT,
3924 conn->conn, NULL, NULL, &room);
3925 return 0;
3926 }
3927
3928 return 1;
3929}
3930
3931/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003932 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
3933 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
3934 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003935 * 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 +02003936 * 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 +02003937 * having returned from this function.
3938 * 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 +01003939 * number field in this packet. <pn_len> will also have the packet number
3940 * length as value.
3941 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003942 * Always succeeds: this is the responsability of the caller to ensure there is
3943 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003944 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003945static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003946 struct quic_tx_packet *pkt, int ack, int nb_pto_dgrams,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003947 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
3948 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003949{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003950 unsigned char *beg;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003951 size_t len, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003952 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3953 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003954 size_t ack_frm_len;
3955 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003956 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003957
Frédéric Lécailleea604992020-12-24 13:01:37 +01003958 /* Length field value with CRYPTO frames if present. */
3959 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003960 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003961 /* When not probing and not acking, reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003962 * the congestion controller window. So, we do not limit the size of this
3963 * packet if we have an ACK frame to send because an ACK frame is not
3964 * ack-eliciting. This size will be limited if we have ack-eliciting
3965 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003966 */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003967 if (!nb_pto_dgrams && !ack) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003968 size_t path_room;
3969
3970 path_room = quic_path_prep_data(conn->path);
3971 if (end - beg > path_room)
3972 end = beg + path_room;
3973 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003974
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003975 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003976 /* packet number length */
3977 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003978 /* Build the header */
3979 if (pkt->type == QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003980 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3981 else
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003982 quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn);
3983 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
3984 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003985 *pos++ = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003986 /* Ensure there is enough room for the TLS encryption tag */
3987 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003988 /* Build an ACK frame if required. */
3989 ack_frm_len = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003990 if (ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003991 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003992 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003993 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
3994 * smallest frame (PING) and <*pn_len> more for the packet number. Note
3995 * that from here, we do not know if we will have to send a PING frame.
3996 * This will be decided after having computed the ack-eliciting frames
3997 * to be added to this packet.
3998 */
3999 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 +01004000 if (!ack_frm_len) {
4001 ssize_t room = end - pos;
4002 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4003 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004004 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004005 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004006 }
4007
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004008 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004009 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004010 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004011 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004012
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004013 /* Initialize the length of the frames built below to <len>.
4014 * If any frame could be successfully built by qc_build_frms(),
4015 * we will have len_frms > len.
4016 */
4017 len_frms = len;
4018 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01004019 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4020 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004021 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004022
4023 add_ping_frm = 0;
4024 padding_len = 0;
4025 if (objt_server(conn->conn->target) &&
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004026 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004027 len < QUIC_INITIAL_PACKET_MINLEN) {
4028 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
4029 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004030 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004031 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004032 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004033 add_ping_frm = 1;
4034 len += 1;
4035 }
4036 /* If there is no frame at all to follow, add at least a PADDING frame. */
4037 if (!ack_frm_len)
4038 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4039 }
4040
4041 /* Length (of the remaining data). Must not fail because, the buffer size
4042 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004043 * for the encryption tag. It must be taken into an account for the length
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004044 * of this packet.
4045 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01004046 if (len_frms)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004047 len = len_frms + QUIC_TLS_TAG_LEN;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004048 else
4049 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004050 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004051 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004052
4053 /* Packet number field address. */
4054 *buf_pn = pos;
4055
4056 /* Packet number encoding. */
4057 quic_packet_number_encode(&pos, end, pn, *pn_len);
4058
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004059 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4060 ssize_t room = end - pos;
4061 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4062 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004063 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004064 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004065
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004066 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004067 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004068 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004069
4070 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02004071 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004072 ssize_t room = end - pos;
4073 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4074 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004075 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004076 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004077 }
4078 }
4079
4080 /* Build a PING frame if needed. */
4081 if (add_ping_frm) {
4082 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004083 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4084 ssize_t room = end - pos;
4085 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4086 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004087 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004088 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004089 }
4090
4091 /* Build a PADDING frame if needed. */
4092 if (padding_len) {
4093 frm.type = QUIC_FT_PADDING;
4094 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004095 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4096 ssize_t room = end - pos;
4097 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4098 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004099 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004100 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004101 }
4102
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004103 /* Always reset this variable as this function has no idea
4104 * if it was set. It is handle by the loss detection timer.
4105 */
4106 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004107 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004108}
4109
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004110static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004111{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004112 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004113 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004114 pkt->cdata_len = 0;
4115 pkt->in_flight_len = 0;
4116 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004117 pkt->next = NULL;
4118 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004119}
4120
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004121/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004122static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4123{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004124 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004125
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004126 if (!pkt)
4127 return;
4128
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004129 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004130 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004131 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004132 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004133 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004134}
4135
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004136/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4137 * type for <qc> QUIC connection from <qel> encryption level.
4138 * Return -2 if the packet could not be allocated or encrypted for any reason,
4139 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004140 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004141static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4142 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004143 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004144 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02004145 int ack, int nb_pto_dgrams, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004146{
4147 /* The pointer to the packet number field. */
4148 unsigned char *buf_pn;
4149 unsigned char *beg, *end, *payload;
4150 int64_t pn;
4151 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004152 struct quic_tls_ctx *tls_ctx;
4153 struct quic_tx_packet *pkt;
4154
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004155 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004156 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004157 pkt = pool_alloc(pool_head_quic_tx_packet);
4158 if (!pkt) {
4159 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 +02004160 *err = -2;
4161 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004162 }
4163
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004164 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004165 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004166 pn_len = 0;
4167 buf_pn = NULL;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004168 if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004169 *err = -1;
4170 goto err;
4171 }
4172
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004173 /* Consume a packet number. */
4174 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004175 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 +01004176
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004177 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004178 payload = buf_pn + pn_len;
4179 payload_len = end - payload;
4180 aad_len = payload - beg;
4181
4182 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004183 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4184 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004185 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004186 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004187
4188 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004189 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004190 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4191 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4192 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004193 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004194 goto err;
4195 }
4196
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004197 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4198 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004199 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004200 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004201 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004202 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004203 pkt->in_flight_len = pkt->len;
4204 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004205 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004206 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004207 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4208
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004209 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004210
4211 err:
4212 free_quic_tx_packet(pkt);
4213 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004214 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004215}
4216
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004217/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4218 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004219 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004220static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4221 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004222{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004223 size_t try, done = 0;
4224
4225 if (!conn_ctrl_ready(conn))
4226 return 0;
4227
4228 if (!fd_recv_ready(conn->handle.fd))
4229 return 0;
4230
4231 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004232
4233 /* read the largest possible block. For this, we perform only one call
4234 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004235 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004236 */
4237 while (count > 0) {
4238 try = b_contig_space(buf);
4239 if (!try)
4240 break;
4241
4242 if (try > count)
4243 try = count;
4244
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004245 b_add(buf, try);
4246 done += try;
4247 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004248 }
4249
4250 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4251 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4252
4253 leave:
4254 return done;
4255
4256 read0:
4257 conn_sock_read0(conn);
4258 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4259
4260 /* Now a final check for a possible asynchronous low-level error
4261 * report. This can happen when a connection receives a reset
4262 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4263 * we might have come from there by just checking POLL_HUP instead
4264 * of recv()'s return value 0, so we have no way to tell there was
4265 * an error without checking.
4266 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004267 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004268 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4269 goto leave;
4270}
4271
4272
4273/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4274 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4275 * other pending data for example, but this flag is ignored at the moment.
4276 * Only one call to send() is performed, unless the buffer wraps, in which case
4277 * a second call may be performed. The connection's flags are updated with
4278 * whatever special event is detected (error, empty). The caller is responsible
4279 * for taking care of those events and avoiding the call if inappropriate. The
4280 * function does not call the connection's polling update function, so the caller
4281 * is responsible for this. It's up to the caller to update the buffer's contents
4282 * based on the return value.
4283 */
4284static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4285{
4286 ssize_t ret;
4287 size_t try, done;
4288 int send_flag;
4289
4290 if (!conn_ctrl_ready(conn))
4291 return 0;
4292
4293 if (!fd_send_ready(conn->handle.fd))
4294 return 0;
4295
4296 done = 0;
4297 /* send the largest possible block. For this we perform only one call
4298 * to send() unless the buffer wraps and we exactly fill the first hunk,
4299 * in which case we accept to do it once again.
4300 */
4301 while (count) {
4302 try = b_contig_data(buf, done);
4303 if (try > count)
4304 try = count;
4305
4306 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4307 if (try < count || flags & CO_SFL_MSG_MORE)
4308 send_flag |= MSG_MORE;
4309
4310 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4311 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4312 if (ret > 0) {
4313 count -= ret;
4314 done += ret;
4315
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004316 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004317 conn->flags |= CO_FL_WAIT_L4L6;
4318 /* if the system buffer is full, don't insist */
4319 if (ret < try)
4320 break;
4321 }
4322 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4323 /* nothing written, we need to poll for write first */
4324 fd_cant_send(conn->handle.fd);
4325 break;
4326 }
4327 else if (errno != EINTR) {
4328 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4329 break;
4330 }
4331 }
4332 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4333 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4334
4335 if (done > 0) {
4336 /* we count the total bytes sent, and the send rate for 32-byte
4337 * blocks. The reason for the latter is that freq_ctr are
4338 * limited to 4GB and that it's not enough per second.
4339 */
4340 _HA_ATOMIC_ADD(&global.out_bytes, done);
4341 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4342 }
4343 return done;
4344}
4345
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004346/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4347 * event subscriber <es> is not allowed to change from a previous call as long
4348 * as at least one event is still subscribed. The <event_type> must only be a
4349 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4350 */
4351static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4352{
4353 return conn_subscribe(conn, xprt_ctx, event_type, es);
4354}
4355
4356/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4357 * The <es> pointer is not allowed to differ from the one passed to the
4358 * subscribe() call. It always returns zero.
4359 */
4360static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4361{
4362 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4363}
4364
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004365/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4366 * connection with <xprt_ctx> as address of the xprt context.
4367 * Returns 1 if succeeded, 0 if not.
4368 */
4369static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4370{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004371 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004372
4373 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4374
4375 if (*xprt_ctx)
4376 goto out;
4377
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004378 ctx = pool_alloc(pool_head_quic_conn_ctx);
4379 if (!ctx) {
4380 conn->err_code = CO_ER_SYS_MEMLIM;
4381 goto err;
4382 }
4383
4384 ctx->wait_event.tasklet = tasklet_new();
4385 if (!ctx->wait_event.tasklet) {
4386 conn->err_code = CO_ER_SYS_MEMLIM;
4387 goto err;
4388 }
4389
4390 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4391 ctx->wait_event.tasklet->context = ctx;
4392 ctx->wait_event.events = 0;
4393 ctx->conn = conn;
4394 ctx->subs = NULL;
4395 ctx->xprt_ctx = NULL;
4396
4397 ctx->xprt = xprt_get(XPRT_QUIC);
4398 if (objt_server(conn->target)) {
4399 /* Server */
4400 struct server *srv = __objt_server(conn->target);
4401 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004402 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004403 int ssl_err, ipv4;
4404
4405 ssl_err = SSL_ERROR_NONE;
4406 if (RAND_bytes(dcid, sizeof dcid) != 1)
4407 goto err;
4408
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004409 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004410 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004411 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004412 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004413 goto err;
4414
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004415 /* Insert our SCID, the connection ID for the QUIC client. */
4416 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4417
4418 conn->qc = qc;
4419 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004420 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
4421 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004422 goto err;
4423
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004424 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4425 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4426 goto err;
4427
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004428 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004429 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004430 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4431 qc->enc_params_len =
4432 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4433 &qc->rx.params, 0);
4434 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004435 goto err;
4436
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004437 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004438 SSL_set_connect_state(ctx->ssl);
4439 ssl_err = SSL_do_handshake(ctx->ssl);
4440 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004441 int st;
4442
4443 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004444 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4445 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004446 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004447 }
4448 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004449 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004450 goto err;
4451 }
4452 }
4453 }
4454 else if (objt_listener(conn->target)) {
4455 /* Listener */
4456 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004457 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004458
Frédéric Lécailled4d6aa72021-09-03 15:56:18 +02004459 ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004460 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4461 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4462 goto err;
4463
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004464 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004465 SSL_set_accept_state(ctx->ssl);
4466 }
4467
4468 *xprt_ctx = ctx;
4469
4470 /* Leave init state and start handshake */
4471 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004472
4473 out:
4474 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4475
4476 return 0;
4477
4478 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004479 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004480 tasklet_free(ctx->wait_event.tasklet);
4481 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004482 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004483 return -1;
4484}
4485
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004486/* Start the QUIC transport layer */
4487static int qc_xprt_start(struct connection *conn, void *ctx)
4488{
4489 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004490 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004491
4492 qc = conn->qc;
4493 if (!quic_conn_init_timer(qc)) {
4494 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4495 return 0;
4496 }
4497
4498 tasklet_wakeup(qctx->wait_event.tasklet);
4499 return 1;
4500}
4501
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004502/* transport-layer operations for QUIC connections. */
4503static struct xprt_ops ssl_quic = {
4504 .snd_buf = quic_conn_from_buf,
4505 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004506 .subscribe = quic_conn_subscribe,
4507 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004508 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004509 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004510 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4511 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4512 .name = "QUIC",
4513};
4514
4515__attribute__((constructor))
4516static void __quic_conn_init(void)
4517{
4518 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4519 xprt_register(XPRT_QUIC, &ssl_quic);
4520}
4521
4522__attribute__((destructor))
4523static void __quic_conn_deinit(void)
4524{
4525 BIO_meth_free(ha_quic_meth);
4526}
4527
4528/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4529 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4530 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004531 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004532 */
4533static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4534 struct sockaddr_storage *saddr, qpkt_read_func *func)
4535{
4536 unsigned char *pos;
4537 const unsigned char *end;
4538 struct quic_dgram_ctx dgram_ctx = {
4539 .dcid_node = NULL,
4540 .owner = owner,
4541 };
4542
4543 pos = (unsigned char *)buf;
4544 end = pos + len;
4545
4546 do {
4547 int ret;
4548 struct quic_rx_packet *pkt;
4549
Willy Tarreaue4498932021-03-22 21:13:05 +01004550 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004551 if (!pkt)
4552 goto err;
4553
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004554 quic_rx_packet_refinc(pkt);
4555 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4556 if (ret == -1) {
4557 size_t pkt_len;
4558
4559 pkt_len = pkt->len;
4560 free_quic_rx_packet(pkt);
4561 /* If the packet length could not be found, we cannot continue. */
4562 if (!pkt_len)
4563 break;
4564 }
4565 } while (pos < end);
4566
4567 /* Increasing the received bytes counter by the UDP datagram length
4568 * if this datagram could be associated to a connection.
4569 */
4570 if (dgram_ctx.qc)
4571 dgram_ctx.qc->rx.bytes += len;
4572
4573 return pos - (unsigned char *)buf;
4574
4575 err:
4576 return -1;
4577}
4578
4579ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4580 struct sockaddr_storage *saddr)
4581{
4582 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4583}
4584
4585ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4586 struct sockaddr_storage *saddr)
4587{
4588 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4589}
4590
4591/* QUIC I/O handler for connection to local listeners or remove servers
4592 * depending on <listener> boolean value, with <fd> as socket file
4593 * descriptor and <ctx> as context.
4594 */
4595static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4596{
4597 ssize_t ret;
4598 size_t done = 0;
4599 struct buffer *buf = get_trash_chunk();
4600 /* Source address */
4601 struct sockaddr_storage saddr = {0};
4602 socklen_t saddrlen = sizeof saddr;
4603
4604 if (!fd_recv_ready(fd))
4605 return 0;
4606
4607 do {
4608 ret = recvfrom(fd, buf->area, buf->size, 0,
4609 (struct sockaddr *)&saddr, &saddrlen);
4610 if (ret < 0) {
4611 if (errno == EINTR)
4612 continue;
4613 if (errno == EAGAIN)
4614 fd_cant_recv(fd);
4615 goto out;
4616 }
4617 } while (0);
4618
4619 done = buf->data = ret;
4620 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4621
4622 out:
4623 return done;
4624}
4625
4626/* QUIC I/O handler for connections to local listeners with <fd> as socket
4627 * file descriptor.
4628 */
4629void quic_fd_handler(int fd)
4630{
Willy Tarreauf5090652021-04-06 17:23:40 +02004631 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004632 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4633}
4634
4635/* QUIC I/O handler for connections to remote servers with <fd> as socket
4636 * file descriptor.
4637 */
4638void quic_conn_fd_handler(int fd)
4639{
Willy Tarreauf5090652021-04-06 17:23:40 +02004640 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004641 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4642}
4643
4644/*
4645 * Local variables:
4646 * c-indent-level: 8
4647 * c-basic-offset: 8
4648 * End:
4649 */