blob: 1d1f259d58d44ef6a047a42de3caac7d7aa13c0c [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020025#include <import/ebmbtree.h>
26
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010027#include <haproxy/buf-t.h>
28#include <haproxy/compat.h>
29#include <haproxy/api.h>
30#include <haproxy/debug.h>
31#include <haproxy/tools.h>
32#include <haproxy/ticks.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010033
34#include <haproxy/connection.h>
35#include <haproxy/fd.h>
36#include <haproxy/freq_ctr.h>
37#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010038#include <haproxy/h3.h>
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010039#include <haproxy/hq_interop.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010041#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010042#include <haproxy/pipe.h>
43#include <haproxy/proxy.h>
44#include <haproxy/quic_cc.h>
45#include <haproxy/quic_frame.h>
46#include <haproxy/quic_loss.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020047#include <haproxy/cbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010048#include <haproxy/quic_tls.h>
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +010049#include <haproxy/sink.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010050#include <haproxy/ssl_sock.h>
51#include <haproxy/stream_interface.h>
52#include <haproxy/task.h>
53#include <haproxy/trace.h>
54#include <haproxy/xprt_quic.h>
55
Amaury Denoyellea22d8602021-11-10 15:17:56 +010056/* list of supported QUIC versions by this implementation */
57static int quic_supported_version[] = {
58 0x00000001,
Frédéric Lécaille56d3e1b2021-11-19 14:32:52 +010059 0xff00001d, /* draft-29 */
Amaury Denoyellea22d8602021-11-10 15:17:56 +010060
61 /* placeholder, do not add entry after this */
62 0x0
63};
64
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020065/* This is the values of some QUIC transport parameters when absent.
66 * Should be used to initialize any transport parameters (local or remote)
67 * before updating them with customized values.
68 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010069struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020070 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020071 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
72 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020073 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010074};
75
76/* trace source and events */
77static void quic_trace(enum trace_level level, uint64_t mask, \
78 const struct trace_source *src,
79 const struct ist where, const struct ist func,
80 const void *a1, const void *a2, const void *a3, const void *a4);
81
82static const struct trace_event quic_trace_events[] = {
83 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
84 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
85 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
86 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
87 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
88 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
89 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050090 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010091 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
92 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
93 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
94 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
95 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
96 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
97 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
98 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
99 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
100 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
101 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
102 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
103 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
104 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
105 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
106 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
107 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
108 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
109 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100110 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
111 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
112 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
113 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
114 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
115 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100116 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200117 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
118 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100119 { /* end */ }
120};
121
122static const struct name_desc quic_trace_lockon_args[4] = {
123 /* arg1 */ { /* already used by the connection */ },
124 /* arg2 */ { .name="quic", .desc="QUIC transport" },
125 /* arg3 */ { },
126 /* arg4 */ { }
127};
128
129static const struct name_desc quic_trace_decoding[] = {
130#define QUIC_VERB_CLEAN 1
131 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
132 { /* end */ }
133};
134
135
136struct trace_source trace_quic = {
137 .name = IST("quic"),
138 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100139 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100140 .default_cb = quic_trace,
141 .known_events = quic_trace_events,
142 .lockon_args = quic_trace_lockon_args,
143 .decoding = quic_trace_decoding,
144 .report_events = ~0, /* report everything by default */
145};
146
147#define TRACE_SOURCE &trace_quic
148INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
149
150static BIO_METHOD *ha_quic_meth;
151
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200152DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200153DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100154DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100155DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200156 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100157DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100158DECLARE_POOL(pool_head_quic_connection_id,
159 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160DECLARE_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 +0100161DECLARE_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 +0100162DECLARE_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 +0100163DECLARE_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 +0100164DECLARE_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 +0200165DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100166DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100167
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200168static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +0200169 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100170 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +0100171 int padding, int ack, int nb_pto_dgrams, int cc, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100172
173/* Add traces to <buf> depending on <frm> TX frame type. */
174static inline void chunk_tx_frm_appendf(struct buffer *buf,
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200175 const struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100176{
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100177 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100178 switch (frm->type) {
179 case QUIC_FT_CRYPTO:
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100180 {
181 const struct quic_crypto *cf = &frm->crypto;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100182 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100183 (ull)cf->offset, (ull)cf->len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100184 break;
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100185 }
186 case QUIC_FT_STOP_SENDING:
187 {
188 const struct quic_stop_sending *s = &frm->stop_sending;
189 chunk_appendf(&trace_buf, " id=%llu app_error_code=%llu",
190 (ull)s->id, (ull)s->app_error_code);
191 break;
192 }
193 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
194 {
195 const struct quic_stream *s = &frm->stream;
196 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
197 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
198 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
199 (ull)s->id, (ull)s->offset.key, (ull)s->len);
200 break;
201 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100202 }
203}
204
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100205/* Only for debug purpose */
206struct enc_debug_info {
207 unsigned char *payload;
208 size_t payload_len;
209 unsigned char *aad;
210 size_t aad_len;
211 uint64_t pn;
212};
213
214/* Initializes a enc_debug_info struct (only for debug purpose) */
215static inline void enc_debug_info_init(struct enc_debug_info *edi,
216 unsigned char *payload, size_t payload_len,
217 unsigned char *aad, size_t aad_len, uint64_t pn)
218{
219 edi->payload = payload;
220 edi->payload_len = payload_len;
221 edi->aad = aad;
222 edi->aad_len = aad_len;
223 edi->pn = pn;
224}
225
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100226/* Trace callback for QUIC.
227 * These traces always expect that arg1, if non-null, is of type connection.
228 */
229static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
230 const struct ist where, const struct ist func,
231 const void *a1, const void *a2, const void *a3, const void *a4)
232{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100233 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100234
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100235 if (qc) {
236 const struct quic_tls_secrets *secs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100237
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100238 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100239 if ((mask & QUIC_EV_CONN_INIT) && qc) {
240 chunk_appendf(&trace_buf, "\n odcid");
241 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100242 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100243 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100244 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100245 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100246 }
247
248 if (mask & QUIC_EV_CONN_ADDDATA) {
249 const enum ssl_encryption_level_t *level = a2;
250 const size_t *len = a3;
251
252 if (level) {
253 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
254
255 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
256 }
257 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100258 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100259 }
260 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
261 /* Initial read & write secrets. */
262 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
263 const unsigned char *rx_sec = a2;
264 const unsigned char *tx_sec = a3;
265
266 secs = &qc->els[level].tls_ctx.rx;
267 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
268 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
269 if (rx_sec)
270 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
271 quic_tls_keys_hexdump(&trace_buf, secs);
272 }
273 secs = &qc->els[level].tls_ctx.tx;
274 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
275 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
276 if (tx_sec)
277 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
278 quic_tls_keys_hexdump(&trace_buf, secs);
279 }
280 }
281 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
282 const enum ssl_encryption_level_t *level = a2;
283 const unsigned char *secret = a3;
284 const size_t *secret_len = a4;
285
286 if (level) {
287 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
288
289 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
290 if (secret && secret_len)
291 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
292 secs = &qc->els[lvl].tls_ctx.rx;
293 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
294 quic_tls_keys_hexdump(&trace_buf, secs);
295 }
296 }
297
298 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
299 const enum ssl_encryption_level_t *level = a2;
300 const unsigned char *secret = a3;
301 const size_t *secret_len = a4;
302
303 if (level) {
304 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
305
306 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
307 if (secret && secret_len)
308 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
309 secs = &qc->els[lvl].tls_ctx.tx;
310 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
311 quic_tls_keys_hexdump(&trace_buf, secs);
312 }
313
314 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100315
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100316 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100317 const struct quic_tx_packet *pkt = a2;
318 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100319 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100320
321 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100322 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100323 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
324 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100325 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100326 (unsigned long long)qc->path->cwnd,
327 (unsigned long long)qc->path->prep_in_flight,
328 (unsigned long long)qc->path->in_flight,
329 (unsigned long long)pktns->tx.in_flight,
330 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100331 }
332 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200333 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100334 if (pkt->pn_node.key != (uint64_t)-1)
335 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100336 list_for_each_entry(frm, &pkt->frms, list)
337 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100338 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
339 }
340
341 if (room) {
342 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
343 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
344 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100345 }
346 }
347
348 if (mask & QUIC_EV_CONN_HDSHK) {
349 const enum quic_handshake_state *state = a2;
350 const int *err = a3;
351
352 if (state)
353 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
354 if (err)
355 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
356 }
357
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100358 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100359 const struct quic_rx_packet *pkt = a2;
360 const unsigned long *pktlen = a3;
361 const SSL *ssl = a4;
362
363 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100364 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100365 pkt, quic_packet_type_enc_level_char(pkt->type));
366 if (pkt->pnl)
367 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
368 (unsigned long long)pkt->pn);
369 if (pkt->token_len)
370 chunk_appendf(&trace_buf, " toklen=%llu",
371 (unsigned long long)pkt->token_len);
372 if (pkt->aad_len)
373 chunk_appendf(&trace_buf, " aadlen=%llu",
374 (unsigned long long)pkt->aad_len);
375 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
376 pkt->flags, (unsigned long long)pkt->len);
377 }
378 if (pktlen)
379 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
380 if (ssl) {
381 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
382 chunk_appendf(&trace_buf, " el=%c",
383 quic_enc_level_char(ssl_to_quic_enc_level(level)));
384 }
385 }
386
387 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
388 const struct quic_rx_packet *pkt = a2;
389 const struct quic_rx_crypto_frm *cf = a3;
390 const SSL *ssl = a4;
391
392 if (pkt)
393 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
394 quic_packet_type_enc_level_char(pkt->type),
395 (unsigned long long)pkt->pn);
396 if (cf)
397 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
398 (unsigned long long)cf->offset_node.key,
399 (unsigned long long)cf->len);
400 if (ssl) {
401 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200402 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100403 quic_enc_level_char(ssl_to_quic_enc_level(level)));
404 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100405
406 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100407 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100408 }
409
410 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
411 const struct quic_frame *frm = a2;
412
413 if (frm)
414 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
415 }
416
417 if (mask & QUIC_EV_CONN_PHPKTS) {
418 const struct quic_enc_level *qel = a2;
419
420 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100421 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100422 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200423 " 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 +0100424 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200425 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +0100426 !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100427 (unsigned long long)qc->path->cwnd,
428 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100429 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100430 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
431 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100432 }
433 }
434
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100435 if (mask & QUIC_EV_CONN_ENCPKT) {
436 const struct enc_debug_info *edi = a2;
437
438 if (edi)
439 chunk_appendf(&trace_buf,
440 " payload=@%p payload_len=%llu"
441 " aad=@%p aad_len=%llu pn=%llu",
442 edi->payload, (unsigned long long)edi->payload_len,
443 edi->aad, (unsigned long long)edi->aad_len,
444 (unsigned long long)edi->pn);
445 }
446
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100447 if (mask & QUIC_EV_CONN_RMHP) {
448 const struct quic_rx_packet *pkt = a2;
449
450 if (pkt) {
451 const int *ret = a3;
452
453 chunk_appendf(&trace_buf, " pkt@%p", pkt);
454 if (ret && *ret)
455 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
456 pkt->pnl, (unsigned long long)pkt->pn);
457 }
458 }
459
460 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200461 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100462 const unsigned long *val1 = a3;
463 const unsigned long *val2 = a4;
464
465 if (frm)
466 chunk_tx_frm_appendf(&trace_buf, frm);
467 if (val1)
468 chunk_appendf(&trace_buf, " %lu", *val1);
469 if (val2)
470 chunk_appendf(&trace_buf, "..%lu", *val2);
471 }
472
473 if (mask & QUIC_EV_CONN_RTTUPDT) {
474 const unsigned int *rtt_sample = a2;
475 const unsigned int *ack_delay = a3;
476 const struct quic_loss *ql = a4;
477
478 if (rtt_sample)
479 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
480 if (ack_delay)
481 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
482 if (ql)
483 chunk_appendf(&trace_buf,
484 " srtt=%ums rttvar=%ums min_rtt=%ums",
485 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
486 }
487 if (mask & QUIC_EV_CONN_CC) {
488 const struct quic_cc_event *ev = a2;
489 const struct quic_cc *cc = a3;
490
491 if (a2)
492 quic_cc_event_trace(&trace_buf, ev);
493 if (a3)
494 quic_cc_state_trace(&trace_buf, cc);
495 }
496
497 if (mask & QUIC_EV_CONN_PKTLOSS) {
498 const struct quic_pktns *pktns = a2;
499 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500
501 if (pktns) {
502 chunk_appendf(&trace_buf, " pktns=%s",
503 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
504 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
505 if (pktns->tx.loss_time)
506 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100507 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100508 }
509 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
510 struct quic_tx_packet *pkt;
511
512 chunk_appendf(&trace_buf, " lost_pkts:");
513 list_for_each_entry(pkt, lost_pkts, list)
514 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
515 }
516 }
517
518 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100519 const struct quic_pktns *pktns = a2;
520 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100521 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100522
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100523 if (ifae_pkts)
524 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
525 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100526 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100527 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100528 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100529 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
530 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100531 if (mask & QUIC_EV_CONN_STIMER) {
532 if (pktns->tx.loss_time)
533 chunk_appendf(&trace_buf, " loss_time=%dms",
534 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
535 }
536 if (mask & QUIC_EV_CONN_SPTO) {
537 if (pktns->tx.time_of_last_eliciting)
538 chunk_appendf(&trace_buf, " tole=%dms",
539 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
540 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100541 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100542 }
543 }
544
545 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
546 chunk_appendf(&trace_buf,
547 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
548 }
549 }
550
551 if (mask & QUIC_EV_CONN_SPPKTS) {
552 const struct quic_tx_packet *pkt = a2;
553
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100554 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
555 (unsigned long long)qc->path->cwnd,
556 (unsigned long long)qc->path->prep_in_flight,
557 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100558 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100559 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100560 (unsigned long)pkt->pn_node.key,
561 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
562 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100563 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100564 }
565 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100566
567 if (mask & QUIC_EV_CONN_SSLALERT) {
568 const uint8_t *alert = a2;
569 const enum ssl_encryption_level_t *level = a3;
570
571 if (alert)
572 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
573 if (level)
574 chunk_appendf(&trace_buf, " el=%c",
575 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
576 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100577
578 if (mask & QUIC_EV_CONN_BCFRMS) {
579 const size_t *sz1 = a2;
580 const size_t *sz2 = a3;
581 const size_t *sz3 = a4;
582
583 if (sz1)
584 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
585 if (sz2)
586 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
587 if (sz3)
588 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
589 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100590
591 if (mask & QUIC_EV_CONN_PSTRM) {
592 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100593
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100594 if (frm)
595 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100596 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100597 }
598 if (mask & QUIC_EV_CONN_LPKT) {
599 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200600 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100601
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100602 if (pkt) {
603 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
604 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
605 if (pkt->pn_node.key != (uint64_t)-1)
606 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
607 }
608
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200609 if (len)
610 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100611 }
612
613}
614
615/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200616static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100617{
618 struct quic_conn *qc;
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200619 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100620
621 qc = ctx->conn->qc;
622 if (objt_server(qc->conn->target))
623 return 1;
624
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200625 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
626 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
627 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
628 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200629 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100630 return 1;
631
632 return 0;
633}
634
635/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
636 * both loss detection and PTO and schedule the task assiated to this timer if needed.
637 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200638static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100639{
640 struct quic_conn *qc;
641 struct quic_pktns *pktns;
642 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200643 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100644
Amaury Denoyellec15dd922021-12-21 11:41:52 +0100645 qc = ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100646 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
647 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
648
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100649 pktns = quic_loss_pktns(qc);
650 if (tick_isset(pktns->tx.loss_time)) {
651 qc->timer = pktns->tx.loss_time;
652 goto out;
653 }
654
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100655 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100656 * cancelled for a server which reached the anti-amplification limit.
657 */
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100658 if (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100659 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100660 qc->timer = TICK_ETERNITY;
661 goto out;
662 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100663
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100664 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100665 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100666 /* Timer cancellation. */
667 qc->timer = TICK_ETERNITY;
668 goto out;
669 }
670
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200671 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
672 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100673 if (tick_isset(pto))
674 qc->timer = pto;
675 out:
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200676 if (qc->timer != TICK_ETERNITY)
677 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100678 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100679}
680
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100681/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
682 * connection.
683 * Return 1 if succeeded, 0 if not.
684 */
685static int quic_tls_key_update(struct quic_conn *qc)
686{
687 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
688 struct quic_tls_secrets *rx, *tx;
689 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
690 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
691
692 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
693 rx = &tls_ctx->rx;
694 tx = &tls_ctx->tx;
695 nxt_rx = &qc->ku.nxt_rx;
696 nxt_tx = &qc->ku.nxt_tx;
697
698 /* Prepare new RX secrets */
699 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
700 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100701 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100702 return 0;
703 }
704
705 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
706 nxt_rx->key, nxt_rx->keylen,
707 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
708 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100709 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100710 return 0;
711 }
712
713 /* Prepare new TX secrets */
714 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
715 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100716 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100717 return 0;
718 }
719
720 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
721 nxt_tx->key, nxt_tx->keylen,
722 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
723 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100724 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100725 return 0;
726 }
727
728 return 1;
729}
730
731/* Rotate the Key Update information for <qc> QUIC connection.
732 * Must be used after having updated them.
733 * Always succeeds.
734 */
735static void quic_tls_rotate_keys(struct quic_conn *qc)
736{
737 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
738 unsigned char *curr_secret, *curr_iv, *curr_key;
739
740 /* Rotate the RX secrets */
741 curr_secret = tls_ctx->rx.secret;
742 curr_iv = tls_ctx->rx.iv;
743 curr_key = tls_ctx->rx.key;
744
745 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
746 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
747 tls_ctx->rx.key = qc->ku.nxt_rx.key;
748
749 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
750 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
751 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
752
753 qc->ku.prv_rx.secret = curr_secret;
754 qc->ku.prv_rx.iv = curr_iv;
755 qc->ku.prv_rx.key = curr_key;
756 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
757
758 /* Update the TX secrets */
759 curr_secret = tls_ctx->tx.secret;
760 curr_iv = tls_ctx->tx.iv;
761 curr_key = tls_ctx->tx.key;
762
763 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
764 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
765 tls_ctx->tx.key = qc->ku.nxt_tx.key;
766
767 qc->ku.nxt_tx.secret = curr_secret;
768 qc->ku.nxt_tx.iv = curr_iv;
769 qc->ku.nxt_tx.key = curr_key;
770}
771
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100772#ifndef OPENSSL_IS_BORINGSSL
773int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
774 const uint8_t *read_secret,
775 const uint8_t *write_secret, size_t secret_len)
776{
777 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
778 struct quic_tls_ctx *tls_ctx =
779 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
780 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100781 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100782
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100783 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100784 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100785 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100786 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100787 goto out;
788 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100789
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100790 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100791 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100792 goto err;
793 }
794
795 rx = &tls_ctx->rx;
796 tx = &tls_ctx->tx;
797
798 rx->aead = tx->aead = tls_aead(cipher);
799 rx->md = tx->md = tls_md(cipher);
800 rx->hp = tx->hp = tls_hp(cipher);
801
802 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
803 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100804 read_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100805 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100806 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100807 }
808
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100809 rx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100810
811 if (!write_secret)
812 goto tp;
813
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100814 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
815 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100816 write_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100817 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100818 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100819 }
820
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100821 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100822 tp:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100823 if (objt_server(conn->target) && level == ssl_encryption_application) {
824 const unsigned char *buf;
825 size_t buflen;
826
827 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
828 if (!buflen)
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100829 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100830
831 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100832 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100833 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100834
835 if (level == ssl_encryption_application) {
836 struct quic_conn *qc = conn->qc;
837 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
838 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
839 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
840
841 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
842 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100843 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100844 goto err;
845 }
846
847 memcpy(rx->secret, read_secret, secret_len);
848 rx->secretlen = secret_len;
849 memcpy(tx->secret, write_secret, secret_len);
850 tx->secretlen = secret_len;
851 /* Initialize all the secret keys lengths */
852 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
853 /* Prepare the next key update */
854 if (!quic_tls_key_update(qc))
855 goto err;
856 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100857 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100858 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn->qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100859 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100860
861 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100862 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100863 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100864}
865#else
866/* ->set_read_secret callback to derive the RX secrets at <level> encryption
867 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500868 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100869 */
870int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
871 const SSL_CIPHER *cipher,
872 const uint8_t *secret, size_t secret_len)
873{
874 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
875 struct quic_tls_ctx *tls_ctx =
876 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
877
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100878 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100879 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100880 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100881 goto out;
882 }
883
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100884 tls_ctx->rx.aead = tls_aead(cipher);
885 tls_ctx->rx.md = tls_md(cipher);
886 tls_ctx->rx.hp = tls_hp(cipher);
887
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100888 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
889 goto err;
890
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100891 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100892 tls_ctx->rx.key, tls_ctx->rx.keylen,
893 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100894 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
895 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100896 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100897 goto err;
898 }
899
900 if (objt_server(conn->target) && level == ssl_encryption_application) {
901 const unsigned char *buf;
902 size_t buflen;
903
904 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
905 if (!buflen)
906 goto err;
907
908 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
909 goto err;
910 }
911
912 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100913 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100914 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100915
916 return 1;
917
918 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100919 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100920 return 0;
921}
922
923/* ->set_write_secret callback to derive the TX secrets at <level>
924 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500925 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100926 */
927int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
928 const SSL_CIPHER *cipher,
929 const uint8_t *secret, size_t secret_len)
930{
931 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
932 struct quic_tls_ctx *tls_ctx =
933 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
934
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100935 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100936 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100937 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100938 goto out;
939 }
940
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100941 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
942 goto err;
943
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100944 tls_ctx->tx.aead = tls_aead(cipher);
945 tls_ctx->tx.md = tls_md(cipher);
946 tls_ctx->tx.hp = tls_hp(cipher);
947
948 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100949 tls_ctx->tx.key, tls_ctx->tx.keylen,
950 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100951 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
952 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100953 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100954 goto err;
955 }
956
957 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100958 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100959 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100960 return 1;
961
962 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100963 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100964 return 0;
965}
966#endif
967
968/* This function copies the CRYPTO data provided by the TLS stack found at <data>
969 * with <len> as size in CRYPTO buffers dedicated to store the information about
970 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
971 * It fails only if it could not managed to allocate enough CRYPTO buffers to
972 * store all the data.
973 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
974 */
975static int quic_crypto_data_cpy(struct quic_enc_level *qel,
976 const unsigned char *data, size_t len)
977{
978 struct quic_crypto_buf **qcb;
979 /* The remaining byte to store in CRYPTO buffers. */
980 size_t cf_offset, cf_len, *nb_buf;
981 unsigned char *pos;
982
983 nb_buf = &qel->tx.crypto.nb_buf;
984 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
985 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
986 cf_len = len;
987
988 while (len) {
989 size_t to_copy, room;
990
991 pos = (*qcb)->data + (*qcb)->sz;
992 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
993 to_copy = len > room ? room : len;
994 if (to_copy) {
995 memcpy(pos, data, to_copy);
996 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
997 qel->tx.crypto.sz += to_copy;
998 (*qcb)->sz += to_copy;
999 pos += to_copy;
1000 len -= to_copy;
1001 data += to_copy;
1002 }
1003 else {
1004 struct quic_crypto_buf **tmp;
1005
1006 tmp = realloc(qel->tx.crypto.bufs,
1007 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1008 if (tmp) {
1009 qel->tx.crypto.bufs = tmp;
1010 qcb = &qel->tx.crypto.bufs[*nb_buf];
1011 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1012 if (!*qcb)
1013 return 0;
1014
1015 (*qcb)->sz = 0;
1016 ++*nb_buf;
1017 }
1018 else {
1019 break;
1020 }
1021 }
1022 }
1023
1024 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1025 * have been buffered.
1026 */
1027 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001028 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001029
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001030 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001031 if (!frm)
1032 return 0;
1033
1034 frm->type = QUIC_FT_CRYPTO;
1035 frm->crypto.offset = cf_offset;
1036 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02001037 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001038 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001039 }
1040
1041 return len == 0;
1042}
1043
1044
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001045/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1046void quic_set_tls_alert(struct quic_conn *qc, int alert)
1047{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001048 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001049 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001050 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001051}
1052
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001053/* Set the application for <qc> QUIC connection.
1054 * Return 1 if succeeded, 0 if not.
1055 */
1056int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1057{
1058 const struct qcc_app_ops *app_ops;
1059
1060 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) {
1061 app_ops = qc->qcc->app_ops = &h3_ops;
1062 }
1063 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) {
1064 app_ops = qc->qcc->app_ops = &hq_interop_ops;
1065 }
1066 else
1067 return 0;
1068
1069 if (app_ops->init && !app_ops->init(qc->qcc))
1070 return 0;
1071
1072 if (app_ops->finalize)
1073 app_ops->finalize(qc->qcc->ctx);
1074
1075 return 1;
1076}
1077
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001078/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1079 * wants to provide the QUIC layer with CRYPTO data.
1080 * Returns 1 if succeeded, 0 if not.
1081 */
1082int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1083 const uint8_t *data, size_t len)
1084{
1085 struct connection *conn;
1086 enum quic_tls_enc_level tel;
1087 struct quic_enc_level *qel;
1088
1089 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001090 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001091 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001092 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001093 goto out;
1094 }
1095
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001096 tel = ssl_to_quic_enc_level(level);
1097 qel = &conn->qc->els[tel];
1098
1099 if (tel == -1) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001100 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001101 goto err;
1102 }
1103
1104 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001105 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001106 goto err;
1107 }
1108
1109 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001110 conn->qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001111
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001112 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001113 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001114 return 1;
1115
1116 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001117 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001118 return 0;
1119}
1120
1121int ha_quic_flush_flight(SSL *ssl)
1122{
1123 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1124
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001125 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn->qc);
1126 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001127
1128 return 1;
1129}
1130
1131int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1132{
1133 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1134
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001135 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn->qc, &alert, &level);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001136 quic_set_tls_alert(conn->qc, alert);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001137 HA_ATOMIC_STORE(&conn->qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001138 return 1;
1139}
1140
1141/* QUIC TLS methods */
1142static SSL_QUIC_METHOD ha_quic_method = {
1143#ifdef OPENSSL_IS_BORINGSSL
1144 .set_read_secret = ha_set_rsec,
1145 .set_write_secret = ha_set_wsec,
1146#else
1147 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1148#endif
1149 .add_handshake_data = ha_quic_add_handshake_data,
1150 .flush_flight = ha_quic_flush_flight,
1151 .send_alert = ha_quic_send_alert,
1152};
1153
1154/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1155 * Returns an error count.
1156 */
1157int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1158{
1159 struct proxy *curproxy = bind_conf->frontend;
1160 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1161 int cfgerr = 0;
1162
1163#if 0
1164 /* XXX Did not manage to use this. */
1165 const char *ciphers =
1166 "TLS_AES_128_GCM_SHA256:"
1167 "TLS_AES_256_GCM_SHA384:"
1168 "TLS_CHACHA20_POLY1305_SHA256:"
1169 "TLS_AES_128_CCM_SHA256";
1170#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001171 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001172 long options =
1173 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1174 SSL_OP_SINGLE_ECDH_USE |
1175 SSL_OP_CIPHER_SERVER_PREFERENCE;
1176 SSL_CTX *ctx;
1177
1178 ctx = SSL_CTX_new(TLS_server_method());
1179 bind_conf->initial_ctx = ctx;
1180
1181 SSL_CTX_set_options(ctx, options);
1182#if 0
1183 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1184 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1185 "for bind '%s' at [%s:%d].\n",
1186 curproxy->id, ciphers,
1187 bind_conf->arg, bind_conf->file, bind_conf->line);
1188 cfgerr++;
1189 }
1190#endif
1191
1192 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1193 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1194 "for bind '%s' at [%s:%d].\n",
1195 curproxy->id, groups,
1196 bind_conf->arg, bind_conf->file, bind_conf->line);
1197 cfgerr++;
1198 }
1199
1200 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1201 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1202 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1203 SSL_CTX_set_default_verify_paths(ctx);
1204
1205#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1206#ifdef OPENSSL_IS_BORINGSSL
1207 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1208 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1209#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1210 if (bind_conf->ssl_conf.early_data) {
1211 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001212 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001213 }
1214 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1215 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1216#else
1217 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1218#endif
1219 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1220#endif
1221 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1222
1223 return cfgerr;
1224}
1225
1226/* Decode an expected packet number from <truncated_on> its truncated value,
1227 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1228 * the number of bits used to encode this packet number (its length in bytes * 8).
1229 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1230 */
1231static uint64_t decode_packet_number(uint64_t largest_pn,
1232 uint32_t truncated_pn, unsigned int pn_nbits)
1233{
1234 uint64_t expected_pn = largest_pn + 1;
1235 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1236 uint64_t pn_hwin = pn_win / 2;
1237 uint64_t pn_mask = pn_win - 1;
1238 uint64_t candidate_pn;
1239
1240
1241 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1242 /* Note that <pn_win> > <pn_hwin>. */
1243 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1244 candidate_pn + pn_hwin <= expected_pn)
1245 return candidate_pn + pn_win;
1246
1247 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1248 return candidate_pn - pn_win;
1249
1250 return candidate_pn;
1251}
1252
1253/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1254 * cryptographic context.
1255 * <largest_pn> is the largest received packet number and <pn> the address of
1256 * the packet number field for this packet with <byte0> address of its first byte.
1257 * <end> points to one byte past the end of this packet.
1258 * Returns 1 if succeeded, 0 if not.
1259 */
1260static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1261 int64_t largest_pn, unsigned char *pn,
1262 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001263 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001264{
1265 int ret, outlen, i, pnlen;
1266 uint64_t packet_number;
1267 uint32_t truncated_pn = 0;
1268 unsigned char mask[5] = {0};
1269 unsigned char *sample;
1270 EVP_CIPHER_CTX *cctx;
1271 unsigned char *hp_key;
1272
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001273 /* Check there is enough data in this packet. */
1274 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001275 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001276 return 0;
1277 }
1278
1279 cctx = EVP_CIPHER_CTX_new();
1280 if (!cctx) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001281 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001282 return 0;
1283 }
1284
1285 ret = 0;
1286 sample = pn + QUIC_PACKET_PN_MAXLEN;
1287
1288 hp_key = tls_ctx->rx.hp_key;
1289 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1290 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1291 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001292 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001293 goto out;
1294 }
1295
1296 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1297 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1298 for (i = 0; i < pnlen; i++) {
1299 pn[i] ^= mask[i + 1];
1300 truncated_pn = (truncated_pn << 8) | pn[i];
1301 }
1302
1303 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1304 /* Store remaining information for this unprotected header */
1305 pkt->pn = packet_number;
1306 pkt->pnl = pnlen;
1307
1308 ret = 1;
1309
1310 out:
1311 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001312
1313 return ret;
1314}
1315
1316/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1317 * address, with <payload_len> as payload length, <aad> as address of
1318 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1319 * context.
1320 * Returns 1 if succeeded, 0 if not.
1321 */
1322static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1323 unsigned char *aad, size_t aad_len, uint64_t pn,
1324 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1325{
1326 unsigned char iv[12];
1327 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001328 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001329 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001330
1331 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001332 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn->qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001333 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001334 }
1335
1336 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1337 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001338 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn->qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001339 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001340 }
1341
1342 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001343
1344 err:
1345 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001346 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn->qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001347 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001348}
1349
1350/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1351 * Returns 1 if succeeded, 0 if not.
1352 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001353static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001354{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001355 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001356 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001357 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001358 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001359 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1360 unsigned char *rx_key = tls_ctx->rx.key;
1361
1362 kp_changed = 0;
1363 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1364 /* The two tested bits are not at the same position,
1365 * this is why they are first both inversed.
1366 */
1367 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1368 if (pkt->pn < tls_ctx->rx.pn) {
1369 /* The lowest packet number of a previous key phase
1370 * cannot be null if it really stores previous key phase
1371 * secrets.
1372 */
1373 if (!pkt->qc->ku.prv_rx.pn)
1374 return 0;
1375
1376 rx_iv = pkt->qc->ku.prv_rx.iv;
1377 rx_key = pkt->qc->ku.prv_rx.key;
1378 }
1379 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1380 /* Next key phase */
1381 kp_changed = 1;
1382 rx_iv = pkt->qc->ku.nxt_rx.iv;
1383 rx_key = pkt->qc->ku.nxt_rx.key;
1384 }
1385 }
1386 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001387
1388 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1389 return 0;
1390
1391 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1392 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001393 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001394 if (!ret)
1395 return 0;
1396
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001397 /* Update the keys only if the packet decryption succeeded. */
1398 if (kp_changed) {
1399 quic_tls_rotate_keys(pkt->qc);
1400 /* Toggle the Key Phase bit */
1401 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1402 /* Store the lowest packet number received for the current key phase */
1403 tls_ctx->rx.pn = pkt->pn;
1404 /* Prepare the next key update */
1405 if (!quic_tls_key_update(pkt->qc))
1406 return 0;
1407 }
1408
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001409 /* Update the packet length (required to parse the frames). */
1410 pkt->len = pkt->aad_len + ret;
1411
1412 return 1;
1413}
1414
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001415/* Remove from <qcs> stream the acknowledged frames.
1416 * Never fails.
1417 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001418static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001419{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001420 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001421 struct eb64_node *frm_node;
1422
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001423 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001424 frm_node = eb64_first(&qcs->tx.acked_frms);
1425 while (frm_node) {
1426 struct quic_stream *strm;
1427
1428 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1429 if (strm->offset.key != qcs->tx.ack_offset)
1430 break;
1431
1432 b_del(strm->buf, strm->len);
1433 qcs->tx.ack_offset += strm->len;
1434 frm_node = eb64_next(frm_node);
1435 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001436 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001437 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001438
1439 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001440}
1441
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001442/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001443static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001444 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001445{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001446 int stream_acked;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001447 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001448
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001449 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001450 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001451 switch (frm->type) {
1452 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1453 {
1454 struct qcs *qcs = frm->stream.qcs;
1455 struct quic_stream *strm = &frm->stream;
1456
1457 if (qcs->tx.ack_offset == strm->offset.key) {
1458 b_del(strm->buf, strm->len);
1459 qcs->tx.ack_offset += strm->len;
1460 LIST_DELETE(&frm->list);
1461 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001462 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001463 }
1464 else {
1465 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1466 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001467 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001468 }
1469 break;
1470 default:
1471 LIST_DELETE(&frm->list);
1472 pool_free(pool_head_quic_frame, frm);
1473 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001474
1475 if (stream_acked) {
1476 struct qcc *qcc = qc->qcc;
1477
1478 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1479 tasklet_wakeup(qcc->subs->tasklet);
1480 qcc->subs->events &= ~SUB_RETRY_SEND;
1481 if (!qcc->subs->events)
1482 qcc->subs = NULL;
1483 }
1484 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001485}
1486
1487/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1488 * deallocating them, and their TX frames.
1489 * Returns the last node reached to be used for the next range.
1490 * May be NULL if <largest> node could not be found.
1491 */
1492static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1493 struct list *newly_acked_pkts,
1494 struct eb64_node *largest_node,
1495 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001496 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001497{
1498 struct eb64_node *node;
1499 struct quic_tx_packet *pkt;
1500
1501 if (largest_node)
1502 node = largest_node;
1503 else {
1504 node = eb64_lookup(pkts, largest);
1505 while (!node && largest > smallest) {
1506 node = eb64_lookup(pkts, --largest);
1507 }
1508 }
1509
1510 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001511 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001512
1513 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1514 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001515 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001516 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->qc, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001517 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1518 qc_treat_acked_tx_frm(frm, ctx);
1519 node = eb64_prev(node);
1520 eb64_delete(&pkt->pn_node);
1521 }
1522
1523 return node;
1524}
1525
1526/* Treat <frm> frame whose packet it is attached to has just been detected as non
1527 * acknowledged.
1528 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001529static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001530 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001531 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001532{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001533 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->qc, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001534 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001535 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001536}
1537
1538
1539/* Free the TX packets of <pkts> list */
1540static inline void free_quic_tx_pkts(struct list *pkts)
1541{
1542 struct quic_tx_packet *pkt, *tmp;
1543
1544 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001545 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001546 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001547 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001548 }
1549}
1550
1551/* Send a packet loss event nofification to the congestion controller
1552 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1553 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1554 * at <now_us> current time.
1555 * Always succeeds.
1556 */
1557static inline void qc_cc_loss_event(struct quic_conn *qc,
1558 unsigned int lost_bytes,
1559 unsigned int newest_time_sent,
1560 unsigned int period,
1561 unsigned int now_us)
1562{
1563 struct quic_cc_event ev = {
1564 .type = QUIC_CC_EVT_LOSS,
1565 .loss.now_ms = now_ms,
1566 .loss.max_ack_delay = qc->max_ack_delay,
1567 .loss.lost_bytes = lost_bytes,
1568 .loss.newest_time_sent = newest_time_sent,
1569 .loss.period = period,
1570 };
1571
1572 quic_cc_event(&qc->path->cc, &ev);
1573}
1574
1575/* Send a packet ack event nofication for each newly acked packet of
1576 * <newly_acked_pkts> list and free them.
1577 * Always succeeds.
1578 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001579static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001580 struct list *newly_acked_pkts)
1581{
1582 struct quic_conn *qc = ctx->conn->qc;
1583 struct quic_tx_packet *pkt, *tmp;
1584 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1585
1586 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1587 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001588 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001589 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001590 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001591 ev.ack.acked = pkt->in_flight_len;
1592 ev.ack.time_sent = pkt->time_sent;
1593 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001594 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001595 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001596 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001597 }
1598
1599}
1600
1601/* Handle <pkts> list of lost packets detected at <now_us> handling
1602 * their TX frames.
1603 * Send a packet loss event to the congestion controller if
1604 * in flight packet have been lost.
1605 * Also frees the packet in <pkts> list.
1606 * Never fails.
1607 */
1608static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001609 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001610 struct list *pkts,
1611 uint64_t now_us)
1612{
1613 struct quic_conn *qc = ctx->conn->qc;
1614 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001615 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001616 uint64_t lost_bytes;
1617
1618 lost_bytes = 0;
1619 oldest_lost = newest_lost = NULL;
1620 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1621 lost_bytes += pkt->in_flight_len;
1622 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001623 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001624 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001625 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001626 /* Treat the frames of this lost packet. */
1627 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1628 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001629 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001630 if (!oldest_lost) {
1631 oldest_lost = newest_lost = pkt;
1632 }
1633 else {
1634 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001635 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001636 newest_lost = pkt;
1637 }
1638 }
1639
1640 if (lost_bytes) {
1641 /* Sent a packet loss event to the congestion controller. */
1642 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1643 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001644 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001645 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001646 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001647 }
1648}
1649
1650/* Look for packet loss from sent packets for <qel> encryption level of a
1651 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1652 * their tree if deemed as lost or set the <loss_time> value the packet number
1653 * space if any not deemed lost.
1654 * Should be called after having received an ACK frame with newly acknowledged
1655 * packets or when the the loss detection timer has expired.
1656 * Always succeeds.
1657 */
1658static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1659 struct quic_conn *qc,
1660 struct list *lost_pkts)
1661{
1662 struct eb_root *pkts;
1663 struct eb64_node *node;
1664 struct quic_loss *ql;
1665 unsigned int loss_delay;
1666
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001667 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001668 pkts = &pktns->tx.pkts;
1669 pktns->tx.loss_time = TICK_ETERNITY;
1670 if (eb_is_empty(pkts))
1671 goto out;
1672
1673 ql = &qc->path->loss;
1674 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1675 loss_delay += loss_delay >> 3;
1676 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1677
1678 node = eb64_first(pkts);
1679 while (node) {
1680 struct quic_tx_packet *pkt;
1681 int64_t largest_acked_pn;
1682 unsigned int loss_time_limit, time_sent;
1683
1684 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001685 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001686 node = eb64_next(node);
1687 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1688 break;
1689
1690 time_sent = pkt->time_sent;
1691 loss_time_limit = tick_add(time_sent, loss_delay);
1692 if (tick_is_le(time_sent, now_ms) ||
1693 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1694 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001695 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001696 }
1697 else {
1698 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1699 }
1700 }
1701
1702 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001703 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001704}
1705
1706/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1707 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001708 * 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 +01001709 * acked ack-eliciting packet.
1710 * Return 1, if succeeded, 0 if not.
1711 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001712static 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 +01001713 struct quic_enc_level *qel,
1714 unsigned int *rtt_sample,
1715 const unsigned char **pos, const unsigned char *end)
1716{
1717 struct quic_ack *ack = &frm->ack;
1718 uint64_t smallest, largest;
1719 struct eb_root *pkts;
1720 struct eb64_node *largest_node;
1721 unsigned int time_sent, pkt_flags;
1722 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1723 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1724
1725 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1726 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001727 ctx->qc, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001728 goto err;
1729 }
1730
1731 if (ack->first_ack_range > ack->largest_ack) {
1732 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001733 ctx->qc, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001734 goto err;
1735 }
1736
1737 largest = ack->largest_ack;
1738 smallest = largest - ack->first_ack_range;
1739 pkts = &qel->pktns->tx.pkts;
1740 pkt_flags = 0;
1741 largest_node = NULL;
1742 time_sent = 0;
1743
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001744 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 +01001745 largest_node = eb64_lookup(pkts, largest);
1746 if (!largest_node) {
1747 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001748 QUIC_EV_CONN_PRSAFRM, ctx->qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001749 }
1750 else {
1751 time_sent = eb64_entry(&largest_node->node,
1752 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001753 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001754 }
1755
1756 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001757 ctx->qc, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001758 do {
1759 uint64_t gap, ack_range;
1760
1761 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1762 largest_node, largest, smallest, ctx);
1763 if (!ack->ack_range_num--)
1764 break;
1765
1766 if (!quic_dec_int(&gap, pos, end))
1767 goto err;
1768
1769 if (smallest < gap + 2) {
1770 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001771 ctx->qc, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001772 goto err;
1773 }
1774
1775 largest = smallest - gap - 2;
1776 if (!quic_dec_int(&ack_range, pos, end))
1777 goto err;
1778
1779 if (largest < ack_range) {
1780 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001781 ctx->qc, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001782 goto err;
1783 }
1784
1785 /* Do not use this node anymore. */
1786 largest_node = NULL;
1787 /* Next range */
1788 smallest = largest - ack_range;
1789
1790 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001791 ctx->qc, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001792 } while (1);
1793
1794 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001795 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001796
1797 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1798 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001799 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001800 }
1801
1802 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1803 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1804 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1805 if (!LIST_ISEMPTY(&lost_pkts))
1806 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1807 }
1808 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1809 if (quic_peer_validated_addr(ctx))
1810 ctx->conn->qc->path->loss.pto_count = 0;
1811 qc_set_timer(ctx);
1812 }
1813
1814
1815 return 1;
1816
1817 err:
1818 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001819 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001820 return 0;
1821}
1822
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001823/* This function gives the detail of the SSL error. It is used only
1824 * if the debug mode and the verbose mode are activated. It dump all
1825 * the SSL error until the stack was empty.
1826 */
1827static forceinline void qc_ssl_dump_errors(struct connection *conn)
1828{
1829 if (unlikely(global.mode & MODE_DEBUG)) {
1830 while (1) {
1831 unsigned long ret;
1832
1833 ret = ERR_get_error();
1834 if (!ret)
1835 return;
1836
1837 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1838 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1839 }
1840 }
1841}
1842
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001843int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1844 const char **str, int *len);
1845
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001846/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1847 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001848 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001849 * Return 1 if succeeded, 0 if not.
1850 */
1851static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001852 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001853 const unsigned char *data, size_t len,
1854 struct quic_rx_packet *pkt,
1855 struct quic_rx_crypto_frm *cf)
1856{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001857 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001858 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001859
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001860 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001861 qc = ctx->qc;
1862
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001863 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1864
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001865 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1866 TRACE_PROTO("SSL_provide_quic_data() error",
1867 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1868 goto err;
1869 }
1870
1871 el->rx.crypto.offset += len;
1872 TRACE_PROTO("in order CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001873 QUIC_EV_CONN_SSLDATA, qc, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001874
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001875 state = HA_ATOMIC_LOAD(&qc->state);
1876 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001877 ssl_err = SSL_do_handshake(ctx->ssl);
1878 if (ssl_err != 1) {
1879 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1880 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1881 TRACE_PROTO("SSL handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001882 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001883 goto out;
1884 }
1885
1886 TRACE_DEVEL("SSL handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001887 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001888 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001889 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001890 goto err;
1891 }
1892
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001893 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001894 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001895 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001896 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001897 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001898 } else {
1899 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1900 if (ssl_err != 1) {
1901 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1902 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1903 TRACE_DEVEL("SSL post handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001904 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001905 goto out;
1906 }
1907
1908 TRACE_DEVEL("SSL post handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001909 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001910 goto err;
1911 }
1912
1913 TRACE_PROTO("SSL post handshake succeeded",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001914 QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001915 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001916
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001917 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001918 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001919 return 1;
1920
1921 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001922 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001923 return 0;
1924}
1925
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001926/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1927 * <pkt> RX packet.
1928 * Return it if succeeded, NULL if not.
1929 */
1930static inline
1931struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1932 struct quic_rx_packet *pkt)
1933{
1934 struct quic_rx_strm_frm *frm;
1935
1936 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1937 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001938 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001939 frm->len = stream_frm->len;
1940 frm->data = stream_frm->data;
1941 frm->pkt = pkt;
1942 }
1943
1944 return frm;
1945}
1946
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001947/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001948 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001949 */
1950static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1951{
1952 size_t ret;
1953
1954 ret = 0;
1955 while (strm_frm->len) {
1956 size_t try;
1957
1958 try = b_contig_space(buf);
1959 if (!try)
1960 break;
1961
1962 if (try > strm_frm->len)
1963 try = strm_frm->len;
1964 memcpy(b_tail(buf), strm_frm->data, try);
1965 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001966 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001967 b_add(buf, try);
1968 ret += try;
1969 }
1970
1971 return ret;
1972}
1973
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01001974/* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer.
1975 * Also update <strm_frm> frame to reflect the data which have been consumed.
1976 */
1977static size_t qc_rx_strm_frm_cpy(struct buffer *buf,
1978 struct quic_rx_strm_frm *strm_frm)
1979{
1980 size_t ret;
1981
1982 ret = 0;
1983 while (strm_frm->len) {
1984 size_t try;
1985
1986 try = b_contig_space(buf);
1987 if (!try)
1988 break;
1989
1990 if (try > strm_frm->len)
1991 try = strm_frm->len;
1992 memcpy(b_tail(buf), strm_frm->data, try);
1993 strm_frm->len -= try;
1994 strm_frm->offset_node.key += try;
1995 b_add(buf, try);
1996 ret += try;
1997 }
1998
1999 return ret;
2000}
2001
2002/* Process as much as possible RX STREAM frames received for <qcs> */
2003static size_t qc_treat_rx_strm_frms(struct qcs *qcs)
2004{
2005 int total;
2006 struct eb64_node *frm_node;
2007
2008 total = 0;
2009 frm_node = eb64_first(&qcs->rx.frms);
2010 while (frm_node) {
2011 int ret;
2012 struct quic_rx_strm_frm *frm;
2013
2014 frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node);
2015 if (frm->offset_node.key != qcs->rx.offset)
2016 break;
2017
2018 ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm);
2019 qcs->rx.offset += ret;
2020 total += ret;
2021 if (frm->len) {
2022 /* If there is remaining data in this frame
2023 * this is because the destination buffer is full.
2024 */
2025 break;
2026 }
2027
2028 frm_node = eb64_next(frm_node);
2029 quic_rx_packet_refdec(frm->pkt);
2030 eb64_delete(&frm->offset_node);
2031 pool_free(pool_head_quic_rx_strm_frm, frm);
2032 }
2033
2034 return total;
2035}
2036
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002037/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2038 * streams may be open. The data are copied to the stream RX buffer if possible.
2039 * If not, the STREAM frame is stored to be treated again later.
2040 * We rely on the flow control so that not to store too much STREAM frames.
2041 * Return 1 if succeeded, 0 if not.
2042 */
2043static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2044 struct quic_stream *strm_frm,
2045 struct quic_conn *qc)
2046{
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002047 int total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002048 struct qcs *strm;
2049 struct eb64_node *strm_node, *frm_node;
2050 struct quic_rx_strm_frm *frm;
2051
2052 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2053 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002054 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002055 return 0;
2056 }
2057
2058 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002059 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002060 /* FIXME: handle the case where this frame overlap others */
2061 if (frm_node) {
2062 TRACE_PROTO("Already existing stream data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002063 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002064 goto out;
2065 }
2066
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002067 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002068 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002069 int ret;
2070
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002071 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002072 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002073 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002074
2075 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002076 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002077 strm->rx.offset += ret;
2078 }
2079
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002080 total += qc_treat_rx_strm_frms(strm);
2081 if (total && qc->qcc->app_ops->decode_qcs(strm, strm_frm->fin, qc->qcc->ctx) < 0) {
2082 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
2083 return 0;
2084 }
2085
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002086 if (!strm_frm->len)
2087 goto out;
2088
2089 store_frm:
2090 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2091 if (!frm) {
2092 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002093 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002094 return 0;
2095 }
2096
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002097 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002098 quic_rx_packet_refinc(pkt);
2099
2100 out:
2101 return 1;
2102}
2103
2104/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2105 * streams may be open. The data are copied to the stream RX buffer if possible.
2106 * If not, the STREAM frame is stored to be treated again later.
2107 * We rely on the flow control so that not to store too much STREAM frames.
2108 * Return 1 if succeeded, 0 if not.
2109 */
2110static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2111 struct quic_stream *strm_frm,
2112 struct quic_conn *qc)
2113{
2114 struct qcs *strm;
2115 struct eb64_node *strm_node, *frm_node;
2116 struct quic_rx_strm_frm *frm;
2117 size_t strm_frm_len;
2118
2119 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2120 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002121 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002122 return 0;
2123 }
2124
2125 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002126 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002127 /* FIXME: handle the case where this frame overlap others */
2128 if (frm_node) {
2129 TRACE_PROTO("Already existing stream data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002130 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002131 goto out;
2132 }
2133
2134 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002135 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002136 int ret;
2137
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002138 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002139 goto store_frm;
2140
2141 /* qc_strm_cpy() will modify the offset, depending on the number
2142 * of bytes copied.
2143 */
2144 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2145 /* Inform the application of the arrival of this new stream */
2146 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002147 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002148 return 0;
2149 }
2150
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002151 if (ret)
2152 qcs_notify_recv(strm);
2153
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002154 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002155 }
2156 /* Take this frame into an account for the stream flow control */
2157 strm->rx.offset += strm_frm_len;
2158 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002159 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002160 */
2161 if (!strm_frm->len)
2162 goto out;
2163
2164 store_frm:
2165 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2166 if (!frm) {
2167 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002168 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002169 return 0;
2170 }
2171
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002172 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002173 quic_rx_packet_refinc(pkt);
2174
2175 out:
2176 return 1;
2177}
2178
2179static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2180 struct quic_stream *strm_frm,
2181 struct quic_conn *qc)
2182{
2183 if (strm_frm->id & QCS_ID_DIR_BIT)
2184 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2185 else
2186 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2187}
2188
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002189/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2190 * as I/O handler context and <qel> as encryption level.
2191 * Returns 1 if succeeded, 0 if failed.
2192 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002193static 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 +01002194 struct quic_enc_level *qel)
2195{
2196 struct quic_frame frm;
2197 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002198 struct quic_conn *qc = ctx->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002199
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002200 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002201 /* Skip the AAD */
2202 pos = pkt->data + pkt->aad_len;
2203 end = pkt->data + pkt->len;
2204
2205 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002206 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002207 goto err;
2208
2209 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002210 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002211 break;
2212 case QUIC_FT_PING:
2213 break;
2214 case QUIC_FT_ACK:
2215 {
2216 unsigned int rtt_sample;
2217
2218 rtt_sample = 0;
2219 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
2220 goto err;
2221
2222 if (rtt_sample) {
2223 unsigned int ack_delay;
2224
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002225 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
2226 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay));
2227 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002228 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002229 break;
2230 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002231 case QUIC_FT_STOP_SENDING:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002232 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002233 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002234 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002235 {
2236 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002237
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002238 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2239 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2240 /* XXX TO DO: <cfdebug> is used only for the traces. */
2241 struct quic_rx_crypto_frm cfdebug = { };
2242
2243 cfdebug.offset_node.key = frm.crypto.offset;
2244 cfdebug.len = frm.crypto.len;
2245 /* Nothing to do */
2246 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002247 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002248 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002249 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002250 else {
2251 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2252 /* XXX TO DO: <cfdebug> is used only for the traces. */
2253 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002254
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002255 cfdebug.offset_node.key = frm.crypto.offset;
2256 cfdebug.len = frm.crypto.len;
2257 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002258 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002259 frm.crypto.len -= diff;
2260 frm.crypto.data += diff;
2261 frm.crypto.offset = qel->rx.crypto.offset;
2262 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002263 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002264
2265 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002266 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002267 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002268
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002269 cfdebug.offset_node.key = frm.crypto.offset;
2270 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002271 if (!qc_provide_cdata(qel, ctx,
2272 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002273 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002274 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002275
2276 break;
2277 }
2278
2279 /* frm.crypto.offset > qel->rx.crypto.offset */
2280 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2281 if (!cf) {
2282 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002283 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002284 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002285 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002286
2287 cf->offset_node.key = frm.crypto.offset;
2288 cf->len = frm.crypto.len;
2289 cf->data = frm.crypto.data;
2290 cf->pkt = pkt;
2291 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2292 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002293 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002294 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002295 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002296 {
2297 struct quic_stream *stream = &frm.stream;
2298
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002299 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002300 if (objt_listener(ctx->conn->target)) {
2301 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2302 goto err;
2303 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2304 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002305
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002306 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002307 goto err;
2308
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002309 break;
2310 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002311 case QUIC_FT_MAX_DATA:
2312 case QUIC_FT_MAX_STREAM_DATA:
2313 case QUIC_FT_MAX_STREAMS_BIDI:
2314 case QUIC_FT_MAX_STREAMS_UNI:
2315 case QUIC_FT_DATA_BLOCKED:
2316 case QUIC_FT_STREAM_DATA_BLOCKED:
2317 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2318 case QUIC_FT_STREAMS_BLOCKED_UNI:
2319 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002320 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002321 break;
2322 case QUIC_FT_CONNECTION_CLOSE:
2323 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002324 /* warn the mux to close the connection */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002325 qc->qcc->flags |= QC_CF_CC_RECV;
2326 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002327 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002328 case QUIC_FT_HANDSHAKE_DONE:
2329 if (objt_listener(ctx->conn->target))
2330 goto err;
2331
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002332 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002333 break;
2334 default:
2335 goto err;
2336 }
2337 }
2338
2339 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2340 * has successfully parse a Handshake packet. The Initial encryption must also
2341 * be discarded.
2342 */
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002343 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002344 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002345
2346 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002347 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002348 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002349 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002350 qc_set_timer(ctx);
2351 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002352 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002353 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002354 }
2355
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002356 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002357 return 1;
2358
2359 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002360 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002361 return 0;
2362}
2363
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002364/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002365 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002366 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2367 * This function must be called only after having built a correct datagram.
2368 * Always succeeds.
2369 */
2370static inline void qc_set_dg(struct cbuf *cbuf,
2371 uint16_t dglen, struct quic_tx_packet *pkt)
2372{
2373 write_u16(cb_wr(cbuf), dglen);
2374 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2375 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2376}
2377
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002378/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002379 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2380 * several packets in the same datagram. A header made of two fields is added
2381 * to each datagram: the datagram length followed by the address of the first
2382 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002383 * Returns 1 if succeeded, or 0 if something wrong happened.
2384 */
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002385static int qc_prep_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002386{
2387 struct quic_conn *qc;
2388 enum quic_tls_enc_level tel, next_tel;
2389 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002390 struct cbuf *cbuf;
2391 unsigned char *end_buf, *end, *pos, *spos;
2392 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2393 /* length of datagrams */
2394 uint16_t dglen;
2395 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002396 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002397 /* Each datagram is prepended with its length followed by the
2398 * address of the first packet in the datagram.
2399 */
2400 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002401
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002402 qc = ctx->qc;
2403
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002404 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2405
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01002406 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state), 0)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002407 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002408 goto err;
2409 }
2410
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002411 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002412 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002413 total = 0;
2414 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002415 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002416 cbuf = qr->cbuf;
2417 spos = pos = cb_wr(cbuf);
2418 /* Leave at least <dglen> bytes at the end of this buffer
2419 * to ensure there is enough room to mark the end of prepared
2420 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002421 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002422 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2423 first_pkt = prv_pkt = NULL;
2424 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002425 int err, nb_ptos, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002426 enum quic_pkt_type pkt_type;
2427
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002428 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002429 nb_ptos = ack = 0;
2430 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2431 if (!cc) {
2432 if (!prv_pkt) {
2433 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2434 do {
2435 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2436 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2437 }
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002438 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002439 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002440 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002441 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002442 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002443 * and if there is no more CRYPTO data available or in flight
2444 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002445 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002446 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002447 (!cc && !ack && !nb_ptos &&
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002448 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2449 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002450 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002451 /* Set the current datagram as prepared into <cbuf> if
2452 * the was already a correct packet which was previously written.
2453 */
2454 if (prv_pkt)
2455 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002456 break;
2457 }
2458
2459 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002460 if (!prv_pkt) {
2461 /* Leave room for the datagram header */
2462 pos += dg_headlen;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002463 if (!quic_peer_validated_addr(ctx) && objt_listener(ctx->conn->target)) {
2464 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
2465 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
2466 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2467 }
2468 else {
2469 end = pos + qc->path->mtu;
2470 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002471 }
2472
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002473 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002474 pkt_type, ack, nb_ptos, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002475 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002476 if (err < 0) {
2477 if (!prv_pkt && nb_ptos)
2478 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002479 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002480 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002481 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002482 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002483 case -2:
2484 goto err;
2485 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002486 /* If there was already a correct packet present, set the
2487 * current datagram as prepared into <cbuf>.
2488 */
2489 if (prv_pkt) {
2490 qc_set_dg(cbuf, dglen, first_pkt);
2491 goto stop_build;
2492 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002493 goto out;
2494 default:
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002495 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2496 if (!cur_pkt)
2497 goto err;
2498
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002499 total += cur_pkt->len;
2500 /* keep trace of the first packet in the datagram */
2501 if (!first_pkt)
2502 first_pkt = cur_pkt;
2503 /* Attach the current one to the previous one */
2504 if (prv_pkt)
2505 prv_pkt->next = cur_pkt;
2506 /* Let's say we have to build a new dgram */
2507 prv_pkt = NULL;
2508 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002509 /* Discard the Initial encryption keys as soon as
2510 * a handshake packet could be built.
2511 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002512 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002513 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2514 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002515 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002516 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2517 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002518 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002519 }
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002520 /* If the data for the current encryption level have all been sent,
2521 * select the next level.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002522 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002523 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002524 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2525 (next_tel != QUIC_TLS_ENC_LEVEL_NONE && qc->els[next_tel].pktns->tx.in_flight))) {
Frédéric Lécaille4bade772021-08-23 08:54:28 +02002526 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2527 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2528 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002529 tel = next_tel;
2530 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002531 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002532 /* If there is data for the next level, do not
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002533 * consume a datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002534 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002535 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002536 }
2537 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002538 }
2539
2540 /* If we have to build a new datagram, set the current datagram as
2541 * prepared into <cbuf>.
2542 */
2543 if (!prv_pkt) {
2544 qc_set_dg(cbuf, dglen, first_pkt);
2545 first_pkt = NULL;
2546 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002547 padding = 0;
2548 }
2549 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
2550 (objt_server(qc->conn->target) ||
2551 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2552 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002553 }
2554 }
2555
2556 stop_build:
2557 /* Reset <wr> writer index if in front of <rd> index */
2558 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2559 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2560
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002561 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002562 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2563 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2564 /* Mark the end of contiguous data for the reader */
2565 write_u16(cb_wr(cbuf), 0);
2566 cb_add(cbuf, sizeof(uint16_t));
2567 }
2568 }
2569
2570 if (rd && rd <= cbuf->wr) {
2571 cb_wr_reset(cbuf);
2572 if (pos == spos) {
2573 /* Reuse the same buffer if nothing was built. */
2574 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002575 }
2576 }
2577 }
2578
2579 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002580 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002581 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002582
2583 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002584 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002585 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002586}
2587
2588/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002589 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002590 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002591int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002592{
2593 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002594 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002595
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002596 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002597 cbuf = qr->cbuf;
2598 while (cb_contig_data(cbuf)) {
2599 unsigned char *pos;
2600 struct buffer tmpbuf = { };
2601 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2602 uint16_t dglen;
2603 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002604 unsigned int time_sent;
2605
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002606 pos = cb_rd(cbuf);
2607 dglen = read_u16(pos);
2608 /* End of prepared datagrams.
2609 * Reset the reader index only if in front of the writer index.
2610 */
2611 if (!dglen) {
2612 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2613
2614 if (wr && wr < cbuf->rd) {
2615 cb_rd_reset(cbuf);
2616 continue;
2617 }
2618 break;
2619 }
2620
2621 pos += sizeof dglen;
2622 first_pkt = read_ptr(pos);
2623 pos += sizeof first_pkt;
2624 tmpbuf.area = (char *)pos;
2625 tmpbuf.size = tmpbuf.data = dglen;
2626
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002627 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002628 for (pkt = first_pkt; pkt; pkt = pkt->next)
2629 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002630 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002631 &tmpbuf, tmpbuf.data, 0) <= 0) {
2632 for (pkt = first_pkt; pkt; pkt = pkt->next)
2633 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002634 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002635 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002636
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002637 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002638 qc->tx.bytes += tmpbuf.data;
2639 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002640
2641 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2642 pkt->time_sent = time_sent;
2643 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2644 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002645 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002646 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002647 qc->path->in_flight += pkt->in_flight_len;
2648 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2649 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002650 qc_set_timer(ctx);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002651 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002652 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002653 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002654 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002655 }
2656 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002657
2658 return 1;
2659}
2660
2661/* Build all the frames which must be sent just after the handshake have succeeded.
2662 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2663 * a HANDSHAKE_DONE frame.
2664 * Return 1 if succeeded, 0 if not.
2665 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002666static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002667{
2668 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002669 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002670 struct quic_frame *frm;
2671
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002672 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002674 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002675 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002676 if (!frm)
2677 return 0;
2678
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002679 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002680 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002681 }
2682
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002683 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002684 struct quic_connection_id *cid;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002685 struct listener *l = __objt_listener(qc->conn->target);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002686
2687 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002688 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002689 goto err;
2690
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002691 cid = new_quic_cid(&qc->cids, qc, i);
2692 if (!cid)
2693 goto err;
2694
2695 /* insert the allocated CID in the receiver tree */
2696 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
2697 ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len);
2698 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
2699
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002700 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002701 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002702 }
2703
2704 return 1;
2705
2706 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002707 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002708 return 0;
2709}
2710
2711/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002712void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002713{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002714 struct eb64_node *n;
2715 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002716
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002717 n = eb64_first(&arngs->root);
2718 while (n) {
2719 struct eb64_node *next;
2720
2721 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2722 next = eb64_next(n);
2723 eb64_delete(n);
2724 free(ar);
2725 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002726 }
2727}
2728
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002729/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2730 * descending order.
2731 */
2732static inline size_t sack_gap(struct quic_arng_node *p,
2733 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002734{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002735 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002736}
2737
2738
2739/* Remove the last elements of <ack_ranges> list of ack range updating its
2740 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002741 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002742 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002743static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002744{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002745 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002746
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002747 last = eb64_last(&arngs->root);
2748 while (last && arngs->enc_sz > limit) {
2749 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002750
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002751 prev = eb64_prev(last);
2752 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002753 return 0;
2754
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002755 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2756 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2757 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2758 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2759 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2760 --arngs->sz;
2761 eb64_delete(last);
2762 pool_free(pool_head_quic_arng, last);
2763 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002764 }
2765
2766 return 1;
2767}
2768
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002769/* Set the encoded size of <arngs> QUIC ack ranges. */
2770static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2771{
2772 struct eb64_node *node, *next;
2773 struct quic_arng_node *ar, *ar_next;
2774
2775 node = eb64_last(&arngs->root);
2776 if (!node)
2777 return;
2778
2779 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2780 arngs->enc_sz = quic_int_getsize(ar->last) +
2781 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2782
2783 while ((next = eb64_prev(node))) {
2784 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2785 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2786 quic_int_getsize(ar_next->last - ar_next->first.key);
2787 node = next;
2788 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2789 }
2790}
2791
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002792/* Insert <ar> ack range into <argns> tree of ack ranges.
2793 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002794 */
2795static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002796struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2797 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002798{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002799 struct quic_arng_node *new_ar;
2800
2801 new_ar = pool_alloc(pool_head_quic_arng);
2802 if (new_ar) {
2803 new_ar->first.key = ar->first;
2804 new_ar->last = ar->last;
2805 eb64_insert(&arngs->root, &new_ar->first);
2806 arngs->sz++;
2807 }
2808
2809 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002810}
2811
2812/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002813 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002814 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002815 *
2816 * Descending order
2817 * ------------->
2818 * range1 range2
2819 * ..........|--------|..............|--------|
2820 * ^ ^ ^ ^
2821 * | | | |
2822 * last1 first1 last2 first2
2823 * ..........+--------+--------------+--------+......
2824 * diff1 gap12 diff2
2825 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002826 * To encode the previous list of ranges we must encode integers as follows in
2827 * descending order:
2828 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002829 * with diff1 = last1 - first1
2830 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002831 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002832 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002833 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002834int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2835 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002836{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002837 struct eb64_node *le;
2838 struct quic_arng_node *new_node;
2839 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002840
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002841 new = NULL;
2842 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002843 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002844 if (!new_node)
2845 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002846
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002847 goto out;
2848 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002849
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002850 le = eb64_lookup_le(&arngs->root, ar->first);
2851 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002852 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002853 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002854 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002855
2856 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002857 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002858 else {
2859 struct quic_arng_node *le_ar =
2860 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002861
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002862 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002863 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002864 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002865
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002866 if (le_ar->last + 1 >= ar->first) {
2867 le_ar->last = ar->last;
2868 new = le;
2869 new_node = le_ar;
2870 }
2871 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002872 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002873 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002874 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002875
2876 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002877 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002878 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002879
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002880 /* Verify that the new inserted node does not overlap the nodes
2881 * which follow it.
2882 */
2883 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002884 struct eb64_node *next;
2885 struct quic_arng_node *next_node;
2886
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002887 while ((next = eb64_next(new))) {
2888 next_node =
2889 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002890 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002891 break;
2892
2893 if (next_node->last > new_node->last)
2894 new_node->last = next_node->last;
2895 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002896 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002897 /* Decrement the size of these ranges. */
2898 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002899 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002900 }
2901
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002902 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002903 quic_arngs_set_enc_sz(arngs);
2904
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002905 return 1;
2906}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002907/* Remove the header protection of packets at <el> encryption level.
2908 * Always succeeds.
2909 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002910static 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 +01002911{
2912 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002913 struct quic_rx_packet *pqpkt;
2914 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002915 struct quic_enc_level *app_qel;
2916
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002917 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002918 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2919 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002920 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002921 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002922 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002923 QUIC_EV_CONN_ELRMHP, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002924 goto out;
2925 }
2926 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002927 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002928 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2929 pqpkt->data + pqpkt->pn_offset,
2930 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002931 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002932 /* XXX TO DO XXX */
2933 }
2934 else {
2935 /* The AAD includes the packet number field */
2936 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2937 /* Store the packet into the tree of packets to decrypt. */
2938 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002939 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002940 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2941 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002942 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002943 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002944 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002945 MT_LIST_DELETE_SAFE(pkttmp1);
2946 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002947 }
2948
2949 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002950 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002951}
2952
2953/* Process all the CRYPTO frame at <el> encryption level.
2954 * Return 1 if succeeded, 0 if not.
2955 */
2956static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002957 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002958{
2959 struct eb64_node *node;
2960
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002961 node = eb64_first(&el->rx.crypto.frms);
2962 while (node) {
2963 struct quic_rx_crypto_frm *cf;
2964
2965 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2966 if (cf->offset_node.key != el->rx.crypto.offset)
2967 break;
2968
2969 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2970 goto err;
2971
2972 node = eb64_next(node);
2973 quic_rx_packet_refdec(cf->pkt);
2974 eb64_delete(&cf->offset_node);
2975 pool_free(pool_head_quic_rx_crypto_frm, cf);
2976 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002977 return 1;
2978
2979 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002980 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002981 return 0;
2982}
2983
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002984/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002985 * This is the caller responsibility to check that <cur_el> is different of <next_el>
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002986 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002987 * Return 1 if succeeded, 0 if not.
2988 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002989int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2990 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002991{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002992 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002993 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002994 struct quic_conn *qc = ctx->conn->qc;
2995 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002996
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002997 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002998 qel = cur_el;
2999 next_tel:
3000 if (!qel)
3001 goto out;
3002
3003 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3004 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003005 while (node) {
3006 struct quic_rx_packet *pkt;
3007
3008 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003009 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003010 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003011 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003012 /* Drop the packet */
3013 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003014 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003015 }
3016 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003017 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003018 /* Drop the packet */
3019 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003020 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003021 }
3022 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003023 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3024
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003025 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003026 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003027 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003028 if (pkt->pn > largest_pn)
3029 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003030 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003031 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003032 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003033 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003034 }
3035 }
3036 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003037 eb64_delete(&pkt->pn_node);
3038 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003039 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003040 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003041
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003042 /* Update the largest packet number. */
3043 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003044 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3045 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046 goto err;
3047
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003048 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003049 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003050 qel = next_el;
3051 goto next_tel;
3052 }
3053
3054 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003055 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003056 return 1;
3057
3058 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003059 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003060 return 0;
3061}
3062
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003063/* QUIC connection packet handler task. */
3064struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003065{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003066 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003067 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003068 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003069 enum quic_tls_enc_level tel, next_tel;
3070 struct quic_enc_level *qel, *next_qel;
3071 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003072 struct qring *qr; // Tx ring
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003073 int prev_st, st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003074
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003075 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003076 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003077 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003078 st = HA_ATOMIC_LOAD(&qc->state);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003079 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003080 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003081 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3082 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3083 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003084 start:
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003085 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003086 goto err;
3087
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003088 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003089 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003090
3091 next_level:
3092 tls_ctx = &qel->tls_ctx;
3093
3094 /* If the header protection key for this level has been derived,
3095 * remove the packet header protections.
3096 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003097 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003098 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
3099 qc_rm_hp_pkts(qel, ctx);
3100
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003101 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003102 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3103 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3104 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003105 goto err;
3106
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003107 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3108 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3109 qel = next_qel;
3110 next_qel = NULL;
3111 goto next_level;
3112 }
3113
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003114 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02003115 if (st >= QUIC_HS_ST_COMPLETE &&
3116 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003117 /* Discard the Handshake keys. */
3118 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003119 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003120 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3121 qc_set_timer(ctx);
3122 if (!quic_build_post_handshake_frames(qc))
3123 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003124
3125 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3126 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003127 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003128 }
3129
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003130 if (!qr)
3131 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01003132 ret = qc_prep_pkts(qr, ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003133 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003134 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003135 else if (ret == 0)
3136 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003137
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003138 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003139 goto err;
3140
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003141 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003142 /* Check if there is something to do for the next level.
3143 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003144 if (next_qel && next_qel != qel &&
3145 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003146 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003147 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003148 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003149 goto next_level;
3150 }
3151
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003152 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003153 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003154 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003155
3156 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003157 if (qr)
3158 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003159 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003160 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003161}
3162
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003163/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003164static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3165{
3166 int i;
3167
3168 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3169 if (qel->tx.crypto.bufs[i]) {
3170 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3171 qel->tx.crypto.bufs[i] = NULL;
3172 }
3173 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003174 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003175}
3176
3177/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003178 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003179 * Returns 1 if succeeded, 0 if not.
3180 */
3181static int quic_conn_enc_level_init(struct quic_conn *qc,
3182 enum quic_tls_enc_level level)
3183{
3184 struct quic_enc_level *qel;
3185
3186 qel = &qc->els[level];
3187 qel->level = quic_to_ssl_enc_level(level);
3188 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3189 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3190 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3191 qel->tls_ctx.rx.flags = 0;
3192 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003193 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003194
3195 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003196 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003197 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003198 qel->rx.crypto.offset = 0;
3199 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003200
3201 /* Allocate only one buffer. */
3202 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3203 if (!qel->tx.crypto.bufs)
3204 goto err;
3205
3206 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3207 if (!qel->tx.crypto.bufs[0])
3208 goto err;
3209
3210 qel->tx.crypto.bufs[0]->sz = 0;
3211 qel->tx.crypto.nb_buf = 1;
3212
3213 qel->tx.crypto.sz = 0;
3214 qel->tx.crypto.offset = 0;
3215
3216 return 1;
3217
3218 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003219 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003220 return 0;
3221}
3222
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003223/* Release all the memory allocated for <conn> QUIC connection. */
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003224static void quic_conn_free(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003225{
3226 int i;
3227
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003228 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003229 return;
3230
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003231 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003232
3233 /* remove the connection from receiver cids trees */
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003234 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3235 ebmb_delete(&qc->odcid_node);
3236 ebmb_delete(&qc->scid_node);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003237
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003238 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003239
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003240 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003241 quic_conn_enc_level_uninit(&qc->els[i]);
3242 if (qc->timer_task)
3243 task_destroy(qc->timer_task);
3244 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3245 pool_free(pool_head_quic_conn, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003246}
3247
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003248void quic_close(struct connection *conn, void *xprt_ctx)
3249{
3250 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
3251 struct quic_conn *qc = conn_ctx->conn->qc;
3252 quic_conn_free(qc);
3253}
3254
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003255/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003256static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003257{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003258 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003259 struct quic_conn *qc;
3260 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003261 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003262
3263 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003264 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003265 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003266 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003267 task->expire = TICK_ETERNITY;
3268 pktns = quic_loss_pktns(qc);
3269 if (tick_isset(pktns->tx.loss_time)) {
3270 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3271
3272 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3273 if (!LIST_ISEMPTY(&lost_pkts))
3274 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
3275 qc_set_timer(conn_ctx);
3276 goto out;
3277 }
3278
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003279 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003280 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003281 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003282 pktns->tx.pto_probe = 1;
3283 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003284 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003285 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3286 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3287
3288 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3289 hel->pktns->tx.pto_probe = 1;
3290 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3291 iel->pktns->tx.pto_probe = 1;
3292 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003293 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003294 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3295 qc->path->loss.pto_count++;
3296
3297 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003298 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003299
3300 return task;
3301}
3302
3303/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3304 * connections used to identify the first Initial packets of client connecting
3305 * to listeners. This parameter must be NULL for QUIC connections attached
3306 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3307 * <scid> is the source connection ID with <scid_len> as length.
3308 * Returns 1 if succeeded, 0 if not.
3309 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003310static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003311 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003312 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003313{
3314 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003315 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003316 /* Initial CID. */
3317 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003318 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003319 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003320
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003321 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003322 qc = pool_zalloc(pool_head_quic_conn);
3323 if (!qc) {
3324 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3325 goto err;
3326 }
3327
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003328 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3329 if (!buf_area) {
3330 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT);
3331 goto err;
3332 }
3333
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003334 qc->cids = EB_ROOT;
3335 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003336 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003337 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003338
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003339 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003340 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003341 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003342 qc->odcid.addrlen = dcid_addr_len;
3343 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003344
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003345 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003346 if (scid_len)
3347 memcpy(qc->dcid.data, scid, scid_len);
3348 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003349 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003350 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003351 }
3352 /* QUIC Client (outgoing connection to servers) */
3353 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003354 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003355 if (dcid_len)
3356 memcpy(qc->dcid.data, dcid, dcid_len);
3357 qc->dcid.len = dcid_len;
3358 }
3359
3360 /* Initialize the output buffer */
3361 qc->obuf.pos = qc->obuf.data;
3362
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003363 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003364 if (!icid) {
3365 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
3366 goto err;
3367 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003368
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003369 /* insert the allocated CID in the receiver tree */
3370 if (server) {
3371 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
3372 ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len);
3373 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3374 }
3375
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003376 /* Select our SCID which is the first CID with 0 as sequence number. */
3377 qc->scid = icid->cid;
3378
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003379 /* Packet number spaces initialization. */
3380 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3381 quic_pktns_init(&qc->pktns[i]);
3382 /* QUIC encryption level context initialization. */
3383 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003384 if (!quic_conn_enc_level_init(qc, i)) {
3385 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003386 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003387 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003388 /* Initialize the packet number space. */
3389 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3390 }
3391
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003392 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003393 qc->tps_tls_ext = qc->version & 0xff000000 ?
3394 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3395 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003396 /* TX part. */
3397 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003398 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3399 qc->tx.wbuf = qc->tx.rbuf = 0;
3400 qc->tx.bytes = 0;
3401 qc->tx.nb_pto_dgrams = 0;
3402 /* RX part. */
3403 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003404 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003405 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3406 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3407 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003408 if (!quic_tls_ku_init(qc)) {
3409 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT);
3410 goto err;
3411 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003412
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003413 /* XXX TO DO: Only one path at this time. */
3414 qc->path = &qc->paths[0];
3415 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3416
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003417 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003418
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003419 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003420
3421 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003422 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003423 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003424 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003425}
3426
3427/* Initialize the timer task of <qc> QUIC connection.
3428 * Returns 1 if succeeded, 0 if not.
3429 */
3430static int quic_conn_init_timer(struct quic_conn *qc)
3431{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003432 /* Attach this task to the same thread ID used for the connection */
3433 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003434 if (!qc->timer_task)
3435 return 0;
3436
3437 qc->timer = TICK_ETERNITY;
3438 qc->timer_task->process = process_timer;
3439 qc->timer_task->context = qc->conn->xprt_ctx;
3440
3441 return 1;
3442}
3443
3444/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3445 * past one byte of this buffer.
3446 */
3447static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3448 struct quic_rx_packet *pkt)
3449{
3450 unsigned char dcid_len, scid_len;
3451
3452 /* Version */
3453 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3454 return 0;
3455
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003456 /* Destination Connection ID Length */
3457 dcid_len = *(*buf)++;
3458 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3459 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3460 /* XXX MUST BE DROPPED */
3461 return 0;
3462
3463 if (dcid_len) {
3464 /* Check that the length of this received DCID matches the CID lengths
3465 * of our implementation for non Initials packets only.
3466 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003467 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3468 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003469 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003470 return 0;
3471
3472 memcpy(pkt->dcid.data, *buf, dcid_len);
3473 }
3474
3475 pkt->dcid.len = dcid_len;
3476 *buf += dcid_len;
3477
3478 /* Source Connection ID Length */
3479 scid_len = *(*buf)++;
3480 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3481 /* XXX MUST BE DROPPED */
3482 return 0;
3483
3484 if (scid_len)
3485 memcpy(pkt->scid.data, *buf, scid_len);
3486 pkt->scid.len = scid_len;
3487 *buf += scid_len;
3488
3489 return 1;
3490}
3491
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003492/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3493 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3494 * encryption level matching with the packet type. <*qel> may be null if not found.
3495 * Note that <ctx> may be null (for Initial packets).
3496 */
3497static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003498 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003499 struct quic_enc_level **qel)
3500{
3501 enum quic_tls_enc_level tel;
3502
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003503 /* Special case without connection context (first Initial packets) */
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003504 if (!ctx) {
3505 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3506 return 1;
3507 }
3508
3509 tel = quic_packet_type_enc_level(pkt->type);
3510 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3511 *qel = NULL;
3512 return 0;
3513 }
3514
3515 *qel = &qc->els[tel];
3516 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003517 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003518 return 0;
3519 }
3520
3521 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003522 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3523 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003524 return 1;
3525
3526 return 0;
3527}
3528
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003529/* Insert <pkt> RX packet in its <qel> RX packets tree */
3530static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3531{
3532 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003533 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003534 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3535 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3536 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003537}
3538
3539/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003540 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3541 * byte past the end of the buffer containing this packet and <beg> the address of
3542 * the packet first byte.
3543 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3544 * Returns 1 if succeeded, 0 if not.
3545 */
3546static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3547 unsigned char **buf, unsigned char *beg,
3548 const unsigned char *end,
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003549 struct quic_conn *qc, struct quic_enc_level **el,
3550 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003551{
3552 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003553 struct quic_enc_level *qel;
3554 /* Only for traces. */
3555 struct quic_rx_packet *qpkt_trace;
3556
3557 qpkt_trace = NULL;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003558 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003559 /* The packet number is here. This is also the start minus
3560 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3561 * protection.
3562 */
3563 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003564 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003565 /* Note that the following function enables us to unprotect the packet
3566 * number and its length subsequently used to decrypt the entire
3567 * packets.
3568 */
3569 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3570 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003571 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003572 goto err;
3573 }
3574
3575 /* The AAD includes the packet number field found at <pn>. */
3576 pkt->aad_len = pn - beg + pkt->pnl;
3577 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003578 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003579 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003580 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3581 /* If the packet number space has been discarded, this packet
3582 * will be not parsed.
3583 */
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003584 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003585 goto out;
3586 }
3587
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003588 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003589 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003590 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3591 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003592 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003593 else {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003594 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003595 goto err;
3596 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003597
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003598 *el = qel;
3599 /* No reference counter incrementation here!!! */
3600 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3601 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3602 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3603 b_add(&qc->rx.buf, pkt->len);
3604 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003605 /* Updtate the offset of <*buf> for the next QUIC packet. */
3606 *buf = beg + pkt->len;
3607
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003608 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003609 return 1;
3610
3611 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003612 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003613 return 0;
3614}
3615
3616/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3617 * Also set <*long_header> to 1 if this form is long, 0 if not.
3618 */
3619static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3620 unsigned char byte0, int *long_header)
3621{
3622 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3623 pkt->type =
3624 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3625 *long_header = 1;
3626 }
3627 else {
3628 pkt->type = QUIC_PACKET_TYPE_SHORT;
3629 *long_header = 0;
3630 }
3631}
3632
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003633/*
3634 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3635 */
3636static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3637{
3638 int j = 0, version;
3639
3640 do {
3641 version = quic_supported_version[j];
3642 if (version == pkt->version)
3643 return 1;
3644
3645 version = quic_supported_version[++j];
3646 } while(version);
3647
3648 return 0;
3649}
3650
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003651__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003652static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3653 struct quic_rx_packet *pkt,
3654 struct quic_dgram_ctx *dgram_ctx,
3655 struct sockaddr_storage *saddr)
3656{
3657 unsigned char *beg;
3658 uint64_t len;
3659 struct quic_conn *qc;
3660 struct eb_root *cids;
3661 struct ebmb_node *node;
3662 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003663 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003664 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003665 size_t b_cspace;
3666 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003667
3668 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003669 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003670 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3671 if (end <= *buf)
3672 goto err;
3673
3674 /* Fixed bit */
3675 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3676 /* XXX TO BE DISCARDED */
3677 goto err;
3678
3679 srv_conn = dgram_ctx->owner;
3680 beg = *buf;
3681 /* Header form */
3682 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3683 if (long_header) {
3684 size_t cid_lookup_len;
3685
3686 if (!quic_packet_read_long_header(buf, end, pkt))
3687 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003688
3689 /* unsupported QUIC version */
3690 if (!qc_pkt_is_supported_version(pkt)) {
3691 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3692 goto err;
3693 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003694
3695 /* For Initial packets, and for servers (QUIC clients connections),
3696 * there is no Initial connection IDs storage.
3697 */
3698 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3699 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3700 cid_lookup_len = pkt->dcid.len;
3701 }
3702 else {
3703 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003704 cid_lookup_len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003705 }
3706
3707 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3708 if (!node)
3709 goto err;
3710
3711 qc = ebmb_entry(node, struct quic_conn, scid_node);
3712
3713 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3714 qc->dcid.len = pkt->scid.len;
3715 if (pkt->scid.len)
3716 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3717 }
3718
3719 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3720 uint64_t token_len;
3721
3722 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3723 goto err;
3724
3725 /* XXX TO DO XXX 0 value means "the token is not present".
3726 * A server which sends an Initial packet must not set the token.
3727 * So, a client which receives an Initial packet with a token
3728 * MUST discard the packet or generate a connection error with
3729 * PROTOCOL_VIOLATION as type.
3730 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3731 */
3732 pkt->token_len = token_len;
3733 }
3734 }
3735 else {
3736 /* XXX TO DO: Short header XXX */
Amaury Denoyelled4962512021-12-14 17:17:28 +01003737 if (end - *buf < QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003738 goto err;
3739
3740 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003741 node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003742 if (!node)
3743 goto err;
3744
3745 qc = ebmb_entry(node, struct quic_conn, scid_node);
Amaury Denoyelled4962512021-12-14 17:17:28 +01003746 *buf += QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003747 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003748
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003749 /* Only packets packets with long headers and not RETRY or VERSION as type
3750 * have a length field.
3751 */
3752 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3753 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3754 goto err;
3755
3756 pkt->len = len;
3757 }
3758 else if (!long_header) {
3759 /* A short packet is the last one of an UDP datagram. */
3760 pkt->len = end - *buf;
3761 }
3762
3763 conn_ctx = qc->conn->xprt_ctx;
3764
3765 /* Increase the total length of this packet by the header length. */
3766 pkt->len += *buf - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003767
3768 /* When multiple QUIC packets are coalesced on the same UDP datagram,
3769 * they must have the same DCID.
3770 *
3771 * This check must be done after the final update to pkt.len to
3772 * properly drop the packet on failure.
3773 */
3774 if (!dgram_ctx->dcid.len) {
3775 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
3776 }
3777 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003778 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003779 goto err;
3780 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003781 dgram_ctx->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003782
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003783 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3784 b_cspace = b_contig_space(&qc->rx.buf);
3785 if (b_cspace < pkt->len) {
3786 /* Let us consume the remaining contiguous space. */
3787 b_add(&qc->rx.buf, b_cspace);
3788 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3789 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003790 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003791 goto err;
3792 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003793 }
3794
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003795 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) {
3796 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003797 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003798 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003799 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003800
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003801 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3802 if (pkt->aad_len)
3803 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003804 /* Wake the tasklet of the QUIC connection packet handler. */
3805 if (conn_ctx)
3806 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3807
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003808 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003809
3810 return pkt->len;
3811
3812 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003813 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003814 return -1;
3815}
3816
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003817/*
3818 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3819 * address <addr>.
3820 * Implementation of RFC9000 6. Version Negotiation
3821 *
3822 * TODO implement a rate-limiting sending of Version Negotiation packets
3823 *
3824 * Returns 0 on success else non-zero
3825 */
3826static int qc_send_version_negotiation(int fd, struct sockaddr_storage *addr,
3827 struct quic_rx_packet *pkt)
3828{
3829 char buf[256];
3830 int i = 0, j, version;
3831 const socklen_t addrlen = get_addr_len(addr);
3832
3833 /*
3834 * header form
3835 * long header, fixed bit to 0 for Version Negotiation
3836 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003837 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3838 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003839
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003840 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003841 /* null version for Version Negotiation */
3842 buf[i++] = '\x00';
3843 buf[i++] = '\x00';
3844 buf[i++] = '\x00';
3845 buf[i++] = '\x00';
3846
3847 /* source connection id */
3848 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003849 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003850 i += pkt->scid.len;
3851
3852 /* destination connection id */
3853 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003854 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003855 i += pkt->dcid.len;
3856
3857 /* supported version */
3858 j = 0;
3859 do {
3860 version = htonl(quic_supported_version[j]);
3861 memcpy(&buf[i], &version, sizeof(version));
3862 i += sizeof(version);
3863
3864 version = quic_supported_version[++j];
3865 } while (version);
3866
3867 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3868 return 1;
3869
3870 return 0;
3871}
3872
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003873/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
3874 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
3875 * concatenated to the <pkt> DCID field.
3876 *
3877 * Returns the instance or NULL if not found.
3878 */
3879static struct quic_conn *qc_retrieve_conn_from_cid(struct quic_rx_packet *pkt,
3880 struct listener *l,
3881 struct sockaddr_storage *saddr)
3882{
3883 struct quic_conn *qc = NULL;
3884 struct ebmb_node *node;
3885 struct quic_connection_id *id;
3886
3887 HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock);
3888
3889 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
3890 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
3891 pkt->type == QUIC_PACKET_TYPE_0RTT) {
3892 /* DCIDs of first packets coming from multiple clients may have
3893 * the same values. Let's distinguish them by concatenating the
3894 * socket addresses.
3895 */
3896 quic_cid_saddr_cat(&pkt->dcid, saddr);
3897 node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data,
3898 pkt->dcid.len + pkt->dcid.addrlen);
3899 if (node) {
3900 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3901 goto end;
3902 }
3903 }
3904
3905 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
3906 * also for INITIAL/0-RTT non-first packets with the final DCID in
3907 * used.
3908 */
3909 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len);
3910 if (!node)
3911 goto end;
3912
3913 id = ebmb_entry(node, struct quic_connection_id, node);
3914 qc = id->qc;
3915
3916 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
3917 * If already done, this is a noop.
3918 */
Amaury Denoyelledbef9852021-12-16 16:15:18 +01003919 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003920
3921 end:
3922 HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3923
3924 return qc;
3925}
3926
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003927static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3928 struct quic_rx_packet *pkt,
3929 struct quic_dgram_ctx *dgram_ctx,
3930 struct sockaddr_storage *saddr)
3931{
3932 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003933 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003934 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003935 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003936 int long_header = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003937 size_t b_cspace;
3938 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003939
3940 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003941 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003942 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01003943 TRACE_ENTER(QUIC_EV_CONN_LPKT);
3944 /* This ist only to please to traces and distinguish the
3945 * packet with parsed packet number from others.
3946 */
3947 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003948 if (end <= *buf)
3949 goto err;
3950
3951 /* Fixed bit */
3952 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3953 /* XXX TO BE DISCARDED */
3954 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3955 goto err;
3956 }
3957
3958 l = dgram_ctx->owner;
3959 beg = *buf;
3960 /* Header form */
3961 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3962 if (long_header) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003963 if (!quic_packet_read_long_header(buf, end, pkt)) {
3964 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3965 goto err;
3966 }
3967
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003968 /* RFC9000 6. Version Negotiation */
3969 if (!qc_pkt_is_supported_version(pkt)) {
3970 /* do not send Version Negotiation in response to a
3971 * Version Negotiation packet.
3972 */
3973 if (!pkt->version) {
3974 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3975 goto err;
3976 }
3977
3978 /* unsupported version, send Negotiation packet */
3979 if (qc_send_version_negotiation(l->rx.fd, saddr, pkt)) {
3980 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
3981 goto err;
3982 }
3983
3984 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
3985 goto out;
3986 }
3987
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003988 /* For Initial packets, and for servers (QUIC clients connections),
3989 * there is no Initial connection IDs storage.
3990 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003991 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003992 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003993
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003994 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3995 end - *buf < token_len) {
3996 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3997 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003998 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003999
4000 /* XXX TO DO XXX 0 value means "the token is not present".
4001 * A server which sends an Initial packet must not set the token.
4002 * So, a client which receives an Initial packet with a token
4003 * MUST discard the packet or generate a connection error with
4004 * PROTOCOL_VIOLATION as type.
4005 * The token must be provided in a Retry packet or NEW_TOKEN frame.
4006 */
4007 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004008 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004009 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004010 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004011 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4012 goto err;
4013 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004014 }
4015
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004016 /* Only packets packets with long headers and not RETRY or VERSION as type
4017 * have a length field.
4018 */
4019 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
4020 uint64_t len;
4021
4022 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
4023 end - *buf < len) {
4024 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4025 goto err;
4026 }
4027
4028 pkt->len = len;
4029 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004030
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004031 qc = qc_retrieve_conn_from_cid(pkt, l, saddr);
4032 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004033 int ipv4;
4034 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004035 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004036 const unsigned char *salt = initial_salt_v1;
4037 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004038
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004039 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004040 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004041 goto err;
4042 }
4043
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004044 pkt->saddr = *saddr;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004045 ipv4 = saddr->ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004046 qc = qc_new_conn(pkt->version, ipv4,
4047 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004048 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004049 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004050 goto err;
4051
4052 odcid = &qc->rx.params.original_destination_connection_id;
4053 /* Copy the transport parameters. */
4054 qc->rx.params = l->bind_conf->quic_params;
4055 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004056 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4057 odcid->len = pkt->dcid.len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004058 /* Copy the initial source connection ID. */
4059 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4060 qc->enc_params_len =
4061 quic_transport_params_encode(qc->enc_params,
4062 qc->enc_params + sizeof qc->enc_params,
4063 &qc->rx.params, 1);
4064 if (!qc->enc_params_len)
4065 goto err;
4066
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004067 /* NOTE: the socket address has been concatenated to the destination ID
4068 * chosen by the client for Initial packets.
4069 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004070 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4071 salt = initial_salt_draft_29;
4072 salt_len = sizeof initial_salt_draft_29;
4073 }
4074 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004075 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004076 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004077 goto err;
4078 }
4079
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004080 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004081 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004082 n = ebmb_insert(&l->rx.odcids, &qc->odcid_node,
4083 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004084 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004085
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004086 /* If the insertion failed, it means that another
4087 * thread has already allocated a QUIC connection for
4088 * the same CID. Liberate our allocated connection.
4089 */
4090 if (unlikely(n != &qc->odcid_node)) {
4091 quic_conn_free(qc);
4092 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4093 pkt->qc = qc;
4094 }
4095 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004096 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004097 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004098 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
4099 /* Try to accept a new connection. */
4100 listener_accept(l);
4101 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004102 }
4103 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004104 pkt->qc = qc;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01004105 if (HA_ATOMIC_LOAD(&qc->conn))
4106 conn_ctx = HA_ATOMIC_LOAD(&qc->conn->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004107 }
4108 }
4109 else {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004110 if (end - *buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004111 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4112 goto err;
4113 }
4114
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004115 memcpy(pkt->dcid.data, *buf, QUIC_HAP_CID_LEN);
4116 pkt->dcid.len = QUIC_HAP_CID_LEN;
4117 *buf += QUIC_HAP_CID_LEN;
4118
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004119 qc = qc_retrieve_conn_from_cid(pkt, l, saddr);
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01004120 if (HA_ATOMIC_LOAD(&qc->conn))
4121 conn_ctx = HA_ATOMIC_LOAD(&qc->conn->xprt_ctx);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004122
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004123 pkt->qc = qc;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004124 /* A short packet is the last one of an UDP datagram. */
4125 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004126 }
4127
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004128 /* Increase the total length of this packet by the header length. */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004129 pkt->raw_len = pkt->len += *buf - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004130
4131 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4132 * they must have the same DCID.
4133 *
4134 * This check must be done after the final update to pkt.len to
4135 * properly drop the packet on failure.
4136 */
4137 if (!dgram_ctx->dcid.len) {
4138 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
4139 }
4140 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004141 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004142 goto err;
4143 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004144 dgram_ctx->qc = qc;
4145
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004146
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004147 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004148 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004149 goto out;
4150 }
4151
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004152 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004153 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004154 b_cspace = b_contig_space(&qc->rx.buf);
4155 if (b_cspace < pkt->len) {
4156 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004157 if (b_cspace) {
4158 b_putchr(&qc->rx.buf, 0x00);
4159 b_cspace--;
4160 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004161 b_add(&qc->rx.buf, b_cspace);
4162 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4163 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004164 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004165 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004166 goto err;
4167 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004168 }
4169
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004170 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) {
4171 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004172 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004173 goto err;
4174 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004175
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004176 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004177 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004178 if (pkt->aad_len)
4179 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004180 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004181 /* Wake up the connection packet handler task from here only if all
4182 * the contexts have been initialized, especially the mux context
4183 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4184 * will start it if these contexts for the connection are not already
4185 * initialized.
4186 */
4187 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004188 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004189
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004190 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004191
4192 return pkt->len;
4193
4194 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004195 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004196 qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004197 return -1;
4198}
4199
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004200/* This function builds into <buf> buffer a QUIC long packet header.
4201 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004202 */
4203static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4204 int type, size_t pn_len, struct quic_conn *conn)
4205{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004206 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004207 return 0;
4208
4209 /* #0 byte flags */
4210 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4211 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4212 /* Version */
4213 quic_write_uint32(buf, end, conn->version);
4214 *(*buf)++ = conn->dcid.len;
4215 /* Destination connection ID */
4216 if (conn->dcid.len) {
4217 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4218 *buf += conn->dcid.len;
4219 }
4220 /* Source connection ID */
4221 *(*buf)++ = conn->scid.len;
4222 if (conn->scid.len) {
4223 memcpy(*buf, conn->scid.data, conn->scid.len);
4224 *buf += conn->scid.len;
4225 }
4226
4227 return 1;
4228}
4229
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004230/* This function builds into <buf> buffer a QUIC short packet header.
4231 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004232 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004233static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4234 size_t pn_len, struct quic_conn *conn,
4235 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004236{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004237 if (end - *buf < 1 + conn->dcid.len)
4238 return 0;
4239
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004240 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004241 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4242 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004243 /* Destination connection ID */
4244 if (conn->dcid.len) {
4245 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4246 *buf += conn->dcid.len;
4247 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004248
4249 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004250}
4251
4252/* Apply QUIC header protection to the packet with <buf> as first byte address,
4253 * <pn> as address of the Packet number field, <pnlen> being this field length
4254 * with <aead> as AEAD cipher and <key> as secret key.
4255 * Returns 1 if succeeded or 0 if failed.
4256 */
4257static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4258 const EVP_CIPHER *aead, const unsigned char *key)
4259{
4260 int i, ret, outlen;
4261 EVP_CIPHER_CTX *ctx;
4262 /* We need an IV of at least 5 bytes: one byte for bytes #0
4263 * and at most 4 bytes for the packet number
4264 */
4265 unsigned char mask[5] = {0};
4266
4267 ret = 0;
4268 ctx = EVP_CIPHER_CTX_new();
4269 if (!ctx)
4270 return 0;
4271
4272 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4273 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4274 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4275 goto out;
4276
4277 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4278 for (i = 0; i < pnlen; i++)
4279 pn[i] ^= mask[i + 1];
4280
4281 ret = 1;
4282
4283 out:
4284 EVP_CIPHER_CTX_free(ctx);
4285
4286 return ret;
4287}
4288
4289/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4290 * ACK ranges if needed to a value below <limit> in bytes.
4291 * Return 1 if succeeded, 0 if not.
4292 */
4293static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4294{
4295 size_t room, ack_delay_sz;
4296
4297 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4298 /* A frame is made of 1 byte for the frame type. */
4299 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004300 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004301 return 0;
4302
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004303 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004304}
4305
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004306/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4307 * 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 +01004308 * and <*len> the packet Length field initialized with the number of bytes already present
4309 * 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 +02004310 * <headlen> is the number of bytes already present in this packet before building frames.
4311 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004312 * Update consequently <*len> to reflect the size of these frames built
4313 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004314 * Return 1 if succeeded, 0 if not.
4315 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004316static inline int qc_build_frms(struct quic_tx_packet *pkt,
4317 size_t room, size_t *len, size_t headlen,
4318 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004319 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004320{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004321 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004322 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004323 struct mt_list *tmp1, tmp2;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004324 size_t remain = quic_path_prep_data(qc->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004325
Frédéric Lécailleea604992020-12-24 13:01:37 +01004326 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004327 if (*len > room || headlen > remain)
4328 return 0;
4329
Frédéric Lécailleea604992020-12-24 13:01:37 +01004330 /* If we are not probing we must take into an account the congestion
4331 * control window.
4332 */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004333 if (!qc->tx.nb_pto_dgrams)
4334 room = QUIC_MIN(room, quic_path_prep_data(qc->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004335 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004336 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004337 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 +01004338 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004339 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004340
Frédéric Lécailleea604992020-12-24 13:01:37 +01004341 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004342 break;
4343
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004344 switch (cf->type) {
4345 case QUIC_FT_CRYPTO:
4346 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004347 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004348 /* Compute the length of this CRYPTO frame header */
4349 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4350 /* Compute the data length of this CRyPTO frame. */
4351 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4352 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004353 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004354 if (!dlen)
4355 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004356
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004357 /* CRYPTO frame length. */
4358 flen = hlen + quic_int_getsize(dlen) + dlen;
4359 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004360 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004361 /* Add the CRYPTO data length and its encoded length to the packet
4362 * length and the length of this length.
4363 */
4364 *len += flen;
4365 room -= flen;
4366 if (dlen == cf->crypto.len) {
4367 /* <cf> CRYPTO data have been consumed. */
4368 MT_LIST_DELETE_SAFE(tmp1);
4369 LIST_APPEND(&pkt->frms, &cf->list);
4370 }
4371 else {
4372 struct quic_frame *new_cf;
4373
4374 new_cf = pool_alloc(pool_head_quic_frame);
4375 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004376 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004377 return 0;
4378 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004379
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004380 new_cf->type = QUIC_FT_CRYPTO;
4381 new_cf->crypto.len = dlen;
4382 new_cf->crypto.offset = cf->crypto.offset;
4383 new_cf->crypto.qel = qel;
4384 LIST_APPEND(&pkt->frms, &new_cf->list);
4385 /* Consume <dlen> bytes of the current frame. */
4386 cf->crypto.len -= dlen;
4387 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004388 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004389 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004390
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004391 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004392 /* Note that these frames are accepted in short packets only without
4393 * "Length" packet field. Here, <*len> is used only to compute the
4394 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004395 *
4396 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004397 * excepting the variable ones. Note that +1 is for the type of this frame.
4398 */
4399 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004400 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004401 /* Compute the data length of this STREAM frame. */
4402 avail_room = room - hlen - *len;
4403 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004404 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004405
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004406 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004407 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004408 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4409 dlen = max_available_room(avail_room, &dlen_sz);
4410 if (dlen > cf->stream.len) {
4411 dlen = cf->stream.len;
4412 }
4413 dlen_sz = quic_int_getsize(dlen);
4414 flen = hlen + dlen_sz + dlen;
4415 }
4416 else {
4417 dlen = QUIC_MIN(avail_room, cf->stream.len);
4418 flen = hlen + dlen;
4419 }
4420 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004421 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004422 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004423 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004424 /* Add the STREAM data length and its encoded length to the packet
4425 * length and the length of this length.
4426 */
4427 *len += flen;
4428 room -= flen;
4429 if (dlen == cf->stream.len) {
4430 /* <cf> STREAM data have been consumed. */
4431 MT_LIST_DELETE_SAFE(tmp1);
4432 LIST_APPEND(&pkt->frms, &cf->list);
4433 }
4434 else {
4435 struct quic_frame *new_cf;
4436
4437 new_cf = pool_zalloc(pool_head_quic_frame);
4438 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004439 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004440 return 0;
4441 }
4442
4443 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004444 new_cf->stream.qcs = cf->stream.qcs;
4445 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004446 new_cf->stream.id = cf->stream.id;
4447 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4448 new_cf->stream.offset = cf->stream.offset;
4449 new_cf->stream.len = dlen;
4450 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4451 /* FIN bit reset */
4452 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4453 new_cf->stream.data = cf->stream.data;
4454 LIST_APPEND(&pkt->frms, &new_cf->list);
4455 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4456 /* Consume <dlen> bytes of the current frame. */
4457 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004458 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004459 cf->stream.data += dlen;
4460 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004461 break;
4462
4463 default:
4464 flen = qc_frm_len(cf);
4465 BUG_ON(!flen);
4466 if (flen > room)
4467 continue;
4468
4469 *len += flen;
4470 room -= flen;
4471 MT_LIST_DELETE_SAFE(tmp1);
4472 LIST_APPEND(&pkt->frms, &cf->list);
4473 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004474 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004475 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004476 }
4477
Frédéric Lécailleea604992020-12-24 13:01:37 +01004478 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004479}
4480
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004481/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004482 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4483 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4484 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004485 * 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 +02004486 * 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 +02004487 * having returned from this function.
4488 * 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 +01004489 * number field in this packet. <pn_len> will also have the packet number
4490 * length as value.
4491 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004492 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004493 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004494static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4495 size_t dglen, struct quic_tx_packet *pkt,
4496 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
4497 int ack, int padding, int cc, int nb_pto_dgrams,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004498 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004499{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004500 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004501 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004502 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4503 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004504 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004505 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004506 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004507 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004508
Frédéric Lécailleea604992020-12-24 13:01:37 +01004509 /* Length field value with CRYPTO frames if present. */
4510 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004511 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004512 /* When not probing and not acking, and no immediate close is required,
4513 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004514 * the congestion controller window. So, we do not limit the size of this
4515 * packet if we have an ACK frame to send because an ACK frame is not
4516 * ack-eliciting. This size will be limited if we have ack-eliciting
4517 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004518 */
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004519 if (!nb_pto_dgrams && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004520 size_t path_room;
4521
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004522 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004523 if (end - beg > path_room)
4524 end = beg + path_room;
4525 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004526
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004527 /* Ensure there is enough room for the TLS encryption tag and a zero token
4528 * length field if any.
4529 */
4530 if (end - pos < QUIC_TLS_TAG_LEN +
4531 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4532 goto no_room;
4533
4534 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004535 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004536 /* packet number length */
4537 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004538 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004539 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004540 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004541 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004542 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004543 goto no_room;
4544
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004545 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4546 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004547 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004548 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004549 /* Build an ACK frame if required. */
4550 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004551 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004552 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004553 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004554 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4555 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4556 * that from here, we do not know if we will have to send a PING frame.
4557 * This will be decided after having computed the ack-eliciting frames
4558 * to be added to this packet.
4559 */
4560 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - 1 - *pn_len - pos);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004561 if (!ack_frm_len)
4562 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004563 }
4564
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004565 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004566 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004567 len_frms = 0;
4568 if (!cc && !MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004569 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004570
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004571 /* Initialize the length of the frames built below to <len>.
4572 * If any frame could be successfully built by qc_build_frms(),
4573 * we will have len_frms > len.
4574 */
4575 len_frms = len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004576 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004577 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004578 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004579 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004580 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004581
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004582 /* Length (of the remaining data). Must not fail because, the buffer size
4583 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4584 * for the encryption tag. It must be taken into an account for the length
4585 * of this packet.
4586 */
4587 if (len_frms)
4588 len = len_frms + QUIC_TLS_TAG_LEN;
4589 else
4590 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004591 /* CONNECTION_CLOSE frame */
4592 if (cc) {
4593 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004594
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004595 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004596 len += qc_frm_len(&cc_frm);
4597 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004598 add_ping_frm = 0;
4599 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004600 len_sz = quic_int_getsize(len);
4601 /* Add this packet size to <dglen> */
4602 dglen += head_len + len_sz + len;
4603 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4604 /* This is a maximum padding size */
4605 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4606 /* The length field value is of this packet is <len> + <padding_len>
4607 * the size of which may be greater than the initial computed size
4608 * <len_sz>. So, let's deduce the difference betwen these to packet
4609 * sizes from <padding_len>.
4610 */
4611 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4612 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004613 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004614 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004615 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004616 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004617 add_ping_frm = 1;
4618 len += 1;
4619 }
4620 /* If there is no frame at all to follow, add at least a PADDING frame. */
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004621 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004622 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4623 }
4624
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004625 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4626 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004627
4628 /* Packet number field address. */
4629 *buf_pn = pos;
4630
4631 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004632 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4633 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004634
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004635 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004636 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004637
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004638 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004639 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004640 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004641
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004642 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004643 list_for_each_entry(cf, &pkt->frms, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004644 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004645 ssize_t room = end - pos;
4646 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004647 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004648 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004649 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004650 }
4651 }
4652
4653 /* Build a PING frame if needed. */
4654 if (add_ping_frm) {
4655 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004656 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004657 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004658 }
4659
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004660 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004661 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004662 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004663
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004664 /* Build a PADDING frame if needed. */
4665 if (padding_len) {
4666 frm.type = QUIC_FT_PADDING;
4667 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004668 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004669 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004670 }
4671
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004672 /* Always reset this variable as this function has no idea
4673 * if it was set. It is handle by the loss detection timer.
4674 */
4675 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004676 pkt->len = pos - beg;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004677
4678 return 1;
4679
4680 no_room:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004681 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004682 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004683}
4684
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004685static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004686{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004687 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004688 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004689 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01004690 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004691 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004692 pkt->next = NULL;
4693 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004694}
4695
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004696/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004697static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4698{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004699 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004700
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004701 if (!pkt)
4702 return;
4703
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004704 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004705 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004706 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004707 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004708 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004709}
4710
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004711/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4712 * type for <qc> QUIC connection from <qel> encryption level.
4713 * Return -2 if the packet could not be allocated or encrypted for any reason,
4714 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004715 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004716static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4717 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004718 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004719 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004720 int pkt_type, int ack, int nb_pto_dgrams, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004721{
4722 /* The pointer to the packet number field. */
4723 unsigned char *buf_pn;
4724 unsigned char *beg, *end, *payload;
4725 int64_t pn;
4726 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004727 struct quic_tls_ctx *tls_ctx;
4728 struct quic_tx_packet *pkt;
4729
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004730 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004731 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004732 pkt = pool_alloc(pool_head_quic_tx_packet);
4733 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004734 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004735 *err = -2;
4736 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004737 }
4738
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004739 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004740 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004741 pn_len = 0;
4742 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004743
4744 pn = qel->pktns->tx.next_pn + 1;
4745 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
4746 ack, padding, cc, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004747 *err = -1;
4748 goto err;
4749 }
4750
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004751 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004752 payload = buf_pn + pn_len;
4753 payload_len = end - payload;
4754 aad_len = payload - beg;
4755
4756 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004757 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4758 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004759 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004760 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004761
4762 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004763 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004764 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4765 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004766 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004767 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004768 goto err;
4769 }
4770
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004771 /* Consume a packet number */
4772 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01004773 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004774 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4775 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004776 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004777 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004778 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004779 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004780 pkt->in_flight_len = pkt->len;
4781 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004782 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004783 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004784 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004785
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004786 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004787
4788 err:
4789 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004790 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004791 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004792}
4793
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004794/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4795 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004796 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004797static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4798 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004799{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004800 size_t try, done = 0;
4801
4802 if (!conn_ctrl_ready(conn))
4803 return 0;
4804
4805 if (!fd_recv_ready(conn->handle.fd))
4806 return 0;
4807
4808 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004809
4810 /* read the largest possible block. For this, we perform only one call
4811 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004812 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004813 */
4814 while (count > 0) {
4815 try = b_contig_space(buf);
4816 if (!try)
4817 break;
4818
4819 if (try > count)
4820 try = count;
4821
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004822 b_add(buf, try);
4823 done += try;
4824 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004825 }
4826
4827 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4828 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4829
4830 leave:
4831 return done;
4832
4833 read0:
4834 conn_sock_read0(conn);
4835 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4836
4837 /* Now a final check for a possible asynchronous low-level error
4838 * report. This can happen when a connection receives a reset
4839 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4840 * we might have come from there by just checking POLL_HUP instead
4841 * of recv()'s return value 0, so we have no way to tell there was
4842 * an error without checking.
4843 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004844 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004845 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4846 goto leave;
4847}
4848
4849
4850/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4851 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4852 * other pending data for example, but this flag is ignored at the moment.
4853 * Only one call to send() is performed, unless the buffer wraps, in which case
4854 * a second call may be performed. The connection's flags are updated with
4855 * whatever special event is detected (error, empty). The caller is responsible
4856 * for taking care of those events and avoiding the call if inappropriate. The
4857 * function does not call the connection's polling update function, so the caller
4858 * is responsible for this. It's up to the caller to update the buffer's contents
4859 * based on the return value.
4860 */
4861static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4862{
4863 ssize_t ret;
4864 size_t try, done;
4865 int send_flag;
4866
4867 if (!conn_ctrl_ready(conn))
4868 return 0;
4869
4870 if (!fd_send_ready(conn->handle.fd))
4871 return 0;
4872
4873 done = 0;
4874 /* send the largest possible block. For this we perform only one call
4875 * to send() unless the buffer wraps and we exactly fill the first hunk,
4876 * in which case we accept to do it once again.
4877 */
4878 while (count) {
4879 try = b_contig_data(buf, done);
4880 if (try > count)
4881 try = count;
4882
4883 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4884 if (try < count || flags & CO_SFL_MSG_MORE)
4885 send_flag |= MSG_MORE;
4886
4887 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4888 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4889 if (ret > 0) {
4890 count -= ret;
4891 done += ret;
4892
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004893 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004894 conn->flags |= CO_FL_WAIT_L4L6;
4895 /* if the system buffer is full, don't insist */
4896 if (ret < try)
4897 break;
4898 }
4899 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4900 /* nothing written, we need to poll for write first */
4901 fd_cant_send(conn->handle.fd);
4902 break;
4903 }
4904 else if (errno != EINTR) {
4905 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4906 break;
4907 }
4908 }
4909 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4910 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4911
4912 if (done > 0) {
4913 /* we count the total bytes sent, and the send rate for 32-byte
4914 * blocks. The reason for the latter is that freq_ctr are
4915 * limited to 4GB and that it's not enough per second.
4916 */
4917 _HA_ATOMIC_ADD(&global.out_bytes, done);
4918 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4919 }
4920 return done;
4921}
4922
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004923/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4924 * event subscriber <es> is not allowed to change from a previous call as long
4925 * as at least one event is still subscribed. The <event_type> must only be a
4926 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4927 */
4928static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4929{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004930 struct qcc *qcc = conn->qc->qcc;
4931
4932 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4933 BUG_ON(qcc->subs && qcc->subs != es);
4934
4935 es->events |= event_type;
4936 qcc->subs = es;
4937
4938 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004939 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004940
4941 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004942 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004943
4944 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004945}
4946
4947/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4948 * The <es> pointer is not allowed to differ from the one passed to the
4949 * subscribe() call. It always returns zero.
4950 */
4951static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4952{
4953 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4954}
4955
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004956/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4957 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4958 * parameters of this session.
4959 * This is the responsibility of the caller to check the validity of all the
4960 * pointers passed as parameter to this function.
4961 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4962 * CO_ER_SSL_NO_MEM.
4963 */
4964static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4965 unsigned char *params, size_t params_len)
4966{
4967 int retry;
4968
4969 retry = 1;
4970 retry:
4971 *ssl = SSL_new(ssl_ctx);
4972 if (!*ssl) {
4973 if (!retry--)
4974 goto err;
4975
4976 pool_gc(NULL);
4977 goto retry;
4978 }
4979
4980 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4981 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
4982 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
4983 goto err;
4984
4985 SSL_free(*ssl);
4986 *ssl = NULL;
4987 if (!retry--)
4988 goto err;
4989
4990 pool_gc(NULL);
4991 goto retry;
4992 }
4993
4994 return 0;
4995
4996 err:
4997 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4998 return -1;
4999}
5000
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005001/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
5002 * connection with <xprt_ctx> as address of the xprt context.
5003 * Returns 1 if succeeded, 0 if not.
5004 */
5005static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5006{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005007 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005008
5009 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5010
5011 if (*xprt_ctx)
5012 goto out;
5013
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005014 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005015 if (!ctx) {
5016 conn->err_code = CO_ER_SYS_MEMLIM;
5017 goto err;
5018 }
5019
5020 ctx->wait_event.tasklet = tasklet_new();
5021 if (!ctx->wait_event.tasklet) {
5022 conn->err_code = CO_ER_SYS_MEMLIM;
5023 goto err;
5024 }
5025
5026 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5027 ctx->wait_event.tasklet->context = ctx;
5028 ctx->wait_event.events = 0;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005029 HA_ATOMIC_STORE(&ctx->conn, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005030 ctx->subs = NULL;
5031 ctx->xprt_ctx = NULL;
5032
5033 ctx->xprt = xprt_get(XPRT_QUIC);
5034 if (objt_server(conn->target)) {
5035 /* Server */
5036 struct server *srv = __objt_server(conn->target);
Amaury Denoyelled4962512021-12-14 17:17:28 +01005037 unsigned char dcid[QUIC_HAP_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005038 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005039 int ssl_err, ipv4;
5040
5041 ssl_err = SSL_ERROR_NONE;
5042 if (RAND_bytes(dcid, sizeof dcid) != 1)
5043 goto err;
5044
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005045 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005046 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005047 dcid, sizeof dcid, 0, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005048 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005049 goto err;
5050
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005051 ctx->qc = qc;
5052
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005053 /* Insert our SCID, the connection ID for the QUIC client. */
5054 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
5055
5056 conn->qc = qc;
5057 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005058 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
5059 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005060 goto err;
5061
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005062 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005063 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005064 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
5065 qc->enc_params_len =
5066 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
5067 &qc->rx.params, 0);
5068 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005069 goto err;
5070
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005071 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
5072 qc->enc_params, qc->enc_params_len) == -1)
5073 goto err;
5074
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005075 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005076 SSL_set_connect_state(ctx->ssl);
5077 ssl_err = SSL_do_handshake(ctx->ssl);
5078 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005079 int st;
5080
5081 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005082 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
5083 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005084 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005085 }
5086 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005087 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005088 goto err;
5089 }
5090 }
5091 }
5092 else if (objt_listener(conn->target)) {
5093 /* Listener */
5094 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02005095 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005096
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005097 ctx->qc = qc;
5098
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01005099 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005100 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5101 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005102 goto err;
5103
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01005104 /* Enabling 0-RTT */
5105 if (bc->ssl_conf.early_data)
5106 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5107
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005108 SSL_set_accept_state(ctx->ssl);
5109 }
5110
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005111 HA_ATOMIC_STORE(xprt_ctx, ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005112
5113 /* Leave init state and start handshake */
5114 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005115
5116 out:
5117 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
5118
5119 return 0;
5120
5121 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02005122 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005123 tasklet_free(ctx->wait_event.tasklet);
5124 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005125 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005126 return -1;
5127}
5128
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005129/* Start the QUIC transport layer */
5130static int qc_xprt_start(struct connection *conn, void *ctx)
5131{
5132 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005133 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005134
5135 qc = conn->qc;
5136 if (!quic_conn_init_timer(qc)) {
5137 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
5138 return 0;
5139 }
5140
5141 tasklet_wakeup(qctx->wait_event.tasklet);
5142 return 1;
5143}
5144
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005145/* transport-layer operations for QUIC connections. */
5146static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005147 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005148 .snd_buf = quic_conn_from_buf,
5149 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005150 .subscribe = quic_conn_subscribe,
5151 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005152 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005153 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005154 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5155 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005156 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005157 .name = "QUIC",
5158};
5159
5160__attribute__((constructor))
5161static void __quic_conn_init(void)
5162{
5163 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5164 xprt_register(XPRT_QUIC, &ssl_quic);
5165}
5166
5167__attribute__((destructor))
5168static void __quic_conn_deinit(void)
5169{
5170 BIO_meth_free(ha_quic_meth);
5171}
5172
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005173/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5174 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005175 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005176 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005177static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005178 struct sockaddr_storage *saddr, qpkt_read_func *func)
5179{
5180 unsigned char *pos;
5181 const unsigned char *end;
5182 struct quic_dgram_ctx dgram_ctx = {
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005183 .qc = NULL,
5184 .dcid.len = 0,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005185 .owner = owner,
5186 };
5187
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005188 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005189 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005190 do {
5191 int ret;
5192 struct quic_rx_packet *pkt;
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005193 size_t pkt_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005194
Willy Tarreaue4498932021-03-22 21:13:05 +01005195 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005196 if (!pkt)
5197 goto err;
5198
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005199 quic_rx_packet_refinc(pkt);
5200 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005201 pkt_len = pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02005202 quic_rx_packet_refdec(pkt);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005203 if (ret == -1 && !pkt_len)
5204 /* If the packet length could not be found, we cannot continue. */
5205 break;
5206
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005207 } while (pos < end);
5208
5209 /* Increasing the received bytes counter by the UDP datagram length
5210 * if this datagram could be associated to a connection.
5211 */
5212 if (dgram_ctx.qc)
5213 dgram_ctx.qc->rx.bytes += len;
5214
5215 return pos - (unsigned char *)buf;
5216
5217 err:
5218 return -1;
5219}
5220
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005221ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005222 struct sockaddr_storage *saddr)
5223{
5224 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
5225}
5226
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005227/* Function to automatically activate QUIC traces on stdout.
5228 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5229 * Main use for now is in the docker image for QUIC interop testing.
5230 */
5231static void quic_init_stdout_traces(void)
5232{
5233#ifdef ENABLE_QUIC_STDOUT_TRACES
5234 trace_quic.sink = sink_find("stdout");
5235 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005236 trace_quic.state = TRACE_STATE_RUNNING;
5237#endif
5238}
5239INITCALL0(STG_INIT, quic_init_stdout_traces);
5240
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005241/*
5242 * Local variables:
5243 * c-indent-level: 8
5244 * c-basic-offset: 8
5245 * End:
5246 */