blob: 8c4b38c9c07ff5e1717c6263312b80b59f0c63b1 [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);
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001516 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->qc, NULL, &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,
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001727 ctx->qc, NULL, &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,
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001733 ctx->qc, NULL, &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,
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001757 ctx->qc, NULL, &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,
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001771 ctx->qc, NULL, &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,
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001781 ctx->qc, NULL, &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,
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001791 ctx->qc, NULL, &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",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001873 QUIC_EV_CONN_SSLDATA, qc, NULL, 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;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002049 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002050 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écaille10250b22021-12-22 16:13:43 +01002059 if (strm_frm->offset.key < strm->rx.offset) {
2060 size_t diff;
2061
2062 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2063 TRACE_PROTO("Already received STREAM data",
2064 QUIC_EV_CONN_PSTRM, qc);
2065 goto out;
2066 }
2067
2068 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2069 diff = strm->rx.offset - strm_frm->offset.key;
2070 strm_frm->offset.key = strm->rx.offset;
2071 strm_frm->len -= diff;
2072 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002073 }
2074
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002075 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002076 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002077 int ret;
2078
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002079 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002080 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002081 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002082
2083 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002084 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002085 strm->rx.offset += ret;
2086 }
2087
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002088 total += qc_treat_rx_strm_frms(strm);
2089 if (total && qc->qcc->app_ops->decode_qcs(strm, strm_frm->fin, qc->qcc->ctx) < 0) {
2090 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
2091 return 0;
2092 }
2093
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002094 if (!strm_frm->len)
2095 goto out;
2096
2097 store_frm:
2098 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2099 if (!frm) {
2100 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002101 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002102 return 0;
2103 }
2104
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002105 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002106 quic_rx_packet_refinc(pkt);
2107
2108 out:
2109 return 1;
2110}
2111
2112/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2113 * streams may be open. The data are copied to the stream RX buffer if possible.
2114 * If not, the STREAM frame is stored to be treated again later.
2115 * We rely on the flow control so that not to store too much STREAM frames.
2116 * Return 1 if succeeded, 0 if not.
2117 */
2118static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2119 struct quic_stream *strm_frm,
2120 struct quic_conn *qc)
2121{
2122 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002123 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002124 struct quic_rx_strm_frm *frm;
2125 size_t strm_frm_len;
2126
2127 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2128 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002129 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002130 return 0;
2131 }
2132
2133 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002134 if (strm_frm->offset.key < strm->rx.offset) {
2135 size_t diff;
2136
2137 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2138 TRACE_PROTO("Already received STREAM data",
2139 QUIC_EV_CONN_PSTRM, qc);
2140 goto out;
2141 }
2142
2143 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2144 diff = strm->rx.offset - strm_frm->offset.key;
2145 strm_frm->offset.key = strm->rx.offset;
2146 strm_frm->len -= diff;
2147 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002148 }
2149
2150 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002151 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002152 int ret;
2153
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002154 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002155 goto store_frm;
2156
2157 /* qc_strm_cpy() will modify the offset, depending on the number
2158 * of bytes copied.
2159 */
2160 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2161 /* Inform the application of the arrival of this new stream */
2162 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002163 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002164 return 0;
2165 }
2166
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002167 if (ret)
2168 qcs_notify_recv(strm);
2169
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002170 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002171 }
2172 /* Take this frame into an account for the stream flow control */
2173 strm->rx.offset += strm_frm_len;
2174 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002175 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002176 */
2177 if (!strm_frm->len)
2178 goto out;
2179
2180 store_frm:
2181 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2182 if (!frm) {
2183 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002184 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002185 return 0;
2186 }
2187
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002188 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002189 quic_rx_packet_refinc(pkt);
2190
2191 out:
2192 return 1;
2193}
2194
2195static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2196 struct quic_stream *strm_frm,
2197 struct quic_conn *qc)
2198{
2199 if (strm_frm->id & QCS_ID_DIR_BIT)
2200 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2201 else
2202 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2203}
2204
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002205/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2206 * as I/O handler context and <qel> as encryption level.
2207 * Returns 1 if succeeded, 0 if failed.
2208 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002209static 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 +01002210 struct quic_enc_level *qel)
2211{
2212 struct quic_frame frm;
2213 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002214 struct quic_conn *qc = ctx->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002215
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002216 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002217 /* Skip the AAD */
2218 pos = pkt->data + pkt->aad_len;
2219 end = pkt->data + pkt->len;
2220
2221 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002222 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002223 goto err;
2224
2225 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002226 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002227 break;
2228 case QUIC_FT_PING:
2229 break;
2230 case QUIC_FT_ACK:
2231 {
2232 unsigned int rtt_sample;
2233
2234 rtt_sample = 0;
2235 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
2236 goto err;
2237
2238 if (rtt_sample) {
2239 unsigned int ack_delay;
2240
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002241 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
2242 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay));
2243 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002244 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002245 break;
2246 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002247 case QUIC_FT_STOP_SENDING:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002248 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002249 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002250 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002251 {
2252 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002253
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002254 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2255 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2256 /* XXX TO DO: <cfdebug> is used only for the traces. */
2257 struct quic_rx_crypto_frm cfdebug = { };
2258
2259 cfdebug.offset_node.key = frm.crypto.offset;
2260 cfdebug.len = frm.crypto.len;
2261 /* Nothing to do */
2262 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002263 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002264 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002265 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002266 else {
2267 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2268 /* XXX TO DO: <cfdebug> is used only for the traces. */
2269 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002270
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002271 cfdebug.offset_node.key = frm.crypto.offset;
2272 cfdebug.len = frm.crypto.len;
2273 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002274 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002275 frm.crypto.len -= diff;
2276 frm.crypto.data += diff;
2277 frm.crypto.offset = qel->rx.crypto.offset;
2278 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002279 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002280
2281 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002282 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002283 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002284
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002285 cfdebug.offset_node.key = frm.crypto.offset;
2286 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002287 if (!qc_provide_cdata(qel, ctx,
2288 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002289 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002290 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002291
2292 break;
2293 }
2294
2295 /* frm.crypto.offset > qel->rx.crypto.offset */
2296 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2297 if (!cf) {
2298 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002299 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002300 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002301 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002302
2303 cf->offset_node.key = frm.crypto.offset;
2304 cf->len = frm.crypto.len;
2305 cf->data = frm.crypto.data;
2306 cf->pkt = pkt;
2307 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2308 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002309 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002310 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002311 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002312 {
2313 struct quic_stream *stream = &frm.stream;
2314
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002315 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002316 if (objt_listener(ctx->conn->target)) {
2317 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2318 goto err;
2319 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2320 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002321
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002322 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002323 goto err;
2324
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002325 break;
2326 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002327 case QUIC_FT_MAX_DATA:
2328 case QUIC_FT_MAX_STREAM_DATA:
2329 case QUIC_FT_MAX_STREAMS_BIDI:
2330 case QUIC_FT_MAX_STREAMS_UNI:
2331 case QUIC_FT_DATA_BLOCKED:
2332 case QUIC_FT_STREAM_DATA_BLOCKED:
2333 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2334 case QUIC_FT_STREAMS_BLOCKED_UNI:
2335 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002336 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002337 break;
2338 case QUIC_FT_CONNECTION_CLOSE:
2339 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002340 /* warn the mux to close the connection */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002341 qc->qcc->flags |= QC_CF_CC_RECV;
2342 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002343 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002344 case QUIC_FT_HANDSHAKE_DONE:
2345 if (objt_listener(ctx->conn->target))
2346 goto err;
2347
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002348 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002349 break;
2350 default:
2351 goto err;
2352 }
2353 }
2354
2355 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2356 * has successfully parse a Handshake packet. The Initial encryption must also
2357 * be discarded.
2358 */
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002359 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002360 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002361
2362 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002363 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002364 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002365 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002366 qc_set_timer(ctx);
2367 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002368 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002369 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002370 }
2371
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002372 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002373 return 1;
2374
2375 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002376 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002377 return 0;
2378}
2379
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002380/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002381 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002382 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2383 * This function must be called only after having built a correct datagram.
2384 * Always succeeds.
2385 */
2386static inline void qc_set_dg(struct cbuf *cbuf,
2387 uint16_t dglen, struct quic_tx_packet *pkt)
2388{
2389 write_u16(cb_wr(cbuf), dglen);
2390 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2391 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2392}
2393
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002394/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002395 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2396 * several packets in the same datagram. A header made of two fields is added
2397 * to each datagram: the datagram length followed by the address of the first
2398 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002399 * Returns 1 if succeeded, or 0 if something wrong happened.
2400 */
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002401static int qc_prep_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002402{
2403 struct quic_conn *qc;
2404 enum quic_tls_enc_level tel, next_tel;
2405 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002406 struct cbuf *cbuf;
2407 unsigned char *end_buf, *end, *pos, *spos;
2408 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2409 /* length of datagrams */
2410 uint16_t dglen;
2411 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002412 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002413 /* Each datagram is prepended with its length followed by the
2414 * address of the first packet in the datagram.
2415 */
2416 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002417
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002418 qc = ctx->qc;
2419
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002420 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2421
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01002422 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state), 0)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002423 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002424 goto err;
2425 }
2426
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002427 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002428 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002429 total = 0;
2430 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002431 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002432 cbuf = qr->cbuf;
2433 spos = pos = cb_wr(cbuf);
2434 /* Leave at least <dglen> bytes at the end of this buffer
2435 * to ensure there is enough room to mark the end of prepared
2436 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002437 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002438 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2439 first_pkt = prv_pkt = NULL;
2440 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002441 int err, nb_ptos, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002442 enum quic_pkt_type pkt_type;
2443
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002444 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002445 nb_ptos = ack = 0;
2446 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2447 if (!cc) {
2448 if (!prv_pkt) {
2449 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2450 do {
2451 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2452 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2453 }
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002454 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002455 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002456 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002457 * 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 +01002458 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002459 * and if there is no more CRYPTO data available or in flight
2460 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002461 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002462 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002463 (!cc && !ack && !nb_ptos &&
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002464 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2465 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002466 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002467 /* Set the current datagram as prepared into <cbuf> if
2468 * the was already a correct packet which was previously written.
2469 */
2470 if (prv_pkt)
2471 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002472 break;
2473 }
2474
2475 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002476 if (!prv_pkt) {
2477 /* Leave room for the datagram header */
2478 pos += dg_headlen;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002479 if (!quic_peer_validated_addr(ctx) && objt_listener(ctx->conn->target)) {
2480 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
2481 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
2482 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2483 }
2484 else {
2485 end = pos + qc->path->mtu;
2486 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002487 }
2488
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002489 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002490 pkt_type, ack, nb_ptos, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002491 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002492 if (err < 0) {
2493 if (!prv_pkt && nb_ptos)
2494 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002495 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002496 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002497 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002498 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002499 case -2:
2500 goto err;
2501 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002502 /* If there was already a correct packet present, set the
2503 * current datagram as prepared into <cbuf>.
2504 */
2505 if (prv_pkt) {
2506 qc_set_dg(cbuf, dglen, first_pkt);
2507 goto stop_build;
2508 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002509 goto out;
2510 default:
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002511 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2512 if (!cur_pkt)
2513 goto err;
2514
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002515 total += cur_pkt->len;
2516 /* keep trace of the first packet in the datagram */
2517 if (!first_pkt)
2518 first_pkt = cur_pkt;
2519 /* Attach the current one to the previous one */
2520 if (prv_pkt)
2521 prv_pkt->next = cur_pkt;
2522 /* Let's say we have to build a new dgram */
2523 prv_pkt = NULL;
2524 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002525 /* Discard the Initial encryption keys as soon as
2526 * a handshake packet could be built.
2527 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002528 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002529 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2530 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002531 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002532 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2533 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002534 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002535 }
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002536 /* If the data for the current encryption level have all been sent,
2537 * select the next level.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002538 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002539 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002540 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2541 (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 +02002542 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2543 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2544 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002545 tel = next_tel;
2546 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002547 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002548 /* If there is data for the next level, do not
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002549 * consume a datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002550 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002551 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002552 }
2553 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002554 }
2555
2556 /* If we have to build a new datagram, set the current datagram as
2557 * prepared into <cbuf>.
2558 */
2559 if (!prv_pkt) {
2560 qc_set_dg(cbuf, dglen, first_pkt);
2561 first_pkt = NULL;
2562 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002563 padding = 0;
2564 }
2565 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
2566 (objt_server(qc->conn->target) ||
2567 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2568 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002569 }
2570 }
2571
2572 stop_build:
2573 /* Reset <wr> writer index if in front of <rd> index */
2574 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2575 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2576
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002577 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002578 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2579 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2580 /* Mark the end of contiguous data for the reader */
2581 write_u16(cb_wr(cbuf), 0);
2582 cb_add(cbuf, sizeof(uint16_t));
2583 }
2584 }
2585
2586 if (rd && rd <= cbuf->wr) {
2587 cb_wr_reset(cbuf);
2588 if (pos == spos) {
2589 /* Reuse the same buffer if nothing was built. */
2590 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002591 }
2592 }
2593 }
2594
2595 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002596 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002597 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002598
2599 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002600 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002601 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002602}
2603
2604/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002605 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002606 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002607int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002608{
2609 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002610 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002611
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002612 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002613 cbuf = qr->cbuf;
2614 while (cb_contig_data(cbuf)) {
2615 unsigned char *pos;
2616 struct buffer tmpbuf = { };
2617 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2618 uint16_t dglen;
2619 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002620 unsigned int time_sent;
2621
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002622 pos = cb_rd(cbuf);
2623 dglen = read_u16(pos);
2624 /* End of prepared datagrams.
2625 * Reset the reader index only if in front of the writer index.
2626 */
2627 if (!dglen) {
2628 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2629
2630 if (wr && wr < cbuf->rd) {
2631 cb_rd_reset(cbuf);
2632 continue;
2633 }
2634 break;
2635 }
2636
2637 pos += sizeof dglen;
2638 first_pkt = read_ptr(pos);
2639 pos += sizeof first_pkt;
2640 tmpbuf.area = (char *)pos;
2641 tmpbuf.size = tmpbuf.data = dglen;
2642
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002643 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002644 for (pkt = first_pkt; pkt; pkt = pkt->next)
2645 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002646 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002647 &tmpbuf, tmpbuf.data, 0) <= 0) {
2648 for (pkt = first_pkt; pkt; pkt = pkt->next)
2649 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002650 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002651 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002652
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002653 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002654 qc->tx.bytes += tmpbuf.data;
2655 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002656
2657 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2658 pkt->time_sent = time_sent;
2659 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2660 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002661 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002662 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002663 qc->path->in_flight += pkt->in_flight_len;
2664 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2665 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002666 qc_set_timer(ctx);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002667 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002668 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002669 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002670 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002671 }
2672 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673
2674 return 1;
2675}
2676
2677/* Build all the frames which must be sent just after the handshake have succeeded.
2678 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2679 * a HANDSHAKE_DONE frame.
2680 * Return 1 if succeeded, 0 if not.
2681 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002682static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002683{
2684 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002685 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002686 struct quic_frame *frm;
2687
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002688 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002689 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002690 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002691 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002692 if (!frm)
2693 return 0;
2694
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002695 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002696 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002697 }
2698
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002699 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002700 struct quic_connection_id *cid;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002701 struct listener *l = __objt_listener(qc->conn->target);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002702
2703 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002704 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002705 goto err;
2706
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002707 cid = new_quic_cid(&qc->cids, qc, i);
2708 if (!cid)
2709 goto err;
2710
2711 /* insert the allocated CID in the receiver tree */
2712 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
2713 ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len);
2714 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
2715
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002716 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002717 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002718 }
2719
2720 return 1;
2721
2722 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002723 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002724 return 0;
2725}
2726
2727/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002728void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002729{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002730 struct eb64_node *n;
2731 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002732
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002733 n = eb64_first(&arngs->root);
2734 while (n) {
2735 struct eb64_node *next;
2736
2737 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2738 next = eb64_next(n);
2739 eb64_delete(n);
2740 free(ar);
2741 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002742 }
2743}
2744
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002745/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2746 * descending order.
2747 */
2748static inline size_t sack_gap(struct quic_arng_node *p,
2749 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002750{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002751 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002752}
2753
2754
2755/* Remove the last elements of <ack_ranges> list of ack range updating its
2756 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002757 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002758 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002759static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002761 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002762
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002763 last = eb64_last(&arngs->root);
2764 while (last && arngs->enc_sz > limit) {
2765 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002766
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002767 prev = eb64_prev(last);
2768 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002769 return 0;
2770
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002771 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2772 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2773 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2774 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2775 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2776 --arngs->sz;
2777 eb64_delete(last);
2778 pool_free(pool_head_quic_arng, last);
2779 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002780 }
2781
2782 return 1;
2783}
2784
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002785/* Set the encoded size of <arngs> QUIC ack ranges. */
2786static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2787{
2788 struct eb64_node *node, *next;
2789 struct quic_arng_node *ar, *ar_next;
2790
2791 node = eb64_last(&arngs->root);
2792 if (!node)
2793 return;
2794
2795 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2796 arngs->enc_sz = quic_int_getsize(ar->last) +
2797 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2798
2799 while ((next = eb64_prev(node))) {
2800 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2801 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2802 quic_int_getsize(ar_next->last - ar_next->first.key);
2803 node = next;
2804 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2805 }
2806}
2807
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002808/* Insert <ar> ack range into <argns> tree of ack ranges.
2809 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002810 */
2811static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002812struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2813 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002814{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002815 struct quic_arng_node *new_ar;
2816
2817 new_ar = pool_alloc(pool_head_quic_arng);
2818 if (new_ar) {
2819 new_ar->first.key = ar->first;
2820 new_ar->last = ar->last;
2821 eb64_insert(&arngs->root, &new_ar->first);
2822 arngs->sz++;
2823 }
2824
2825 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002826}
2827
2828/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002829 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002830 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002831 *
2832 * Descending order
2833 * ------------->
2834 * range1 range2
2835 * ..........|--------|..............|--------|
2836 * ^ ^ ^ ^
2837 * | | | |
2838 * last1 first1 last2 first2
2839 * ..........+--------+--------------+--------+......
2840 * diff1 gap12 diff2
2841 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002842 * To encode the previous list of ranges we must encode integers as follows in
2843 * descending order:
2844 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002845 * with diff1 = last1 - first1
2846 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002847 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002848 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002849 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002850int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2851 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002852{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002853 struct eb64_node *le;
2854 struct quic_arng_node *new_node;
2855 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002856
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002857 new = NULL;
2858 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002859 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002860 if (!new_node)
2861 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002862
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002863 goto out;
2864 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002865
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002866 le = eb64_lookup_le(&arngs->root, ar->first);
2867 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002868 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002869 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002870 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002871
2872 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002873 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002874 else {
2875 struct quic_arng_node *le_ar =
2876 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002877
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002878 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002879 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002880 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002881
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002882 if (le_ar->last + 1 >= ar->first) {
2883 le_ar->last = ar->last;
2884 new = le;
2885 new_node = le_ar;
2886 }
2887 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002888 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002889 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002890 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002891
2892 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002893 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002894 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002895
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002896 /* Verify that the new inserted node does not overlap the nodes
2897 * which follow it.
2898 */
2899 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002900 struct eb64_node *next;
2901 struct quic_arng_node *next_node;
2902
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002903 while ((next = eb64_next(new))) {
2904 next_node =
2905 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002906 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002907 break;
2908
2909 if (next_node->last > new_node->last)
2910 new_node->last = next_node->last;
2911 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002912 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002913 /* Decrement the size of these ranges. */
2914 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002915 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002916 }
2917
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002918 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002919 quic_arngs_set_enc_sz(arngs);
2920
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002921 return 1;
2922}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002923/* Remove the header protection of packets at <el> encryption level.
2924 * Always succeeds.
2925 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002926static 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 +01002927{
2928 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002929 struct quic_rx_packet *pqpkt;
2930 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002931 struct quic_enc_level *app_qel;
2932
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002933 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002934 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2935 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002936 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002937 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002938 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002939 QUIC_EV_CONN_ELRMHP, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002940 goto out;
2941 }
2942 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002943 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002944 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2945 pqpkt->data + pqpkt->pn_offset,
2946 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002947 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002948 /* XXX TO DO XXX */
2949 }
2950 else {
2951 /* The AAD includes the packet number field */
2952 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2953 /* Store the packet into the tree of packets to decrypt. */
2954 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002955 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002956 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2957 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002958 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002959 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002960 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002961 MT_LIST_DELETE_SAFE(pkttmp1);
2962 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002963 }
2964
2965 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002966 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002967}
2968
2969/* Process all the CRYPTO frame at <el> encryption level.
2970 * Return 1 if succeeded, 0 if not.
2971 */
2972static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002973 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002974{
2975 struct eb64_node *node;
2976
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002977 node = eb64_first(&el->rx.crypto.frms);
2978 while (node) {
2979 struct quic_rx_crypto_frm *cf;
2980
2981 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2982 if (cf->offset_node.key != el->rx.crypto.offset)
2983 break;
2984
2985 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2986 goto err;
2987
2988 node = eb64_next(node);
2989 quic_rx_packet_refdec(cf->pkt);
2990 eb64_delete(&cf->offset_node);
2991 pool_free(pool_head_quic_rx_crypto_frm, cf);
2992 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002993 return 1;
2994
2995 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002996 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002997 return 0;
2998}
2999
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003000/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003001 * 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 +02003002 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003003 * Return 1 if succeeded, 0 if not.
3004 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003005int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3006 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003007{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003008 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003009 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003010 struct quic_conn *qc = ctx->conn->qc;
3011 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003012
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003013 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003014 qel = cur_el;
3015 next_tel:
3016 if (!qel)
3017 goto out;
3018
3019 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3020 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003021 while (node) {
3022 struct quic_rx_packet *pkt;
3023
3024 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003025 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003026 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003027 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003028 /* Drop the packet */
3029 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003030 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003031 }
3032 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003033 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003034 /* Drop the packet */
3035 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003036 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003037 }
3038 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003039 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3040
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003041 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003042 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003043 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003044 if (pkt->pn > largest_pn)
3045 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003047 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003048 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003049 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003050 }
3051 }
3052 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003053 eb64_delete(&pkt->pn_node);
3054 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003055 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003056 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003057
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003058 /* Update the largest packet number. */
3059 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003060 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3061 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003062 goto err;
3063
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003064 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003065 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003066 qel = next_el;
3067 goto next_tel;
3068 }
3069
3070 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003071 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003072 return 1;
3073
3074 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003075 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003076 return 0;
3077}
3078
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003079/* QUIC connection packet handler task. */
3080struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003081{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003082 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003083 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003084 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003085 enum quic_tls_enc_level tel, next_tel;
3086 struct quic_enc_level *qel, *next_qel;
3087 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003088 struct qring *qr; // Tx ring
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003089 int prev_st, st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003090
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003091 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003092 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003093 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003094 st = HA_ATOMIC_LOAD(&qc->state);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003095 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003096 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003097 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3098 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3099 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003100 start:
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003101 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003102 goto err;
3103
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003104 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003105 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003106
3107 next_level:
3108 tls_ctx = &qel->tls_ctx;
3109
3110 /* If the header protection key for this level has been derived,
3111 * remove the packet header protections.
3112 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003113 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003114 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
3115 qc_rm_hp_pkts(qel, ctx);
3116
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003117 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003118 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3119 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3120 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003121 goto err;
3122
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003123 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3124 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3125 qel = next_qel;
3126 next_qel = NULL;
3127 goto next_level;
3128 }
3129
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003130 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02003131 if (st >= QUIC_HS_ST_COMPLETE &&
3132 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003133 /* Discard the Handshake keys. */
3134 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003135 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003136 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3137 qc_set_timer(ctx);
3138 if (!quic_build_post_handshake_frames(qc))
3139 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003140
3141 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3142 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003143 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003144 }
3145
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003146 if (!qr)
3147 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01003148 ret = qc_prep_pkts(qr, ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003149 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003150 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003151 else if (ret == 0)
3152 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003153
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003154 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003155 goto err;
3156
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003157 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003158 /* Check if there is something to do for the next level.
3159 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003160 if (next_qel && next_qel != qel &&
3161 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003162 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003163 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003164 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003165 goto next_level;
3166 }
3167
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003168 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003169 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003170 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003171
3172 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003173 if (qr)
3174 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003175 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003176 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003177}
3178
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003179/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003180static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3181{
3182 int i;
3183
3184 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3185 if (qel->tx.crypto.bufs[i]) {
3186 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3187 qel->tx.crypto.bufs[i] = NULL;
3188 }
3189 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003190 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003191}
3192
3193/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003194 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003195 * Returns 1 if succeeded, 0 if not.
3196 */
3197static int quic_conn_enc_level_init(struct quic_conn *qc,
3198 enum quic_tls_enc_level level)
3199{
3200 struct quic_enc_level *qel;
3201
3202 qel = &qc->els[level];
3203 qel->level = quic_to_ssl_enc_level(level);
3204 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3205 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3206 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3207 qel->tls_ctx.rx.flags = 0;
3208 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003209 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003210
3211 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003212 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003213 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003214 qel->rx.crypto.offset = 0;
3215 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003216
3217 /* Allocate only one buffer. */
3218 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3219 if (!qel->tx.crypto.bufs)
3220 goto err;
3221
3222 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3223 if (!qel->tx.crypto.bufs[0])
3224 goto err;
3225
3226 qel->tx.crypto.bufs[0]->sz = 0;
3227 qel->tx.crypto.nb_buf = 1;
3228
3229 qel->tx.crypto.sz = 0;
3230 qel->tx.crypto.offset = 0;
3231
3232 return 1;
3233
3234 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003235 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003236 return 0;
3237}
3238
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003239/* Release all the memory allocated for <conn> QUIC connection. */
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003240static void quic_conn_free(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003241{
3242 int i;
3243
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003244 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003245 return;
3246
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003247 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003248
3249 /* remove the connection from receiver cids trees */
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003250 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3251 ebmb_delete(&qc->odcid_node);
3252 ebmb_delete(&qc->scid_node);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003253
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003254 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003255
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003256 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003257 quic_conn_enc_level_uninit(&qc->els[i]);
3258 if (qc->timer_task)
3259 task_destroy(qc->timer_task);
3260 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3261 pool_free(pool_head_quic_conn, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003262}
3263
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003264void quic_close(struct connection *conn, void *xprt_ctx)
3265{
3266 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
3267 struct quic_conn *qc = conn_ctx->conn->qc;
3268 quic_conn_free(qc);
3269}
3270
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003271/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003272static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003273{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003274 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003275 struct quic_conn *qc;
3276 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003277 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003278
3279 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003280 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003281 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003282 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003283 task->expire = TICK_ETERNITY;
3284 pktns = quic_loss_pktns(qc);
3285 if (tick_isset(pktns->tx.loss_time)) {
3286 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3287
3288 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3289 if (!LIST_ISEMPTY(&lost_pkts))
3290 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
3291 qc_set_timer(conn_ctx);
3292 goto out;
3293 }
3294
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003295 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003296 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003297 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003298 pktns->tx.pto_probe = 1;
3299 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003300 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003301 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3302 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3303
3304 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3305 hel->pktns->tx.pto_probe = 1;
3306 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3307 iel->pktns->tx.pto_probe = 1;
3308 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003309 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003310 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3311 qc->path->loss.pto_count++;
3312
3313 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003314 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003315
3316 return task;
3317}
3318
3319/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3320 * connections used to identify the first Initial packets of client connecting
3321 * to listeners. This parameter must be NULL for QUIC connections attached
3322 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3323 * <scid> is the source connection ID with <scid_len> as length.
3324 * Returns 1 if succeeded, 0 if not.
3325 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003326static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003327 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003328 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003329{
3330 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003331 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003332 /* Initial CID. */
3333 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003334 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003335 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003336
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003337 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003338 qc = pool_zalloc(pool_head_quic_conn);
3339 if (!qc) {
3340 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3341 goto err;
3342 }
3343
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003344 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3345 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003346 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003347 goto err;
3348 }
3349
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003350 qc->cids = EB_ROOT;
3351 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003352 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003353 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003354
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003355 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003356 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003357 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003358 qc->odcid.addrlen = dcid_addr_len;
3359 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003360
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003361 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003362 if (scid_len)
3363 memcpy(qc->dcid.data, scid, scid_len);
3364 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003365 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003366 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003367 }
3368 /* QUIC Client (outgoing connection to servers) */
3369 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003370 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003371 if (dcid_len)
3372 memcpy(qc->dcid.data, dcid, dcid_len);
3373 qc->dcid.len = dcid_len;
3374 }
3375
3376 /* Initialize the output buffer */
3377 qc->obuf.pos = qc->obuf.data;
3378
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003379 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003380 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003381 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003382 goto err;
3383 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003384
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003385 /* insert the allocated CID in the receiver tree */
3386 if (server) {
3387 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
3388 ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len);
3389 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3390 }
3391
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003392 /* Select our SCID which is the first CID with 0 as sequence number. */
3393 qc->scid = icid->cid;
3394
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003395 /* Packet number spaces initialization. */
3396 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3397 quic_pktns_init(&qc->pktns[i]);
3398 /* QUIC encryption level context initialization. */
3399 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003400 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003401 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003402 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003403 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003404 /* Initialize the packet number space. */
3405 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3406 }
3407
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003408 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003409 qc->tps_tls_ext = qc->version & 0xff000000 ?
3410 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3411 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003412 /* TX part. */
3413 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003414 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3415 qc->tx.wbuf = qc->tx.rbuf = 0;
3416 qc->tx.bytes = 0;
3417 qc->tx.nb_pto_dgrams = 0;
3418 /* RX part. */
3419 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003420 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003421 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3422 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3423 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003424 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003425 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003426 goto err;
3427 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003428
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003429 /* XXX TO DO: Only one path at this time. */
3430 qc->path = &qc->paths[0];
3431 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3432
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003433 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003434
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003435 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003436
3437 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003438 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003439 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003440 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003441}
3442
3443/* Initialize the timer task of <qc> QUIC connection.
3444 * Returns 1 if succeeded, 0 if not.
3445 */
3446static int quic_conn_init_timer(struct quic_conn *qc)
3447{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003448 /* Attach this task to the same thread ID used for the connection */
3449 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003450 if (!qc->timer_task)
3451 return 0;
3452
3453 qc->timer = TICK_ETERNITY;
3454 qc->timer_task->process = process_timer;
3455 qc->timer_task->context = qc->conn->xprt_ctx;
3456
3457 return 1;
3458}
3459
3460/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3461 * past one byte of this buffer.
3462 */
3463static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3464 struct quic_rx_packet *pkt)
3465{
3466 unsigned char dcid_len, scid_len;
3467
3468 /* Version */
3469 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3470 return 0;
3471
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003472 /* Destination Connection ID Length */
3473 dcid_len = *(*buf)++;
3474 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3475 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3476 /* XXX MUST BE DROPPED */
3477 return 0;
3478
3479 if (dcid_len) {
3480 /* Check that the length of this received DCID matches the CID lengths
3481 * of our implementation for non Initials packets only.
3482 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003483 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3484 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003485 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003486 return 0;
3487
3488 memcpy(pkt->dcid.data, *buf, dcid_len);
3489 }
3490
3491 pkt->dcid.len = dcid_len;
3492 *buf += dcid_len;
3493
3494 /* Source Connection ID Length */
3495 scid_len = *(*buf)++;
3496 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3497 /* XXX MUST BE DROPPED */
3498 return 0;
3499
3500 if (scid_len)
3501 memcpy(pkt->scid.data, *buf, scid_len);
3502 pkt->scid.len = scid_len;
3503 *buf += scid_len;
3504
3505 return 1;
3506}
3507
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003508/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3509 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3510 * encryption level matching with the packet type. <*qel> may be null if not found.
3511 * Note that <ctx> may be null (for Initial packets).
3512 */
3513static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003514 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003515 struct quic_enc_level **qel)
3516{
3517 enum quic_tls_enc_level tel;
3518
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003519 /* Special case without connection context (first Initial packets) */
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003520 if (!ctx) {
3521 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3522 return 1;
3523 }
3524
3525 tel = quic_packet_type_enc_level(pkt->type);
3526 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3527 *qel = NULL;
3528 return 0;
3529 }
3530
3531 *qel = &qc->els[tel];
3532 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003533 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003534 return 0;
3535 }
3536
3537 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003538 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3539 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003540 return 1;
3541
3542 return 0;
3543}
3544
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003545/* Insert <pkt> RX packet in its <qel> RX packets tree */
3546static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3547{
3548 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003549 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003550 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3551 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3552 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003553}
3554
3555/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003556 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3557 * byte past the end of the buffer containing this packet and <beg> the address of
3558 * the packet first byte.
3559 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3560 * Returns 1 if succeeded, 0 if not.
3561 */
3562static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003563 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003564 const unsigned char *end,
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003565 struct quic_conn *qc, struct quic_enc_level **el,
3566 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003567{
3568 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003569 struct quic_enc_level *qel;
3570 /* Only for traces. */
3571 struct quic_rx_packet *qpkt_trace;
3572
3573 qpkt_trace = NULL;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003574 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003575 /* The packet number is here. This is also the start minus
3576 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3577 * protection.
3578 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003579 pn = buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003580 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003581 /* Note that the following function enables us to unprotect the packet
3582 * number and its length subsequently used to decrypt the entire
3583 * packets.
3584 */
3585 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3586 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003587 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003588 goto err;
3589 }
3590
3591 /* The AAD includes the packet number field found at <pn>. */
3592 pkt->aad_len = pn - beg + pkt->pnl;
3593 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003594 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003595 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003596 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3597 /* If the packet number space has been discarded, this packet
3598 * will be not parsed.
3599 */
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003600 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003601 goto out;
3602 }
3603
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003604 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003605 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003606 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3607 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003608 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003609 else {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003610 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003611 goto err;
3612 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003613
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003614 *el = qel;
3615 /* No reference counter incrementation here!!! */
3616 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3617 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3618 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3619 b_add(&qc->rx.buf, pkt->len);
3620 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003621 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->qc : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003622 return 1;
3623
3624 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003625 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 +01003626 return 0;
3627}
3628
3629/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3630 * Also set <*long_header> to 1 if this form is long, 0 if not.
3631 */
3632static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3633 unsigned char byte0, int *long_header)
3634{
3635 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3636 pkt->type =
3637 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3638 *long_header = 1;
3639 }
3640 else {
3641 pkt->type = QUIC_PACKET_TYPE_SHORT;
3642 *long_header = 0;
3643 }
3644}
3645
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003646/*
3647 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3648 */
3649static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3650{
3651 int j = 0, version;
3652
3653 do {
3654 version = quic_supported_version[j];
3655 if (version == pkt->version)
3656 return 1;
3657
3658 version = quic_supported_version[++j];
3659 } while(version);
3660
3661 return 0;
3662}
3663
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003664__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003665static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3666 struct quic_rx_packet *pkt,
3667 struct quic_dgram_ctx *dgram_ctx,
3668 struct sockaddr_storage *saddr)
3669{
3670 unsigned char *beg;
3671 uint64_t len;
3672 struct quic_conn *qc;
3673 struct eb_root *cids;
3674 struct ebmb_node *node;
3675 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003676 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003677 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003678 size_t b_cspace;
3679 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003680
3681 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003682 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003683 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3684 if (end <= *buf)
3685 goto err;
3686
3687 /* Fixed bit */
3688 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3689 /* XXX TO BE DISCARDED */
3690 goto err;
3691
3692 srv_conn = dgram_ctx->owner;
3693 beg = *buf;
3694 /* Header form */
3695 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3696 if (long_header) {
3697 size_t cid_lookup_len;
3698
3699 if (!quic_packet_read_long_header(buf, end, pkt))
3700 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003701
3702 /* unsupported QUIC version */
3703 if (!qc_pkt_is_supported_version(pkt)) {
3704 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3705 goto err;
3706 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003707
3708 /* For Initial packets, and for servers (QUIC clients connections),
3709 * there is no Initial connection IDs storage.
3710 */
3711 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3712 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3713 cid_lookup_len = pkt->dcid.len;
3714 }
3715 else {
3716 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003717 cid_lookup_len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003718 }
3719
3720 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3721 if (!node)
3722 goto err;
3723
3724 qc = ebmb_entry(node, struct quic_conn, scid_node);
3725
3726 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3727 qc->dcid.len = pkt->scid.len;
3728 if (pkt->scid.len)
3729 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3730 }
3731
3732 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3733 uint64_t token_len;
3734
3735 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3736 goto err;
3737
3738 /* XXX TO DO XXX 0 value means "the token is not present".
3739 * A server which sends an Initial packet must not set the token.
3740 * So, a client which receives an Initial packet with a token
3741 * MUST discard the packet or generate a connection error with
3742 * PROTOCOL_VIOLATION as type.
3743 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3744 */
3745 pkt->token_len = token_len;
3746 }
3747 }
3748 else {
3749 /* XXX TO DO: Short header XXX */
Amaury Denoyelled4962512021-12-14 17:17:28 +01003750 if (end - *buf < QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003751 goto err;
3752
3753 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003754 node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003755 if (!node)
3756 goto err;
3757
3758 qc = ebmb_entry(node, struct quic_conn, scid_node);
Amaury Denoyelled4962512021-12-14 17:17:28 +01003759 *buf += QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003760 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003761
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003762 /* Only packets packets with long headers and not RETRY or VERSION as type
3763 * have a length field.
3764 */
3765 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3766 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3767 goto err;
3768
3769 pkt->len = len;
3770 }
3771 else if (!long_header) {
3772 /* A short packet is the last one of an UDP datagram. */
3773 pkt->len = end - *buf;
3774 }
3775
3776 conn_ctx = qc->conn->xprt_ctx;
3777
3778 /* Increase the total length of this packet by the header length. */
3779 pkt->len += *buf - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003780
3781 /* When multiple QUIC packets are coalesced on the same UDP datagram,
3782 * they must have the same DCID.
3783 *
3784 * This check must be done after the final update to pkt.len to
3785 * properly drop the packet on failure.
3786 */
3787 if (!dgram_ctx->dcid.len) {
3788 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
3789 }
3790 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003791 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003792 goto err;
3793 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003794 dgram_ctx->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003795
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003796 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3797 b_cspace = b_contig_space(&qc->rx.buf);
3798 if (b_cspace < pkt->len) {
3799 /* Let us consume the remaining contiguous space. */
3800 b_add(&qc->rx.buf, b_cspace);
3801 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3802 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003803 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003804 goto err;
3805 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003806 }
3807
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003808 if (!qc_try_rm_hp(pkt, *buf, beg, end, qc, &qel, conn_ctx)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003809 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003810 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003811 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003812 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003813
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003814 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3815 if (pkt->aad_len)
3816 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003817 /* Wake the tasklet of the QUIC connection packet handler. */
3818 if (conn_ctx)
3819 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3820
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003821 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003822
3823 return pkt->len;
3824
3825 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003826 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003827 return -1;
3828}
3829
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003830/*
3831 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3832 * address <addr>.
3833 * Implementation of RFC9000 6. Version Negotiation
3834 *
3835 * TODO implement a rate-limiting sending of Version Negotiation packets
3836 *
3837 * Returns 0 on success else non-zero
3838 */
3839static int qc_send_version_negotiation(int fd, struct sockaddr_storage *addr,
3840 struct quic_rx_packet *pkt)
3841{
3842 char buf[256];
3843 int i = 0, j, version;
3844 const socklen_t addrlen = get_addr_len(addr);
3845
3846 /*
3847 * header form
3848 * long header, fixed bit to 0 for Version Negotiation
3849 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003850 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3851 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003852
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003853 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003854 /* null version for Version Negotiation */
3855 buf[i++] = '\x00';
3856 buf[i++] = '\x00';
3857 buf[i++] = '\x00';
3858 buf[i++] = '\x00';
3859
3860 /* source connection id */
3861 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003862 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003863 i += pkt->scid.len;
3864
3865 /* destination connection id */
3866 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003867 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003868 i += pkt->dcid.len;
3869
3870 /* supported version */
3871 j = 0;
3872 do {
3873 version = htonl(quic_supported_version[j]);
3874 memcpy(&buf[i], &version, sizeof(version));
3875 i += sizeof(version);
3876
3877 version = quic_supported_version[++j];
3878 } while (version);
3879
3880 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3881 return 1;
3882
3883 return 0;
3884}
3885
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003886/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
3887 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
3888 * concatenated to the <pkt> DCID field.
3889 *
3890 * Returns the instance or NULL if not found.
3891 */
3892static struct quic_conn *qc_retrieve_conn_from_cid(struct quic_rx_packet *pkt,
3893 struct listener *l,
3894 struct sockaddr_storage *saddr)
3895{
3896 struct quic_conn *qc = NULL;
3897 struct ebmb_node *node;
3898 struct quic_connection_id *id;
3899
3900 HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock);
3901
3902 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
3903 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
3904 pkt->type == QUIC_PACKET_TYPE_0RTT) {
3905 /* DCIDs of first packets coming from multiple clients may have
3906 * the same values. Let's distinguish them by concatenating the
3907 * socket addresses.
3908 */
3909 quic_cid_saddr_cat(&pkt->dcid, saddr);
3910 node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data,
3911 pkt->dcid.len + pkt->dcid.addrlen);
3912 if (node) {
3913 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3914 goto end;
3915 }
3916 }
3917
3918 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
3919 * also for INITIAL/0-RTT non-first packets with the final DCID in
3920 * used.
3921 */
3922 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len);
3923 if (!node)
3924 goto end;
3925
3926 id = ebmb_entry(node, struct quic_connection_id, node);
3927 qc = id->qc;
3928
3929 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
3930 * If already done, this is a noop.
3931 */
Amaury Denoyelledbef9852021-12-16 16:15:18 +01003932 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003933
3934 end:
3935 HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3936
3937 return qc;
3938}
3939
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003940static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003941 struct quic_rx_packet *pkt,
3942 struct quic_dgram_ctx *dgram_ctx,
3943 struct sockaddr_storage *saddr)
3944{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003945 unsigned char *beg, *payload;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003946 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003947 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003948 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003949 int long_header = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003950 size_t b_cspace;
3951 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003952
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003953 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003954 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003955 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003956 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01003957 TRACE_ENTER(QUIC_EV_CONN_LPKT);
3958 /* This ist only to please to traces and distinguish the
3959 * packet with parsed packet number from others.
3960 */
3961 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003962 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003963 goto err;
3964
3965 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003966 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003967 /* XXX TO BE DISCARDED */
3968 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3969 goto err;
3970 }
3971
3972 l = dgram_ctx->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003973 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003974 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003975 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01003976 uint64_t len;
3977
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003978 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003979 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3980 goto err;
3981 }
3982
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01003983 /* Retry of Version Negotiation packets are only sent by servers */
3984 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
3985 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3986 goto err;
3987 }
3988
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003989 /* RFC9000 6. Version Negotiation */
3990 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003991 /* unsupported version, send Negotiation packet */
3992 if (qc_send_version_negotiation(l->rx.fd, saddr, pkt)) {
3993 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
3994 goto err;
3995 }
3996
3997 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003998 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003999 }
4000
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004001 /* For Initial packets, and for servers (QUIC clients connections),
4002 * there is no Initial connection IDs storage.
4003 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004004 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004005 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004006
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004007 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4008 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004009 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4010 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004011 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004012
4013 /* XXX TO DO XXX 0 value means "the token is not present".
4014 * A server which sends an Initial packet must not set the token.
4015 * So, a client which receives an Initial packet with a token
4016 * MUST discard the packet or generate a connection error with
4017 * PROTOCOL_VIOLATION as type.
4018 * The token must be provided in a Retry packet or NEW_TOKEN frame.
4019 */
4020 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004021 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004022 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004023 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004024 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4025 goto err;
4026 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004027 }
4028
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004029 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4030 end - buf < len) {
4031 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4032 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004033 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004034
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004035 payload = buf;
4036 pkt->len = len + payload - beg;
4037
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004038 qc = qc_retrieve_conn_from_cid(pkt, l, saddr);
4039 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004040 int ipv4;
4041 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004042 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004043 const unsigned char *salt = initial_salt_v1;
4044 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004045
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004046 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004047 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004048 goto err;
4049 }
4050
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004051 pkt->saddr = *saddr;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004052 ipv4 = saddr->ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004053 qc = qc_new_conn(pkt->version, ipv4,
4054 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004055 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004056 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004057 goto err;
4058
4059 odcid = &qc->rx.params.original_destination_connection_id;
4060 /* Copy the transport parameters. */
4061 qc->rx.params = l->bind_conf->quic_params;
4062 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004063 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4064 odcid->len = pkt->dcid.len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004065 /* Copy the initial source connection ID. */
4066 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4067 qc->enc_params_len =
4068 quic_transport_params_encode(qc->enc_params,
4069 qc->enc_params + sizeof qc->enc_params,
4070 &qc->rx.params, 1);
4071 if (!qc->enc_params_len)
4072 goto err;
4073
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004074 /* NOTE: the socket address has been concatenated to the destination ID
4075 * chosen by the client for Initial packets.
4076 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004077 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4078 salt = initial_salt_draft_29;
4079 salt_len = sizeof initial_salt_draft_29;
4080 }
4081 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004082 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004083 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004084 goto err;
4085 }
4086
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004087 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004088 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004089 n = ebmb_insert(&l->rx.odcids, &qc->odcid_node,
4090 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004091 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004092
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004093 /* If the insertion failed, it means that another
4094 * thread has already allocated a QUIC connection for
4095 * the same CID. Liberate our allocated connection.
4096 */
4097 if (unlikely(n != &qc->odcid_node)) {
4098 quic_conn_free(qc);
4099 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4100 pkt->qc = qc;
4101 }
4102 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004103 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004104 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004105 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
4106 /* Try to accept a new connection. */
4107 listener_accept(l);
4108 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004109 }
4110 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004111 pkt->qc = qc;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01004112 if (HA_ATOMIC_LOAD(&qc->conn))
4113 conn_ctx = HA_ATOMIC_LOAD(&qc->conn->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004114 }
4115 }
4116 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004117 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004118 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4119 goto err;
4120 }
4121
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004122 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004123 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004124 buf += QUIC_HAP_CID_LEN;
4125
4126 /* A short packet is the last one of a UDP datagram. */
4127 payload = buf;
4128 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004129
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004130 qc = qc_retrieve_conn_from_cid(pkt, l, saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004131 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004132 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004133 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4134 goto err;
4135 }
4136
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01004137 if (HA_ATOMIC_LOAD(&qc->conn))
4138 conn_ctx = HA_ATOMIC_LOAD(&qc->conn->xprt_ctx);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004139
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004140 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004141 }
4142
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004143
4144 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4145 * they must have the same DCID.
4146 *
4147 * This check must be done after the final update to pkt.len to
4148 * properly drop the packet on failure.
4149 */
4150 if (!dgram_ctx->dcid.len) {
4151 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
4152 }
4153 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004154 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004155 goto err;
4156 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004157 dgram_ctx->qc = qc;
4158
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004159
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004160 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004161 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004162 goto out;
4163 }
4164
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004165 pkt->raw_len = pkt->len;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004166 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004167 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004168 b_cspace = b_contig_space(&qc->rx.buf);
4169 if (b_cspace < pkt->len) {
4170 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004171 if (b_cspace) {
4172 b_putchr(&qc->rx.buf, 0x00);
4173 b_cspace--;
4174 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004175 b_add(&qc->rx.buf, b_cspace);
4176 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4177 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004178 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004179 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004180 goto err;
4181 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004182 }
4183
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004184 if (!qc_try_rm_hp(pkt, payload, beg, end, qc, &qel, conn_ctx)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004185 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004186 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004187 goto err;
4188 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004189
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004190 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004191 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004192 if (pkt->aad_len)
4193 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004194 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004195 /* Wake up the connection packet handler task from here only if all
4196 * the contexts have been initialized, especially the mux context
4197 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4198 * will start it if these contexts for the connection are not already
4199 * initialized.
4200 */
4201 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004202 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004203
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004204 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004205
4206 return pkt->len;
4207
4208 err:
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004209 /* If length not found, consume the entire packet */
4210 if (!pkt->len)
4211 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004212 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004213 qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004214 return -1;
4215}
4216
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004217/* This function builds into <buf> buffer a QUIC long packet header.
4218 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004219 */
4220static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4221 int type, size_t pn_len, struct quic_conn *conn)
4222{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004223 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004224 return 0;
4225
4226 /* #0 byte flags */
4227 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4228 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4229 /* Version */
4230 quic_write_uint32(buf, end, conn->version);
4231 *(*buf)++ = conn->dcid.len;
4232 /* Destination connection ID */
4233 if (conn->dcid.len) {
4234 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4235 *buf += conn->dcid.len;
4236 }
4237 /* Source connection ID */
4238 *(*buf)++ = conn->scid.len;
4239 if (conn->scid.len) {
4240 memcpy(*buf, conn->scid.data, conn->scid.len);
4241 *buf += conn->scid.len;
4242 }
4243
4244 return 1;
4245}
4246
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004247/* This function builds into <buf> buffer a QUIC short packet header.
4248 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004249 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004250static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4251 size_t pn_len, struct quic_conn *conn,
4252 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004253{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004254 if (end - *buf < 1 + conn->dcid.len)
4255 return 0;
4256
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004257 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004258 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4259 ((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 +01004260 /* Destination connection ID */
4261 if (conn->dcid.len) {
4262 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4263 *buf += conn->dcid.len;
4264 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004265
4266 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004267}
4268
4269/* Apply QUIC header protection to the packet with <buf> as first byte address,
4270 * <pn> as address of the Packet number field, <pnlen> being this field length
4271 * with <aead> as AEAD cipher and <key> as secret key.
4272 * Returns 1 if succeeded or 0 if failed.
4273 */
4274static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4275 const EVP_CIPHER *aead, const unsigned char *key)
4276{
4277 int i, ret, outlen;
4278 EVP_CIPHER_CTX *ctx;
4279 /* We need an IV of at least 5 bytes: one byte for bytes #0
4280 * and at most 4 bytes for the packet number
4281 */
4282 unsigned char mask[5] = {0};
4283
4284 ret = 0;
4285 ctx = EVP_CIPHER_CTX_new();
4286 if (!ctx)
4287 return 0;
4288
4289 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4290 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4291 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4292 goto out;
4293
4294 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4295 for (i = 0; i < pnlen; i++)
4296 pn[i] ^= mask[i + 1];
4297
4298 ret = 1;
4299
4300 out:
4301 EVP_CIPHER_CTX_free(ctx);
4302
4303 return ret;
4304}
4305
4306/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4307 * ACK ranges if needed to a value below <limit> in bytes.
4308 * Return 1 if succeeded, 0 if not.
4309 */
4310static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4311{
4312 size_t room, ack_delay_sz;
4313
4314 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4315 /* A frame is made of 1 byte for the frame type. */
4316 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004317 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004318 return 0;
4319
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004320 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004321}
4322
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004323/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4324 * 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 +01004325 * and <*len> the packet Length field initialized with the number of bytes already present
4326 * 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 +02004327 * <headlen> is the number of bytes already present in this packet before building frames.
4328 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004329 * Update consequently <*len> to reflect the size of these frames built
4330 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004331 * Return 1 if succeeded, 0 if not.
4332 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004333static inline int qc_build_frms(struct quic_tx_packet *pkt,
4334 size_t room, size_t *len, size_t headlen,
4335 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004336 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004337{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004338 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004339 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004340 struct mt_list *tmp1, tmp2;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004341 size_t remain = quic_path_prep_data(qc->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004342
Frédéric Lécailleea604992020-12-24 13:01:37 +01004343 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004344 if (*len > room || headlen > remain)
4345 return 0;
4346
Frédéric Lécailleea604992020-12-24 13:01:37 +01004347 /* If we are not probing we must take into an account the congestion
4348 * control window.
4349 */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004350 if (!qc->tx.nb_pto_dgrams)
4351 room = QUIC_MIN(room, quic_path_prep_data(qc->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004352 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004353 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004354 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 +01004355 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004356 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004357
Frédéric Lécailleea604992020-12-24 13:01:37 +01004358 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004359 break;
4360
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004361 switch (cf->type) {
4362 case QUIC_FT_CRYPTO:
4363 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004364 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004365 /* Compute the length of this CRYPTO frame header */
4366 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4367 /* Compute the data length of this CRyPTO frame. */
4368 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4369 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004370 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004371 if (!dlen)
4372 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004373
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004374 /* CRYPTO frame length. */
4375 flen = hlen + quic_int_getsize(dlen) + dlen;
4376 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004377 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004378 /* Add the CRYPTO data length and its encoded length to the packet
4379 * length and the length of this length.
4380 */
4381 *len += flen;
4382 room -= flen;
4383 if (dlen == cf->crypto.len) {
4384 /* <cf> CRYPTO data have been consumed. */
4385 MT_LIST_DELETE_SAFE(tmp1);
4386 LIST_APPEND(&pkt->frms, &cf->list);
4387 }
4388 else {
4389 struct quic_frame *new_cf;
4390
4391 new_cf = pool_alloc(pool_head_quic_frame);
4392 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004393 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004394 return 0;
4395 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004396
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004397 new_cf->type = QUIC_FT_CRYPTO;
4398 new_cf->crypto.len = dlen;
4399 new_cf->crypto.offset = cf->crypto.offset;
4400 new_cf->crypto.qel = qel;
4401 LIST_APPEND(&pkt->frms, &new_cf->list);
4402 /* Consume <dlen> bytes of the current frame. */
4403 cf->crypto.len -= dlen;
4404 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004405 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004406 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004407
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004408 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004409 /* Note that these frames are accepted in short packets only without
4410 * "Length" packet field. Here, <*len> is used only to compute the
4411 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004412 *
4413 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004414 * excepting the variable ones. Note that +1 is for the type of this frame.
4415 */
4416 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004417 ((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 +02004418 /* Compute the data length of this STREAM frame. */
4419 avail_room = room - hlen - *len;
4420 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004421 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004422
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004423 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004424 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004425 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4426 dlen = max_available_room(avail_room, &dlen_sz);
4427 if (dlen > cf->stream.len) {
4428 dlen = cf->stream.len;
4429 }
4430 dlen_sz = quic_int_getsize(dlen);
4431 flen = hlen + dlen_sz + dlen;
4432 }
4433 else {
4434 dlen = QUIC_MIN(avail_room, cf->stream.len);
4435 flen = hlen + dlen;
4436 }
4437 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004438 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004439 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004440 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004441 /* Add the STREAM data length and its encoded length to the packet
4442 * length and the length of this length.
4443 */
4444 *len += flen;
4445 room -= flen;
4446 if (dlen == cf->stream.len) {
4447 /* <cf> STREAM data have been consumed. */
4448 MT_LIST_DELETE_SAFE(tmp1);
4449 LIST_APPEND(&pkt->frms, &cf->list);
4450 }
4451 else {
4452 struct quic_frame *new_cf;
4453
4454 new_cf = pool_zalloc(pool_head_quic_frame);
4455 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004456 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004457 return 0;
4458 }
4459
4460 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004461 new_cf->stream.qcs = cf->stream.qcs;
4462 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004463 new_cf->stream.id = cf->stream.id;
4464 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4465 new_cf->stream.offset = cf->stream.offset;
4466 new_cf->stream.len = dlen;
4467 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4468 /* FIN bit reset */
4469 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4470 new_cf->stream.data = cf->stream.data;
4471 LIST_APPEND(&pkt->frms, &new_cf->list);
4472 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4473 /* Consume <dlen> bytes of the current frame. */
4474 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004475 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004476 cf->stream.data += dlen;
4477 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004478 break;
4479
4480 default:
4481 flen = qc_frm_len(cf);
4482 BUG_ON(!flen);
4483 if (flen > room)
4484 continue;
4485
4486 *len += flen;
4487 room -= flen;
4488 MT_LIST_DELETE_SAFE(tmp1);
4489 LIST_APPEND(&pkt->frms, &cf->list);
4490 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004491 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004492 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004493 }
4494
Frédéric Lécailleea604992020-12-24 13:01:37 +01004495 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004496}
4497
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004498/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004499 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4500 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4501 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004502 * 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 +02004503 * 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 +02004504 * having returned from this function.
4505 * 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 +01004506 * number field in this packet. <pn_len> will also have the packet number
4507 * length as value.
4508 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004509 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004510 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004511static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4512 size_t dglen, struct quic_tx_packet *pkt,
4513 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
4514 int ack, int padding, int cc, int nb_pto_dgrams,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004515 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004516{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004517 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004518 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004519 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4520 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004521 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004522 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004523 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004524 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004525
Frédéric Lécailleea604992020-12-24 13:01:37 +01004526 /* Length field value with CRYPTO frames if present. */
4527 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004528 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004529 /* When not probing and not acking, and no immediate close is required,
4530 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004531 * the congestion controller window. So, we do not limit the size of this
4532 * packet if we have an ACK frame to send because an ACK frame is not
4533 * ack-eliciting. This size will be limited if we have ack-eliciting
4534 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004535 */
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004536 if (!nb_pto_dgrams && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004537 size_t path_room;
4538
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004539 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004540 if (end - beg > path_room)
4541 end = beg + path_room;
4542 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004543
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004544 /* Ensure there is enough room for the TLS encryption tag and a zero token
4545 * length field if any.
4546 */
4547 if (end - pos < QUIC_TLS_TAG_LEN +
4548 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4549 goto no_room;
4550
4551 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004552 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004553 /* packet number length */
4554 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004555 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004556 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004557 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004558 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004559 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004560 goto no_room;
4561
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004562 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4563 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004564 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004565 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004566 /* Build an ACK frame if required. */
4567 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004568 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004569 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004570 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004571 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4572 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4573 * that from here, we do not know if we will have to send a PING frame.
4574 * This will be decided after having computed the ack-eliciting frames
4575 * to be added to this packet.
4576 */
4577 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 +01004578 if (!ack_frm_len)
4579 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004580 }
4581
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004582 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004583 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004584 len_frms = 0;
4585 if (!cc && !MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004586 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004587
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004588 /* Initialize the length of the frames built below to <len>.
4589 * If any frame could be successfully built by qc_build_frms(),
4590 * we will have len_frms > len.
4591 */
4592 len_frms = len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004593 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004594 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004595 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004596 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004597 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004598
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004599 /* Length (of the remaining data). Must not fail because, the buffer size
4600 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4601 * for the encryption tag. It must be taken into an account for the length
4602 * of this packet.
4603 */
4604 if (len_frms)
4605 len = len_frms + QUIC_TLS_TAG_LEN;
4606 else
4607 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004608 /* CONNECTION_CLOSE frame */
4609 if (cc) {
4610 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004611
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004612 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004613 len += qc_frm_len(&cc_frm);
4614 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004615 add_ping_frm = 0;
4616 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004617 len_sz = quic_int_getsize(len);
4618 /* Add this packet size to <dglen> */
4619 dglen += head_len + len_sz + len;
4620 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4621 /* This is a maximum padding size */
4622 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4623 /* The length field value is of this packet is <len> + <padding_len>
4624 * the size of which may be greater than the initial computed size
4625 * <len_sz>. So, let's deduce the difference betwen these to packet
4626 * sizes from <padding_len>.
4627 */
4628 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4629 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004630 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004631 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004632 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004633 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004634 add_ping_frm = 1;
4635 len += 1;
4636 }
4637 /* 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 +01004638 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004639 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4640 }
4641
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004642 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4643 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004644
4645 /* Packet number field address. */
4646 *buf_pn = pos;
4647
4648 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004649 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4650 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004651
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004652 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004653 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004654
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004655 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004656 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004657 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004658
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004659 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004660 list_for_each_entry(cf, &pkt->frms, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004661 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004662 ssize_t room = end - pos;
4663 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004664 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004665 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004666 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004667 }
4668 }
4669
4670 /* Build a PING frame if needed. */
4671 if (add_ping_frm) {
4672 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004673 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004674 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004675 }
4676
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004677 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004678 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004679 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004680
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004681 /* Build a PADDING frame if needed. */
4682 if (padding_len) {
4683 frm.type = QUIC_FT_PADDING;
4684 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004685 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004686 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004687 }
4688
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004689 /* Always reset this variable as this function has no idea
4690 * if it was set. It is handle by the loss detection timer.
4691 */
4692 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004693 pkt->len = pos - beg;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004694
4695 return 1;
4696
4697 no_room:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004698 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004699 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004700}
4701
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004702static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004703{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004704 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004705 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004706 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01004707 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004708 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004709 pkt->next = NULL;
4710 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004711}
4712
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004713/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004714static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4715{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004716 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004717
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004718 if (!pkt)
4719 return;
4720
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004721 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004722 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004723 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004724 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004725 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004726}
4727
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004728/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4729 * type for <qc> QUIC connection from <qel> encryption level.
4730 * Return -2 if the packet could not be allocated or encrypted for any reason,
4731 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004732 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004733static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4734 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004735 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004736 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004737 int pkt_type, int ack, int nb_pto_dgrams, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004738{
4739 /* The pointer to the packet number field. */
4740 unsigned char *buf_pn;
4741 unsigned char *beg, *end, *payload;
4742 int64_t pn;
4743 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004744 struct quic_tls_ctx *tls_ctx;
4745 struct quic_tx_packet *pkt;
4746
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004747 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004748 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004749 pkt = pool_alloc(pool_head_quic_tx_packet);
4750 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004751 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004752 *err = -2;
4753 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004754 }
4755
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004756 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004757 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004758 pn_len = 0;
4759 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004760
4761 pn = qel->pktns->tx.next_pn + 1;
4762 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
4763 ack, padding, cc, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004764 *err = -1;
4765 goto err;
4766 }
4767
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004768 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004769 payload = buf_pn + pn_len;
4770 payload_len = end - payload;
4771 aad_len = payload - beg;
4772
4773 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004774 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4775 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004776 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004777 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004778
4779 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004780 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004781 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4782 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004783 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004784 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004785 goto err;
4786 }
4787
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004788 /* Consume a packet number */
4789 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01004790 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004791 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4792 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004793 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004794 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004795 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004796 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004797 pkt->in_flight_len = pkt->len;
4798 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004799 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004800 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004801 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004802
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004803 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004804
4805 err:
4806 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004807 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004808 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004809}
4810
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004811/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4812 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004813 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004814static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4815 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004816{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004817 size_t try, done = 0;
4818
4819 if (!conn_ctrl_ready(conn))
4820 return 0;
4821
4822 if (!fd_recv_ready(conn->handle.fd))
4823 return 0;
4824
4825 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004826
4827 /* read the largest possible block. For this, we perform only one call
4828 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004829 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004830 */
4831 while (count > 0) {
4832 try = b_contig_space(buf);
4833 if (!try)
4834 break;
4835
4836 if (try > count)
4837 try = count;
4838
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004839 b_add(buf, try);
4840 done += try;
4841 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004842 }
4843
4844 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4845 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4846
4847 leave:
4848 return done;
4849
4850 read0:
4851 conn_sock_read0(conn);
4852 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4853
4854 /* Now a final check for a possible asynchronous low-level error
4855 * report. This can happen when a connection receives a reset
4856 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4857 * we might have come from there by just checking POLL_HUP instead
4858 * of recv()'s return value 0, so we have no way to tell there was
4859 * an error without checking.
4860 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004861 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004862 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4863 goto leave;
4864}
4865
4866
4867/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4868 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4869 * other pending data for example, but this flag is ignored at the moment.
4870 * Only one call to send() is performed, unless the buffer wraps, in which case
4871 * a second call may be performed. The connection's flags are updated with
4872 * whatever special event is detected (error, empty). The caller is responsible
4873 * for taking care of those events and avoiding the call if inappropriate. The
4874 * function does not call the connection's polling update function, so the caller
4875 * is responsible for this. It's up to the caller to update the buffer's contents
4876 * based on the return value.
4877 */
4878static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4879{
4880 ssize_t ret;
4881 size_t try, done;
4882 int send_flag;
4883
4884 if (!conn_ctrl_ready(conn))
4885 return 0;
4886
4887 if (!fd_send_ready(conn->handle.fd))
4888 return 0;
4889
4890 done = 0;
4891 /* send the largest possible block. For this we perform only one call
4892 * to send() unless the buffer wraps and we exactly fill the first hunk,
4893 * in which case we accept to do it once again.
4894 */
4895 while (count) {
4896 try = b_contig_data(buf, done);
4897 if (try > count)
4898 try = count;
4899
4900 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4901 if (try < count || flags & CO_SFL_MSG_MORE)
4902 send_flag |= MSG_MORE;
4903
4904 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4905 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4906 if (ret > 0) {
4907 count -= ret;
4908 done += ret;
4909
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004910 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004911 conn->flags |= CO_FL_WAIT_L4L6;
4912 /* if the system buffer is full, don't insist */
4913 if (ret < try)
4914 break;
4915 }
4916 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4917 /* nothing written, we need to poll for write first */
4918 fd_cant_send(conn->handle.fd);
4919 break;
4920 }
4921 else if (errno != EINTR) {
4922 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4923 break;
4924 }
4925 }
4926 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4927 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4928
4929 if (done > 0) {
4930 /* we count the total bytes sent, and the send rate for 32-byte
4931 * blocks. The reason for the latter is that freq_ctr are
4932 * limited to 4GB and that it's not enough per second.
4933 */
4934 _HA_ATOMIC_ADD(&global.out_bytes, done);
4935 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4936 }
4937 return done;
4938}
4939
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004940/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4941 * event subscriber <es> is not allowed to change from a previous call as long
4942 * as at least one event is still subscribed. The <event_type> must only be a
4943 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4944 */
4945static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4946{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004947 struct qcc *qcc = conn->qc->qcc;
4948
4949 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4950 BUG_ON(qcc->subs && qcc->subs != es);
4951
4952 es->events |= event_type;
4953 qcc->subs = es;
4954
4955 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004956 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004957
4958 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004959 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004960
4961 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004962}
4963
4964/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4965 * The <es> pointer is not allowed to differ from the one passed to the
4966 * subscribe() call. It always returns zero.
4967 */
4968static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4969{
4970 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4971}
4972
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004973/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4974 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4975 * parameters of this session.
4976 * This is the responsibility of the caller to check the validity of all the
4977 * pointers passed as parameter to this function.
4978 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4979 * CO_ER_SSL_NO_MEM.
4980 */
4981static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4982 unsigned char *params, size_t params_len)
4983{
4984 int retry;
4985
4986 retry = 1;
4987 retry:
4988 *ssl = SSL_new(ssl_ctx);
4989 if (!*ssl) {
4990 if (!retry--)
4991 goto err;
4992
4993 pool_gc(NULL);
4994 goto retry;
4995 }
4996
4997 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4998 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
4999 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
5000 goto err;
5001
5002 SSL_free(*ssl);
5003 *ssl = NULL;
5004 if (!retry--)
5005 goto err;
5006
5007 pool_gc(NULL);
5008 goto retry;
5009 }
5010
5011 return 0;
5012
5013 err:
5014 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5015 return -1;
5016}
5017
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005018/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
5019 * connection with <xprt_ctx> as address of the xprt context.
5020 * Returns 1 if succeeded, 0 if not.
5021 */
5022static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5023{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005024 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005025
5026 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5027
5028 if (*xprt_ctx)
5029 goto out;
5030
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005031 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005032 if (!ctx) {
5033 conn->err_code = CO_ER_SYS_MEMLIM;
5034 goto err;
5035 }
5036
5037 ctx->wait_event.tasklet = tasklet_new();
5038 if (!ctx->wait_event.tasklet) {
5039 conn->err_code = CO_ER_SYS_MEMLIM;
5040 goto err;
5041 }
5042
5043 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5044 ctx->wait_event.tasklet->context = ctx;
5045 ctx->wait_event.events = 0;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005046 HA_ATOMIC_STORE(&ctx->conn, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005047 ctx->subs = NULL;
5048 ctx->xprt_ctx = NULL;
5049
5050 ctx->xprt = xprt_get(XPRT_QUIC);
5051 if (objt_server(conn->target)) {
5052 /* Server */
5053 struct server *srv = __objt_server(conn->target);
Amaury Denoyelled4962512021-12-14 17:17:28 +01005054 unsigned char dcid[QUIC_HAP_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005055 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005056 int ssl_err, ipv4;
5057
5058 ssl_err = SSL_ERROR_NONE;
5059 if (RAND_bytes(dcid, sizeof dcid) != 1)
5060 goto err;
5061
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005062 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005063 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005064 dcid, sizeof dcid, 0, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005065 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005066 goto err;
5067
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005068 ctx->qc = qc;
5069
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005070 /* Insert our SCID, the connection ID for the QUIC client. */
5071 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
5072
5073 conn->qc = qc;
5074 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005075 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
5076 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005077 goto err;
5078
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005079 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005080 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005081 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
5082 qc->enc_params_len =
5083 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
5084 &qc->rx.params, 0);
5085 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005086 goto err;
5087
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005088 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
5089 qc->enc_params, qc->enc_params_len) == -1)
5090 goto err;
5091
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005092 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005093 SSL_set_connect_state(ctx->ssl);
5094 ssl_err = SSL_do_handshake(ctx->ssl);
5095 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005096 int st;
5097
5098 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005099 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
5100 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005101 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005102 }
5103 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005104 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005105 goto err;
5106 }
5107 }
5108 }
5109 else if (objt_listener(conn->target)) {
5110 /* Listener */
5111 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02005112 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005113
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005114 ctx->qc = qc;
5115
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01005116 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005117 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5118 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005119 goto err;
5120
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01005121 /* Enabling 0-RTT */
5122 if (bc->ssl_conf.early_data)
5123 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5124
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005125 SSL_set_accept_state(ctx->ssl);
5126 }
5127
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005128 HA_ATOMIC_STORE(xprt_ctx, ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005129
5130 /* Leave init state and start handshake */
5131 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005132
5133 out:
5134 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
5135
5136 return 0;
5137
5138 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02005139 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005140 tasklet_free(ctx->wait_event.tasklet);
5141 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005142 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005143 return -1;
5144}
5145
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005146/* Start the QUIC transport layer */
5147static int qc_xprt_start(struct connection *conn, void *ctx)
5148{
5149 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005150 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005151
5152 qc = conn->qc;
5153 if (!quic_conn_init_timer(qc)) {
5154 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
5155 return 0;
5156 }
5157
5158 tasklet_wakeup(qctx->wait_event.tasklet);
5159 return 1;
5160}
5161
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005162/* transport-layer operations for QUIC connections. */
5163static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005164 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005165 .snd_buf = quic_conn_from_buf,
5166 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005167 .subscribe = quic_conn_subscribe,
5168 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005169 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005170 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005171 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5172 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005173 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005174 .name = "QUIC",
5175};
5176
5177__attribute__((constructor))
5178static void __quic_conn_init(void)
5179{
5180 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5181 xprt_register(XPRT_QUIC, &ssl_quic);
5182}
5183
5184__attribute__((destructor))
5185static void __quic_conn_deinit(void)
5186{
5187 BIO_meth_free(ha_quic_meth);
5188}
5189
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005190/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5191 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005192 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005193 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005194static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005195 struct sockaddr_storage *saddr, qpkt_read_func *func)
5196{
5197 unsigned char *pos;
5198 const unsigned char *end;
5199 struct quic_dgram_ctx dgram_ctx = {
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005200 .qc = NULL,
5201 .dcid.len = 0,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005202 .owner = owner,
5203 };
5204
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005205 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005206 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005207 do {
5208 int ret;
5209 struct quic_rx_packet *pkt;
5210
Willy Tarreaue4498932021-03-22 21:13:05 +01005211 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005212 if (!pkt)
5213 goto err;
5214
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005215 quic_rx_packet_refinc(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005216 ret = func(pos, end, pkt, &dgram_ctx, saddr);
5217 pos += pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02005218 quic_rx_packet_refdec(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005219 if (ret == -1)
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005220 /* If the packet length could not be found, we cannot continue. */
5221 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005222 } while (pos < end);
5223
5224 /* Increasing the received bytes counter by the UDP datagram length
5225 * if this datagram could be associated to a connection.
5226 */
5227 if (dgram_ctx.qc)
5228 dgram_ctx.qc->rx.bytes += len;
5229
5230 return pos - (unsigned char *)buf;
5231
5232 err:
5233 return -1;
5234}
5235
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005236ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005237 struct sockaddr_storage *saddr)
5238{
5239 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
5240}
5241
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005242/* Function to automatically activate QUIC traces on stdout.
5243 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5244 * Main use for now is in the docker image for QUIC interop testing.
5245 */
5246static void quic_init_stdout_traces(void)
5247{
5248#ifdef ENABLE_QUIC_STDOUT_TRACES
5249 trace_quic.sink = sink_find("stdout");
5250 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005251 trace_quic.state = TRACE_STATE_RUNNING;
5252#endif
5253}
5254INITCALL0(STG_INIT, quic_init_stdout_traces);
5255
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005256/*
5257 * Local variables:
5258 * c-indent-level: 8
5259 * c-basic-offset: 8
5260 * End:
5261 */