blob: cfd8a687703094dad7d2043e44100ea80eef5f09 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
25#include <haproxy/buf-t.h>
26#include <haproxy/compat.h>
27#include <haproxy/api.h>
28#include <haproxy/debug.h>
29#include <haproxy/tools.h>
30#include <haproxy/ticks.h>
31#include <haproxy/time.h>
32
33#include <haproxy/connection.h>
34#include <haproxy/fd.h>
35#include <haproxy/freq_ctr.h>
36#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037#include <haproxy/h3.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010038#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010039#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/pipe.h>
41#include <haproxy/proxy.h>
42#include <haproxy/quic_cc.h>
43#include <haproxy/quic_frame.h>
44#include <haproxy/quic_loss.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020045#include <haproxy/cbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010046#include <haproxy/quic_tls.h>
47#include <haproxy/ssl_sock.h>
48#include <haproxy/stream_interface.h>
49#include <haproxy/task.h>
50#include <haproxy/trace.h>
51#include <haproxy/xprt_quic.h>
52
53struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020054 .max_udp_payload_size = QUIC_DFLT_MAX_UDP_PAYLOAD_SIZE,
55 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
56 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010057};
58
59/* trace source and events */
60static void quic_trace(enum trace_level level, uint64_t mask, \
61 const struct trace_source *src,
62 const struct ist where, const struct ist func,
63 const void *a1, const void *a2, const void *a3, const void *a4);
64
65static const struct trace_event quic_trace_events[] = {
66 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
67 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
68 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
69 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
70 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
71 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
72 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050073 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010074 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
75 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
76 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
77 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
78 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
79 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
80 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
81 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
82 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
83 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
84 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
85 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
86 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
87 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
88 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
89 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
90 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
91 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
92 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010093 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
94 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
95 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
96 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
97 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
98 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +010099 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100100 { /* end */ }
101};
102
103static const struct name_desc quic_trace_lockon_args[4] = {
104 /* arg1 */ { /* already used by the connection */ },
105 /* arg2 */ { .name="quic", .desc="QUIC transport" },
106 /* arg3 */ { },
107 /* arg4 */ { }
108};
109
110static const struct name_desc quic_trace_decoding[] = {
111#define QUIC_VERB_CLEAN 1
112 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
113 { /* end */ }
114};
115
116
117struct trace_source trace_quic = {
118 .name = IST("quic"),
119 .desc = "QUIC xprt",
120 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
121 .default_cb = quic_trace,
122 .known_events = quic_trace_events,
123 .lockon_args = quic_trace_lockon_args,
124 .decoding = quic_trace_decoding,
125 .report_events = ~0, /* report everything by default */
126};
127
128#define TRACE_SOURCE &trace_quic
129INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
130
131static BIO_METHOD *ha_quic_meth;
132
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200133DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100134DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200135 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100136DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100137DECLARE_POOL(pool_head_quic_connection_id,
138 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100139DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100140DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100141DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100142DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100143DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200144DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100145DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100146
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200147static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
148 struct quic_conn *qc, int pkt_type,
149 struct quic_enc_level *qel, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100150
151/* Add traces to <buf> depending on <frm> TX frame type. */
152static inline void chunk_tx_frm_appendf(struct buffer *buf,
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200153 const struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100154{
155 switch (frm->type) {
156 case QUIC_FT_CRYPTO:
157 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
158 (unsigned long long)frm->crypto.offset,
159 (unsigned long long)frm->crypto.len);
160 break;
161 default:
162 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
163 }
164}
165
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100166/* Only for debug purpose */
167struct enc_debug_info {
168 unsigned char *payload;
169 size_t payload_len;
170 unsigned char *aad;
171 size_t aad_len;
172 uint64_t pn;
173};
174
175/* Initializes a enc_debug_info struct (only for debug purpose) */
176static inline void enc_debug_info_init(struct enc_debug_info *edi,
177 unsigned char *payload, size_t payload_len,
178 unsigned char *aad, size_t aad_len, uint64_t pn)
179{
180 edi->payload = payload;
181 edi->payload_len = payload_len;
182 edi->aad = aad;
183 edi->aad_len = aad_len;
184 edi->pn = pn;
185}
186
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100187/* Trace callback for QUIC.
188 * These traces always expect that arg1, if non-null, is of type connection.
189 */
190static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
191 const struct ist where, const struct ist func,
192 const void *a1, const void *a2, const void *a3, const void *a4)
193{
194 const struct connection *conn = a1;
195
196 if (conn) {
197 struct quic_tls_secrets *secs;
198 struct quic_conn *qc;
199
200 qc = conn->qc;
201 chunk_appendf(&trace_buf, " : conn@%p", conn);
202 if ((mask & QUIC_EV_CONN_INIT) && qc) {
203 chunk_appendf(&trace_buf, "\n odcid");
204 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100205 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100206 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100207 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100208 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100209 }
210
211 if (mask & QUIC_EV_CONN_ADDDATA) {
212 const enum ssl_encryption_level_t *level = a2;
213 const size_t *len = a3;
214
215 if (level) {
216 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
217
218 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
219 }
220 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100221 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100222 }
223 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
224 /* Initial read & write secrets. */
225 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
226 const unsigned char *rx_sec = a2;
227 const unsigned char *tx_sec = a3;
228
229 secs = &qc->els[level].tls_ctx.rx;
230 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
231 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
232 if (rx_sec)
233 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
234 quic_tls_keys_hexdump(&trace_buf, secs);
235 }
236 secs = &qc->els[level].tls_ctx.tx;
237 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
238 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
239 if (tx_sec)
240 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
241 quic_tls_keys_hexdump(&trace_buf, secs);
242 }
243 }
244 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
245 const enum ssl_encryption_level_t *level = a2;
246 const unsigned char *secret = a3;
247 const size_t *secret_len = a4;
248
249 if (level) {
250 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
251
252 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
253 if (secret && secret_len)
254 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
255 secs = &qc->els[lvl].tls_ctx.rx;
256 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
257 quic_tls_keys_hexdump(&trace_buf, secs);
258 }
259 }
260
261 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
262 const enum ssl_encryption_level_t *level = a2;
263 const unsigned char *secret = a3;
264 const size_t *secret_len = a4;
265
266 if (level) {
267 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
268
269 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
270 if (secret && secret_len)
271 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
272 secs = &qc->els[lvl].tls_ctx.tx;
273 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
274 quic_tls_keys_hexdump(&trace_buf, secs);
275 }
276
277 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100278
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100279 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100280 const struct quic_tx_packet *pkt = a2;
281 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100282 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100283
284 if (qel) {
285 struct quic_pktns *pktns;
286
287 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100288 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
289 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100291 (unsigned long long)qc->path->cwnd,
292 (unsigned long long)qc->path->prep_in_flight,
293 (unsigned long long)qc->path->in_flight,
294 (unsigned long long)pktns->tx.in_flight,
295 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100296 }
297 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200298 const struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100299 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
300 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
301 list_for_each_entry(frm, &pkt->frms, list)
302 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100303 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
304 }
305
306 if (room) {
307 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
308 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
309 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100310 }
311 }
312
313 if (mask & QUIC_EV_CONN_HDSHK) {
314 const enum quic_handshake_state *state = a2;
315 const int *err = a3;
316
317 if (state)
318 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
319 if (err)
320 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
321 }
322
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100323 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100324 const struct quic_rx_packet *pkt = a2;
325 const unsigned long *pktlen = a3;
326 const SSL *ssl = a4;
327
328 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100329 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100330 pkt, quic_packet_type_enc_level_char(pkt->type));
331 if (pkt->pnl)
332 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
333 (unsigned long long)pkt->pn);
334 if (pkt->token_len)
335 chunk_appendf(&trace_buf, " toklen=%llu",
336 (unsigned long long)pkt->token_len);
337 if (pkt->aad_len)
338 chunk_appendf(&trace_buf, " aadlen=%llu",
339 (unsigned long long)pkt->aad_len);
340 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
341 pkt->flags, (unsigned long long)pkt->len);
342 }
343 if (pktlen)
344 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
345 if (ssl) {
346 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
347 chunk_appendf(&trace_buf, " el=%c",
348 quic_enc_level_char(ssl_to_quic_enc_level(level)));
349 }
350 }
351
352 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
353 const struct quic_rx_packet *pkt = a2;
354 const struct quic_rx_crypto_frm *cf = a3;
355 const SSL *ssl = a4;
356
357 if (pkt)
358 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
359 quic_packet_type_enc_level_char(pkt->type),
360 (unsigned long long)pkt->pn);
361 if (cf)
362 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
363 (unsigned long long)cf->offset_node.key,
364 (unsigned long long)cf->len);
365 if (ssl) {
366 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
367 chunk_appendf(&trace_buf, " el=%c",
368 quic_enc_level_char(ssl_to_quic_enc_level(level)));
369 }
370 }
371
372 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
373 const struct quic_frame *frm = a2;
374
375 if (frm)
376 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
377 }
378
379 if (mask & QUIC_EV_CONN_PHPKTS) {
380 const struct quic_enc_level *qel = a2;
381
382 if (qel) {
383 struct quic_pktns *pktns;
384
385 pktns = qc->pktns;
386 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200387 " 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 +0100388 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200389 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100390 !!(pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100391 (unsigned long long)qc->path->cwnd,
392 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100393 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100394 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
395 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100396 }
397 }
398
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100399 if (mask & QUIC_EV_CONN_ENCPKT) {
400 const struct enc_debug_info *edi = a2;
401
402 if (edi)
403 chunk_appendf(&trace_buf,
404 " payload=@%p payload_len=%llu"
405 " aad=@%p aad_len=%llu pn=%llu",
406 edi->payload, (unsigned long long)edi->payload_len,
407 edi->aad, (unsigned long long)edi->aad_len,
408 (unsigned long long)edi->pn);
409 }
410
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100411 if (mask & QUIC_EV_CONN_RMHP) {
412 const struct quic_rx_packet *pkt = a2;
413
414 if (pkt) {
415 const int *ret = a3;
416
417 chunk_appendf(&trace_buf, " pkt@%p", pkt);
418 if (ret && *ret)
419 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
420 pkt->pnl, (unsigned long long)pkt->pn);
421 }
422 }
423
424 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200425 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100426 const unsigned long *val1 = a3;
427 const unsigned long *val2 = a4;
428
429 if (frm)
430 chunk_tx_frm_appendf(&trace_buf, frm);
431 if (val1)
432 chunk_appendf(&trace_buf, " %lu", *val1);
433 if (val2)
434 chunk_appendf(&trace_buf, "..%lu", *val2);
435 }
436
437 if (mask & QUIC_EV_CONN_RTTUPDT) {
438 const unsigned int *rtt_sample = a2;
439 const unsigned int *ack_delay = a3;
440 const struct quic_loss *ql = a4;
441
442 if (rtt_sample)
443 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
444 if (ack_delay)
445 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
446 if (ql)
447 chunk_appendf(&trace_buf,
448 " srtt=%ums rttvar=%ums min_rtt=%ums",
449 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
450 }
451 if (mask & QUIC_EV_CONN_CC) {
452 const struct quic_cc_event *ev = a2;
453 const struct quic_cc *cc = a3;
454
455 if (a2)
456 quic_cc_event_trace(&trace_buf, ev);
457 if (a3)
458 quic_cc_state_trace(&trace_buf, cc);
459 }
460
461 if (mask & QUIC_EV_CONN_PKTLOSS) {
462 const struct quic_pktns *pktns = a2;
463 const struct list *lost_pkts = a3;
464 struct quic_conn *qc = conn->qc;
465
466 if (pktns) {
467 chunk_appendf(&trace_buf, " pktns=%s",
468 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
469 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
470 if (pktns->tx.loss_time)
471 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100472 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100473 }
474 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
475 struct quic_tx_packet *pkt;
476
477 chunk_appendf(&trace_buf, " lost_pkts:");
478 list_for_each_entry(pkt, lost_pkts, list)
479 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
480 }
481 }
482
483 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
484 struct quic_conn *qc = conn->qc;
485 const struct quic_pktns *pktns = a2;
486 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100487 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100488
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100489 if (ifae_pkts)
490 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
491 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100492 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100493 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100495 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
496 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100497 if (mask & QUIC_EV_CONN_STIMER) {
498 if (pktns->tx.loss_time)
499 chunk_appendf(&trace_buf, " loss_time=%dms",
500 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
501 }
502 if (mask & QUIC_EV_CONN_SPTO) {
503 if (pktns->tx.time_of_last_eliciting)
504 chunk_appendf(&trace_buf, " tole=%dms",
505 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
506 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100507 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100508 }
509 }
510
511 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
512 chunk_appendf(&trace_buf,
513 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
514 }
515 }
516
517 if (mask & QUIC_EV_CONN_SPPKTS) {
518 const struct quic_tx_packet *pkt = a2;
519
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100520 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
521 (unsigned long long)qc->path->cwnd,
522 (unsigned long long)qc->path->prep_in_flight,
523 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100524 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100525 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100526 (unsigned long)pkt->pn_node.key,
527 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
528 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100529 (unsigned long long)pkt->in_flight_len,
530 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100531 }
532 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100533
534 if (mask & QUIC_EV_CONN_SSLALERT) {
535 const uint8_t *alert = a2;
536 const enum ssl_encryption_level_t *level = a3;
537
538 if (alert)
539 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
540 if (level)
541 chunk_appendf(&trace_buf, " el=%c",
542 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
543 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100544
545 if (mask & QUIC_EV_CONN_BCFRMS) {
546 const size_t *sz1 = a2;
547 const size_t *sz2 = a3;
548 const size_t *sz3 = a4;
549
550 if (sz1)
551 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
552 if (sz2)
553 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
554 if (sz3)
555 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
556 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100557
558 if (mask & QUIC_EV_CONN_PSTRM) {
559 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100560
561 if (a2) {
562 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100563
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100564 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
565 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
566 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
567 (unsigned long long)s->id,
568 (unsigned long long)s->offset,
569 (unsigned long long)s->len);
570 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100571 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100572 }
573 if (mask & QUIC_EV_CONN_LPKT) {
574 const struct quic_rx_packet *pkt = a2;
575
576 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200577 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100578 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200579 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
580 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100581 }
582
583}
584
585/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200586static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587{
588 struct quic_conn *qc;
589
590 qc = ctx->conn->qc;
591 if (objt_server(qc->conn->target))
592 return 1;
593
594 if ((qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
595 (qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +0200596 (qc->state & QUIC_HS_ST_COMPLETE))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100597 return 1;
598
599 return 0;
600}
601
602/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
603 * both loss detection and PTO and schedule the task assiated to this timer if needed.
604 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200605static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100606{
607 struct quic_conn *qc;
608 struct quic_pktns *pktns;
609 unsigned int pto;
610
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100611 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
612 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613 qc = ctx->conn->qc;
614 pktns = quic_loss_pktns(qc);
615 if (tick_isset(pktns->tx.loss_time)) {
616 qc->timer = pktns->tx.loss_time;
617 goto out;
618 }
619
620 /* XXX TODO: anti-amplification: the timer must be
621 * cancelled for a server which reached the anti-amplification limit.
622 */
623
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100624 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
625 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100626 /* Timer cancellation. */
627 qc->timer = TICK_ETERNITY;
628 goto out;
629 }
630
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +0200631 pktns = quic_pto_pktns(qc, qc->state & QUIC_HS_ST_COMPLETE, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100632 if (tick_isset(pto))
633 qc->timer = pto;
634 out:
635 task_schedule(qc->timer_task, qc->timer);
636 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
637}
638
639#ifndef OPENSSL_IS_BORINGSSL
640int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
641 const uint8_t *read_secret,
642 const uint8_t *write_secret, size_t secret_len)
643{
644 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
645 struct quic_tls_ctx *tls_ctx =
646 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
647 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
648
649 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
650 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
651 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
652 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
653
654 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
655 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
656 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
657 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
658 read_secret, secret_len)) {
659 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
660 return 0;
661 }
662
663 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
664 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
665 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
666 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
667 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
668 write_secret, secret_len)) {
669 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
670 return 0;
671 }
672
673 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
674 if (objt_server(conn->target) && level == ssl_encryption_application) {
675 const unsigned char *buf;
676 size_t buflen;
677
678 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
679 if (!buflen)
680 return 0;
681
682 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
683 return 0;
684 }
685 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
686
687 return 1;
688}
689#else
690/* ->set_read_secret callback to derive the RX secrets at <level> encryption
691 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500692 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100693 */
694int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
695 const SSL_CIPHER *cipher,
696 const uint8_t *secret, size_t secret_len)
697{
698 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
699 struct quic_tls_ctx *tls_ctx =
700 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
701
702 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
703 tls_ctx->rx.aead = tls_aead(cipher);
704 tls_ctx->rx.md = tls_md(cipher);
705 tls_ctx->rx.hp = tls_hp(cipher);
706
707 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
708 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
709 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
710 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
711 secret, secret_len)) {
712 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
713 goto err;
714 }
715
716 if (objt_server(conn->target) && level == ssl_encryption_application) {
717 const unsigned char *buf;
718 size_t buflen;
719
720 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
721 if (!buflen)
722 goto err;
723
724 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
725 goto err;
726 }
727
728 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
729 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
730
731 return 1;
732
733 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100734 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100735 return 0;
736}
737
738/* ->set_write_secret callback to derive the TX secrets at <level>
739 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500740 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100741 */
742int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
743 const SSL_CIPHER *cipher,
744 const uint8_t *secret, size_t secret_len)
745{
746 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
747 struct quic_tls_ctx *tls_ctx =
748 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
749
750 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
751 tls_ctx->tx.aead = tls_aead(cipher);
752 tls_ctx->tx.md = tls_md(cipher);
753 tls_ctx->tx.hp = tls_hp(cipher);
754
755 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
756 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
757 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
758 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
759 secret, secret_len)) {
760 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
761 goto err;
762 }
763
764 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
765 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
766
767 return 1;
768
769 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100770 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100771 return 0;
772}
773#endif
774
775/* This function copies the CRYPTO data provided by the TLS stack found at <data>
776 * with <len> as size in CRYPTO buffers dedicated to store the information about
777 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
778 * It fails only if it could not managed to allocate enough CRYPTO buffers to
779 * store all the data.
780 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
781 */
782static int quic_crypto_data_cpy(struct quic_enc_level *qel,
783 const unsigned char *data, size_t len)
784{
785 struct quic_crypto_buf **qcb;
786 /* The remaining byte to store in CRYPTO buffers. */
787 size_t cf_offset, cf_len, *nb_buf;
788 unsigned char *pos;
789
790 nb_buf = &qel->tx.crypto.nb_buf;
791 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
792 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
793 cf_len = len;
794
795 while (len) {
796 size_t to_copy, room;
797
798 pos = (*qcb)->data + (*qcb)->sz;
799 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
800 to_copy = len > room ? room : len;
801 if (to_copy) {
802 memcpy(pos, data, to_copy);
803 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
804 qel->tx.crypto.sz += to_copy;
805 (*qcb)->sz += to_copy;
806 pos += to_copy;
807 len -= to_copy;
808 data += to_copy;
809 }
810 else {
811 struct quic_crypto_buf **tmp;
812
813 tmp = realloc(qel->tx.crypto.bufs,
814 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
815 if (tmp) {
816 qel->tx.crypto.bufs = tmp;
817 qcb = &qel->tx.crypto.bufs[*nb_buf];
818 *qcb = pool_alloc(pool_head_quic_crypto_buf);
819 if (!*qcb)
820 return 0;
821
822 (*qcb)->sz = 0;
823 ++*nb_buf;
824 }
825 else {
826 break;
827 }
828 }
829 }
830
831 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
832 * have been buffered.
833 */
834 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200835 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100836
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200837 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100838 if (!frm)
839 return 0;
840
841 frm->type = QUIC_FT_CRYPTO;
842 frm->crypto.offset = cf_offset;
843 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +0200844 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +0200845 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100846 }
847
848 return len == 0;
849}
850
851
852/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
853 * wants to provide the QUIC layer with CRYPTO data.
854 * Returns 1 if succeeded, 0 if not.
855 */
856int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
857 const uint8_t *data, size_t len)
858{
859 struct connection *conn;
860 enum quic_tls_enc_level tel;
861 struct quic_enc_level *qel;
862
863 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
864 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
865 tel = ssl_to_quic_enc_level(level);
866 qel = &conn->qc->els[tel];
867
868 if (tel == -1) {
869 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
870 goto err;
871 }
872
873 if (!quic_crypto_data_cpy(qel, data, len)) {
874 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
875 goto err;
876 }
877
878 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
879 conn, &level, &len);
880
881 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
882 return 1;
883
884 err:
885 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
886 return 0;
887}
888
889int ha_quic_flush_flight(SSL *ssl)
890{
891 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
892
893 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
894 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
895
896 return 1;
897}
898
899int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
900{
901 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
902
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100903 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100904 return 1;
905}
906
907/* QUIC TLS methods */
908static SSL_QUIC_METHOD ha_quic_method = {
909#ifdef OPENSSL_IS_BORINGSSL
910 .set_read_secret = ha_set_rsec,
911 .set_write_secret = ha_set_wsec,
912#else
913 .set_encryption_secrets = ha_quic_set_encryption_secrets,
914#endif
915 .add_handshake_data = ha_quic_add_handshake_data,
916 .flush_flight = ha_quic_flush_flight,
917 .send_alert = ha_quic_send_alert,
918};
919
920/* Initialize the TLS context of a listener with <bind_conf> as configuration.
921 * Returns an error count.
922 */
923int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
924{
925 struct proxy *curproxy = bind_conf->frontend;
926 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
927 int cfgerr = 0;
928
929#if 0
930 /* XXX Did not manage to use this. */
931 const char *ciphers =
932 "TLS_AES_128_GCM_SHA256:"
933 "TLS_AES_256_GCM_SHA384:"
934 "TLS_CHACHA20_POLY1305_SHA256:"
935 "TLS_AES_128_CCM_SHA256";
936#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200937 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100938 long options =
939 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
940 SSL_OP_SINGLE_ECDH_USE |
941 SSL_OP_CIPHER_SERVER_PREFERENCE;
942 SSL_CTX *ctx;
943
944 ctx = SSL_CTX_new(TLS_server_method());
945 bind_conf->initial_ctx = ctx;
946
947 SSL_CTX_set_options(ctx, options);
948#if 0
949 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
950 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
951 "for bind '%s' at [%s:%d].\n",
952 curproxy->id, ciphers,
953 bind_conf->arg, bind_conf->file, bind_conf->line);
954 cfgerr++;
955 }
956#endif
957
958 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
959 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
960 "for bind '%s' at [%s:%d].\n",
961 curproxy->id, groups,
962 bind_conf->arg, bind_conf->file, bind_conf->line);
963 cfgerr++;
964 }
965
966 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
967 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
968 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
969 SSL_CTX_set_default_verify_paths(ctx);
970
971#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
972#ifdef OPENSSL_IS_BORINGSSL
973 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
974 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
975#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
976 if (bind_conf->ssl_conf.early_data) {
977 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
978 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
979 }
980 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
981 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
982#else
983 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
984#endif
985 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
986#endif
987 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
988
989 return cfgerr;
990}
991
992/* Decode an expected packet number from <truncated_on> its truncated value,
993 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
994 * the number of bits used to encode this packet number (its length in bytes * 8).
995 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
996 */
997static uint64_t decode_packet_number(uint64_t largest_pn,
998 uint32_t truncated_pn, unsigned int pn_nbits)
999{
1000 uint64_t expected_pn = largest_pn + 1;
1001 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1002 uint64_t pn_hwin = pn_win / 2;
1003 uint64_t pn_mask = pn_win - 1;
1004 uint64_t candidate_pn;
1005
1006
1007 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1008 /* Note that <pn_win> > <pn_hwin>. */
1009 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1010 candidate_pn + pn_hwin <= expected_pn)
1011 return candidate_pn + pn_win;
1012
1013 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1014 return candidate_pn - pn_win;
1015
1016 return candidate_pn;
1017}
1018
1019/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1020 * cryptographic context.
1021 * <largest_pn> is the largest received packet number and <pn> the address of
1022 * the packet number field for this packet with <byte0> address of its first byte.
1023 * <end> points to one byte past the end of this packet.
1024 * Returns 1 if succeeded, 0 if not.
1025 */
1026static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1027 int64_t largest_pn, unsigned char *pn,
1028 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001029 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001030{
1031 int ret, outlen, i, pnlen;
1032 uint64_t packet_number;
1033 uint32_t truncated_pn = 0;
1034 unsigned char mask[5] = {0};
1035 unsigned char *sample;
1036 EVP_CIPHER_CTX *cctx;
1037 unsigned char *hp_key;
1038
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001039 /* Check there is enough data in this packet. */
1040 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1041 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1042 return 0;
1043 }
1044
1045 cctx = EVP_CIPHER_CTX_new();
1046 if (!cctx) {
1047 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1048 return 0;
1049 }
1050
1051 ret = 0;
1052 sample = pn + QUIC_PACKET_PN_MAXLEN;
1053
1054 hp_key = tls_ctx->rx.hp_key;
1055 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1056 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1057 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1058 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1059 goto out;
1060 }
1061
1062 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1063 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1064 for (i = 0; i < pnlen; i++) {
1065 pn[i] ^= mask[i + 1];
1066 truncated_pn = (truncated_pn << 8) | pn[i];
1067 }
1068
1069 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1070 /* Store remaining information for this unprotected header */
1071 pkt->pn = packet_number;
1072 pkt->pnl = pnlen;
1073
1074 ret = 1;
1075
1076 out:
1077 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001078
1079 return ret;
1080}
1081
1082/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1083 * address, with <payload_len> as payload length, <aad> as address of
1084 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1085 * context.
1086 * Returns 1 if succeeded, 0 if not.
1087 */
1088static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1089 unsigned char *aad, size_t aad_len, uint64_t pn,
1090 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1091{
1092 unsigned char iv[12];
1093 unsigned char *tx_iv = tls_ctx->tx.iv;
1094 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001095 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001096
1097 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1098 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001099 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001100 }
1101
1102 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1103 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1104 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001105 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001106 }
1107
1108 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001109
1110 err:
1111 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1112 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1113 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001114}
1115
1116/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1117 * Returns 1 if succeeded, 0 if not.
1118 */
1119static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1120{
1121 int ret;
1122 unsigned char iv[12];
1123 unsigned char *rx_iv = tls_ctx->rx.iv;
1124 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1125
1126 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1127 return 0;
1128
1129 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1130 pkt->data, pkt->aad_len,
1131 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1132 if (!ret)
1133 return 0;
1134
1135 /* Update the packet length (required to parse the frames). */
1136 pkt->len = pkt->aad_len + ret;
1137
1138 return 1;
1139}
1140
1141/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001142static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001143 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001144{
1145 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001146 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001147 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001148}
1149
1150/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1151 * deallocating them, and their TX frames.
1152 * Returns the last node reached to be used for the next range.
1153 * May be NULL if <largest> node could not be found.
1154 */
1155static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1156 struct list *newly_acked_pkts,
1157 struct eb64_node *largest_node,
1158 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001159 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001160{
1161 struct eb64_node *node;
1162 struct quic_tx_packet *pkt;
1163
1164 if (largest_node)
1165 node = largest_node;
1166 else {
1167 node = eb64_lookup(pkts, largest);
1168 while (!node && largest > smallest) {
1169 node = eb64_lookup(pkts, --largest);
1170 }
1171 }
1172
1173 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001174 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001175
1176 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1177 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001178 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001179 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1180 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1181 qc_treat_acked_tx_frm(frm, ctx);
1182 node = eb64_prev(node);
1183 eb64_delete(&pkt->pn_node);
1184 }
1185
1186 return node;
1187}
1188
1189/* Treat <frm> frame whose packet it is attached to has just been detected as non
1190 * acknowledged.
1191 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001192static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001193 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001194 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001195{
1196 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001197 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001198 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001199}
1200
1201
1202/* Free the TX packets of <pkts> list */
1203static inline void free_quic_tx_pkts(struct list *pkts)
1204{
1205 struct quic_tx_packet *pkt, *tmp;
1206
1207 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001208 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001209 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001210 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001211 }
1212}
1213
1214/* Send a packet loss event nofification to the congestion controller
1215 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1216 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1217 * at <now_us> current time.
1218 * Always succeeds.
1219 */
1220static inline void qc_cc_loss_event(struct quic_conn *qc,
1221 unsigned int lost_bytes,
1222 unsigned int newest_time_sent,
1223 unsigned int period,
1224 unsigned int now_us)
1225{
1226 struct quic_cc_event ev = {
1227 .type = QUIC_CC_EVT_LOSS,
1228 .loss.now_ms = now_ms,
1229 .loss.max_ack_delay = qc->max_ack_delay,
1230 .loss.lost_bytes = lost_bytes,
1231 .loss.newest_time_sent = newest_time_sent,
1232 .loss.period = period,
1233 };
1234
1235 quic_cc_event(&qc->path->cc, &ev);
1236}
1237
1238/* Send a packet ack event nofication for each newly acked packet of
1239 * <newly_acked_pkts> list and free them.
1240 * Always succeeds.
1241 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001242static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001243 struct list *newly_acked_pkts)
1244{
1245 struct quic_conn *qc = ctx->conn->qc;
1246 struct quic_tx_packet *pkt, *tmp;
1247 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1248
1249 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1250 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001251 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001252 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001253 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001254 ev.ack.acked = pkt->in_flight_len;
1255 ev.ack.time_sent = pkt->time_sent;
1256 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001257 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001258 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001259 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001260 }
1261
1262}
1263
1264/* Handle <pkts> list of lost packets detected at <now_us> handling
1265 * their TX frames.
1266 * Send a packet loss event to the congestion controller if
1267 * in flight packet have been lost.
1268 * Also frees the packet in <pkts> list.
1269 * Never fails.
1270 */
1271static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001272 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001273 struct list *pkts,
1274 uint64_t now_us)
1275{
1276 struct quic_conn *qc = ctx->conn->qc;
1277 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001278 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001279 uint64_t lost_bytes;
1280
1281 lost_bytes = 0;
1282 oldest_lost = newest_lost = NULL;
1283 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1284 lost_bytes += pkt->in_flight_len;
1285 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001286 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001287 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001288 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001289 /* Treat the frames of this lost packet. */
1290 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1291 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001292 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001293 if (!oldest_lost) {
1294 oldest_lost = newest_lost = pkt;
1295 }
1296 else {
1297 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001298 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001299 newest_lost = pkt;
1300 }
1301 }
1302
1303 if (lost_bytes) {
1304 /* Sent a packet loss event to the congestion controller. */
1305 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1306 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001307 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001308 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001309 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001310 }
1311}
1312
1313/* Look for packet loss from sent packets for <qel> encryption level of a
1314 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1315 * their tree if deemed as lost or set the <loss_time> value the packet number
1316 * space if any not deemed lost.
1317 * Should be called after having received an ACK frame with newly acknowledged
1318 * packets or when the the loss detection timer has expired.
1319 * Always succeeds.
1320 */
1321static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1322 struct quic_conn *qc,
1323 struct list *lost_pkts)
1324{
1325 struct eb_root *pkts;
1326 struct eb64_node *node;
1327 struct quic_loss *ql;
1328 unsigned int loss_delay;
1329
1330 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1331 pkts = &pktns->tx.pkts;
1332 pktns->tx.loss_time = TICK_ETERNITY;
1333 if (eb_is_empty(pkts))
1334 goto out;
1335
1336 ql = &qc->path->loss;
1337 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1338 loss_delay += loss_delay >> 3;
1339 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1340
1341 node = eb64_first(pkts);
1342 while (node) {
1343 struct quic_tx_packet *pkt;
1344 int64_t largest_acked_pn;
1345 unsigned int loss_time_limit, time_sent;
1346
1347 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001348 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001349 node = eb64_next(node);
1350 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1351 break;
1352
1353 time_sent = pkt->time_sent;
1354 loss_time_limit = tick_add(time_sent, loss_delay);
1355 if (tick_is_le(time_sent, now_ms) ||
1356 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1357 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001358 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001359 }
1360 else {
1361 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1362 }
1363 }
1364
1365 out:
1366 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1367}
1368
1369/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1370 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001371 * 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 +01001372 * acked ack-eliciting packet.
1373 * Return 1, if succeeded, 0 if not.
1374 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001375static 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 +01001376 struct quic_enc_level *qel,
1377 unsigned int *rtt_sample,
1378 const unsigned char **pos, const unsigned char *end)
1379{
1380 struct quic_ack *ack = &frm->ack;
1381 uint64_t smallest, largest;
1382 struct eb_root *pkts;
1383 struct eb64_node *largest_node;
1384 unsigned int time_sent, pkt_flags;
1385 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1386 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1387
1388 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1389 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1390 ctx->conn,, &ack->largest_ack);
1391 goto err;
1392 }
1393
1394 if (ack->first_ack_range > ack->largest_ack) {
1395 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1396 ctx->conn,, &ack->first_ack_range);
1397 goto err;
1398 }
1399
1400 largest = ack->largest_ack;
1401 smallest = largest - ack->first_ack_range;
1402 pkts = &qel->pktns->tx.pkts;
1403 pkt_flags = 0;
1404 largest_node = NULL;
1405 time_sent = 0;
1406
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001407 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 +01001408 largest_node = eb64_lookup(pkts, largest);
1409 if (!largest_node) {
1410 TRACE_DEVEL("Largest acked packet not found",
1411 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1412 goto err;
1413 }
1414
1415 time_sent = eb64_entry(&largest_node->node,
1416 struct quic_tx_packet, pn_node)->time_sent;
1417 }
1418
1419 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1420 ctx->conn,, &largest, &smallest);
1421 do {
1422 uint64_t gap, ack_range;
1423
1424 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1425 largest_node, largest, smallest, ctx);
1426 if (!ack->ack_range_num--)
1427 break;
1428
1429 if (!quic_dec_int(&gap, pos, end))
1430 goto err;
1431
1432 if (smallest < gap + 2) {
1433 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1434 ctx->conn,, &gap, &smallest);
1435 goto err;
1436 }
1437
1438 largest = smallest - gap - 2;
1439 if (!quic_dec_int(&ack_range, pos, end))
1440 goto err;
1441
1442 if (largest < ack_range) {
1443 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1444 ctx->conn,, &largest, &ack_range);
1445 goto err;
1446 }
1447
1448 /* Do not use this node anymore. */
1449 largest_node = NULL;
1450 /* Next range */
1451 smallest = largest - ack_range;
1452
1453 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1454 ctx->conn,, &largest, &smallest);
1455 } while (1);
1456
1457 /* Flag this packet number space as having received an ACK. */
1458 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_RECEIVED;
1459
1460 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1461 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001462 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001463 }
1464
1465 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1466 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1467 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1468 if (!LIST_ISEMPTY(&lost_pkts))
1469 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1470 }
1471 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1472 if (quic_peer_validated_addr(ctx))
1473 ctx->conn->qc->path->loss.pto_count = 0;
1474 qc_set_timer(ctx);
1475 }
1476
1477
1478 return 1;
1479
1480 err:
1481 free_quic_tx_pkts(&newly_acked_pkts);
1482 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1483 return 0;
1484}
1485
1486/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1487 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001488 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001489 * Return 1 if succeeded, 0 if not.
1490 */
1491static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001492 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001493 const unsigned char *data, size_t len,
1494 struct quic_rx_packet *pkt,
1495 struct quic_rx_crypto_frm *cf)
1496{
1497 int ssl_err;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001498 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001499
1500 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1501 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001502 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001503 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1504 TRACE_PROTO("SSL_provide_quic_data() error",
1505 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1506 goto err;
1507 }
1508
1509 el->rx.crypto.offset += len;
1510 TRACE_PROTO("in order CRYPTO data",
1511 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1512
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001513 if (qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001514 ssl_err = SSL_do_handshake(ctx->ssl);
1515 if (ssl_err != 1) {
1516 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1517 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1518 TRACE_PROTO("SSL handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001519 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001520 goto out;
1521 }
1522
1523 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001524 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001525 goto err;
1526 }
1527
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001528 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001529 if (objt_listener(ctx->conn->target))
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001530 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001531 else
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001532 qc->state = QUIC_HS_ST_COMPLETE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001533 } else {
1534 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1535 if (ssl_err != 1) {
1536 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1537 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1538 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001539 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001540 goto out;
1541 }
1542
1543 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001544 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001545 goto err;
1546 }
1547
1548 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001549 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001550 }
1551
1552 out:
1553 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1554 return 1;
1555
1556 err:
1557 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1558 return 0;
1559}
1560
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001561/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1562 * <pkt> RX packet.
1563 * Return it if succeeded, NULL if not.
1564 */
1565static inline
1566struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1567 struct quic_rx_packet *pkt)
1568{
1569 struct quic_rx_strm_frm *frm;
1570
1571 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1572 if (frm) {
1573 frm->offset_node.key = stream_frm->offset;
1574 frm->len = stream_frm->len;
1575 frm->data = stream_frm->data;
1576 frm->pkt = pkt;
1577 }
1578
1579 return frm;
1580}
1581
1582/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1583 * several streams, depending on the already open onces.
1584 * Return this node if succeeded, NULL if not.
1585 */
1586static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1587{
1588 unsigned int strm_type;
1589 int64_t sub_id;
1590 struct eb64_node *strm_node;
1591
1592 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1593
1594 strm_type = id & QCS_ID_TYPE_MASK;
1595 sub_id = id >> QCS_ID_TYPE_SHIFT;
1596 strm_node = NULL;
1597 if (qc_local_stream_id(qcc, id)) {
1598 /* Local streams: this stream must be already opened. */
1599 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1600 if (!strm_node) {
1601 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1602 goto out;
1603 }
1604 }
1605 else {
1606 /* Remote streams. */
1607 struct eb_root *strms;
1608 uint64_t largest_id;
1609 enum qcs_type qcs_type;
1610
1611 strms = &qcc->streams_by_id;
1612 qcs_type = qcs_id_type(id);
1613 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1614 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1615 goto out;
1616 }
1617
1618 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1619 * correct value.
1620 */
1621 largest_id = qcc->strms[qcs_type].largest_id;
1622 if (sub_id > (int64_t)largest_id) {
1623 /* RFC: "A stream ID that is used out of order results in all streams
1624 * of that type with lower-numbered stream IDs also being opened".
1625 * So, let's "open" these streams.
1626 */
1627 int64_t i;
1628 struct qcs *qcs;
1629
1630 qcs = NULL;
1631 for (i = largest_id + 1; i <= sub_id; i++) {
1632 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1633 if (!qcs) {
1634 TRACE_PROTO("Could not allocate a new stream",
1635 QUIC_EV_CONN_PSTRM, qcc->conn);
1636 goto out;
1637 }
1638
1639 qcc->strms[qcs_type].largest_id = i;
1640 }
1641 if (qcs)
1642 strm_node = &qcs->by_id;
1643 }
1644 else {
1645 strm_node = eb64_lookup(strms, id);
1646 }
1647 }
1648
1649 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1650 return strm_node;
1651
1652 out:
1653 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1654 return NULL;
1655}
1656
1657/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1658 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1659 * to reflect the data which have been consumed.
1660 */
1661static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1662{
1663 size_t ret;
1664
1665 ret = 0;
1666 while (strm_frm->len) {
1667 size_t try;
1668
1669 try = b_contig_space(buf);
1670 if (!try)
1671 break;
1672
1673 if (try > strm_frm->len)
1674 try = strm_frm->len;
1675 memcpy(b_tail(buf), strm_frm->data, try);
1676 strm_frm->len -= try;
1677 strm_frm->offset += try;
1678 b_add(buf, try);
1679 ret += try;
1680 }
1681
1682 return ret;
1683}
1684
1685/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1686 * streams may be open. The data are copied to the stream RX buffer if possible.
1687 * If not, the STREAM frame is stored to be treated again later.
1688 * We rely on the flow control so that not to store too much STREAM frames.
1689 * Return 1 if succeeded, 0 if not.
1690 */
1691static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1692 struct quic_stream *strm_frm,
1693 struct quic_conn *qc)
1694{
1695 struct qcs *strm;
1696 struct eb64_node *strm_node, *frm_node;
1697 struct quic_rx_strm_frm *frm;
1698
1699 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1700 if (!strm_node) {
1701 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1702 return 0;
1703 }
1704
1705 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1706 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1707 /* FIXME: handle the case where this frame overlap others */
1708 if (frm_node) {
1709 TRACE_PROTO("Already existing stream data",
1710 QUIC_EV_CONN_PSTRM, qc->conn);
1711 goto out;
1712 }
1713
1714 if (strm_frm->offset == strm->rx.offset) {
1715 int ret;
1716
1717 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1718 goto store_frm;
1719
1720 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1721 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1722 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1723 return 0;
1724 }
1725
1726 strm->rx.offset += ret;
1727 }
1728
1729 if (!strm_frm->len)
1730 goto out;
1731
1732 store_frm:
1733 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1734 if (!frm) {
1735 TRACE_PROTO("Could not alloc RX STREAM frame",
1736 QUIC_EV_CONN_PSTRM, qc->conn);
1737 return 0;
1738 }
1739
1740 eb64_insert(&strm->frms, &frm->offset_node);
1741 quic_rx_packet_refinc(pkt);
1742
1743 out:
1744 return 1;
1745}
1746
1747/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1748 * streams may be open. The data are copied to the stream RX buffer if possible.
1749 * If not, the STREAM frame is stored to be treated again later.
1750 * We rely on the flow control so that not to store too much STREAM frames.
1751 * Return 1 if succeeded, 0 if not.
1752 */
1753static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1754 struct quic_stream *strm_frm,
1755 struct quic_conn *qc)
1756{
1757 struct qcs *strm;
1758 struct eb64_node *strm_node, *frm_node;
1759 struct quic_rx_strm_frm *frm;
1760 size_t strm_frm_len;
1761
1762 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1763 if (!strm_node) {
1764 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1765 return 0;
1766 }
1767
1768 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1769 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1770 /* FIXME: handle the case where this frame overlap others */
1771 if (frm_node) {
1772 TRACE_PROTO("Already existing stream data",
1773 QUIC_EV_CONN_PSTRM, qc->conn);
1774 goto out;
1775 }
1776
1777 strm_frm_len = strm_frm->len;
1778 if (strm_frm->offset == strm->rx.offset) {
1779 int ret;
1780
1781 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1782 goto store_frm;
1783
1784 /* qc_strm_cpy() will modify the offset, depending on the number
1785 * of bytes copied.
1786 */
1787 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1788 /* Inform the application of the arrival of this new stream */
1789 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1790 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1791 return 0;
1792 }
1793
1794 if (ret)
1795 ruqs_notify_recv(strm);
1796
1797 strm_frm->offset += ret;
1798 }
1799 /* Take this frame into an account for the stream flow control */
1800 strm->rx.offset += strm_frm_len;
1801 /* It all the data were provided to the application, there is no need to
1802 * store any more inforamtion for it.
1803 */
1804 if (!strm_frm->len)
1805 goto out;
1806
1807 store_frm:
1808 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1809 if (!frm) {
1810 TRACE_PROTO("Could not alloc RX STREAM frame",
1811 QUIC_EV_CONN_PSTRM, qc->conn);
1812 return 0;
1813 }
1814
1815 eb64_insert(&strm->frms, &frm->offset_node);
1816 quic_rx_packet_refinc(pkt);
1817
1818 out:
1819 return 1;
1820}
1821
1822static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1823 struct quic_stream *strm_frm,
1824 struct quic_conn *qc)
1825{
1826 if (strm_frm->id & QCS_ID_DIR_BIT)
1827 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1828 else
1829 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1830}
1831
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001832/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1833 * as I/O handler context and <qel> as encryption level.
1834 * Returns 1 if succeeded, 0 if failed.
1835 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001836static 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 +01001837 struct quic_enc_level *qel)
1838{
1839 struct quic_frame frm;
1840 const unsigned char *pos, *end;
1841 struct quic_conn *conn = ctx->conn->qc;
1842
1843 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1844 /* Skip the AAD */
1845 pos = pkt->data + pkt->aad_len;
1846 end = pkt->data + pkt->len;
1847
1848 while (pos < end) {
1849 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1850 goto err;
1851
1852 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001853 case QUIC_FT_PADDING:
1854 if (pos != end) {
1855 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1856 goto err;
1857 }
1858 break;
1859 case QUIC_FT_PING:
1860 break;
1861 case QUIC_FT_ACK:
1862 {
1863 unsigned int rtt_sample;
1864
1865 rtt_sample = 0;
1866 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1867 goto err;
1868
1869 if (rtt_sample) {
1870 unsigned int ack_delay;
1871
1872 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1873 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1874 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1875 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001876 break;
1877 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001878 case QUIC_FT_CRYPTO:
1879 if (frm.crypto.offset != qel->rx.crypto.offset) {
1880 struct quic_rx_crypto_frm *cf;
1881
1882 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1883 if (!cf) {
1884 TRACE_DEVEL("CRYPTO frame allocation failed",
1885 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1886 goto err;
1887 }
1888
1889 cf->offset_node.key = frm.crypto.offset;
1890 cf->len = frm.crypto.len;
1891 cf->data = frm.crypto.data;
1892 cf->pkt = pkt;
1893 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1894 quic_rx_packet_refinc(pkt);
1895 }
1896 else {
1897 /* XXX TO DO: <cf> is used only for the traces. */
1898 struct quic_rx_crypto_frm cf = { };
1899
1900 cf.offset_node.key = frm.crypto.offset;
1901 cf.len = frm.crypto.len;
1902 if (!qc_provide_cdata(qel, ctx,
1903 frm.crypto.data, frm.crypto.len,
1904 pkt, &cf))
1905 goto err;
1906 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001907 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001908 case QUIC_FT_STREAM_8:
1909 case QUIC_FT_STREAM_9:
1910 case QUIC_FT_STREAM_A:
1911 case QUIC_FT_STREAM_B:
1912 case QUIC_FT_STREAM_C:
1913 case QUIC_FT_STREAM_D:
1914 case QUIC_FT_STREAM_E:
1915 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001916 {
1917 struct quic_stream *stream = &frm.stream;
1918
1919 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
1920 if (objt_listener(ctx->conn->target)) {
1921 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
1922 goto err;
1923 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
1924 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001925
1926 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
1927 goto err;
1928
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001929 break;
1930 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001931 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001932 break;
1933 case QUIC_FT_CONNECTION_CLOSE:
1934 case QUIC_FT_CONNECTION_CLOSE_APP:
1935 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001936 case QUIC_FT_HANDSHAKE_DONE:
1937 if (objt_listener(ctx->conn->target))
1938 goto err;
1939
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001940 conn->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001941 break;
1942 default:
1943 goto err;
1944 }
1945 }
1946
1947 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
1948 * has successfully parse a Handshake packet. The Initial encryption must also
1949 * be discarded.
1950 */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001951 if (conn->state == QUIC_HS_ST_SERVER_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001952 pkt->type == QUIC_PACKET_TYPE_HANDSHAKE) {
1953 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
1954 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
1955 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001956 conn->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001957 }
1958
1959 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1960 return 1;
1961
1962 err:
1963 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
1964 return 0;
1965}
1966
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001967/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
1968 * buffer. This is the responsability of the caller to check there is enough
1969 * room in <cbuf>. Also increase the <cbuf> write index consequently.
1970 * This function must be called only after having built a correct datagram.
1971 * Always succeeds.
1972 */
1973static inline void qc_set_dg(struct cbuf *cbuf,
1974 uint16_t dglen, struct quic_tx_packet *pkt)
1975{
1976 write_u16(cb_wr(cbuf), dglen);
1977 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
1978 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
1979}
1980
1981/* Prepare as much as possible handshake packets into <qr> ring buffer for
1982 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
1983 * several packets in the same datagram. A header made of two fields is added
1984 * to each datagram: the datagram length followed by the address of the first
1985 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001986 * Returns 1 if succeeded, or 0 if something wrong happened.
1987 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001988static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001989{
1990 struct quic_conn *qc;
1991 enum quic_tls_enc_level tel, next_tel;
1992 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001993 struct cbuf *cbuf;
1994 unsigned char *end_buf, *end, *pos, *spos;
1995 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
1996 /* length of datagrams */
1997 uint16_t dglen;
1998 size_t total;
1999 /* Each datagram is prepended with its length followed by the
2000 * address of the first packet in the datagram.
2001 */
2002 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002003
2004 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2005 qc = ctx->conn->qc;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002006 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc->state)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002007 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002008 goto err;
2009 }
2010
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002011 start:
2012 total = 0;
2013 dglen = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002014 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002015 cbuf = qr->cbuf;
2016 spos = pos = cb_wr(cbuf);
2017 /* Leave at least <dglen> bytes at the end of this buffer
2018 * to ensure there is enough room to mark the end of prepared
2019 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002020 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002021 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2022 first_pkt = prv_pkt = NULL;
2023 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
2024 int err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002025 enum quic_pkt_type pkt_type;
2026
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002027 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002028 /* Do not build any more packet if the TX secrets are not available or
2029 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002030 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002031 * and if there is no more CRYPTO data available or in flight
2032 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002033 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002034 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2035 (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002036 !qc->tx.nb_pto_dgrams &&
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002037 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002038 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002039 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002040 /* Set the current datagram as prepared into <cbuf> if
2041 * the was already a correct packet which was previously written.
2042 */
2043 if (prv_pkt)
2044 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002045 break;
2046 }
2047
2048 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002049 if (!prv_pkt) {
2050 /* Leave room for the datagram header */
2051 pos += dg_headlen;
2052 end = pos + qc->path->mtu;
2053 }
2054
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02002055 cur_pkt = qc_build_pkt(&pos, end, qc, pkt_type, qel, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002056 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002057 case -2:
2058 goto err;
2059 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002060 /* If there was already a correct packet present, set the
2061 * current datagram as prepared into <cbuf>.
2062 */
2063 if (prv_pkt) {
2064 qc_set_dg(cbuf, dglen, first_pkt);
2065 goto stop_build;
2066 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002067 goto out;
2068 default:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002069 total += cur_pkt->len;
2070 /* keep trace of the first packet in the datagram */
2071 if (!first_pkt)
2072 first_pkt = cur_pkt;
2073 /* Attach the current one to the previous one */
2074 if (prv_pkt)
2075 prv_pkt->next = cur_pkt;
2076 /* Let's say we have to build a new dgram */
2077 prv_pkt = NULL;
2078 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002079 /* Discard the Initial encryption keys as soon as
2080 * a handshake packet could be built.
2081 */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002082 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002083 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2084 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2085 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2086 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002087 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002088 }
2089 /* Special case for Initial packets: when they have all
2090 * been sent, select the next level.
2091 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002092 if (tel == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002093 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) || qc->els[next_tel].pktns->tx.in_flight)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002094 tel = next_tel;
2095 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002096 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002097 /* If there is data for the next level, do not
2098 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002099 * which sends only one Initial packet, then wait
2100 * for additional CRYPTO data from the server to enter the
2101 * next level.
2102 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002103 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002104 }
2105 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002106 }
2107
2108 /* If we have to build a new datagram, set the current datagram as
2109 * prepared into <cbuf>.
2110 */
2111 if (!prv_pkt) {
2112 qc_set_dg(cbuf, dglen, first_pkt);
2113 first_pkt = NULL;
2114 dglen = 0;
2115 }
2116 }
2117
2118 stop_build:
2119 /* Reset <wr> writer index if in front of <rd> index */
2120 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2121 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2122
2123 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2124 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2125 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2126 /* Mark the end of contiguous data for the reader */
2127 write_u16(cb_wr(cbuf), 0);
2128 cb_add(cbuf, sizeof(uint16_t));
2129 }
2130 }
2131
2132 if (rd && rd <= cbuf->wr) {
2133 cb_wr_reset(cbuf);
2134 if (pos == spos) {
2135 /* Reuse the same buffer if nothing was built. */
2136 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002137 }
2138 }
2139 }
2140
2141 out:
2142 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002143 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002144
2145 err:
2146 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002147 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002148}
2149
2150/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002151 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002152 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002153int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002154{
2155 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002156 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002157
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002158 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002159 cbuf = qr->cbuf;
2160 while (cb_contig_data(cbuf)) {
2161 unsigned char *pos;
2162 struct buffer tmpbuf = { };
2163 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2164 uint16_t dglen;
2165 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002166 unsigned int time_sent;
2167
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002168 pos = cb_rd(cbuf);
2169 dglen = read_u16(pos);
2170 /* End of prepared datagrams.
2171 * Reset the reader index only if in front of the writer index.
2172 */
2173 if (!dglen) {
2174 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2175
2176 if (wr && wr < cbuf->rd) {
2177 cb_rd_reset(cbuf);
2178 continue;
2179 }
2180 break;
2181 }
2182
2183 pos += sizeof dglen;
2184 first_pkt = read_ptr(pos);
2185 pos += sizeof first_pkt;
2186 tmpbuf.area = (char *)pos;
2187 tmpbuf.size = tmpbuf.data = dglen;
2188
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002189 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002190 for (pkt = first_pkt; pkt; pkt = pkt->next)
2191 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002192 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002193 &tmpbuf, tmpbuf.data, 0) <= 0) {
2194 for (pkt = first_pkt; pkt; pkt = pkt->next)
2195 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002196 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002197 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002198
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002199 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002200 qc->tx.bytes += tmpbuf.data;
2201 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002202
2203 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2204 pkt->time_sent = time_sent;
2205 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2206 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002207 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002208 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002209 qc->path->in_flight += pkt->in_flight_len;
2210 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2211 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002212 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002213 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2214 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002215 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002216 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002217 }
2218 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002219
2220 return 1;
2221}
2222
2223/* Build all the frames which must be sent just after the handshake have succeeded.
2224 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2225 * a HANDSHAKE_DONE frame.
2226 * Return 1 if succeeded, 0 if not.
2227 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002228static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002229{
2230 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002231 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002232 struct quic_frame *frm;
2233
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002234 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002235 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002236 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002237 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002238 if (!frm)
2239 return 0;
2240
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002241 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002242 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002243 }
2244
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002245 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002246 struct quic_connection_id *cid;
2247
2248 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002249 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002250 if (!frm || !cid)
2251 goto err;
2252
2253 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002254 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002255 }
2256
2257 return 1;
2258
2259 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002260 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002261 return 0;
2262}
2263
2264/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002265void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002266{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002267 struct eb64_node *n;
2268 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002269
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002270 n = eb64_first(&arngs->root);
2271 while (n) {
2272 struct eb64_node *next;
2273
2274 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2275 next = eb64_next(n);
2276 eb64_delete(n);
2277 free(ar);
2278 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002279 }
2280}
2281
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002282/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2283 * descending order.
2284 */
2285static inline size_t sack_gap(struct quic_arng_node *p,
2286 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002287{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002288 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002289}
2290
2291
2292/* Remove the last elements of <ack_ranges> list of ack range updating its
2293 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002294 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002295 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002296static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002297{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002298 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002299
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002300 last = eb64_last(&arngs->root);
2301 while (last && arngs->enc_sz > limit) {
2302 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002303
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002304 prev = eb64_prev(last);
2305 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002306 return 0;
2307
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002308 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2309 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2310 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2311 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2312 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2313 --arngs->sz;
2314 eb64_delete(last);
2315 pool_free(pool_head_quic_arng, last);
2316 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002317 }
2318
2319 return 1;
2320}
2321
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002322/* Set the encoded size of <arngs> QUIC ack ranges. */
2323static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2324{
2325 struct eb64_node *node, *next;
2326 struct quic_arng_node *ar, *ar_next;
2327
2328 node = eb64_last(&arngs->root);
2329 if (!node)
2330 return;
2331
2332 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2333 arngs->enc_sz = quic_int_getsize(ar->last) +
2334 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2335
2336 while ((next = eb64_prev(node))) {
2337 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2338 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2339 quic_int_getsize(ar_next->last - ar_next->first.key);
2340 node = next;
2341 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2342 }
2343}
2344
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002345/* Insert <ar> ack range into <argns> tree of ack ranges.
2346 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002347 */
2348static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002349struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2350 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002351{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002352 struct quic_arng_node *new_ar;
2353
2354 new_ar = pool_alloc(pool_head_quic_arng);
2355 if (new_ar) {
2356 new_ar->first.key = ar->first;
2357 new_ar->last = ar->last;
2358 eb64_insert(&arngs->root, &new_ar->first);
2359 arngs->sz++;
2360 }
2361
2362 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002363}
2364
2365/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002366 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002367 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002368 *
2369 * Descending order
2370 * ------------->
2371 * range1 range2
2372 * ..........|--------|..............|--------|
2373 * ^ ^ ^ ^
2374 * | | | |
2375 * last1 first1 last2 first2
2376 * ..........+--------+--------------+--------+......
2377 * diff1 gap12 diff2
2378 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002379 * To encode the previous list of ranges we must encode integers as follows in
2380 * descending order:
2381 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002382 * with diff1 = last1 - first1
2383 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002384 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002385 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002386 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002387int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2388 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002389{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002390 struct eb64_node *le;
2391 struct quic_arng_node *new_node;
2392 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002393
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002394 new = NULL;
2395 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002396 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002397 if (!new_node)
2398 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002399
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002400 goto out;
2401 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002402
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002403 le = eb64_lookup_le(&arngs->root, ar->first);
2404 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002405 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002406 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002407 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002408 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002409 else {
2410 struct quic_arng_node *le_ar =
2411 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002412
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002413 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002414 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002415 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002416
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002417 if (le_ar->last + 1 >= ar->first) {
2418 le_ar->last = ar->last;
2419 new = le;
2420 new_node = le_ar;
2421 }
2422 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002423 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002424 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002425 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002426
2427 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002428 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002429 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002430
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002431 /* Verify that the new inserted node does not overlap the nodes
2432 * which follow it.
2433 */
2434 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002435 struct eb64_node *next;
2436 struct quic_arng_node *next_node;
2437
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002438 while ((next = eb64_next(new))) {
2439 next_node =
2440 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002441 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002442 break;
2443
2444 if (next_node->last > new_node->last)
2445 new_node->last = next_node->last;
2446 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002447 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002448 /* Decrement the size of these ranges. */
2449 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002450 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002451 }
2452
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002453 quic_arngs_set_enc_sz(arngs);
2454
2455 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002456 return 1;
2457}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002458/* Remove the header protection of packets at <el> encryption level.
2459 * Always succeeds.
2460 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002461static 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 +01002462{
2463 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002464 struct quic_rx_packet *pqpkt;
2465 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002466 struct quic_enc_level *app_qel;
2467
2468 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2469 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2470 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002471 if (el == app_qel && objt_listener(ctx->conn->target) &&
2472 ctx->conn->qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002473 TRACE_PROTO("hp not removed (handshake not completed)",
2474 QUIC_EV_CONN_ELRMHP, ctx->conn);
2475 goto out;
2476 }
2477 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002478 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002479 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2480 pqpkt->data + pqpkt->pn_offset,
2481 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2482 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2483 /* XXX TO DO XXX */
2484 }
2485 else {
2486 /* The AAD includes the packet number field */
2487 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2488 /* Store the packet into the tree of packets to decrypt. */
2489 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002490 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002491 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002492 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002493 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2494 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002495 MT_LIST_DELETE_SAFE(pkttmp1);
2496 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002497 }
2498
2499 out:
2500 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2501}
2502
2503/* Process all the CRYPTO frame at <el> encryption level.
2504 * Return 1 if succeeded, 0 if not.
2505 */
2506static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002507 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002508{
2509 struct eb64_node *node;
2510
2511 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002512 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002513 node = eb64_first(&el->rx.crypto.frms);
2514 while (node) {
2515 struct quic_rx_crypto_frm *cf;
2516
2517 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2518 if (cf->offset_node.key != el->rx.crypto.offset)
2519 break;
2520
2521 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2522 goto err;
2523
2524 node = eb64_next(node);
2525 quic_rx_packet_refdec(cf->pkt);
2526 eb64_delete(&cf->offset_node);
2527 pool_free(pool_head_quic_rx_crypto_frm, cf);
2528 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002529 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002530 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2531 return 1;
2532
2533 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002534 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002535 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2536 return 0;
2537}
2538
2539/* Process all the packets at <el> encryption level.
2540 * Return 1 if succeeded, 0 if not.
2541 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002542int qc_treat_rx_pkts(struct quic_enc_level *el, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002543{
2544 struct quic_tls_ctx *tls_ctx;
2545 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002546 int64_t largest_pn = -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002547
2548 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2549 tls_ctx = &el->tls_ctx;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002550 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002551 node = eb64_first(&el->rx.pkts);
2552 while (node) {
2553 struct quic_rx_packet *pkt;
2554
2555 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
2556 if (!qc_pkt_decrypt(pkt, tls_ctx)) {
2557 /* Drop the packet */
2558 TRACE_PROTO("packet decryption failed -> dropped",
2559 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2560 }
2561 else {
Frédéric Lécaillec4b93ea2021-06-04 10:12:43 +02002562 if (!qc_parse_pkt_frms(pkt, ctx, el)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002563 /* Drop the packet */
2564 TRACE_PROTO("packet parsing failed -> dropped",
2565 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2566 }
2567 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002568 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2569
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002570 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
2571 !(HA_ATOMIC_ADD_FETCH(&el->pktns->rx.nb_ack_eliciting, 1) & 1))
2572 HA_ATOMIC_OR(&el->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002573
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002574 if (pkt->pn > largest_pn)
2575 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002576 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille654c6912021-06-04 10:27:23 +02002577 if (!quic_update_ack_ranges_list(&el->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002578 TRACE_DEVEL("Could not update ack range list",
2579 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002580 }
2581 }
2582 node = eb64_next(node);
2583 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002584 }
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002585 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002586
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002587 /* Update the largest packet number. */
2588 if (largest_pn != -1)
2589 HA_ATOMIC_UPDATE_MAX(&el->pktns->rx.largest_pn, largest_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002590 if (!qc_treat_rx_crypto_frms(el, ctx))
2591 goto err;
2592
2593 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2594 return 1;
2595
2596 err:
2597 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2598 return 0;
2599}
2600
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002601/* QUIC connection packet handler task. */
2602struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002603{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002604 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002605 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002606 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002607 enum quic_tls_enc_level tel, next_tel;
2608 struct quic_enc_level *qel, *next_qel;
2609 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002610 struct qring *qr; // Tx ring
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002611 int prev_st, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002612
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002613 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002614 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002615 qr = NULL;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002616 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002617 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002618 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc->state))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002619 goto err;
2620
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002621 qel = &qc->els[tel];
2622 next_qel = &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002623
2624 next_level:
2625 tls_ctx = &qel->tls_ctx;
2626
2627 /* If the header protection key for this level has been derived,
2628 * remove the packet header protections.
2629 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002630 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002631 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2632 qc_rm_hp_pkts(qel, ctx);
2633
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002634 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002635 if (!qc_treat_rx_pkts(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002636 goto err;
2637
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002638 st = HA_ATOMIC_LOAD(&qc->state);
2639 if (prev_st == QUIC_HS_ST_SERVER_HANDSHAKE && st >= QUIC_HS_ST_COMPLETE) {
2640 /* Discard the Handshake keys. */
2641 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2642 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2643 qc_set_timer(ctx);
2644 if (!quic_build_post_handshake_frames(qc))
2645 goto err;
2646 }
2647
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002648 if (!qr)
2649 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2650 ret = qc_prep_hdshk_pkts(qr, ctx);
2651 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002652 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002653 else if (ret == 0)
2654 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002655
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002656 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002657 goto err;
2658
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002659 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002660 /* Check if there is something to do for the next level.
2661 */
2662 if ((next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002663 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002664 qel = next_qel;
2665 goto next_level;
2666 }
2667
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668 out:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002669 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002670 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002671 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002672
2673 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002674 if (qr)
2675 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002676 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002677 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002678}
2679
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002680/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002681static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2682{
2683 int i;
2684
2685 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2686 if (qel->tx.crypto.bufs[i]) {
2687 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2688 qel->tx.crypto.bufs[i] = NULL;
2689 }
2690 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002691 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002692}
2693
2694/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002695 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002696 * Returns 1 if succeeded, 0 if not.
2697 */
2698static int quic_conn_enc_level_init(struct quic_conn *qc,
2699 enum quic_tls_enc_level level)
2700{
2701 struct quic_enc_level *qel;
2702
2703 qel = &qc->els[level];
2704 qel->level = quic_to_ssl_enc_level(level);
2705 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2706 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2707 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2708 qel->tls_ctx.rx.flags = 0;
2709 qel->tls_ctx.tx.flags = 0;
2710
2711 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002712 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002713 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002714 qel->rx.crypto.offset = 0;
2715 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2716 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002717
2718 /* Allocate only one buffer. */
2719 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2720 if (!qel->tx.crypto.bufs)
2721 goto err;
2722
2723 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2724 if (!qel->tx.crypto.bufs[0])
2725 goto err;
2726
2727 qel->tx.crypto.bufs[0]->sz = 0;
2728 qel->tx.crypto.nb_buf = 1;
2729
2730 qel->tx.crypto.sz = 0;
2731 qel->tx.crypto.offset = 0;
2732
2733 return 1;
2734
2735 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002736 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002737 return 0;
2738}
2739
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002740/* Release all the memory allocated for <conn> QUIC connection. */
2741static void quic_conn_free(struct quic_conn *conn)
2742{
2743 int i;
2744
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002745 if (!conn)
2746 return;
2747
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002748 free_quic_conn_cids(conn);
2749 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2750 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002751 if (conn->timer_task)
2752 task_destroy(conn->timer_task);
2753 pool_free(pool_head_quic_conn, conn);
2754}
2755
2756/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002757static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002758{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002759 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760 struct quic_conn *qc;
2761 struct quic_pktns *pktns;
2762
2763
2764 conn_ctx = task->context;
2765 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002766 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2767 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002768 task->expire = TICK_ETERNITY;
2769 pktns = quic_loss_pktns(qc);
2770 if (tick_isset(pktns->tx.loss_time)) {
2771 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2772
2773 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2774 if (!LIST_ISEMPTY(&lost_pkts))
2775 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2776 qc_set_timer(conn_ctx);
2777 goto out;
2778 }
2779
2780 if (qc->path->in_flight) {
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002781 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002782 pktns->tx.pto_probe = 1;
2783 }
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002784 else if (objt_server(qc->conn->target) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002785 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2786 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2787
2788 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2789 hel->pktns->tx.pto_probe = 1;
2790 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2791 iel->pktns->tx.pto_probe = 1;
2792 }
2793 qc->tx.nb_pto_dgrams = QUIC_MAX_NB_PTO_DGRAMS;
2794 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2795 qc->path->loss.pto_count++;
2796
2797 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002798 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002799
2800 return task;
2801}
2802
2803/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2804 * connections used to identify the first Initial packets of client connecting
2805 * to listeners. This parameter must be NULL for QUIC connections attached
2806 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2807 * <scid> is the source connection ID with <scid_len> as length.
2808 * Returns 1 if succeeded, 0 if not.
2809 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002810static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2811 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002812 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002813{
2814 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002815 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002816 /* Initial CID. */
2817 struct quic_connection_id *icid;
2818
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002819 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002820 qc = pool_zalloc(pool_head_quic_conn);
2821 if (!qc) {
2822 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2823 goto err;
2824 }
2825
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002826 qc->cids = EB_ROOT;
2827 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002828 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002829 struct listener *l = owner;
2830
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002831 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002832 /* Copy the initial DCID. */
2833 qc->odcid.len = dcid_len;
2834 if (qc->odcid.len)
2835 memcpy(qc->odcid.data, dcid, dcid_len);
2836
2837 /* Copy the SCID as our DCID for this connection. */
2838 if (scid_len)
2839 memcpy(qc->dcid.data, scid, scid_len);
2840 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002841 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002842 }
2843 /* QUIC Client (outgoing connection to servers) */
2844 else {
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002845 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002846 if (dcid_len)
2847 memcpy(qc->dcid.data, dcid, dcid_len);
2848 qc->dcid.len = dcid_len;
2849 }
2850
2851 /* Initialize the output buffer */
2852 qc->obuf.pos = qc->obuf.data;
2853
2854 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002855 if (!icid) {
2856 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
2857 goto err;
2858 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002859
2860 /* Select our SCID which is the first CID with 0 as sequence number. */
2861 qc->scid = icid->cid;
2862
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002863 /* Packet number spaces initialization. */
2864 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2865 quic_pktns_init(&qc->pktns[i]);
2866 /* QUIC encryption level context initialization. */
2867 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002868 if (!quic_conn_enc_level_init(qc, i)) {
2869 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002870 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002871 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002872 /* Initialize the packet number space. */
2873 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2874 }
2875
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02002876 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002877 /* TX part. */
2878 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002879 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2880 qc->tx.wbuf = qc->tx.rbuf = 0;
2881 qc->tx.bytes = 0;
2882 qc->tx.nb_pto_dgrams = 0;
2883 /* RX part. */
2884 qc->rx.bytes = 0;
2885
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002886 /* XXX TO DO: Only one path at this time. */
2887 qc->path = &qc->paths[0];
2888 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2889
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002890 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002891
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002892 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002893
2894 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002895 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002896 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002897 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002898}
2899
2900/* Initialize the timer task of <qc> QUIC connection.
2901 * Returns 1 if succeeded, 0 if not.
2902 */
2903static int quic_conn_init_timer(struct quic_conn *qc)
2904{
2905 qc->timer_task = task_new(MAX_THREADS_MASK);
2906 if (!qc->timer_task)
2907 return 0;
2908
2909 qc->timer = TICK_ETERNITY;
2910 qc->timer_task->process = process_timer;
2911 qc->timer_task->context = qc->conn->xprt_ctx;
2912
2913 return 1;
2914}
2915
2916/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
2917 * past one byte of this buffer.
2918 */
2919static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
2920 struct quic_rx_packet *pkt)
2921{
2922 unsigned char dcid_len, scid_len;
2923
2924 /* Version */
2925 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
2926 return 0;
2927
2928 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
2929
2930 /* Destination Connection ID Length */
2931 dcid_len = *(*buf)++;
2932 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
2933 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
2934 /* XXX MUST BE DROPPED */
2935 return 0;
2936
2937 if (dcid_len) {
2938 /* Check that the length of this received DCID matches the CID lengths
2939 * of our implementation for non Initials packets only.
2940 */
2941 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
2942 return 0;
2943
2944 memcpy(pkt->dcid.data, *buf, dcid_len);
2945 }
2946
2947 pkt->dcid.len = dcid_len;
2948 *buf += dcid_len;
2949
2950 /* Source Connection ID Length */
2951 scid_len = *(*buf)++;
2952 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
2953 /* XXX MUST BE DROPPED */
2954 return 0;
2955
2956 if (scid_len)
2957 memcpy(pkt->scid.data, *buf, scid_len);
2958 pkt->scid.len = scid_len;
2959 *buf += scid_len;
2960
2961 return 1;
2962}
2963
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002964/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
2965 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
2966 * encryption level matching with the packet type. <*qel> may be null if not found.
2967 * Note that <ctx> may be null (for Initial packets).
2968 */
2969static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002970 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002971 struct quic_enc_level **qel)
2972{
2973 enum quic_tls_enc_level tel;
2974
2975 /* Special case without connection context (firt Initial packets) */
2976 if (!ctx) {
2977 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2978 return 1;
2979 }
2980
2981 tel = quic_packet_type_enc_level(pkt->type);
2982 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
2983 *qel = NULL;
2984 return 0;
2985 }
2986
2987 *qel = &qc->els[tel];
2988 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
2989 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
2990 return 0;
2991 }
2992
2993 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002994 (tel != QUIC_TLS_ENC_LEVEL_APP || ctx->conn->qc->state >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002995 return 1;
2996
2997 return 0;
2998}
2999
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003000/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3001 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3002 * byte past the end of the buffer containing this packet and <beg> the address of
3003 * the packet first byte.
3004 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3005 * Returns 1 if succeeded, 0 if not.
3006 */
3007static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3008 unsigned char **buf, unsigned char *beg,
3009 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003010 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003011{
3012 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003013 struct quic_enc_level *qel;
3014 /* Only for traces. */
3015 struct quic_rx_packet *qpkt_trace;
3016
3017 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003018 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003019 /* The packet number is here. This is also the start minus
3020 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3021 * protection.
3022 */
3023 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003024 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003025 /* Note that the following function enables us to unprotect the packet
3026 * number and its length subsequently used to decrypt the entire
3027 * packets.
3028 */
3029 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3030 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003031 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003032 goto err;
3033 }
3034
3035 /* The AAD includes the packet number field found at <pn>. */
3036 pkt->aad_len = pn - beg + pkt->pnl;
3037 qpkt_trace = pkt;
3038 /* Store the packet */
3039 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003040 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003041 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003042 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003043 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003044 else if (qel) {
3045 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046 pkt->pn_offset = pn - beg;
3047 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3048 }
3049
3050 memcpy(pkt->data, beg, pkt->len);
3051 /* Updtate the offset of <*buf> for the next QUIC packet. */
3052 *buf = beg + pkt->len;
3053
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003054 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003055 return 1;
3056
3057 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003058 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 +01003059 return 0;
3060}
3061
3062/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3063 * Also set <*long_header> to 1 if this form is long, 0 if not.
3064 */
3065static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3066 unsigned char byte0, int *long_header)
3067{
3068 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3069 pkt->type =
3070 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3071 *long_header = 1;
3072 }
3073 else {
3074 pkt->type = QUIC_PACKET_TYPE_SHORT;
3075 *long_header = 0;
3076 }
3077}
3078
3079static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3080 struct quic_rx_packet *pkt,
3081 struct quic_dgram_ctx *dgram_ctx,
3082 struct sockaddr_storage *saddr)
3083{
3084 unsigned char *beg;
3085 uint64_t len;
3086 struct quic_conn *qc;
3087 struct eb_root *cids;
3088 struct ebmb_node *node;
3089 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003090 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003091 int long_header;
3092
3093 qc = NULL;
3094 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3095 if (end <= *buf)
3096 goto err;
3097
3098 /* Fixed bit */
3099 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3100 /* XXX TO BE DISCARDED */
3101 goto err;
3102
3103 srv_conn = dgram_ctx->owner;
3104 beg = *buf;
3105 /* Header form */
3106 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3107 if (long_header) {
3108 size_t cid_lookup_len;
3109
3110 if (!quic_packet_read_long_header(buf, end, pkt))
3111 goto err;
3112
3113 /* For Initial packets, and for servers (QUIC clients connections),
3114 * there is no Initial connection IDs storage.
3115 */
3116 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3117 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3118 cid_lookup_len = pkt->dcid.len;
3119 }
3120 else {
3121 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3122 cid_lookup_len = QUIC_CID_LEN;
3123 }
3124
3125 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3126 if (!node)
3127 goto err;
3128
3129 qc = ebmb_entry(node, struct quic_conn, scid_node);
3130
3131 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3132 qc->dcid.len = pkt->scid.len;
3133 if (pkt->scid.len)
3134 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3135 }
3136
3137 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3138 uint64_t token_len;
3139
3140 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3141 goto err;
3142
3143 /* XXX TO DO XXX 0 value means "the token is not present".
3144 * A server which sends an Initial packet must not set the token.
3145 * So, a client which receives an Initial packet with a token
3146 * MUST discard the packet or generate a connection error with
3147 * PROTOCOL_VIOLATION as type.
3148 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3149 */
3150 pkt->token_len = token_len;
3151 }
3152 }
3153 else {
3154 /* XXX TO DO: Short header XXX */
3155 if (end - *buf < QUIC_CID_LEN)
3156 goto err;
3157
3158 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3159 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3160 if (!node)
3161 goto err;
3162
3163 qc = ebmb_entry(node, struct quic_conn, scid_node);
3164 *buf += QUIC_CID_LEN;
3165 }
3166 /* Store the DCID used for this packet to check the packet which
3167 * come in this UDP datagram match with it.
3168 */
3169 if (!dgram_ctx->dcid_node)
3170 dgram_ctx->dcid_node = node;
3171 /* Only packets packets with long headers and not RETRY or VERSION as type
3172 * have a length field.
3173 */
3174 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3175 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3176 goto err;
3177
3178 pkt->len = len;
3179 }
3180 else if (!long_header) {
3181 /* A short packet is the last one of an UDP datagram. */
3182 pkt->len = end - *buf;
3183 }
3184
3185 conn_ctx = qc->conn->xprt_ctx;
3186
3187 /* Increase the total length of this packet by the header length. */
3188 pkt->len += *buf - beg;
3189 /* Do not check the DCID node before the length. */
3190 if (dgram_ctx->dcid_node != node) {
3191 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3192 goto err;
3193 }
3194
3195 if (pkt->len > sizeof pkt->data) {
3196 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3197 goto err;
3198 }
3199
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003200 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003201 goto err;
3202
3203 /* Wake the tasklet of the QUIC connection packet handler. */
3204 if (conn_ctx)
3205 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3206
3207 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3208
3209 return pkt->len;
3210
3211 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003212 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003213 return -1;
3214}
3215
3216static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3217 struct quic_rx_packet *pkt,
3218 struct quic_dgram_ctx *dgram_ctx,
3219 struct sockaddr_storage *saddr)
3220{
3221 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003222 struct quic_conn *qc;
3223 struct eb_root *cids;
3224 struct ebmb_node *node;
3225 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003226 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003227 int long_header = 0;
3228
3229 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003230 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003231 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003232 if (end <= *buf)
3233 goto err;
3234
3235 /* Fixed bit */
3236 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3237 /* XXX TO BE DISCARDED */
3238 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3239 goto err;
3240 }
3241
3242 l = dgram_ctx->owner;
3243 beg = *buf;
3244 /* Header form */
3245 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3246 if (long_header) {
3247 unsigned char dcid_len;
3248
3249 if (!quic_packet_read_long_header(buf, end, pkt)) {
3250 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3251 goto err;
3252 }
3253
3254 dcid_len = pkt->dcid.len;
3255 /* For Initial packets, and for servers (QUIC clients connections),
3256 * there is no Initial connection IDs storage.
3257 */
3258 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003259 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003260 /* DCIDs of first packets coming from clients may have the same values.
3261 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3262 */
3263 quic_cid_saddr_cat(&pkt->dcid, saddr);
3264 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003265
3266 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3267 end - *buf < token_len) {
3268 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3269 goto err;
3270 }
3271
3272 /* XXX TO DO XXX 0 value means "the token is not present".
3273 * A server which sends an Initial packet must not set the token.
3274 * So, a client which receives an Initial packet with a token
3275 * MUST discard the packet or generate a connection error with
3276 * PROTOCOL_VIOLATION as type.
3277 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3278 */
3279 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003280 }
3281 else {
3282 if (pkt->dcid.len != QUIC_CID_LEN) {
3283 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3284 goto err;
3285 }
3286
3287 cids = &l->rx.cids;
3288 }
3289
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003290 /* Only packets packets with long headers and not RETRY or VERSION as type
3291 * have a length field.
3292 */
3293 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3294 uint64_t len;
3295
3296 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3297 end - *buf < len) {
3298 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3299 goto err;
3300 }
3301
3302 pkt->len = len;
3303 }
3304
3305
3306 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003307 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3308 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3309 cids == &l->rx.odcids) {
3310 /* Switch to the definitive tree ->cids containing the final CIDs. */
3311 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3312 if (node) {
3313 /* If found, signal this with NULL as special value for <cids>. */
3314 pkt->dcid.len = dcid_len;
3315 cids = NULL;
3316 }
3317 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003318 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003319
3320 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003321 int ipv4;
3322 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003323 struct ebmb_node *n = NULL;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003324
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003325 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3326 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3327 goto err;
3328 }
3329
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003330 pkt->saddr = *saddr;
3331 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3332 * of <saddr>.
3333 */
3334 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003335 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003336 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003337 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003338 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003339 goto err;
3340
3341 odcid = &qc->rx.params.original_destination_connection_id;
3342 /* Copy the transport parameters. */
3343 qc->rx.params = l->bind_conf->quic_params;
3344 /* Copy original_destination_connection_id transport parameter. */
3345 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3346 odcid->len = pkt->odcid_len;
3347 /* Copy the initial source connection ID. */
3348 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3349 qc->enc_params_len =
3350 quic_transport_params_encode(qc->enc_params,
3351 qc->enc_params + sizeof qc->enc_params,
3352 &qc->rx.params, 1);
3353 if (!qc->enc_params_len)
3354 goto err;
3355
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003356 /* NOTE: the socket address has been concatenated to the destination ID
3357 * chosen by the client for Initial packets.
3358 */
3359 if (!qc_new_isecs(qc, pkt->dcid.data, pkt->odcid_len, 1)) {
3360 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3361 goto err;
3362 }
3363
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003364 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003365 /* This is the DCID node sent in this packet by the client. */
3366 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003367 /* Enqueue this packet. */
3368 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3369 /* Try to accept a new connection. */
3370 listener_accept(l);
3371
3372 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3373 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3374 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3375 /* Insert our SCID, the connection ID for the QUIC client. */
3376 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3377 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3378 if (n != &qc->scid_node) {
3379 quic_conn_free(qc);
3380 qc = ebmb_entry(n, struct quic_conn, scid_node);
3381 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003382 }
3383 else {
3384 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3385 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3386 else
3387 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003388 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003389 }
3390 }
3391 else {
3392 if (end - *buf < QUIC_CID_LEN) {
3393 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3394 goto err;
3395 }
3396
3397 cids = &l->rx.cids;
3398 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3399 if (!node) {
3400 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3401 goto err;
3402 }
3403
3404 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003405 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003406 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003407 /* A short packet is the last one of an UDP datagram. */
3408 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003409 }
3410
3411 /* Store the DCID used for this packet to check the packet which
3412 * come in this UDP datagram match with it.
3413 */
3414 if (!dgram_ctx->dcid_node) {
3415 dgram_ctx->dcid_node = node;
3416 dgram_ctx->qc = qc;
3417 }
3418
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003419 /* Increase the total length of this packet by the header length. */
3420 pkt->len += *buf - beg;
3421 /* Do not check the DCID node before the length. */
3422 if (dgram_ctx->dcid_node != node) {
3423 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3424 goto err;
3425 }
3426
3427 if (pkt->len > sizeof pkt->data) {
3428 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3429 goto err;
3430 }
3431
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003432 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3433 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3434 goto err;
3435 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003436
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003437
3438 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003439 /* Wake up the connection packet handler task from here only if all
3440 * the contexts have been initialized, especially the mux context
3441 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3442 * will start it if these contexts for the connection are not already
3443 * initialized.
3444 */
3445 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003446 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003447
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003448 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3449
3450 return pkt->len;
3451
3452 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003453 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003454 qc ? qc->conn : NULL, pkt);
3455 return -1;
3456}
3457
3458/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3459 * in advance. This is the reponsability of the caller to check there is enough room in this
3460 * buffer to build a long header.
3461 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3462 */
3463static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3464 int type, size_t pn_len, struct quic_conn *conn)
3465{
3466 if (type > QUIC_PACKET_TYPE_RETRY)
3467 return 0;
3468
3469 /* #0 byte flags */
3470 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3471 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3472 /* Version */
3473 quic_write_uint32(buf, end, conn->version);
3474 *(*buf)++ = conn->dcid.len;
3475 /* Destination connection ID */
3476 if (conn->dcid.len) {
3477 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3478 *buf += conn->dcid.len;
3479 }
3480 /* Source connection ID */
3481 *(*buf)++ = conn->scid.len;
3482 if (conn->scid.len) {
3483 memcpy(*buf, conn->scid.data, conn->scid.len);
3484 *buf += conn->scid.len;
3485 }
3486
3487 return 1;
3488}
3489
3490/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3491 * in advance. This is the reponsability of the caller to check there is enough room in this
3492 * buffer to build a long header.
3493 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3494 */
3495static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3496 size_t pn_len, struct quic_conn *conn)
3497{
3498 /* #0 byte flags */
3499 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3500 /* Destination connection ID */
3501 if (conn->dcid.len) {
3502 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3503 *buf += conn->dcid.len;
3504 }
3505
3506 return 1;
3507}
3508
3509/* Apply QUIC header protection to the packet with <buf> as first byte address,
3510 * <pn> as address of the Packet number field, <pnlen> being this field length
3511 * with <aead> as AEAD cipher and <key> as secret key.
3512 * Returns 1 if succeeded or 0 if failed.
3513 */
3514static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3515 const EVP_CIPHER *aead, const unsigned char *key)
3516{
3517 int i, ret, outlen;
3518 EVP_CIPHER_CTX *ctx;
3519 /* We need an IV of at least 5 bytes: one byte for bytes #0
3520 * and at most 4 bytes for the packet number
3521 */
3522 unsigned char mask[5] = {0};
3523
3524 ret = 0;
3525 ctx = EVP_CIPHER_CTX_new();
3526 if (!ctx)
3527 return 0;
3528
3529 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3530 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3531 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3532 goto out;
3533
3534 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3535 for (i = 0; i < pnlen; i++)
3536 pn[i] ^= mask[i + 1];
3537
3538 ret = 1;
3539
3540 out:
3541 EVP_CIPHER_CTX_free(ctx);
3542
3543 return ret;
3544}
3545
3546/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3547 * ACK ranges if needed to a value below <limit> in bytes.
3548 * Return 1 if succeeded, 0 if not.
3549 */
3550static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3551{
3552 size_t room, ack_delay_sz;
3553
3554 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3555 /* A frame is made of 1 byte for the frame type. */
3556 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003557 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003558 return 0;
3559
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003560 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003561}
3562
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003563/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3564 * 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 +01003565 * and <*len> the packet Length field initialized with the number of bytes already present
3566 * 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 +02003567 * <headlen> is the number of bytes already present in this packet before building frames.
3568 *
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003569 * This is the responsibility of the caller to check that <*len> < <room> as this is
3570 * the responsibility to check that <headlen> < quic_path_prep_data(conn->path).
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003571 * Update consequently <*len> to reflect the size of these frames built
3572 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003573 * Return 1 if succeeded, 0 if not.
3574 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003575static inline int qc_build_frms(struct quic_tx_packet *pkt,
3576 size_t room, size_t *len, size_t headlen,
3577 struct quic_enc_level *qel,
3578 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003579{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003580 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003581 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003582 struct mt_list *tmp1, tmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003583
Frédéric Lécailleea604992020-12-24 13:01:37 +01003584 ret = 0;
3585 /* If we are not probing we must take into an account the congestion
3586 * control window.
3587 */
3588 if (!conn->tx.nb_pto_dgrams)
3589 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003590 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003591 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003592 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 +01003593 /* header length, data length, frame length. */
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003594 size_t hlen, dlen, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003595
Frédéric Lécailleea604992020-12-24 13:01:37 +01003596 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003597 break;
3598
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003599 switch (cf->type) {
3600 case QUIC_FT_CRYPTO:
3601 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3602 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3603 /* Compute the length of this CRYPTO frame header */
3604 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3605 /* Compute the data length of this CRyPTO frame. */
3606 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3607 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3608 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3609 if (!dlen)
3610 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003611
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003612 pkt->cdata_len += dlen;
3613 /* CRYPTO frame length. */
3614 flen = hlen + quic_int_getsize(dlen) + dlen;
3615 TRACE_PROTO(" CRYPTO frame length (flen)",
3616 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3617 /* Add the CRYPTO data length and its encoded length to the packet
3618 * length and the length of this length.
3619 */
3620 *len += flen;
3621 room -= flen;
3622 if (dlen == cf->crypto.len) {
3623 /* <cf> CRYPTO data have been consumed. */
3624 MT_LIST_DELETE_SAFE(tmp1);
3625 LIST_APPEND(&pkt->frms, &cf->list);
3626 }
3627 else {
3628 struct quic_frame *new_cf;
3629
3630 new_cf = pool_alloc(pool_head_quic_frame);
3631 if (!new_cf) {
3632 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3633 return 0;
3634 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003635
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003636 new_cf->type = QUIC_FT_CRYPTO;
3637 new_cf->crypto.len = dlen;
3638 new_cf->crypto.offset = cf->crypto.offset;
3639 new_cf->crypto.qel = qel;
3640 LIST_APPEND(&pkt->frms, &new_cf->list);
3641 /* Consume <dlen> bytes of the current frame. */
3642 cf->crypto.len -= dlen;
3643 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003644 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003645 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003646
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003647 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
3648 break;
3649
3650 default:
3651 flen = qc_frm_len(cf);
3652 BUG_ON(!flen);
3653 if (flen > room)
3654 continue;
3655
3656 *len += flen;
3657 room -= flen;
3658 MT_LIST_DELETE_SAFE(tmp1);
3659 LIST_APPEND(&pkt->frms, &cf->list);
3660 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003661 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003662 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003663 }
3664
Frédéric Lécailleea604992020-12-24 13:01:37 +01003665 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003666}
3667
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003668/* This function builds a clear packet with <pkt_type> as type
3669 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
3670 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
3671 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003672 * 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 +02003673 * 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 +02003674 * having returned from this function.
3675 * 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 +01003676 * number field in this packet. <pn_len> will also have the packet number
3677 * length as value.
3678 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003679 * Always succeeds: this is the responsability of the caller to ensure there is
3680 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003681 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003682static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
3683 struct quic_tx_packet *pkt, int pkt_type,
3684 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
3685 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003686{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003687 unsigned char *beg;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003688 size_t len, len_frms, token_fields_len, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003689 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3690 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003691 size_t ack_frm_len;
3692 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003693 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003694
Frédéric Lécailleea604992020-12-24 13:01:37 +01003695 /* Length field value with CRYPTO frames if present. */
3696 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003697 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003698 /* When not probing and not acking, reduce the size of this buffer to respect
3699 * the congestion controller window.
3700 */
3701 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3702 size_t path_room;
3703
3704 path_room = quic_path_prep_data(conn->path);
3705 if (end - beg > path_room)
3706 end = beg + path_room;
3707 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003708
3709 /* For a server, the token field of an Initial packet is empty. */
3710 token_fields_len = pkt_type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3711
3712 /* Check there is enough room to build the header followed by a token. */
3713 if (end - pos < QUIC_LONG_PACKET_MINLEN + conn->dcid.len +
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003714 conn->scid.len + token_fields_len + QUIC_TLS_TAG_LEN) {
3715 ssize_t room = end - pos;
3716 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3717 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003718 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003719 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003720
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003721 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003722 /* packet number length */
3723 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3724
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003725 if (pkt_type == QUIC_PACKET_TYPE_SHORT)
3726 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3727 else
3728 quic_build_packet_long_header(&pos, end, pkt_type, *pn_len, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003729
3730 /* Encode the token length (0) for an Initial packet. */
3731 if (pkt_type == QUIC_PACKET_TYPE_INITIAL)
3732 *pos++ = 0;
3733
3734 /* Build an ACK frame if required. */
3735 ack_frm_len = 0;
3736 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003737 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003738 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003739 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003740 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003741 if (!ack_frm_len) {
3742 ssize_t room = end - pos;
3743 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3744 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003745 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003746 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003747
3748 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3749 }
3750
3751 /* Length field value without the CRYPTO frames data length. */
3752 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003753 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003754 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003755
3756 len_frms = len + QUIC_TLS_TAG_LEN;
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003757 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01003758 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3759 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003760 BUG_ON(1);
Frédéric Lécailleea604992020-12-24 13:01:37 +01003761 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003762 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003763
3764 add_ping_frm = 0;
3765 padding_len = 0;
3766 if (objt_server(conn->conn->target) &&
3767 pkt_type == QUIC_PACKET_TYPE_INITIAL &&
3768 len < QUIC_INITIAL_PACKET_MINLEN) {
3769 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
3770 }
3771 else if (LIST_ISEMPTY(&pkt->frms)) {
3772 if (qel->pktns->tx.pto_probe) {
3773 /* If we cannot send a CRYPTO frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003774 add_ping_frm = 1;
3775 len += 1;
3776 }
3777 /* If there is no frame at all to follow, add at least a PADDING frame. */
3778 if (!ack_frm_len)
3779 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
3780 }
3781
3782 /* Length (of the remaining data). Must not fail because, the buffer size
3783 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
3784 * for the encryption TAG. It must be taken into an account for the length
3785 * of this packet.
3786 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01003787 if (len_frms)
3788 len = len_frms;
3789 else
3790 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003791 if (pkt_type != QUIC_PACKET_TYPE_SHORT)
3792 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003793
3794 /* Packet number field address. */
3795 *buf_pn = pos;
3796
3797 /* Packet number encoding. */
3798 quic_packet_number_encode(&pos, end, pn, *pn_len);
3799
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003800 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3801 ssize_t room = end - pos;
3802 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3803 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003804 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003805 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003806
3807 /* Crypto frame */
3808 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003809 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003810
3811 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02003812 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003813 ssize_t room = end - pos;
3814 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3815 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003816 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003817 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003818 }
3819 }
3820
3821 /* Build a PING frame if needed. */
3822 if (add_ping_frm) {
3823 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003824 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3825 ssize_t room = end - pos;
3826 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3827 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003828 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003829 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003830 }
3831
3832 /* Build a PADDING frame if needed. */
3833 if (padding_len) {
3834 frm.type = QUIC_FT_PADDING;
3835 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003836 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3837 ssize_t room = end - pos;
3838 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3839 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003840 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003841 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003842 }
3843
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003844 /* Always reset this variable as this function has no idea
3845 * if it was set. It is handle by the loss detection timer.
3846 */
3847 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003848 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003849}
3850
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02003851static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003852{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02003853 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003854 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003855 pkt->cdata_len = 0;
3856 pkt->in_flight_len = 0;
3857 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003858 pkt->next = NULL;
3859 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003860}
3861
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003862/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003863static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
3864{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003865 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003866
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003867 if (!pkt)
3868 return;
3869
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003870 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003871 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003872 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003873 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003874 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003875}
3876
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003877/* Build a packet into <buf> packet buffer with <pkt_type> as packet
3878 * type for <qc> QUIC connection from <qel> encryption level.
3879 * Return -2 if the packet could not be allocated or encrypted for any reason,
3880 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003881 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003882static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
3883 const unsigned char *buf_end,
3884 struct quic_conn *qc, int pkt_type,
3885 struct quic_enc_level *qel, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003886{
3887 /* The pointer to the packet number field. */
3888 unsigned char *buf_pn;
3889 unsigned char *beg, *end, *payload;
3890 int64_t pn;
3891 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003892 struct quic_tls_ctx *tls_ctx;
3893 struct quic_tx_packet *pkt;
3894
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003895 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003896 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003897 pkt = pool_alloc(pool_head_quic_tx_packet);
3898 if (!pkt) {
3899 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 +02003900 *err = -2;
3901 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003902 }
3903
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02003904 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003905 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003906 pn_len = 0;
3907 buf_pn = NULL;
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02003908 /* Consume a packet number. */
3909 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003910 qc_do_build_pkt(*pos, buf_end, pkt, pkt_type, pn, &pn_len, &buf_pn, qel, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003911
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003912 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003913 payload = buf_pn + pn_len;
3914 payload_len = end - payload;
3915 aad_len = payload - beg;
3916
3917 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003918 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
3919 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003920 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003921 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003922
3923 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003924 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003925 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
3926 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
3927 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003928 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003929 goto err;
3930 }
3931
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003932 /* Now that a correct packet is built, let us consume <*pos> buffer. */
3933 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003934 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02003935 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003936 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003937 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003938 pkt->in_flight_len = pkt->len;
3939 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003940 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003941 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003942 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
3943
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003944 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003945
3946 err:
3947 free_quic_tx_packet(pkt);
3948 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003949 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003950}
3951
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01003952/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
3953 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003954 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01003955static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
3956 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003957{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003958 size_t try, done = 0;
3959
3960 if (!conn_ctrl_ready(conn))
3961 return 0;
3962
3963 if (!fd_recv_ready(conn->handle.fd))
3964 return 0;
3965
3966 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003967
3968 /* read the largest possible block. For this, we perform only one call
3969 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01003970 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003971 */
3972 while (count > 0) {
3973 try = b_contig_space(buf);
3974 if (!try)
3975 break;
3976
3977 if (try > count)
3978 try = count;
3979
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01003980 b_add(buf, try);
3981 done += try;
3982 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003983 }
3984
3985 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
3986 conn->flags &= ~CO_FL_WAIT_L4_CONN;
3987
3988 leave:
3989 return done;
3990
3991 read0:
3992 conn_sock_read0(conn);
3993 conn->flags &= ~CO_FL_WAIT_L4_CONN;
3994
3995 /* Now a final check for a possible asynchronous low-level error
3996 * report. This can happen when a connection receives a reset
3997 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
3998 * we might have come from there by just checking POLL_HUP instead
3999 * of recv()'s return value 0, so we have no way to tell there was
4000 * an error without checking.
4001 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004002 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004003 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4004 goto leave;
4005}
4006
4007
4008/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4009 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4010 * other pending data for example, but this flag is ignored at the moment.
4011 * Only one call to send() is performed, unless the buffer wraps, in which case
4012 * a second call may be performed. The connection's flags are updated with
4013 * whatever special event is detected (error, empty). The caller is responsible
4014 * for taking care of those events and avoiding the call if inappropriate. The
4015 * function does not call the connection's polling update function, so the caller
4016 * is responsible for this. It's up to the caller to update the buffer's contents
4017 * based on the return value.
4018 */
4019static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4020{
4021 ssize_t ret;
4022 size_t try, done;
4023 int send_flag;
4024
4025 if (!conn_ctrl_ready(conn))
4026 return 0;
4027
4028 if (!fd_send_ready(conn->handle.fd))
4029 return 0;
4030
4031 done = 0;
4032 /* send the largest possible block. For this we perform only one call
4033 * to send() unless the buffer wraps and we exactly fill the first hunk,
4034 * in which case we accept to do it once again.
4035 */
4036 while (count) {
4037 try = b_contig_data(buf, done);
4038 if (try > count)
4039 try = count;
4040
4041 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4042 if (try < count || flags & CO_SFL_MSG_MORE)
4043 send_flag |= MSG_MORE;
4044
4045 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4046 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4047 if (ret > 0) {
4048 count -= ret;
4049 done += ret;
4050
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004051 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004052 conn->flags |= CO_FL_WAIT_L4L6;
4053 /* if the system buffer is full, don't insist */
4054 if (ret < try)
4055 break;
4056 }
4057 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4058 /* nothing written, we need to poll for write first */
4059 fd_cant_send(conn->handle.fd);
4060 break;
4061 }
4062 else if (errno != EINTR) {
4063 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4064 break;
4065 }
4066 }
4067 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4068 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4069
4070 if (done > 0) {
4071 /* we count the total bytes sent, and the send rate for 32-byte
4072 * blocks. The reason for the latter is that freq_ctr are
4073 * limited to 4GB and that it's not enough per second.
4074 */
4075 _HA_ATOMIC_ADD(&global.out_bytes, done);
4076 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4077 }
4078 return done;
4079}
4080
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004081/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4082 * event subscriber <es> is not allowed to change from a previous call as long
4083 * as at least one event is still subscribed. The <event_type> must only be a
4084 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4085 */
4086static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4087{
4088 return conn_subscribe(conn, xprt_ctx, event_type, es);
4089}
4090
4091/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4092 * The <es> pointer is not allowed to differ from the one passed to the
4093 * subscribe() call. It always returns zero.
4094 */
4095static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4096{
4097 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4098}
4099
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004100/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4101 * connection with <xprt_ctx> as address of the xprt context.
4102 * Returns 1 if succeeded, 0 if not.
4103 */
4104static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4105{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004106 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004107
4108 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4109
4110 if (*xprt_ctx)
4111 goto out;
4112
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004113 ctx = pool_alloc(pool_head_quic_conn_ctx);
4114 if (!ctx) {
4115 conn->err_code = CO_ER_SYS_MEMLIM;
4116 goto err;
4117 }
4118
4119 ctx->wait_event.tasklet = tasklet_new();
4120 if (!ctx->wait_event.tasklet) {
4121 conn->err_code = CO_ER_SYS_MEMLIM;
4122 goto err;
4123 }
4124
4125 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4126 ctx->wait_event.tasklet->context = ctx;
4127 ctx->wait_event.events = 0;
4128 ctx->conn = conn;
4129 ctx->subs = NULL;
4130 ctx->xprt_ctx = NULL;
4131
4132 ctx->xprt = xprt_get(XPRT_QUIC);
4133 if (objt_server(conn->target)) {
4134 /* Server */
4135 struct server *srv = __objt_server(conn->target);
4136 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004137 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004138 int ssl_err, ipv4;
4139
4140 ssl_err = SSL_ERROR_NONE;
4141 if (RAND_bytes(dcid, sizeof dcid) != 1)
4142 goto err;
4143
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004144 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004145 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004146 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004147 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004148 goto err;
4149
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004150 /* Insert our SCID, the connection ID for the QUIC client. */
4151 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4152
4153 conn->qc = qc;
4154 qc->conn = conn;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004155 if (!qc_new_isecs(qc, dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004156 goto err;
4157
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004158 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4159 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4160 goto err;
4161
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004162 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004163 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004164 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4165 qc->enc_params_len =
4166 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4167 &qc->rx.params, 0);
4168 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004169 goto err;
4170
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004171 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004172 SSL_set_connect_state(ctx->ssl);
4173 ssl_err = SSL_do_handshake(ctx->ssl);
4174 if (ssl_err != 1) {
4175 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4176 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
4177 TRACE_PROTO("SSL handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004178 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004179 }
4180 else {
4181 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004182 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004183 goto err;
4184 }
4185 }
4186 }
4187 else if (objt_listener(conn->target)) {
4188 /* Listener */
4189 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004190 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004191
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004192 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4193 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4194 goto err;
4195
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004196 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004197 SSL_set_accept_state(ctx->ssl);
4198 }
4199
4200 *xprt_ctx = ctx;
4201
4202 /* Leave init state and start handshake */
4203 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004204
4205 out:
4206 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4207
4208 return 0;
4209
4210 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004211 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004212 tasklet_free(ctx->wait_event.tasklet);
4213 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004214 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004215 return -1;
4216}
4217
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004218/* Start the QUIC transport layer */
4219static int qc_xprt_start(struct connection *conn, void *ctx)
4220{
4221 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004222 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004223
4224 qc = conn->qc;
4225 if (!quic_conn_init_timer(qc)) {
4226 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4227 return 0;
4228 }
4229
4230 tasklet_wakeup(qctx->wait_event.tasklet);
4231 return 1;
4232}
4233
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004234/* transport-layer operations for QUIC connections. */
4235static struct xprt_ops ssl_quic = {
4236 .snd_buf = quic_conn_from_buf,
4237 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004238 .subscribe = quic_conn_subscribe,
4239 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004240 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004241 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004242 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4243 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4244 .name = "QUIC",
4245};
4246
4247__attribute__((constructor))
4248static void __quic_conn_init(void)
4249{
4250 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4251 xprt_register(XPRT_QUIC, &ssl_quic);
4252}
4253
4254__attribute__((destructor))
4255static void __quic_conn_deinit(void)
4256{
4257 BIO_meth_free(ha_quic_meth);
4258}
4259
4260/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4261 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4262 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004263 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004264 */
4265static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4266 struct sockaddr_storage *saddr, qpkt_read_func *func)
4267{
4268 unsigned char *pos;
4269 const unsigned char *end;
4270 struct quic_dgram_ctx dgram_ctx = {
4271 .dcid_node = NULL,
4272 .owner = owner,
4273 };
4274
4275 pos = (unsigned char *)buf;
4276 end = pos + len;
4277
4278 do {
4279 int ret;
4280 struct quic_rx_packet *pkt;
4281
Willy Tarreaue4498932021-03-22 21:13:05 +01004282 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004283 if (!pkt)
4284 goto err;
4285
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004286 quic_rx_packet_refinc(pkt);
4287 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4288 if (ret == -1) {
4289 size_t pkt_len;
4290
4291 pkt_len = pkt->len;
4292 free_quic_rx_packet(pkt);
4293 /* If the packet length could not be found, we cannot continue. */
4294 if (!pkt_len)
4295 break;
4296 }
4297 } while (pos < end);
4298
4299 /* Increasing the received bytes counter by the UDP datagram length
4300 * if this datagram could be associated to a connection.
4301 */
4302 if (dgram_ctx.qc)
4303 dgram_ctx.qc->rx.bytes += len;
4304
4305 return pos - (unsigned char *)buf;
4306
4307 err:
4308 return -1;
4309}
4310
4311ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4312 struct sockaddr_storage *saddr)
4313{
4314 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4315}
4316
4317ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4318 struct sockaddr_storage *saddr)
4319{
4320 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4321}
4322
4323/* QUIC I/O handler for connection to local listeners or remove servers
4324 * depending on <listener> boolean value, with <fd> as socket file
4325 * descriptor and <ctx> as context.
4326 */
4327static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4328{
4329 ssize_t ret;
4330 size_t done = 0;
4331 struct buffer *buf = get_trash_chunk();
4332 /* Source address */
4333 struct sockaddr_storage saddr = {0};
4334 socklen_t saddrlen = sizeof saddr;
4335
4336 if (!fd_recv_ready(fd))
4337 return 0;
4338
4339 do {
4340 ret = recvfrom(fd, buf->area, buf->size, 0,
4341 (struct sockaddr *)&saddr, &saddrlen);
4342 if (ret < 0) {
4343 if (errno == EINTR)
4344 continue;
4345 if (errno == EAGAIN)
4346 fd_cant_recv(fd);
4347 goto out;
4348 }
4349 } while (0);
4350
4351 done = buf->data = ret;
4352 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4353
4354 out:
4355 return done;
4356}
4357
4358/* QUIC I/O handler for connections to local listeners with <fd> as socket
4359 * file descriptor.
4360 */
4361void quic_fd_handler(int fd)
4362{
Willy Tarreauf5090652021-04-06 17:23:40 +02004363 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004364 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4365}
4366
4367/* QUIC I/O handler for connections to remote servers with <fd> as socket
4368 * file descriptor.
4369 */
4370void quic_conn_fd_handler(int fd)
4371{
Willy Tarreauf5090652021-04-06 17:23:40 +02004372 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004373 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4374}
4375
4376/*
4377 * Local variables:
4378 * c-indent-level: 8
4379 * c-basic-offset: 8
4380 * End:
4381 */