blob: 0dfd1082bd3a5d0b23a61bfa9e38c3f9886ae348 [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",
139 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
140 .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{
233 const struct connection *conn = a1;
234
235 if (conn) {
236 struct quic_tls_secrets *secs;
237 struct quic_conn *qc;
238
239 qc = conn->qc;
240 chunk_appendf(&trace_buf, " : conn@%p", conn);
241 if ((mask & QUIC_EV_CONN_INIT) && qc) {
242 chunk_appendf(&trace_buf, "\n odcid");
243 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100244 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100245 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100246 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100247 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100248 }
249
250 if (mask & QUIC_EV_CONN_ADDDATA) {
251 const enum ssl_encryption_level_t *level = a2;
252 const size_t *len = a3;
253
254 if (level) {
255 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
256
257 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
258 }
259 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100260 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100261 }
262 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
263 /* Initial read & write secrets. */
264 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
265 const unsigned char *rx_sec = a2;
266 const unsigned char *tx_sec = a3;
267
268 secs = &qc->els[level].tls_ctx.rx;
269 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
270 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
271 if (rx_sec)
272 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
273 quic_tls_keys_hexdump(&trace_buf, secs);
274 }
275 secs = &qc->els[level].tls_ctx.tx;
276 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
277 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
278 if (tx_sec)
279 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
280 quic_tls_keys_hexdump(&trace_buf, secs);
281 }
282 }
283 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
284 const enum ssl_encryption_level_t *level = a2;
285 const unsigned char *secret = a3;
286 const size_t *secret_len = a4;
287
288 if (level) {
289 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
290
291 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
292 if (secret && secret_len)
293 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
294 secs = &qc->els[lvl].tls_ctx.rx;
295 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
296 quic_tls_keys_hexdump(&trace_buf, secs);
297 }
298 }
299
300 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
301 const enum ssl_encryption_level_t *level = a2;
302 const unsigned char *secret = a3;
303 const size_t *secret_len = a4;
304
305 if (level) {
306 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
307
308 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
309 if (secret && secret_len)
310 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
311 secs = &qc->els[lvl].tls_ctx.tx;
312 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
313 quic_tls_keys_hexdump(&trace_buf, secs);
314 }
315
316 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100317
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100318 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100319 const struct quic_tx_packet *pkt = a2;
320 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100321 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100322
323 if (qel) {
324 struct quic_pktns *pktns;
325
326 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100327 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
328 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100329 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100330 (unsigned long long)qc->path->cwnd,
331 (unsigned long long)qc->path->prep_in_flight,
332 (unsigned long long)qc->path->in_flight,
333 (unsigned long long)pktns->tx.in_flight,
334 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100335 }
336 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200337 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100338 if (pkt->pn_node.key != (uint64_t)-1)
339 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100340 list_for_each_entry(frm, &pkt->frms, list)
341 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100342 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
343 }
344
345 if (room) {
346 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
347 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
348 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100349 }
350 }
351
352 if (mask & QUIC_EV_CONN_HDSHK) {
353 const enum quic_handshake_state *state = a2;
354 const int *err = a3;
355
356 if (state)
357 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
358 if (err)
359 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
360 }
361
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100362 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100363 const struct quic_rx_packet *pkt = a2;
364 const unsigned long *pktlen = a3;
365 const SSL *ssl = a4;
366
367 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100368 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100369 pkt, quic_packet_type_enc_level_char(pkt->type));
370 if (pkt->pnl)
371 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
372 (unsigned long long)pkt->pn);
373 if (pkt->token_len)
374 chunk_appendf(&trace_buf, " toklen=%llu",
375 (unsigned long long)pkt->token_len);
376 if (pkt->aad_len)
377 chunk_appendf(&trace_buf, " aadlen=%llu",
378 (unsigned long long)pkt->aad_len);
379 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
380 pkt->flags, (unsigned long long)pkt->len);
381 }
382 if (pktlen)
383 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
384 if (ssl) {
385 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
386 chunk_appendf(&trace_buf, " el=%c",
387 quic_enc_level_char(ssl_to_quic_enc_level(level)));
388 }
389 }
390
391 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
392 const struct quic_rx_packet *pkt = a2;
393 const struct quic_rx_crypto_frm *cf = a3;
394 const SSL *ssl = a4;
395
396 if (pkt)
397 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
398 quic_packet_type_enc_level_char(pkt->type),
399 (unsigned long long)pkt->pn);
400 if (cf)
401 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
402 (unsigned long long)cf->offset_node.key,
403 (unsigned long long)cf->len);
404 if (ssl) {
405 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200406 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100407 quic_enc_level_char(ssl_to_quic_enc_level(level)));
408 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100409
410 if (qc->err_code)
411 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)conn->qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100412 }
413
414 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
415 const struct quic_frame *frm = a2;
416
417 if (frm)
418 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
419 }
420
421 if (mask & QUIC_EV_CONN_PHPKTS) {
422 const struct quic_enc_level *qel = a2;
423
424 if (qel) {
425 struct quic_pktns *pktns;
426
427 pktns = qc->pktns;
428 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200429 " 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 +0100430 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200431 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille2766e782021-08-30 17:16:07 +0200432 !!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100433 (unsigned long long)qc->path->cwnd,
434 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100435 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100436 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
437 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100438 }
439 }
440
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100441 if (mask & QUIC_EV_CONN_ENCPKT) {
442 const struct enc_debug_info *edi = a2;
443
444 if (edi)
445 chunk_appendf(&trace_buf,
446 " payload=@%p payload_len=%llu"
447 " aad=@%p aad_len=%llu pn=%llu",
448 edi->payload, (unsigned long long)edi->payload_len,
449 edi->aad, (unsigned long long)edi->aad_len,
450 (unsigned long long)edi->pn);
451 }
452
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100453 if (mask & QUIC_EV_CONN_RMHP) {
454 const struct quic_rx_packet *pkt = a2;
455
456 if (pkt) {
457 const int *ret = a3;
458
459 chunk_appendf(&trace_buf, " pkt@%p", pkt);
460 if (ret && *ret)
461 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
462 pkt->pnl, (unsigned long long)pkt->pn);
463 }
464 }
465
466 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200467 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100468 const unsigned long *val1 = a3;
469 const unsigned long *val2 = a4;
470
471 if (frm)
472 chunk_tx_frm_appendf(&trace_buf, frm);
473 if (val1)
474 chunk_appendf(&trace_buf, " %lu", *val1);
475 if (val2)
476 chunk_appendf(&trace_buf, "..%lu", *val2);
477 }
478
479 if (mask & QUIC_EV_CONN_RTTUPDT) {
480 const unsigned int *rtt_sample = a2;
481 const unsigned int *ack_delay = a3;
482 const struct quic_loss *ql = a4;
483
484 if (rtt_sample)
485 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
486 if (ack_delay)
487 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
488 if (ql)
489 chunk_appendf(&trace_buf,
490 " srtt=%ums rttvar=%ums min_rtt=%ums",
491 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
492 }
493 if (mask & QUIC_EV_CONN_CC) {
494 const struct quic_cc_event *ev = a2;
495 const struct quic_cc *cc = a3;
496
497 if (a2)
498 quic_cc_event_trace(&trace_buf, ev);
499 if (a3)
500 quic_cc_state_trace(&trace_buf, cc);
501 }
502
503 if (mask & QUIC_EV_CONN_PKTLOSS) {
504 const struct quic_pktns *pktns = a2;
505 const struct list *lost_pkts = a3;
506 struct quic_conn *qc = conn->qc;
507
508 if (pktns) {
509 chunk_appendf(&trace_buf, " pktns=%s",
510 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
511 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
512 if (pktns->tx.loss_time)
513 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100514 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100515 }
516 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
517 struct quic_tx_packet *pkt;
518
519 chunk_appendf(&trace_buf, " lost_pkts:");
520 list_for_each_entry(pkt, lost_pkts, list)
521 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
522 }
523 }
524
525 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
526 struct quic_conn *qc = conn->qc;
527 const struct quic_pktns *pktns = a2;
528 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100529 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100530
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100531 if (ifae_pkts)
532 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
533 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100535 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100536 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100537 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
538 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100539 if (mask & QUIC_EV_CONN_STIMER) {
540 if (pktns->tx.loss_time)
541 chunk_appendf(&trace_buf, " loss_time=%dms",
542 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
543 }
544 if (mask & QUIC_EV_CONN_SPTO) {
545 if (pktns->tx.time_of_last_eliciting)
546 chunk_appendf(&trace_buf, " tole=%dms",
547 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
548 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100549 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100550 }
551 }
552
553 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
554 chunk_appendf(&trace_buf,
555 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
556 }
557 }
558
559 if (mask & QUIC_EV_CONN_SPPKTS) {
560 const struct quic_tx_packet *pkt = a2;
561
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100562 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
563 (unsigned long long)qc->path->cwnd,
564 (unsigned long long)qc->path->prep_in_flight,
565 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100566 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100567 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100568 (unsigned long)pkt->pn_node.key,
569 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
570 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100571 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100572 }
573 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100574
575 if (mask & QUIC_EV_CONN_SSLALERT) {
576 const uint8_t *alert = a2;
577 const enum ssl_encryption_level_t *level = a3;
578
579 if (alert)
580 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
581 if (level)
582 chunk_appendf(&trace_buf, " el=%c",
583 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
584 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100585
586 if (mask & QUIC_EV_CONN_BCFRMS) {
587 const size_t *sz1 = a2;
588 const size_t *sz2 = a3;
589 const size_t *sz3 = a4;
590
591 if (sz1)
592 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
593 if (sz2)
594 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
595 if (sz3)
596 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
597 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100598
599 if (mask & QUIC_EV_CONN_PSTRM) {
600 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100601
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100602 if (frm)
603 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100604 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100605 }
606 if (mask & QUIC_EV_CONN_LPKT) {
607 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200608 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100609
610 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200611 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100612 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200613 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
614 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200615 if (len)
616 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100617 }
618
619}
620
621/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200622static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100623{
624 struct quic_conn *qc;
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200625 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100626
627 qc = ctx->conn->qc;
628 if (objt_server(qc->conn->target))
629 return 1;
630
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200631 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
632 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
633 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
634 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200635 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100636 return 1;
637
638 return 0;
639}
640
641/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
642 * both loss detection and PTO and schedule the task assiated to this timer if needed.
643 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200644static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100645{
646 struct quic_conn *qc;
647 struct quic_pktns *pktns;
648 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200649 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100650
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100651 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
652 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100653 qc = ctx->conn->qc;
654 pktns = quic_loss_pktns(qc);
655 if (tick_isset(pktns->tx.loss_time)) {
656 qc->timer = pktns->tx.loss_time;
657 goto out;
658 }
659
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100660 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100661 * cancelled for a server which reached the anti-amplification limit.
662 */
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100663 if (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
664 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, ctx->conn);
665 qc->timer = TICK_ETERNITY;
666 goto out;
667 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100668
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100669 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
670 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100671 /* Timer cancellation. */
672 qc->timer = TICK_ETERNITY;
673 goto out;
674 }
675
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200676 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
677 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100678 if (tick_isset(pto))
679 qc->timer = pto;
680 out:
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200681 if (qc->timer != TICK_ETERNITY)
682 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100683 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
684}
685
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100686/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
687 * connection.
688 * Return 1 if succeeded, 0 if not.
689 */
690static int quic_tls_key_update(struct quic_conn *qc)
691{
692 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
693 struct quic_tls_secrets *rx, *tx;
694 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
695 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
696
697 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
698 rx = &tls_ctx->rx;
699 tx = &tls_ctx->tx;
700 nxt_rx = &qc->ku.nxt_rx;
701 nxt_tx = &qc->ku.nxt_tx;
702
703 /* Prepare new RX secrets */
704 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
705 rx->secret, rx->secretlen)) {
706 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc->conn);
707 return 0;
708 }
709
710 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
711 nxt_rx->key, nxt_rx->keylen,
712 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
713 nxt_rx->secret, nxt_rx->secretlen)) {
714 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc->conn);
715 return 0;
716 }
717
718 /* Prepare new TX secrets */
719 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
720 tx->secret, tx->secretlen)) {
721 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc->conn);
722 return 0;
723 }
724
725 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
726 nxt_tx->key, nxt_tx->keylen,
727 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
728 nxt_tx->secret, nxt_tx->secretlen)) {
729 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc->conn);
730 return 0;
731 }
732
733 return 1;
734}
735
736/* Rotate the Key Update information for <qc> QUIC connection.
737 * Must be used after having updated them.
738 * Always succeeds.
739 */
740static void quic_tls_rotate_keys(struct quic_conn *qc)
741{
742 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
743 unsigned char *curr_secret, *curr_iv, *curr_key;
744
745 /* Rotate the RX secrets */
746 curr_secret = tls_ctx->rx.secret;
747 curr_iv = tls_ctx->rx.iv;
748 curr_key = tls_ctx->rx.key;
749
750 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
751 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
752 tls_ctx->rx.key = qc->ku.nxt_rx.key;
753
754 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
755 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
756 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
757
758 qc->ku.prv_rx.secret = curr_secret;
759 qc->ku.prv_rx.iv = curr_iv;
760 qc->ku.prv_rx.key = curr_key;
761 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
762
763 /* Update the TX secrets */
764 curr_secret = tls_ctx->tx.secret;
765 curr_iv = tls_ctx->tx.iv;
766 curr_key = tls_ctx->tx.key;
767
768 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
769 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
770 tls_ctx->tx.key = qc->ku.nxt_tx.key;
771
772 qc->ku.nxt_tx.secret = curr_secret;
773 qc->ku.nxt_tx.iv = curr_iv;
774 qc->ku.nxt_tx.key = curr_key;
775}
776
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100777#ifndef OPENSSL_IS_BORINGSSL
778int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
779 const uint8_t *read_secret,
780 const uint8_t *write_secret, size_t secret_len)
781{
782 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
783 struct quic_tls_ctx *tls_ctx =
784 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
785 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100786 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100787
788 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100789 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100790 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
791 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, conn);
792 goto out;
793 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100794
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100795 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
796 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, conn);
797 goto err;
798 }
799
800 rx = &tls_ctx->rx;
801 tx = &tls_ctx->tx;
802
803 rx->aead = tx->aead = tls_aead(cipher);
804 rx->md = tx->md = tls_md(cipher);
805 rx->hp = tx->hp = tls_hp(cipher);
806
807 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
808 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100809 read_secret, secret_len)) {
810 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100811 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100812 }
813
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100814 rx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100815
816 if (!write_secret)
817 goto tp;
818
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100819 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
820 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100821 write_secret, secret_len)) {
822 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100823 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100824 }
825
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100826 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100827 tp:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100828 if (objt_server(conn->target) && level == ssl_encryption_application) {
829 const unsigned char *buf;
830 size_t buflen;
831
832 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
833 if (!buflen)
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100834 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100835
836 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100837 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100838 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100839
840 if (level == ssl_encryption_application) {
841 struct quic_conn *qc = conn->qc;
842 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
843 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
844 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
845
846 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
847 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
848 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, conn);
849 goto err;
850 }
851
852 memcpy(rx->secret, read_secret, secret_len);
853 rx->secretlen = secret_len;
854 memcpy(tx->secret, write_secret, secret_len);
855 tx->secretlen = secret_len;
856 /* Initialize all the secret keys lengths */
857 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
858 /* Prepare the next key update */
859 if (!quic_tls_key_update(qc))
860 goto err;
861 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100862 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100863 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100864 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100865
866 err:
867 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, conn);
868 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100869}
870#else
871/* ->set_read_secret callback to derive the RX secrets at <level> encryption
872 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500873 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100874 */
875int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
876 const SSL_CIPHER *cipher,
877 const uint8_t *secret, size_t secret_len)
878{
879 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
880 struct quic_tls_ctx *tls_ctx =
881 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
882
883 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100884 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
885 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, conn);
886 goto out;
887 }
888
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100889 tls_ctx->rx.aead = tls_aead(cipher);
890 tls_ctx->rx.md = tls_md(cipher);
891 tls_ctx->rx.hp = tls_hp(cipher);
892
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100893 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
894 goto err;
895
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100896 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 +0100897 tls_ctx->rx.key, tls_ctx->rx.keylen,
898 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100899 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
900 secret, secret_len)) {
901 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
902 goto err;
903 }
904
905 if (objt_server(conn->target) && level == ssl_encryption_application) {
906 const unsigned char *buf;
907 size_t buflen;
908
909 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
910 if (!buflen)
911 goto err;
912
913 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
914 goto err;
915 }
916
917 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100918 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100919 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
920
921 return 1;
922
923 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100924 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100925 return 0;
926}
927
928/* ->set_write_secret callback to derive the TX secrets at <level>
929 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500930 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100931 */
932int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
933 const SSL_CIPHER *cipher,
934 const uint8_t *secret, size_t secret_len)
935{
936 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
937 struct quic_tls_ctx *tls_ctx =
938 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
939
940 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100941 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
942 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, conn);
943 goto out;
944 }
945
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100946 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
947 goto err;
948
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100949 tls_ctx->tx.aead = tls_aead(cipher);
950 tls_ctx->tx.md = tls_md(cipher);
951 tls_ctx->tx.hp = tls_hp(cipher);
952
953 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 +0100954 tls_ctx->tx.key, tls_ctx->tx.keylen,
955 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100956 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
957 secret, secret_len)) {
958 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
959 goto err;
960 }
961
962 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
963 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100964 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100965 return 1;
966
967 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100968 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100969 return 0;
970}
971#endif
972
973/* This function copies the CRYPTO data provided by the TLS stack found at <data>
974 * with <len> as size in CRYPTO buffers dedicated to store the information about
975 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
976 * It fails only if it could not managed to allocate enough CRYPTO buffers to
977 * store all the data.
978 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
979 */
980static int quic_crypto_data_cpy(struct quic_enc_level *qel,
981 const unsigned char *data, size_t len)
982{
983 struct quic_crypto_buf **qcb;
984 /* The remaining byte to store in CRYPTO buffers. */
985 size_t cf_offset, cf_len, *nb_buf;
986 unsigned char *pos;
987
988 nb_buf = &qel->tx.crypto.nb_buf;
989 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
990 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
991 cf_len = len;
992
993 while (len) {
994 size_t to_copy, room;
995
996 pos = (*qcb)->data + (*qcb)->sz;
997 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
998 to_copy = len > room ? room : len;
999 if (to_copy) {
1000 memcpy(pos, data, to_copy);
1001 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1002 qel->tx.crypto.sz += to_copy;
1003 (*qcb)->sz += to_copy;
1004 pos += to_copy;
1005 len -= to_copy;
1006 data += to_copy;
1007 }
1008 else {
1009 struct quic_crypto_buf **tmp;
1010
1011 tmp = realloc(qel->tx.crypto.bufs,
1012 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1013 if (tmp) {
1014 qel->tx.crypto.bufs = tmp;
1015 qcb = &qel->tx.crypto.bufs[*nb_buf];
1016 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1017 if (!*qcb)
1018 return 0;
1019
1020 (*qcb)->sz = 0;
1021 ++*nb_buf;
1022 }
1023 else {
1024 break;
1025 }
1026 }
1027 }
1028
1029 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1030 * have been buffered.
1031 */
1032 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001033 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001034
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001035 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001036 if (!frm)
1037 return 0;
1038
1039 frm->type = QUIC_FT_CRYPTO;
1040 frm->crypto.offset = cf_offset;
1041 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02001042 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001043 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001044 }
1045
1046 return len == 0;
1047}
1048
1049
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001050/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1051void quic_set_tls_alert(struct quic_conn *qc, int alert)
1052{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001053 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001054 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
1055 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc->conn);
1056}
1057
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001058/* Set the application for <qc> QUIC connection.
1059 * Return 1 if succeeded, 0 if not.
1060 */
1061int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1062{
1063 const struct qcc_app_ops *app_ops;
1064
1065 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) {
1066 app_ops = qc->qcc->app_ops = &h3_ops;
1067 }
1068 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) {
1069 app_ops = qc->qcc->app_ops = &hq_interop_ops;
1070 }
1071 else
1072 return 0;
1073
1074 if (app_ops->init && !app_ops->init(qc->qcc))
1075 return 0;
1076
1077 if (app_ops->finalize)
1078 app_ops->finalize(qc->qcc->ctx);
1079
1080 return 1;
1081}
1082
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001083/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1084 * wants to provide the QUIC layer with CRYPTO data.
1085 * Returns 1 if succeeded, 0 if not.
1086 */
1087int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1088 const uint8_t *data, size_t len)
1089{
1090 struct connection *conn;
1091 enum quic_tls_enc_level tel;
1092 struct quic_enc_level *qel;
1093
1094 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1095 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001096 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1097 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, conn);
1098 goto out;
1099 }
1100
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001101 tel = ssl_to_quic_enc_level(level);
1102 qel = &conn->qc->els[tel];
1103
1104 if (tel == -1) {
1105 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
1106 goto err;
1107 }
1108
1109 if (!quic_crypto_data_cpy(qel, data, len)) {
1110 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
1111 goto err;
1112 }
1113
1114 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
1115 conn, &level, &len);
1116
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001117 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001118 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
1119 return 1;
1120
1121 err:
1122 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
1123 return 0;
1124}
1125
1126int ha_quic_flush_flight(SSL *ssl)
1127{
1128 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1129
1130 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
1131 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
1132
1133 return 1;
1134}
1135
1136int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1137{
1138 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1139
Frédéric Lécaille47c433f2020-12-10 17:03:11 +01001140 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001141 quic_set_tls_alert(conn->qc, alert);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001142 HA_ATOMIC_STORE(&conn->qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001143 return 1;
1144}
1145
1146/* QUIC TLS methods */
1147static SSL_QUIC_METHOD ha_quic_method = {
1148#ifdef OPENSSL_IS_BORINGSSL
1149 .set_read_secret = ha_set_rsec,
1150 .set_write_secret = ha_set_wsec,
1151#else
1152 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1153#endif
1154 .add_handshake_data = ha_quic_add_handshake_data,
1155 .flush_flight = ha_quic_flush_flight,
1156 .send_alert = ha_quic_send_alert,
1157};
1158
1159/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1160 * Returns an error count.
1161 */
1162int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1163{
1164 struct proxy *curproxy = bind_conf->frontend;
1165 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1166 int cfgerr = 0;
1167
1168#if 0
1169 /* XXX Did not manage to use this. */
1170 const char *ciphers =
1171 "TLS_AES_128_GCM_SHA256:"
1172 "TLS_AES_256_GCM_SHA384:"
1173 "TLS_CHACHA20_POLY1305_SHA256:"
1174 "TLS_AES_128_CCM_SHA256";
1175#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001176 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001177 long options =
1178 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1179 SSL_OP_SINGLE_ECDH_USE |
1180 SSL_OP_CIPHER_SERVER_PREFERENCE;
1181 SSL_CTX *ctx;
1182
1183 ctx = SSL_CTX_new(TLS_server_method());
1184 bind_conf->initial_ctx = ctx;
1185
1186 SSL_CTX_set_options(ctx, options);
1187#if 0
1188 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1189 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1190 "for bind '%s' at [%s:%d].\n",
1191 curproxy->id, ciphers,
1192 bind_conf->arg, bind_conf->file, bind_conf->line);
1193 cfgerr++;
1194 }
1195#endif
1196
1197 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1198 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1199 "for bind '%s' at [%s:%d].\n",
1200 curproxy->id, groups,
1201 bind_conf->arg, bind_conf->file, bind_conf->line);
1202 cfgerr++;
1203 }
1204
1205 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1206 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1207 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1208 SSL_CTX_set_default_verify_paths(ctx);
1209
1210#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1211#ifdef OPENSSL_IS_BORINGSSL
1212 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1213 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1214#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1215 if (bind_conf->ssl_conf.early_data) {
1216 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001217 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001218 }
1219 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1220 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1221#else
1222 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1223#endif
1224 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1225#endif
1226 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1227
1228 return cfgerr;
1229}
1230
1231/* Decode an expected packet number from <truncated_on> its truncated value,
1232 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1233 * the number of bits used to encode this packet number (its length in bytes * 8).
1234 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1235 */
1236static uint64_t decode_packet_number(uint64_t largest_pn,
1237 uint32_t truncated_pn, unsigned int pn_nbits)
1238{
1239 uint64_t expected_pn = largest_pn + 1;
1240 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1241 uint64_t pn_hwin = pn_win / 2;
1242 uint64_t pn_mask = pn_win - 1;
1243 uint64_t candidate_pn;
1244
1245
1246 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1247 /* Note that <pn_win> > <pn_hwin>. */
1248 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1249 candidate_pn + pn_hwin <= expected_pn)
1250 return candidate_pn + pn_win;
1251
1252 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1253 return candidate_pn - pn_win;
1254
1255 return candidate_pn;
1256}
1257
1258/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1259 * cryptographic context.
1260 * <largest_pn> is the largest received packet number and <pn> the address of
1261 * the packet number field for this packet with <byte0> address of its first byte.
1262 * <end> points to one byte past the end of this packet.
1263 * Returns 1 if succeeded, 0 if not.
1264 */
1265static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1266 int64_t largest_pn, unsigned char *pn,
1267 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001268 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001269{
1270 int ret, outlen, i, pnlen;
1271 uint64_t packet_number;
1272 uint32_t truncated_pn = 0;
1273 unsigned char mask[5] = {0};
1274 unsigned char *sample;
1275 EVP_CIPHER_CTX *cctx;
1276 unsigned char *hp_key;
1277
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001278 /* Check there is enough data in this packet. */
1279 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1280 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1281 return 0;
1282 }
1283
1284 cctx = EVP_CIPHER_CTX_new();
1285 if (!cctx) {
1286 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1287 return 0;
1288 }
1289
1290 ret = 0;
1291 sample = pn + QUIC_PACKET_PN_MAXLEN;
1292
1293 hp_key = tls_ctx->rx.hp_key;
1294 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1295 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1296 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1297 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1298 goto out;
1299 }
1300
1301 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1302 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1303 for (i = 0; i < pnlen; i++) {
1304 pn[i] ^= mask[i + 1];
1305 truncated_pn = (truncated_pn << 8) | pn[i];
1306 }
1307
1308 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1309 /* Store remaining information for this unprotected header */
1310 pkt->pn = packet_number;
1311 pkt->pnl = pnlen;
1312
1313 ret = 1;
1314
1315 out:
1316 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001317
1318 return ret;
1319}
1320
1321/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1322 * address, with <payload_len> as payload length, <aad> as address of
1323 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1324 * context.
1325 * Returns 1 if succeeded, 0 if not.
1326 */
1327static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1328 unsigned char *aad, size_t aad_len, uint64_t pn,
1329 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1330{
1331 unsigned char iv[12];
1332 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001333 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001334 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001335
1336 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1337 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001338 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001339 }
1340
1341 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1342 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1343 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001344 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001345 }
1346
1347 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001348
1349 err:
1350 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1351 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1352 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001353}
1354
1355/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1356 * Returns 1 if succeeded, 0 if not.
1357 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001358static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001359{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001360 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001361 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001362 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001363 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001364 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1365 unsigned char *rx_key = tls_ctx->rx.key;
1366
1367 kp_changed = 0;
1368 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1369 /* The two tested bits are not at the same position,
1370 * this is why they are first both inversed.
1371 */
1372 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1373 if (pkt->pn < tls_ctx->rx.pn) {
1374 /* The lowest packet number of a previous key phase
1375 * cannot be null if it really stores previous key phase
1376 * secrets.
1377 */
1378 if (!pkt->qc->ku.prv_rx.pn)
1379 return 0;
1380
1381 rx_iv = pkt->qc->ku.prv_rx.iv;
1382 rx_key = pkt->qc->ku.prv_rx.key;
1383 }
1384 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1385 /* Next key phase */
1386 kp_changed = 1;
1387 rx_iv = pkt->qc->ku.nxt_rx.iv;
1388 rx_key = pkt->qc->ku.nxt_rx.key;
1389 }
1390 }
1391 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001392
1393 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1394 return 0;
1395
1396 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1397 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001398 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001399 if (!ret)
1400 return 0;
1401
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001402 /* Update the keys only if the packet decryption succeeded. */
1403 if (kp_changed) {
1404 quic_tls_rotate_keys(pkt->qc);
1405 /* Toggle the Key Phase bit */
1406 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1407 /* Store the lowest packet number received for the current key phase */
1408 tls_ctx->rx.pn = pkt->pn;
1409 /* Prepare the next key update */
1410 if (!quic_tls_key_update(pkt->qc))
1411 return 0;
1412 }
1413
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001414 /* Update the packet length (required to parse the frames). */
1415 pkt->len = pkt->aad_len + ret;
1416
1417 return 1;
1418}
1419
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001420/* Remove from <qcs> stream the acknowledged frames.
1421 * Never fails.
1422 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001423static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001424{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001425 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001426 struct eb64_node *frm_node;
1427
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001428 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001429 frm_node = eb64_first(&qcs->tx.acked_frms);
1430 while (frm_node) {
1431 struct quic_stream *strm;
1432
1433 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1434 if (strm->offset.key != qcs->tx.ack_offset)
1435 break;
1436
1437 b_del(strm->buf, strm->len);
1438 qcs->tx.ack_offset += strm->len;
1439 frm_node = eb64_next(frm_node);
1440 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001441 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001442 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001443
1444 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001445}
1446
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001447/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001448static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001449 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001450{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001451 int stream_acked;
1452 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001453
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001454 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001455 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001456 switch (frm->type) {
1457 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1458 {
1459 struct qcs *qcs = frm->stream.qcs;
1460 struct quic_stream *strm = &frm->stream;
1461
1462 if (qcs->tx.ack_offset == strm->offset.key) {
1463 b_del(strm->buf, strm->len);
1464 qcs->tx.ack_offset += strm->len;
1465 LIST_DELETE(&frm->list);
1466 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001467 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001468 }
1469 else {
1470 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1471 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001472 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001473 }
1474 break;
1475 default:
1476 LIST_DELETE(&frm->list);
1477 pool_free(pool_head_quic_frame, frm);
1478 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001479
1480 if (stream_acked) {
1481 struct qcc *qcc = qc->qcc;
1482
1483 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1484 tasklet_wakeup(qcc->subs->tasklet);
1485 qcc->subs->events &= ~SUB_RETRY_SEND;
1486 if (!qcc->subs->events)
1487 qcc->subs = NULL;
1488 }
1489 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001490}
1491
1492/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1493 * deallocating them, and their TX frames.
1494 * Returns the last node reached to be used for the next range.
1495 * May be NULL if <largest> node could not be found.
1496 */
1497static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1498 struct list *newly_acked_pkts,
1499 struct eb64_node *largest_node,
1500 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001501 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001502{
1503 struct eb64_node *node;
1504 struct quic_tx_packet *pkt;
1505
1506 if (largest_node)
1507 node = largest_node;
1508 else {
1509 node = eb64_lookup(pkts, largest);
1510 while (!node && largest > smallest) {
1511 node = eb64_lookup(pkts, --largest);
1512 }
1513 }
1514
1515 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001516 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001517
1518 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1519 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001520 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001521 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1522 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1523 qc_treat_acked_tx_frm(frm, ctx);
1524 node = eb64_prev(node);
1525 eb64_delete(&pkt->pn_node);
1526 }
1527
1528 return node;
1529}
1530
1531/* Treat <frm> frame whose packet it is attached to has just been detected as non
1532 * acknowledged.
1533 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001534static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001535 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001536 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001537{
1538 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001539 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001540 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001541}
1542
1543
1544/* Free the TX packets of <pkts> list */
1545static inline void free_quic_tx_pkts(struct list *pkts)
1546{
1547 struct quic_tx_packet *pkt, *tmp;
1548
1549 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001550 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001551 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001552 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001553 }
1554}
1555
1556/* Send a packet loss event nofification to the congestion controller
1557 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1558 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1559 * at <now_us> current time.
1560 * Always succeeds.
1561 */
1562static inline void qc_cc_loss_event(struct quic_conn *qc,
1563 unsigned int lost_bytes,
1564 unsigned int newest_time_sent,
1565 unsigned int period,
1566 unsigned int now_us)
1567{
1568 struct quic_cc_event ev = {
1569 .type = QUIC_CC_EVT_LOSS,
1570 .loss.now_ms = now_ms,
1571 .loss.max_ack_delay = qc->max_ack_delay,
1572 .loss.lost_bytes = lost_bytes,
1573 .loss.newest_time_sent = newest_time_sent,
1574 .loss.period = period,
1575 };
1576
1577 quic_cc_event(&qc->path->cc, &ev);
1578}
1579
1580/* Send a packet ack event nofication for each newly acked packet of
1581 * <newly_acked_pkts> list and free them.
1582 * Always succeeds.
1583 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001584static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001585 struct list *newly_acked_pkts)
1586{
1587 struct quic_conn *qc = ctx->conn->qc;
1588 struct quic_tx_packet *pkt, *tmp;
1589 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1590
1591 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1592 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001593 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001594 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001595 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001596 ev.ack.acked = pkt->in_flight_len;
1597 ev.ack.time_sent = pkt->time_sent;
1598 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001599 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001600 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001601 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001602 }
1603
1604}
1605
1606/* Handle <pkts> list of lost packets detected at <now_us> handling
1607 * their TX frames.
1608 * Send a packet loss event to the congestion controller if
1609 * in flight packet have been lost.
1610 * Also frees the packet in <pkts> list.
1611 * Never fails.
1612 */
1613static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001614 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001615 struct list *pkts,
1616 uint64_t now_us)
1617{
1618 struct quic_conn *qc = ctx->conn->qc;
1619 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001620 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001621 uint64_t lost_bytes;
1622
1623 lost_bytes = 0;
1624 oldest_lost = newest_lost = NULL;
1625 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1626 lost_bytes += pkt->in_flight_len;
1627 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001628 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001629 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001630 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001631 /* Treat the frames of this lost packet. */
1632 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1633 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001634 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001635 if (!oldest_lost) {
1636 oldest_lost = newest_lost = pkt;
1637 }
1638 else {
1639 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001640 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001641 newest_lost = pkt;
1642 }
1643 }
1644
1645 if (lost_bytes) {
1646 /* Sent a packet loss event to the congestion controller. */
1647 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1648 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001649 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001650 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001651 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001652 }
1653}
1654
1655/* Look for packet loss from sent packets for <qel> encryption level of a
1656 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1657 * their tree if deemed as lost or set the <loss_time> value the packet number
1658 * space if any not deemed lost.
1659 * Should be called after having received an ACK frame with newly acknowledged
1660 * packets or when the the loss detection timer has expired.
1661 * Always succeeds.
1662 */
1663static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1664 struct quic_conn *qc,
1665 struct list *lost_pkts)
1666{
1667 struct eb_root *pkts;
1668 struct eb64_node *node;
1669 struct quic_loss *ql;
1670 unsigned int loss_delay;
1671
1672 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1673 pkts = &pktns->tx.pkts;
1674 pktns->tx.loss_time = TICK_ETERNITY;
1675 if (eb_is_empty(pkts))
1676 goto out;
1677
1678 ql = &qc->path->loss;
1679 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1680 loss_delay += loss_delay >> 3;
1681 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1682
1683 node = eb64_first(pkts);
1684 while (node) {
1685 struct quic_tx_packet *pkt;
1686 int64_t largest_acked_pn;
1687 unsigned int loss_time_limit, time_sent;
1688
1689 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001690 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001691 node = eb64_next(node);
1692 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1693 break;
1694
1695 time_sent = pkt->time_sent;
1696 loss_time_limit = tick_add(time_sent, loss_delay);
1697 if (tick_is_le(time_sent, now_ms) ||
1698 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1699 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001700 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001701 }
1702 else {
1703 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1704 }
1705 }
1706
1707 out:
1708 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1709}
1710
1711/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1712 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001713 * 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 +01001714 * acked ack-eliciting packet.
1715 * Return 1, if succeeded, 0 if not.
1716 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001717static 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 +01001718 struct quic_enc_level *qel,
1719 unsigned int *rtt_sample,
1720 const unsigned char **pos, const unsigned char *end)
1721{
1722 struct quic_ack *ack = &frm->ack;
1723 uint64_t smallest, largest;
1724 struct eb_root *pkts;
1725 struct eb64_node *largest_node;
1726 unsigned int time_sent, pkt_flags;
1727 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1728 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1729
1730 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1731 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1732 ctx->conn,, &ack->largest_ack);
1733 goto err;
1734 }
1735
1736 if (ack->first_ack_range > ack->largest_ack) {
1737 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1738 ctx->conn,, &ack->first_ack_range);
1739 goto err;
1740 }
1741
1742 largest = ack->largest_ack;
1743 smallest = largest - ack->first_ack_range;
1744 pkts = &qel->pktns->tx.pkts;
1745 pkt_flags = 0;
1746 largest_node = NULL;
1747 time_sent = 0;
1748
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001749 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 +01001750 largest_node = eb64_lookup(pkts, largest);
1751 if (!largest_node) {
1752 TRACE_DEVEL("Largest acked packet not found",
1753 QUIC_EV_CONN_PRSAFRM, ctx->conn);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001754 }
1755 else {
1756 time_sent = eb64_entry(&largest_node->node,
1757 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001758 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001759 }
1760
1761 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1762 ctx->conn,, &largest, &smallest);
1763 do {
1764 uint64_t gap, ack_range;
1765
1766 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1767 largest_node, largest, smallest, ctx);
1768 if (!ack->ack_range_num--)
1769 break;
1770
1771 if (!quic_dec_int(&gap, pos, end))
1772 goto err;
1773
1774 if (smallest < gap + 2) {
1775 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1776 ctx->conn,, &gap, &smallest);
1777 goto err;
1778 }
1779
1780 largest = smallest - gap - 2;
1781 if (!quic_dec_int(&ack_range, pos, end))
1782 goto err;
1783
1784 if (largest < ack_range) {
1785 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1786 ctx->conn,, &largest, &ack_range);
1787 goto err;
1788 }
1789
1790 /* Do not use this node anymore. */
1791 largest_node = NULL;
1792 /* Next range */
1793 smallest = largest - ack_range;
1794
1795 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1796 ctx->conn,, &largest, &smallest);
1797 } while (1);
1798
1799 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001800 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001801
1802 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1803 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001804 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001805 }
1806
1807 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1808 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1809 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1810 if (!LIST_ISEMPTY(&lost_pkts))
1811 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1812 }
1813 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1814 if (quic_peer_validated_addr(ctx))
1815 ctx->conn->qc->path->loss.pto_count = 0;
1816 qc_set_timer(ctx);
1817 }
1818
1819
1820 return 1;
1821
1822 err:
1823 free_quic_tx_pkts(&newly_acked_pkts);
1824 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1825 return 0;
1826}
1827
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001828/* This function gives the detail of the SSL error. It is used only
1829 * if the debug mode and the verbose mode are activated. It dump all
1830 * the SSL error until the stack was empty.
1831 */
1832static forceinline void qc_ssl_dump_errors(struct connection *conn)
1833{
1834 if (unlikely(global.mode & MODE_DEBUG)) {
1835 while (1) {
1836 unsigned long ret;
1837
1838 ret = ERR_get_error();
1839 if (!ret)
1840 return;
1841
1842 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1843 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1844 }
1845 }
1846}
1847
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001848int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1849 const char **str, int *len);
1850
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001851/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1852 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001853 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001854 * Return 1 if succeeded, 0 if not.
1855 */
1856static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001857 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001858 const unsigned char *data, size_t len,
1859 struct quic_rx_packet *pkt,
1860 struct quic_rx_crypto_frm *cf)
1861{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001862 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001863 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001864
1865 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1866 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001867 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001868 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1869 TRACE_PROTO("SSL_provide_quic_data() error",
1870 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1871 goto err;
1872 }
1873
1874 el->rx.crypto.offset += len;
1875 TRACE_PROTO("in order CRYPTO data",
1876 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1877
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001878 state = HA_ATOMIC_LOAD(&qc->state);
1879 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001880 ssl_err = SSL_do_handshake(ctx->ssl);
1881 if (ssl_err != 1) {
1882 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1883 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1884 TRACE_PROTO("SSL handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001885 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001886 goto out;
1887 }
1888
1889 TRACE_DEVEL("SSL handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001890 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001891 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001892 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001893 goto err;
1894 }
1895
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001896 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001897 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001898 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001899 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001900 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001901 } else {
1902 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1903 if (ssl_err != 1) {
1904 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1905 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1906 TRACE_DEVEL("SSL post handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001907 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001908 goto out;
1909 }
1910
1911 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001912 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001913 goto err;
1914 }
1915
1916 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001917 QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001918 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001919
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001920 out:
1921 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1922 return 1;
1923
1924 err:
1925 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1926 return 0;
1927}
1928
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001929/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1930 * <pkt> RX packet.
1931 * Return it if succeeded, NULL if not.
1932 */
1933static inline
1934struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1935 struct quic_rx_packet *pkt)
1936{
1937 struct quic_rx_strm_frm *frm;
1938
1939 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1940 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001941 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001942 frm->len = stream_frm->len;
1943 frm->data = stream_frm->data;
1944 frm->pkt = pkt;
1945 }
1946
1947 return frm;
1948}
1949
1950/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05001951 * several streams, depending on the already open ones.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001952 * Return this node if succeeded, NULL if not.
1953 */
1954static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1955{
1956 unsigned int strm_type;
1957 int64_t sub_id;
1958 struct eb64_node *strm_node;
1959
1960 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1961
1962 strm_type = id & QCS_ID_TYPE_MASK;
1963 sub_id = id >> QCS_ID_TYPE_SHIFT;
1964 strm_node = NULL;
1965 if (qc_local_stream_id(qcc, id)) {
1966 /* Local streams: this stream must be already opened. */
1967 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1968 if (!strm_node) {
1969 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1970 goto out;
1971 }
1972 }
1973 else {
1974 /* Remote streams. */
1975 struct eb_root *strms;
1976 uint64_t largest_id;
1977 enum qcs_type qcs_type;
1978
1979 strms = &qcc->streams_by_id;
1980 qcs_type = qcs_id_type(id);
1981 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1982 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1983 goto out;
1984 }
1985
1986 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1987 * correct value.
1988 */
1989 largest_id = qcc->strms[qcs_type].largest_id;
1990 if (sub_id > (int64_t)largest_id) {
1991 /* RFC: "A stream ID that is used out of order results in all streams
1992 * of that type with lower-numbered stream IDs also being opened".
1993 * So, let's "open" these streams.
1994 */
1995 int64_t i;
1996 struct qcs *qcs;
1997
1998 qcs = NULL;
1999 for (i = largest_id + 1; i <= sub_id; i++) {
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002000 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
2001 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
2002 qcs = qcs_new(qcc, id, type);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002003 if (!qcs) {
2004 TRACE_PROTO("Could not allocate a new stream",
2005 QUIC_EV_CONN_PSTRM, qcc->conn);
2006 goto out;
2007 }
2008
2009 qcc->strms[qcs_type].largest_id = i;
2010 }
2011 if (qcs)
2012 strm_node = &qcs->by_id;
2013 }
2014 else {
2015 strm_node = eb64_lookup(strms, id);
2016 }
2017 }
2018
2019 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
2020 return strm_node;
2021
2022 out:
2023 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
2024 return NULL;
2025}
2026
2027/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
2028 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
2029 * to reflect the data which have been consumed.
2030 */
2031static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
2032{
2033 size_t ret;
2034
2035 ret = 0;
2036 while (strm_frm->len) {
2037 size_t try;
2038
2039 try = b_contig_space(buf);
2040 if (!try)
2041 break;
2042
2043 if (try > strm_frm->len)
2044 try = strm_frm->len;
2045 memcpy(b_tail(buf), strm_frm->data, try);
2046 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002047 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002048 b_add(buf, try);
2049 ret += try;
2050 }
2051
2052 return ret;
2053}
2054
2055/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2056 * streams may be open. The data are copied to the stream RX buffer if possible.
2057 * If not, the STREAM frame is stored to be treated again later.
2058 * We rely on the flow control so that not to store too much STREAM frames.
2059 * Return 1 if succeeded, 0 if not.
2060 */
2061static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2062 struct quic_stream *strm_frm,
2063 struct quic_conn *qc)
2064{
2065 struct qcs *strm;
2066 struct eb64_node *strm_node, *frm_node;
2067 struct quic_rx_strm_frm *frm;
2068
2069 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2070 if (!strm_node) {
2071 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
2072 return 0;
2073 }
2074
2075 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002076 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002077 /* FIXME: handle the case where this frame overlap others */
2078 if (frm_node) {
2079 TRACE_PROTO("Already existing stream data",
2080 QUIC_EV_CONN_PSTRM, qc->conn);
2081 goto out;
2082 }
2083
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002084 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002085 int ret;
2086
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002087 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002088 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002089 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002090
2091 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Amaury Denoyelledb443382021-11-30 11:23:29 +01002092 if (ret && qc->qcc->app_ops->decode_qcs(strm, strm_frm->fin, qc->qcc->ctx) < 0) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002093 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
2094 return 0;
2095 }
2096
2097 strm->rx.offset += ret;
2098 }
2099
2100 if (!strm_frm->len)
2101 goto out;
2102
2103 store_frm:
2104 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2105 if (!frm) {
2106 TRACE_PROTO("Could not alloc RX STREAM frame",
2107 QUIC_EV_CONN_PSTRM, qc->conn);
2108 return 0;
2109 }
2110
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002111 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002112 quic_rx_packet_refinc(pkt);
2113
2114 out:
2115 return 1;
2116}
2117
2118/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2119 * streams may be open. The data are copied to the stream RX buffer if possible.
2120 * If not, the STREAM frame is stored to be treated again later.
2121 * We rely on the flow control so that not to store too much STREAM frames.
2122 * Return 1 if succeeded, 0 if not.
2123 */
2124static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2125 struct quic_stream *strm_frm,
2126 struct quic_conn *qc)
2127{
2128 struct qcs *strm;
2129 struct eb64_node *strm_node, *frm_node;
2130 struct quic_rx_strm_frm *frm;
2131 size_t strm_frm_len;
2132
2133 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2134 if (!strm_node) {
2135 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
2136 return 0;
2137 }
2138
2139 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002140 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002141 /* FIXME: handle the case where this frame overlap others */
2142 if (frm_node) {
2143 TRACE_PROTO("Already existing stream data",
2144 QUIC_EV_CONN_PSTRM, qc->conn);
2145 goto out;
2146 }
2147
2148 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002149 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002150 int ret;
2151
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002152 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002153 goto store_frm;
2154
2155 /* qc_strm_cpy() will modify the offset, depending on the number
2156 * of bytes copied.
2157 */
2158 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2159 /* Inform the application of the arrival of this new stream */
2160 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
2161 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
2162 return 0;
2163 }
2164
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002165 if (ret)
2166 qcs_notify_recv(strm);
2167
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002168 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002169 }
2170 /* Take this frame into an account for the stream flow control */
2171 strm->rx.offset += strm_frm_len;
2172 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002173 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002174 */
2175 if (!strm_frm->len)
2176 goto out;
2177
2178 store_frm:
2179 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2180 if (!frm) {
2181 TRACE_PROTO("Could not alloc RX STREAM frame",
2182 QUIC_EV_CONN_PSTRM, qc->conn);
2183 return 0;
2184 }
2185
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002186 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002187 quic_rx_packet_refinc(pkt);
2188
2189 out:
2190 return 1;
2191}
2192
2193static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2194 struct quic_stream *strm_frm,
2195 struct quic_conn *qc)
2196{
2197 if (strm_frm->id & QCS_ID_DIR_BIT)
2198 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2199 else
2200 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2201}
2202
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002203/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2204 * as I/O handler context and <qel> as encryption level.
2205 * Returns 1 if succeeded, 0 if failed.
2206 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002207static 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 +01002208 struct quic_enc_level *qel)
2209{
2210 struct quic_frame frm;
2211 const unsigned char *pos, *end;
2212 struct quic_conn *conn = ctx->conn->qc;
2213
2214 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
2215 /* Skip the AAD */
2216 pos = pkt->data + pkt->aad_len;
2217 end = pkt->data + pkt->len;
2218
2219 while (pos < end) {
2220 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
2221 goto err;
2222
2223 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002224 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002225 break;
2226 case QUIC_FT_PING:
2227 break;
2228 case QUIC_FT_ACK:
2229 {
2230 unsigned int rtt_sample;
2231
2232 rtt_sample = 0;
2233 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
2234 goto err;
2235
2236 if (rtt_sample) {
2237 unsigned int ack_delay;
2238
2239 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
2240 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
2241 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
2242 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002243 break;
2244 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002245 case QUIC_FT_STOP_SENDING:
2246 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
2247 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002248 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002249 {
2250 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002251
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002252 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2253 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2254 /* XXX TO DO: <cfdebug> is used only for the traces. */
2255 struct quic_rx_crypto_frm cfdebug = { };
2256
2257 cfdebug.offset_node.key = frm.crypto.offset;
2258 cfdebug.len = frm.crypto.len;
2259 /* Nothing to do */
2260 TRACE_PROTO("Already received CRYPTO data",
2261 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt, &cfdebug);
2262 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002263 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002264 else {
2265 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2266 /* XXX TO DO: <cfdebug> is used only for the traces. */
2267 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002268
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002269 cfdebug.offset_node.key = frm.crypto.offset;
2270 cfdebug.len = frm.crypto.len;
2271 TRACE_PROTO("Partially already received CRYPTO data",
2272 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt, &cfdebug);
2273 frm.crypto.len -= diff;
2274 frm.crypto.data += diff;
2275 frm.crypto.offset = qel->rx.crypto.offset;
2276 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002277 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002278
2279 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002280 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002281 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002282
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002283 cfdebug.offset_node.key = frm.crypto.offset;
2284 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002285 if (!qc_provide_cdata(qel, ctx,
2286 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002287 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002288 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002289
2290 break;
2291 }
2292
2293 /* frm.crypto.offset > qel->rx.crypto.offset */
2294 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2295 if (!cf) {
2296 TRACE_DEVEL("CRYPTO frame allocation failed",
2297 QUIC_EV_CONN_PRSHPKT, ctx->conn);
2298 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002299 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002300
2301 cf->offset_node.key = frm.crypto.offset;
2302 cf->len = frm.crypto.len;
2303 cf->data = frm.crypto.data;
2304 cf->pkt = pkt;
2305 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2306 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002307 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002308 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002309 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002310 {
2311 struct quic_stream *stream = &frm.stream;
2312
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002313 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002314 if (objt_listener(ctx->conn->target)) {
2315 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2316 goto err;
2317 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2318 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002319
2320 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
2321 goto err;
2322
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002323 break;
2324 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002325 case QUIC_FT_MAX_DATA:
2326 case QUIC_FT_MAX_STREAM_DATA:
2327 case QUIC_FT_MAX_STREAMS_BIDI:
2328 case QUIC_FT_MAX_STREAMS_UNI:
2329 case QUIC_FT_DATA_BLOCKED:
2330 case QUIC_FT_STREAM_DATA_BLOCKED:
2331 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2332 case QUIC_FT_STREAMS_BLOCKED_UNI:
2333 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002334 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002335 break;
2336 case QUIC_FT_CONNECTION_CLOSE:
2337 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002338 /* warn the mux to close the connection */
2339 conn->qcc->flags |= QC_CF_CC_RECV;
2340 tasklet_wakeup(conn->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002341 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002342 case QUIC_FT_HANDSHAKE_DONE:
2343 if (objt_listener(ctx->conn->target))
2344 goto err;
2345
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002346 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002347 break;
2348 default:
2349 goto err;
2350 }
2351 }
2352
2353 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2354 * has successfully parse a Handshake packet. The Initial encryption must also
2355 * be discarded.
2356 */
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002357 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) {
2358 int state = HA_ATOMIC_LOAD(&conn->state);
2359
2360 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
2361 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2362 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2363 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
2364 qc_set_timer(ctx);
2365 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
2366 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_SERVER_HANDSHAKE);
2367 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002368 }
2369
2370 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
2371 return 1;
2372
2373 err:
2374 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2375 return 0;
2376}
2377
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002378/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002379 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002380 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2381 * This function must be called only after having built a correct datagram.
2382 * Always succeeds.
2383 */
2384static inline void qc_set_dg(struct cbuf *cbuf,
2385 uint16_t dglen, struct quic_tx_packet *pkt)
2386{
2387 write_u16(cb_wr(cbuf), dglen);
2388 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2389 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2390}
2391
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002392/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002393 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2394 * several packets in the same datagram. A header made of two fields is added
2395 * to each datagram: the datagram length followed by the address of the first
2396 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002397 * Returns 1 if succeeded, or 0 if something wrong happened.
2398 */
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002399static int qc_prep_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002400{
2401 struct quic_conn *qc;
2402 enum quic_tls_enc_level tel, next_tel;
2403 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002404 struct cbuf *cbuf;
2405 unsigned char *end_buf, *end, *pos, *spos;
2406 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2407 /* length of datagrams */
2408 uint16_t dglen;
2409 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002410 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002411 /* Each datagram is prepended with its length followed by the
2412 * address of the first packet in the datagram.
2413 */
2414 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002415
2416 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2417 qc = ctx->conn->qc;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01002418 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state), 0)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002419 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002420 goto err;
2421 }
2422
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002423 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002424 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002425 total = 0;
2426 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002427 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002428 cbuf = qr->cbuf;
2429 spos = pos = cb_wr(cbuf);
2430 /* Leave at least <dglen> bytes at the end of this buffer
2431 * to ensure there is enough room to mark the end of prepared
2432 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002433 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002434 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2435 first_pkt = prv_pkt = NULL;
2436 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002437 int err, nb_ptos, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002438 enum quic_pkt_type pkt_type;
2439
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002440 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002441 nb_ptos = ack = 0;
2442 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2443 if (!cc) {
2444 if (!prv_pkt) {
2445 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2446 do {
2447 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2448 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2449 }
2450 ack = HA_ATOMIC_BTR(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002451 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002452 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002453 * 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 +01002454 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002455 * and if there is no more CRYPTO data available or in flight
2456 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002457 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002458 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002459 (!cc && !ack && !nb_ptos &&
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002460 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2461 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002462 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002463 /* Set the current datagram as prepared into <cbuf> if
2464 * the was already a correct packet which was previously written.
2465 */
2466 if (prv_pkt)
2467 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002468 break;
2469 }
2470
2471 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002472 if (!prv_pkt) {
2473 /* Leave room for the datagram header */
2474 pos += dg_headlen;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002475 if (!quic_peer_validated_addr(ctx) && objt_listener(ctx->conn->target)) {
2476 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
2477 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
2478 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2479 }
2480 else {
2481 end = pos + qc->path->mtu;
2482 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002483 }
2484
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002485 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002486 pkt_type, ack, nb_ptos, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002487 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002488 if (err < 0) {
2489 if (!prv_pkt && nb_ptos)
2490 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002491 if (ack)
2492 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002493 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002494 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002495 case -2:
2496 goto err;
2497 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002498 /* If there was already a correct packet present, set the
2499 * current datagram as prepared into <cbuf>.
2500 */
2501 if (prv_pkt) {
2502 qc_set_dg(cbuf, dglen, first_pkt);
2503 goto stop_build;
2504 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002505 goto out;
2506 default:
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002507 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2508 if (!cur_pkt)
2509 goto err;
2510
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002511 total += cur_pkt->len;
2512 /* keep trace of the first packet in the datagram */
2513 if (!first_pkt)
2514 first_pkt = cur_pkt;
2515 /* Attach the current one to the previous one */
2516 if (prv_pkt)
2517 prv_pkt->next = cur_pkt;
2518 /* Let's say we have to build a new dgram */
2519 prv_pkt = NULL;
2520 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002521 /* Discard the Initial encryption keys as soon as
2522 * a handshake packet could be built.
2523 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002524 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002525 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2526 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002527 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002528 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2529 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002530 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002531 }
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002532 /* If the data for the current encryption level have all been sent,
2533 * select the next level.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002534 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002535 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002536 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2537 (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 +02002538 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2539 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2540 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002541 tel = next_tel;
2542 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002543 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002544 /* If there is data for the next level, do not
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002545 * consume a datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002546 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002547 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002548 }
2549 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002550 }
2551
2552 /* If we have to build a new datagram, set the current datagram as
2553 * prepared into <cbuf>.
2554 */
2555 if (!prv_pkt) {
2556 qc_set_dg(cbuf, dglen, first_pkt);
2557 first_pkt = NULL;
2558 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002559 padding = 0;
2560 }
2561 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
2562 (objt_server(qc->conn->target) ||
2563 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2564 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002565 }
2566 }
2567
2568 stop_build:
2569 /* Reset <wr> writer index if in front of <rd> index */
2570 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2571 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2572
2573 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2574 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2575 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2576 /* Mark the end of contiguous data for the reader */
2577 write_u16(cb_wr(cbuf), 0);
2578 cb_add(cbuf, sizeof(uint16_t));
2579 }
2580 }
2581
2582 if (rd && rd <= cbuf->wr) {
2583 cb_wr_reset(cbuf);
2584 if (pos == spos) {
2585 /* Reuse the same buffer if nothing was built. */
2586 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002587 }
2588 }
2589 }
2590
2591 out:
2592 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002593 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002594
2595 err:
2596 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002597 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002598}
2599
2600/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002601 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002602 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002603int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002604{
2605 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002606 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002607
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002608 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002609 cbuf = qr->cbuf;
2610 while (cb_contig_data(cbuf)) {
2611 unsigned char *pos;
2612 struct buffer tmpbuf = { };
2613 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2614 uint16_t dglen;
2615 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002616 unsigned int time_sent;
2617
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002618 pos = cb_rd(cbuf);
2619 dglen = read_u16(pos);
2620 /* End of prepared datagrams.
2621 * Reset the reader index only if in front of the writer index.
2622 */
2623 if (!dglen) {
2624 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2625
2626 if (wr && wr < cbuf->rd) {
2627 cb_rd_reset(cbuf);
2628 continue;
2629 }
2630 break;
2631 }
2632
2633 pos += sizeof dglen;
2634 first_pkt = read_ptr(pos);
2635 pos += sizeof first_pkt;
2636 tmpbuf.area = (char *)pos;
2637 tmpbuf.size = tmpbuf.data = dglen;
2638
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002639 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002640 for (pkt = first_pkt; pkt; pkt = pkt->next)
2641 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002642 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002643 &tmpbuf, tmpbuf.data, 0) <= 0) {
2644 for (pkt = first_pkt; pkt; pkt = pkt->next)
2645 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002646 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002647 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002648
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002649 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002650 qc->tx.bytes += tmpbuf.data;
2651 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002652
2653 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2654 pkt->time_sent = time_sent;
2655 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2656 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002657 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002658 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002659 qc->path->in_flight += pkt->in_flight_len;
2660 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2661 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002662 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002663 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2664 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002665 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002666 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002667 }
2668 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002669
2670 return 1;
2671}
2672
2673/* Build all the frames which must be sent just after the handshake have succeeded.
2674 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2675 * a HANDSHAKE_DONE frame.
2676 * Return 1 if succeeded, 0 if not.
2677 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002678static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002679{
2680 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002681 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002682 struct quic_frame *frm;
2683
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002684 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002685 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002686 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002687 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002688 if (!frm)
2689 return 0;
2690
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002691 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002692 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002693 }
2694
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002695 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002696 struct quic_connection_id *cid;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002697 struct listener *l = __objt_listener(qc->conn->target);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002698
2699 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002700 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002701 goto err;
2702
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002703 cid = new_quic_cid(&qc->cids, qc, i);
2704 if (!cid)
2705 goto err;
2706
2707 /* insert the allocated CID in the receiver tree */
2708 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
2709 ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len);
2710 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
2711
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002712 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002713 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002714 }
2715
2716 return 1;
2717
2718 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002719 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002720 return 0;
2721}
2722
2723/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002724void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002725{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002726 struct eb64_node *n;
2727 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002728
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002729 n = eb64_first(&arngs->root);
2730 while (n) {
2731 struct eb64_node *next;
2732
2733 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2734 next = eb64_next(n);
2735 eb64_delete(n);
2736 free(ar);
2737 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002738 }
2739}
2740
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002741/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2742 * descending order.
2743 */
2744static inline size_t sack_gap(struct quic_arng_node *p,
2745 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002746{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002747 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002748}
2749
2750
2751/* Remove the last elements of <ack_ranges> list of ack range updating its
2752 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002753 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002754 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002755static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002756{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002757 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002758
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002759 last = eb64_last(&arngs->root);
2760 while (last && arngs->enc_sz > limit) {
2761 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002762
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002763 prev = eb64_prev(last);
2764 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002765 return 0;
2766
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002767 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2768 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2769 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2770 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2771 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2772 --arngs->sz;
2773 eb64_delete(last);
2774 pool_free(pool_head_quic_arng, last);
2775 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002776 }
2777
2778 return 1;
2779}
2780
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002781/* Set the encoded size of <arngs> QUIC ack ranges. */
2782static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2783{
2784 struct eb64_node *node, *next;
2785 struct quic_arng_node *ar, *ar_next;
2786
2787 node = eb64_last(&arngs->root);
2788 if (!node)
2789 return;
2790
2791 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2792 arngs->enc_sz = quic_int_getsize(ar->last) +
2793 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2794
2795 while ((next = eb64_prev(node))) {
2796 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2797 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2798 quic_int_getsize(ar_next->last - ar_next->first.key);
2799 node = next;
2800 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2801 }
2802}
2803
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002804/* Insert <ar> ack range into <argns> tree of ack ranges.
2805 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002806 */
2807static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002808struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2809 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002810{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002811 struct quic_arng_node *new_ar;
2812
2813 new_ar = pool_alloc(pool_head_quic_arng);
2814 if (new_ar) {
2815 new_ar->first.key = ar->first;
2816 new_ar->last = ar->last;
2817 eb64_insert(&arngs->root, &new_ar->first);
2818 arngs->sz++;
2819 }
2820
2821 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002822}
2823
2824/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002825 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002826 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002827 *
2828 * Descending order
2829 * ------------->
2830 * range1 range2
2831 * ..........|--------|..............|--------|
2832 * ^ ^ ^ ^
2833 * | | | |
2834 * last1 first1 last2 first2
2835 * ..........+--------+--------------+--------+......
2836 * diff1 gap12 diff2
2837 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002838 * To encode the previous list of ranges we must encode integers as follows in
2839 * descending order:
2840 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002841 * with diff1 = last1 - first1
2842 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002843 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002844 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002845 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002846int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2847 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002848{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002849 struct eb64_node *le;
2850 struct quic_arng_node *new_node;
2851 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002852
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002853 new = NULL;
2854 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002855 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002856 if (!new_node)
2857 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002858
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002859 goto out;
2860 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002861
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002862 le = eb64_lookup_le(&arngs->root, ar->first);
2863 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002864 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002865 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002866 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002867
2868 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002869 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002870 else {
2871 struct quic_arng_node *le_ar =
2872 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002873
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002874 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002875 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002876 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002877
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002878 if (le_ar->last + 1 >= ar->first) {
2879 le_ar->last = ar->last;
2880 new = le;
2881 new_node = le_ar;
2882 }
2883 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002884 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002885 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002886 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002887
2888 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002889 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002890 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002891
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002892 /* Verify that the new inserted node does not overlap the nodes
2893 * which follow it.
2894 */
2895 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002896 struct eb64_node *next;
2897 struct quic_arng_node *next_node;
2898
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002899 while ((next = eb64_next(new))) {
2900 next_node =
2901 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002902 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002903 break;
2904
2905 if (next_node->last > new_node->last)
2906 new_node->last = next_node->last;
2907 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002908 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002909 /* Decrement the size of these ranges. */
2910 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002911 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002912 }
2913
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002914 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002915 quic_arngs_set_enc_sz(arngs);
2916
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002917 return 1;
2918}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002919/* Remove the header protection of packets at <el> encryption level.
2920 * Always succeeds.
2921 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002922static 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 +01002923{
2924 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002925 struct quic_rx_packet *pqpkt;
2926 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002927 struct quic_enc_level *app_qel;
2928
2929 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2930 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2931 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002932 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002933 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002934 TRACE_PROTO("hp not removed (handshake not completed)",
2935 QUIC_EV_CONN_ELRMHP, ctx->conn);
2936 goto out;
2937 }
2938 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002939 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002940 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2941 pqpkt->data + pqpkt->pn_offset,
2942 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2943 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2944 /* XXX TO DO XXX */
2945 }
2946 else {
2947 /* The AAD includes the packet number field */
2948 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2949 /* Store the packet into the tree of packets to decrypt. */
2950 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002951 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002952 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2953 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002954 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002955 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2956 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002957 MT_LIST_DELETE_SAFE(pkttmp1);
2958 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002959 }
2960
2961 out:
2962 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2963}
2964
2965/* Process all the CRYPTO frame at <el> encryption level.
2966 * Return 1 if succeeded, 0 if not.
2967 */
2968static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002969 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002970{
2971 struct eb64_node *node;
2972
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002973 node = eb64_first(&el->rx.crypto.frms);
2974 while (node) {
2975 struct quic_rx_crypto_frm *cf;
2976
2977 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2978 if (cf->offset_node.key != el->rx.crypto.offset)
2979 break;
2980
2981 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2982 goto err;
2983
2984 node = eb64_next(node);
2985 quic_rx_packet_refdec(cf->pkt);
2986 eb64_delete(&cf->offset_node);
2987 pool_free(pool_head_quic_rx_crypto_frm, cf);
2988 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002989 return 1;
2990
2991 err:
2992 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2993 return 0;
2994}
2995
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002996/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002997 * 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 +02002998 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002999 * Return 1 if succeeded, 0 if not.
3000 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003001int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3002 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003003{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003004 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003005 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003006 struct quic_conn *qc = ctx->conn->qc;
3007 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003008
3009 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003010 qel = cur_el;
3011 next_tel:
3012 if (!qel)
3013 goto out;
3014
3015 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3016 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003017 while (node) {
3018 struct quic_rx_packet *pkt;
3019
3020 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003021 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
3022 ctx->conn, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003023 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003024 /* Drop the packet */
3025 TRACE_PROTO("packet decryption failed -> dropped",
3026 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
3027 }
3028 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003029 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003030 /* Drop the packet */
3031 TRACE_PROTO("packet parsing failed -> dropped",
3032 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
3033 }
3034 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003035 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3036
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003037 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003038 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
3039 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003040 if (pkt->pn > largest_pn)
3041 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003042 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003043 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003044 TRACE_DEVEL("Could not update ack range list",
3045 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046 }
3047 }
3048 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003049 eb64_delete(&pkt->pn_node);
3050 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003051 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003052 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003053
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003054 /* Update the largest packet number. */
3055 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003056 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3057 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003058 goto err;
3059
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003060 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003061 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003062 qel = next_el;
3063 goto next_tel;
3064 }
3065
3066 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003067 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
3068 return 1;
3069
3070 err:
3071 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
3072 return 0;
3073}
3074
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003075/* QUIC connection packet handler task. */
3076struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003077{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003078 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003079 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003080 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003081 enum quic_tls_enc_level tel, next_tel;
3082 struct quic_enc_level *qel, *next_qel;
3083 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003084 struct qring *qr; // Tx ring
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003085 int prev_st, st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003086
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003087 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003088 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003089 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003090 st = HA_ATOMIC_LOAD(&qc->state);
3091 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003092 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003093 zero_rtt = !MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003094 start:
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003095 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003096 goto err;
3097
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003098 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003099 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003100
3101 next_level:
3102 tls_ctx = &qel->tls_ctx;
3103
3104 /* If the header protection key for this level has been derived,
3105 * remove the packet header protections.
3106 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003107 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003108 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
3109 qc_rm_hp_pkts(qel, ctx);
3110
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003111 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003112 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3113 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3114 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003115 goto err;
3116
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003117 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3118 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3119 qel = next_qel;
3120 next_qel = NULL;
3121 goto next_level;
3122 }
3123
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003124 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02003125 if (st >= QUIC_HS_ST_COMPLETE &&
3126 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003127 /* Discard the Handshake keys. */
3128 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02003129 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003130 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3131 qc_set_timer(ctx);
3132 if (!quic_build_post_handshake_frames(qc))
3133 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003134
3135 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3136 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003137 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003138 }
3139
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003140 if (!qr)
3141 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01003142 ret = qc_prep_pkts(qr, ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003143 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003144 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003145 else if (ret == 0)
3146 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003147
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003148 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003149 goto err;
3150
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003151 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003152 /* Check if there is something to do for the next level.
3153 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003154 if (next_qel && next_qel != qel &&
3155 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003156 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003157 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003158 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003159 goto next_level;
3160 }
3161
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003162 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003163 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003164 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003165
3166 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003167 if (qr)
3168 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003169 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
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
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003173/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003174static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3175{
3176 int i;
3177
3178 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3179 if (qel->tx.crypto.bufs[i]) {
3180 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3181 qel->tx.crypto.bufs[i] = NULL;
3182 }
3183 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003184 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003185}
3186
3187/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003188 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003189 * Returns 1 if succeeded, 0 if not.
3190 */
3191static int quic_conn_enc_level_init(struct quic_conn *qc,
3192 enum quic_tls_enc_level level)
3193{
3194 struct quic_enc_level *qel;
3195
3196 qel = &qc->els[level];
3197 qel->level = quic_to_ssl_enc_level(level);
3198 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3199 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3200 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3201 qel->tls_ctx.rx.flags = 0;
3202 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003203 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003204
3205 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003206 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003207 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003208 qel->rx.crypto.offset = 0;
3209 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003210
3211 /* Allocate only one buffer. */
3212 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3213 if (!qel->tx.crypto.bufs)
3214 goto err;
3215
3216 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3217 if (!qel->tx.crypto.bufs[0])
3218 goto err;
3219
3220 qel->tx.crypto.bufs[0]->sz = 0;
3221 qel->tx.crypto.nb_buf = 1;
3222
3223 qel->tx.crypto.sz = 0;
3224 qel->tx.crypto.offset = 0;
3225
3226 return 1;
3227
3228 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003229 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003230 return 0;
3231}
3232
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003233/* Release all the memory allocated for <conn> QUIC connection. */
3234static void quic_conn_free(struct quic_conn *conn)
3235{
3236 int i;
3237
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003238 if (!conn)
3239 return;
3240
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003241 free_quic_conn_cids(conn);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003242
3243 /* remove the connection from receiver cids trees */
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01003244 HA_RWLOCK_WRLOCK(QUIC_LOCK, &conn->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003245 ebmb_delete(&conn->odcid_node);
3246 ebmb_delete(&conn->scid_node);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003247
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01003248 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &conn->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003249
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003250 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
3251 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003252 if (conn->timer_task)
3253 task_destroy(conn->timer_task);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003254 pool_free(pool_head_quic_conn_rxbuf, conn->rx.buf.area);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003255 pool_free(pool_head_quic_conn, conn);
3256}
3257
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003258void quic_close(struct connection *conn, void *xprt_ctx)
3259{
3260 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
3261 struct quic_conn *qc = conn_ctx->conn->qc;
3262 quic_conn_free(qc);
3263}
3264
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003265/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003266static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003267{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003268 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003269 struct quic_conn *qc;
3270 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003271 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003272
3273 conn_ctx = task->context;
3274 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003275 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
3276 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003277 task->expire = TICK_ETERNITY;
3278 pktns = quic_loss_pktns(qc);
3279 if (tick_isset(pktns->tx.loss_time)) {
3280 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3281
3282 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3283 if (!LIST_ISEMPTY(&lost_pkts))
3284 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
3285 qc_set_timer(conn_ctx);
3286 goto out;
3287 }
3288
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003289 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003290 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003291 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003292 pktns->tx.pto_probe = 1;
3293 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003294 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003295 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3296 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3297
3298 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3299 hel->pktns->tx.pto_probe = 1;
3300 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3301 iel->pktns->tx.pto_probe = 1;
3302 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003303 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003304 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3305 qc->path->loss.pto_count++;
3306
3307 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003308 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003309
3310 return task;
3311}
3312
3313/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3314 * connections used to identify the first Initial packets of client connecting
3315 * to listeners. This parameter must be NULL for QUIC connections attached
3316 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3317 * <scid> is the source connection ID with <scid_len> as length.
3318 * Returns 1 if succeeded, 0 if not.
3319 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003320static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
3321 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003322 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003323{
3324 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003325 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003326 /* Initial CID. */
3327 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003328 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003329 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003330
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003331 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003332 qc = pool_zalloc(pool_head_quic_conn);
3333 if (!qc) {
3334 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3335 goto err;
3336 }
3337
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003338 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3339 if (!buf_area) {
3340 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT);
3341 goto err;
3342 }
3343
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003344 qc->cids = EB_ROOT;
3345 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003346 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003347 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003348
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003349 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003350 /* Copy the initial DCID. */
3351 qc->odcid.len = dcid_len;
3352 if (qc->odcid.len)
3353 memcpy(qc->odcid.data, dcid, dcid_len);
3354
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003355 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003356 if (scid_len)
3357 memcpy(qc->dcid.data, scid, scid_len);
3358 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003359 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003360 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003361 }
3362 /* QUIC Client (outgoing connection to servers) */
3363 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003364 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003365 if (dcid_len)
3366 memcpy(qc->dcid.data, dcid, dcid_len);
3367 qc->dcid.len = dcid_len;
3368 }
3369
3370 /* Initialize the output buffer */
3371 qc->obuf.pos = qc->obuf.data;
3372
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003373 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003374 if (!icid) {
3375 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
3376 goto err;
3377 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003378
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003379 /* insert the allocated CID in the receiver tree */
3380 if (server) {
3381 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
3382 ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len);
3383 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3384 }
3385
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003386 /* Select our SCID which is the first CID with 0 as sequence number. */
3387 qc->scid = icid->cid;
3388
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003389 /* Packet number spaces initialization. */
3390 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3391 quic_pktns_init(&qc->pktns[i]);
3392 /* QUIC encryption level context initialization. */
3393 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003394 if (!quic_conn_enc_level_init(qc, i)) {
3395 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003396 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003397 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003398 /* Initialize the packet number space. */
3399 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3400 }
3401
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003402 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003403 qc->tps_tls_ext = qc->version & 0xff000000 ?
3404 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3405 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003406 /* TX part. */
3407 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003408 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3409 qc->tx.wbuf = qc->tx.rbuf = 0;
3410 qc->tx.bytes = 0;
3411 qc->tx.nb_pto_dgrams = 0;
3412 /* RX part. */
3413 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003414 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003415 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3416 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3417 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003418 if (!quic_tls_ku_init(qc)) {
3419 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT);
3420 goto err;
3421 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003422
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003423 /* XXX TO DO: Only one path at this time. */
3424 qc->path = &qc->paths[0];
3425 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3426
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003427 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003428
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003429 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003430
3431 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003432 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003433 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003434 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003435}
3436
3437/* Initialize the timer task of <qc> QUIC connection.
3438 * Returns 1 if succeeded, 0 if not.
3439 */
3440static int quic_conn_init_timer(struct quic_conn *qc)
3441{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003442 /* Attach this task to the same thread ID used for the connection */
3443 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003444 if (!qc->timer_task)
3445 return 0;
3446
3447 qc->timer = TICK_ETERNITY;
3448 qc->timer_task->process = process_timer;
3449 qc->timer_task->context = qc->conn->xprt_ctx;
3450
3451 return 1;
3452}
3453
3454/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3455 * past one byte of this buffer.
3456 */
3457static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3458 struct quic_rx_packet *pkt)
3459{
3460 unsigned char dcid_len, scid_len;
3461
3462 /* Version */
3463 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3464 return 0;
3465
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003466 /* Destination Connection ID Length */
3467 dcid_len = *(*buf)++;
3468 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3469 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3470 /* XXX MUST BE DROPPED */
3471 return 0;
3472
3473 if (dcid_len) {
3474 /* Check that the length of this received DCID matches the CID lengths
3475 * of our implementation for non Initials packets only.
3476 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003477 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3478 pkt->type != QUIC_PACKET_TYPE_0RTT &&
3479 dcid_len != QUIC_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003480 return 0;
3481
3482 memcpy(pkt->dcid.data, *buf, dcid_len);
3483 }
3484
3485 pkt->dcid.len = dcid_len;
3486 *buf += dcid_len;
3487
3488 /* Source Connection ID Length */
3489 scid_len = *(*buf)++;
3490 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3491 /* XXX MUST BE DROPPED */
3492 return 0;
3493
3494 if (scid_len)
3495 memcpy(pkt->scid.data, *buf, scid_len);
3496 pkt->scid.len = scid_len;
3497 *buf += scid_len;
3498
3499 return 1;
3500}
3501
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003502/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3503 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3504 * encryption level matching with the packet type. <*qel> may be null if not found.
3505 * Note that <ctx> may be null (for Initial packets).
3506 */
3507static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003508 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003509 struct quic_enc_level **qel)
3510{
3511 enum quic_tls_enc_level tel;
3512
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003513 /* Special case without connection context (first Initial packets) */
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003514 if (!ctx) {
3515 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3516 return 1;
3517 }
3518
3519 tel = quic_packet_type_enc_level(pkt->type);
3520 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3521 *qel = NULL;
3522 return 0;
3523 }
3524
3525 *qel = &qc->els[tel];
3526 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3527 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3528 return 0;
3529 }
3530
3531 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003532 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3533 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003534 return 1;
3535
3536 return 0;
3537}
3538
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003539/* Insert <pkt> RX packet in its <qel> RX packets tree */
3540static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3541{
3542 pkt->pn_node.key = pkt->pn;
3543 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3544 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3545 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3546 quic_rx_packet_refinc(pkt);
3547}
3548
3549/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003550 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3551 * byte past the end of the buffer containing this packet and <beg> the address of
3552 * the packet first byte.
3553 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3554 * Returns 1 if succeeded, 0 if not.
3555 */
3556static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3557 unsigned char **buf, unsigned char *beg,
3558 const unsigned char *end,
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003559 struct quic_conn *qc, struct quic_enc_level **el,
3560 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003561{
3562 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003563 struct quic_enc_level *qel;
3564 /* Only for traces. */
3565 struct quic_rx_packet *qpkt_trace;
3566
3567 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003568 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003569 /* The packet number is here. This is also the start minus
3570 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3571 * protection.
3572 */
3573 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003574 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003575 /* Note that the following function enables us to unprotect the packet
3576 * number and its length subsequently used to decrypt the entire
3577 * packets.
3578 */
3579 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3580 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003581 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003582 goto err;
3583 }
3584
3585 /* The AAD includes the packet number field found at <pn>. */
3586 pkt->aad_len = pn - beg + pkt->pnl;
3587 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003588 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003589 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003590 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3591 /* If the packet number space has been discarded, this packet
3592 * will be not parsed.
3593 */
3594 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
3595 goto out;
3596 }
3597
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003598 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003599 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003600 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3601 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003602 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003603 else {
3604 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
3605 goto err;
3606 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003607
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003608 *el = qel;
3609 /* No reference counter incrementation here!!! */
3610 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3611 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3612 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3613 b_add(&qc->rx.buf, pkt->len);
3614 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003615 /* Updtate the offset of <*buf> for the next QUIC packet. */
3616 *buf = beg + pkt->len;
3617
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003618 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003619 return 1;
3620
3621 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003622 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003623 return 0;
3624}
3625
3626/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3627 * Also set <*long_header> to 1 if this form is long, 0 if not.
3628 */
3629static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3630 unsigned char byte0, int *long_header)
3631{
3632 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3633 pkt->type =
3634 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3635 *long_header = 1;
3636 }
3637 else {
3638 pkt->type = QUIC_PACKET_TYPE_SHORT;
3639 *long_header = 0;
3640 }
3641}
3642
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003643/*
3644 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3645 */
3646static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3647{
3648 int j = 0, version;
3649
3650 do {
3651 version = quic_supported_version[j];
3652 if (version == pkt->version)
3653 return 1;
3654
3655 version = quic_supported_version[++j];
3656 } while(version);
3657
3658 return 0;
3659}
3660
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003661__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003662static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3663 struct quic_rx_packet *pkt,
3664 struct quic_dgram_ctx *dgram_ctx,
3665 struct sockaddr_storage *saddr)
3666{
3667 unsigned char *beg;
3668 uint64_t len;
3669 struct quic_conn *qc;
3670 struct eb_root *cids;
3671 struct ebmb_node *node;
3672 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003673 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003674 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003675 size_t b_cspace;
3676 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003677
3678 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003679 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003680 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3681 if (end <= *buf)
3682 goto err;
3683
3684 /* Fixed bit */
3685 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3686 /* XXX TO BE DISCARDED */
3687 goto err;
3688
3689 srv_conn = dgram_ctx->owner;
3690 beg = *buf;
3691 /* Header form */
3692 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3693 if (long_header) {
3694 size_t cid_lookup_len;
3695
3696 if (!quic_packet_read_long_header(buf, end, pkt))
3697 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003698
3699 /* unsupported QUIC version */
3700 if (!qc_pkt_is_supported_version(pkt)) {
3701 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3702 goto err;
3703 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003704
3705 /* For Initial packets, and for servers (QUIC clients connections),
3706 * there is no Initial connection IDs storage.
3707 */
3708 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3709 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3710 cid_lookup_len = pkt->dcid.len;
3711 }
3712 else {
3713 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3714 cid_lookup_len = QUIC_CID_LEN;
3715 }
3716
3717 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3718 if (!node)
3719 goto err;
3720
3721 qc = ebmb_entry(node, struct quic_conn, scid_node);
3722
3723 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3724 qc->dcid.len = pkt->scid.len;
3725 if (pkt->scid.len)
3726 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3727 }
3728
3729 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3730 uint64_t token_len;
3731
3732 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3733 goto err;
3734
3735 /* XXX TO DO XXX 0 value means "the token is not present".
3736 * A server which sends an Initial packet must not set the token.
3737 * So, a client which receives an Initial packet with a token
3738 * MUST discard the packet or generate a connection error with
3739 * PROTOCOL_VIOLATION as type.
3740 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3741 */
3742 pkt->token_len = token_len;
3743 }
3744 }
3745 else {
3746 /* XXX TO DO: Short header XXX */
3747 if (end - *buf < QUIC_CID_LEN)
3748 goto err;
3749
3750 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3751 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3752 if (!node)
3753 goto err;
3754
3755 qc = ebmb_entry(node, struct quic_conn, scid_node);
3756 *buf += QUIC_CID_LEN;
3757 }
3758 /* Store the DCID used for this packet to check the packet which
3759 * come in this UDP datagram match with it.
3760 */
3761 if (!dgram_ctx->dcid_node)
3762 dgram_ctx->dcid_node = node;
3763 /* Only packets packets with long headers and not RETRY or VERSION as type
3764 * have a length field.
3765 */
3766 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3767 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3768 goto err;
3769
3770 pkt->len = len;
3771 }
3772 else if (!long_header) {
3773 /* A short packet is the last one of an UDP datagram. */
3774 pkt->len = end - *buf;
3775 }
3776
3777 conn_ctx = qc->conn->xprt_ctx;
3778
3779 /* Increase the total length of this packet by the header length. */
3780 pkt->len += *buf - beg;
3781 /* Do not check the DCID node before the length. */
3782 if (dgram_ctx->dcid_node != node) {
3783 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3784 goto err;
3785 }
3786
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003787 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3788 b_cspace = b_contig_space(&qc->rx.buf);
3789 if (b_cspace < pkt->len) {
3790 /* Let us consume the remaining contiguous space. */
3791 b_add(&qc->rx.buf, b_cspace);
3792 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3793 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3794 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3795 goto err;
3796 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003797 }
3798
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003799 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) {
3800 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3801 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003802 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003803 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003804
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003805 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3806 if (pkt->aad_len)
3807 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003808 /* Wake the tasklet of the QUIC connection packet handler. */
3809 if (conn_ctx)
3810 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3811
3812 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3813
3814 return pkt->len;
3815
3816 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003817 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003818 return -1;
3819}
3820
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003821/*
3822 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3823 * address <addr>.
3824 * Implementation of RFC9000 6. Version Negotiation
3825 *
3826 * TODO implement a rate-limiting sending of Version Negotiation packets
3827 *
3828 * Returns 0 on success else non-zero
3829 */
3830static int qc_send_version_negotiation(int fd, struct sockaddr_storage *addr,
3831 struct quic_rx_packet *pkt)
3832{
3833 char buf[256];
3834 int i = 0, j, version;
3835 const socklen_t addrlen = get_addr_len(addr);
3836
3837 /*
3838 * header form
3839 * long header, fixed bit to 0 for Version Negotiation
3840 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003841 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3842 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003843
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003844 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003845 /* null version for Version Negotiation */
3846 buf[i++] = '\x00';
3847 buf[i++] = '\x00';
3848 buf[i++] = '\x00';
3849 buf[i++] = '\x00';
3850
3851 /* source connection id */
3852 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003853 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003854 i += pkt->scid.len;
3855
3856 /* destination connection id */
3857 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003858 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003859 i += pkt->dcid.len;
3860
3861 /* supported version */
3862 j = 0;
3863 do {
3864 version = htonl(quic_supported_version[j]);
3865 memcpy(&buf[i], &version, sizeof(version));
3866 i += sizeof(version);
3867
3868 version = quic_supported_version[++j];
3869 } while (version);
3870
3871 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3872 return 1;
3873
3874 return 0;
3875}
3876
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003877static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3878 struct quic_rx_packet *pkt,
3879 struct quic_dgram_ctx *dgram_ctx,
3880 struct sockaddr_storage *saddr)
3881{
3882 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003883 struct quic_conn *qc;
3884 struct eb_root *cids;
3885 struct ebmb_node *node;
3886 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003887 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003888 int long_header = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003889 size_t b_cspace;
3890 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003891
3892 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003893 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003894 qel = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003895 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003896 if (end <= *buf)
3897 goto err;
3898
3899 /* Fixed bit */
3900 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3901 /* XXX TO BE DISCARDED */
3902 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3903 goto err;
3904 }
3905
3906 l = dgram_ctx->owner;
3907 beg = *buf;
3908 /* Header form */
3909 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3910 if (long_header) {
3911 unsigned char dcid_len;
3912
3913 if (!quic_packet_read_long_header(buf, end, pkt)) {
3914 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3915 goto err;
3916 }
3917
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003918 /* RFC9000 6. Version Negotiation */
3919 if (!qc_pkt_is_supported_version(pkt)) {
3920 /* do not send Version Negotiation in response to a
3921 * Version Negotiation packet.
3922 */
3923 if (!pkt->version) {
3924 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3925 goto err;
3926 }
3927
3928 /* unsupported version, send Negotiation packet */
3929 if (qc_send_version_negotiation(l->rx.fd, saddr, pkt)) {
3930 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
3931 goto err;
3932 }
3933
3934 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
3935 goto out;
3936 }
3937
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003938 dcid_len = pkt->dcid.len;
3939 /* For Initial packets, and for servers (QUIC clients connections),
3940 * there is no Initial connection IDs storage.
3941 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003942 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
3943 pkt->type == QUIC_PACKET_TYPE_0RTT) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003944 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003945 /* DCIDs of first packets coming from clients may have the same values.
3946 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3947 */
3948 quic_cid_saddr_cat(&pkt->dcid, saddr);
3949 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003950
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003951 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3952 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3953 end - *buf < token_len) {
3954 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3955 goto err;
3956 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003957
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003958 /* XXX TO DO XXX 0 value means "the token is not present".
3959 * A server which sends an Initial packet must not set the token.
3960 * So, a client which receives an Initial packet with a token
3961 * MUST discard the packet or generate a connection error with
3962 * PROTOCOL_VIOLATION as type.
3963 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3964 */
3965 pkt->token_len = token_len;
3966 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003967 }
3968 else {
3969 if (pkt->dcid.len != QUIC_CID_LEN) {
3970 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3971 goto err;
3972 }
3973
3974 cids = &l->rx.cids;
3975 }
3976
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003977 /* Only packets packets with long headers and not RETRY or VERSION as type
3978 * have a length field.
3979 */
3980 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3981 uint64_t len;
3982
3983 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3984 end - *buf < len) {
3985 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3986 goto err;
3987 }
3988
3989 pkt->len = len;
3990 }
3991
3992
3993 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003994 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3995 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3996 cids == &l->rx.odcids) {
3997 /* Switch to the definitive tree ->cids containing the final CIDs. */
3998 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3999 if (node) {
4000 /* If found, signal this with NULL as special value for <cids>. */
4001 pkt->dcid.len = dcid_len;
4002 cids = NULL;
4003 }
4004 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004005 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004006
4007 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004008 int ipv4;
4009 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004010 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004011 const unsigned char *salt = initial_salt_v1;
4012 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004013
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004014 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
4015 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
4016 goto err;
4017 }
4018
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004019 pkt->saddr = *saddr;
4020 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
4021 * of <saddr>.
4022 */
4023 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004024 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004025 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004026 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004027 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004028 goto err;
4029
4030 odcid = &qc->rx.params.original_destination_connection_id;
4031 /* Copy the transport parameters. */
4032 qc->rx.params = l->bind_conf->quic_params;
4033 /* Copy original_destination_connection_id transport parameter. */
4034 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
4035 odcid->len = pkt->odcid_len;
4036 /* Copy the initial source connection ID. */
4037 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4038 qc->enc_params_len =
4039 quic_transport_params_encode(qc->enc_params,
4040 qc->enc_params + sizeof qc->enc_params,
4041 &qc->rx.params, 1);
4042 if (!qc->enc_params_len)
4043 goto err;
4044
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004045 /* NOTE: the socket address has been concatenated to the destination ID
4046 * chosen by the client for Initial packets.
4047 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004048 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4049 salt = initial_salt_draft_29;
4050 salt_len = sizeof initial_salt_draft_29;
4051 }
4052 if (!qc_new_isecs(qc, salt, salt_len,
4053 pkt->dcid.data, pkt->odcid_len, 1)) {
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004054 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
4055 goto err;
4056 }
4057
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004058 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004059 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004060 n = ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
Frédéric Lécaille008386b2021-11-30 12:01:32 +01004061 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ODCID_NODE_TO_DELETE);
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004062 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004063
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004064 /* If the insertion failed, it means that another
4065 * thread has already allocated a QUIC connection for
4066 * the same CID. Liberate our allocated connection.
4067 */
4068 if (unlikely(n != &qc->odcid_node)) {
4069 quic_conn_free(qc);
4070 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4071 pkt->qc = qc;
4072 }
4073 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004074 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004075 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004076 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
4077 /* Try to accept a new connection. */
4078 listener_accept(l);
4079 }
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004080
4081 /* This is the DCID node sent in this packet by the client. */
4082 node = &qc->odcid_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004083 }
4084 else {
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004085 if ((pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4086 pkt->type == QUIC_PACKET_TYPE_0RTT) && cids == &l->rx.odcids) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004087 qc = ebmb_entry(node, struct quic_conn, odcid_node);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004088 }
4089 else {
4090 struct quic_connection_id *cid = ebmb_entry(node, struct quic_connection_id, node);
4091 qc = cid->qc;
Frédéric Lécaille008386b2021-11-30 12:01:32 +01004092 if (HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ODCID_NODE_TO_DELETE_BIT)) {
4093 /* Delete the ODCID from its tree */
4094 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
4095 ebmb_delete(&qc->odcid_node);
4096 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4097 }
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004098 }
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004099 pkt->qc = qc;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01004100 if (HA_ATOMIC_LOAD(&qc->conn))
4101 conn_ctx = HA_ATOMIC_LOAD(&qc->conn->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004102 }
4103 }
4104 else {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004105 struct quic_connection_id *cid;
4106
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004107 if (end - *buf < QUIC_CID_LEN) {
4108 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4109 goto err;
4110 }
4111
4112 cids = &l->rx.cids;
4113 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
4114 if (!node) {
4115 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4116 goto err;
4117 }
4118
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004119 cid = ebmb_entry(node, struct quic_connection_id, node);
4120 qc = cid->qc;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01004121 if (HA_ATOMIC_LOAD(&qc->conn))
4122 conn_ctx = HA_ATOMIC_LOAD(&qc->conn->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004123 *buf += QUIC_CID_LEN;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004124 pkt->qc = qc;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004125 /* A short packet is the last one of an UDP datagram. */
4126 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004127 }
4128
4129 /* Store the DCID used for this packet to check the packet which
4130 * come in this UDP datagram match with it.
4131 */
4132 if (!dgram_ctx->dcid_node) {
4133 dgram_ctx->dcid_node = node;
4134 dgram_ctx->qc = qc;
4135 }
4136
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004137 /* Increase the total length of this packet by the header length. */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004138 pkt->raw_len = pkt->len += *buf - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004139 /* Do not check the DCID node before the length. */
4140 if (dgram_ctx->dcid_node != node) {
4141 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
4142 goto err;
4143 }
4144
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004145 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004146 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc->conn);
4147 goto out;
4148 }
4149
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004150 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004151 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004152 b_cspace = b_contig_space(&qc->rx.buf);
4153 if (b_cspace < pkt->len) {
4154 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004155 if (b_cspace) {
4156 b_putchr(&qc->rx.buf, 0x00);
4157 b_cspace--;
4158 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004159 b_add(&qc->rx.buf, b_cspace);
4160 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4161 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
4162 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
4163 goto err;
4164 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004165 }
4166
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004167 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) {
4168 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004169 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
4170 goto err;
4171 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004172
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004173 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02004174 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004175 if (pkt->aad_len)
4176 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004177 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004178 /* Wake up the connection packet handler task from here only if all
4179 * the contexts have been initialized, especially the mux context
4180 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4181 * will start it if these contexts for the connection are not already
4182 * initialized.
4183 */
4184 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004185 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004186
Amaury Denoyelleed66b0f2021-11-18 14:38:00 +01004187 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004188
4189 return pkt->len;
4190
4191 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004192 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004193 qc ? qc->conn : NULL, pkt);
4194 return -1;
4195}
4196
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004197/* This function builds into <buf> buffer a QUIC long packet header.
4198 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004199 */
4200static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4201 int type, size_t pn_len, struct quic_conn *conn)
4202{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004203 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004204 return 0;
4205
4206 /* #0 byte flags */
4207 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4208 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4209 /* Version */
4210 quic_write_uint32(buf, end, conn->version);
4211 *(*buf)++ = conn->dcid.len;
4212 /* Destination connection ID */
4213 if (conn->dcid.len) {
4214 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4215 *buf += conn->dcid.len;
4216 }
4217 /* Source connection ID */
4218 *(*buf)++ = conn->scid.len;
4219 if (conn->scid.len) {
4220 memcpy(*buf, conn->scid.data, conn->scid.len);
4221 *buf += conn->scid.len;
4222 }
4223
4224 return 1;
4225}
4226
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004227/* This function builds into <buf> buffer a QUIC short packet header.
4228 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004229 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004230static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4231 size_t pn_len, struct quic_conn *conn,
4232 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004233{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004234 if (end - *buf < 1 + conn->dcid.len)
4235 return 0;
4236
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004237 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004238 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4239 ((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 +01004240 /* Destination connection ID */
4241 if (conn->dcid.len) {
4242 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4243 *buf += conn->dcid.len;
4244 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004245
4246 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004247}
4248
4249/* Apply QUIC header protection to the packet with <buf> as first byte address,
4250 * <pn> as address of the Packet number field, <pnlen> being this field length
4251 * with <aead> as AEAD cipher and <key> as secret key.
4252 * Returns 1 if succeeded or 0 if failed.
4253 */
4254static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4255 const EVP_CIPHER *aead, const unsigned char *key)
4256{
4257 int i, ret, outlen;
4258 EVP_CIPHER_CTX *ctx;
4259 /* We need an IV of at least 5 bytes: one byte for bytes #0
4260 * and at most 4 bytes for the packet number
4261 */
4262 unsigned char mask[5] = {0};
4263
4264 ret = 0;
4265 ctx = EVP_CIPHER_CTX_new();
4266 if (!ctx)
4267 return 0;
4268
4269 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4270 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4271 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4272 goto out;
4273
4274 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4275 for (i = 0; i < pnlen; i++)
4276 pn[i] ^= mask[i + 1];
4277
4278 ret = 1;
4279
4280 out:
4281 EVP_CIPHER_CTX_free(ctx);
4282
4283 return ret;
4284}
4285
4286/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4287 * ACK ranges if needed to a value below <limit> in bytes.
4288 * Return 1 if succeeded, 0 if not.
4289 */
4290static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4291{
4292 size_t room, ack_delay_sz;
4293
4294 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4295 /* A frame is made of 1 byte for the frame type. */
4296 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004297 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004298 return 0;
4299
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004300 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004301}
4302
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004303/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4304 * 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 +01004305 * and <*len> the packet Length field initialized with the number of bytes already present
4306 * 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 +02004307 * <headlen> is the number of bytes already present in this packet before building frames.
4308 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004309 * Update consequently <*len> to reflect the size of these frames built
4310 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004311 * Return 1 if succeeded, 0 if not.
4312 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004313static inline int qc_build_frms(struct quic_tx_packet *pkt,
4314 size_t room, size_t *len, size_t headlen,
4315 struct quic_enc_level *qel,
4316 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004317{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004318 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004319 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004320 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004321 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004322
Frédéric Lécailleea604992020-12-24 13:01:37 +01004323 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004324 if (*len > room || headlen > remain)
4325 return 0;
4326
Frédéric Lécailleea604992020-12-24 13:01:37 +01004327 /* If we are not probing we must take into an account the congestion
4328 * control window.
4329 */
4330 if (!conn->tx.nb_pto_dgrams)
4331 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004332 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01004333 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004334 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 +01004335 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004336 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004337
Frédéric Lécailleea604992020-12-24 13:01:37 +01004338 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004339 break;
4340
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004341 switch (cf->type) {
4342 case QUIC_FT_CRYPTO:
4343 TRACE_PROTO(" New CRYPTO frame build (room, len)",
4344 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
4345 /* Compute the length of this CRYPTO frame header */
4346 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4347 /* Compute the data length of this CRyPTO frame. */
4348 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4349 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
4350 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
4351 if (!dlen)
4352 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004353
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004354 /* CRYPTO frame length. */
4355 flen = hlen + quic_int_getsize(dlen) + dlen;
4356 TRACE_PROTO(" CRYPTO frame length (flen)",
4357 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
4358 /* Add the CRYPTO data length and its encoded length to the packet
4359 * length and the length of this length.
4360 */
4361 *len += flen;
4362 room -= flen;
4363 if (dlen == cf->crypto.len) {
4364 /* <cf> CRYPTO data have been consumed. */
4365 MT_LIST_DELETE_SAFE(tmp1);
4366 LIST_APPEND(&pkt->frms, &cf->list);
4367 }
4368 else {
4369 struct quic_frame *new_cf;
4370
4371 new_cf = pool_alloc(pool_head_quic_frame);
4372 if (!new_cf) {
4373 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
4374 return 0;
4375 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004376
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004377 new_cf->type = QUIC_FT_CRYPTO;
4378 new_cf->crypto.len = dlen;
4379 new_cf->crypto.offset = cf->crypto.offset;
4380 new_cf->crypto.qel = qel;
4381 LIST_APPEND(&pkt->frms, &new_cf->list);
4382 /* Consume <dlen> bytes of the current frame. */
4383 cf->crypto.len -= dlen;
4384 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004385 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004386 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004387
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004388 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004389 /* Note that these frames are accepted in short packets only without
4390 * "Length" packet field. Here, <*len> is used only to compute the
4391 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004392 *
4393 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004394 * excepting the variable ones. Note that +1 is for the type of this frame.
4395 */
4396 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004397 ((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 +02004398 /* Compute the data length of this STREAM frame. */
4399 avail_room = room - hlen - *len;
4400 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004401 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004402
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004403 TRACE_PROTO(" New STREAM frame build (room, len)",
4404 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004405 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4406 dlen = max_available_room(avail_room, &dlen_sz);
4407 if (dlen > cf->stream.len) {
4408 dlen = cf->stream.len;
4409 }
4410 dlen_sz = quic_int_getsize(dlen);
4411 flen = hlen + dlen_sz + dlen;
4412 }
4413 else {
4414 dlen = QUIC_MIN(avail_room, cf->stream.len);
4415 flen = hlen + dlen;
4416 }
4417 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
4418 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen);
4419 TRACE_PROTO(" STREAM frame length (flen)",
4420 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
4421 /* Add the STREAM data length and its encoded length to the packet
4422 * length and the length of this length.
4423 */
4424 *len += flen;
4425 room -= flen;
4426 if (dlen == cf->stream.len) {
4427 /* <cf> STREAM data have been consumed. */
4428 MT_LIST_DELETE_SAFE(tmp1);
4429 LIST_APPEND(&pkt->frms, &cf->list);
4430 }
4431 else {
4432 struct quic_frame *new_cf;
4433
4434 new_cf = pool_zalloc(pool_head_quic_frame);
4435 if (!new_cf) {
4436 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn);
4437 return 0;
4438 }
4439
4440 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004441 new_cf->stream.qcs = cf->stream.qcs;
4442 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004443 new_cf->stream.id = cf->stream.id;
4444 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4445 new_cf->stream.offset = cf->stream.offset;
4446 new_cf->stream.len = dlen;
4447 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4448 /* FIN bit reset */
4449 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4450 new_cf->stream.data = cf->stream.data;
4451 LIST_APPEND(&pkt->frms, &new_cf->list);
4452 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4453 /* Consume <dlen> bytes of the current frame. */
4454 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004455 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004456 cf->stream.data += dlen;
4457 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004458 break;
4459
4460 default:
4461 flen = qc_frm_len(cf);
4462 BUG_ON(!flen);
4463 if (flen > room)
4464 continue;
4465
4466 *len += flen;
4467 room -= flen;
4468 MT_LIST_DELETE_SAFE(tmp1);
4469 LIST_APPEND(&pkt->frms, &cf->list);
4470 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004471 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004472 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004473 }
4474
Frédéric Lécailleea604992020-12-24 13:01:37 +01004475 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004476}
4477
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004478/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004479 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4480 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4481 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004482 * 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 +02004483 * 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 +02004484 * having returned from this function.
4485 * 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 +01004486 * number field in this packet. <pn_len> will also have the packet number
4487 * length as value.
4488 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004489 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004490 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004491static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4492 size_t dglen, struct quic_tx_packet *pkt,
4493 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
4494 int ack, int padding, int cc, int nb_pto_dgrams,
4495 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004496{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004497 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004498 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004499 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4500 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004501 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004502 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004503 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004504 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004505
Frédéric Lécailleea604992020-12-24 13:01:37 +01004506 /* Length field value with CRYPTO frames if present. */
4507 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004508 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004509 /* When not probing and not acking, and no immediate close is required,
4510 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004511 * the congestion controller window. So, we do not limit the size of this
4512 * packet if we have an ACK frame to send because an ACK frame is not
4513 * ack-eliciting. This size will be limited if we have ack-eliciting
4514 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004515 */
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004516 if (!nb_pto_dgrams && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004517 size_t path_room;
4518
4519 path_room = quic_path_prep_data(conn->path);
4520 if (end - beg > path_room)
4521 end = beg + path_room;
4522 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004523
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004524 /* Ensure there is enough room for the TLS encryption tag and a zero token
4525 * length field if any.
4526 */
4527 if (end - pos < QUIC_TLS_TAG_LEN +
4528 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4529 goto no_room;
4530
4531 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004532 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004533 /* packet number length */
4534 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004535 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004536 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
4537 !quic_build_packet_short_header(&pos, end, *pn_len, conn, qel->tls_ctx.flags)) ||
4538 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
4539 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn)))
4540 goto no_room;
4541
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004542 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4543 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004544 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004545 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004546 /* Build an ACK frame if required. */
4547 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004548 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004549 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004550 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004551 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4552 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4553 * that from here, we do not know if we will have to send a PING frame.
4554 * This will be decided after having computed the ack-eliciting frames
4555 * to be added to this packet.
4556 */
4557 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 +01004558 if (!ack_frm_len)
4559 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004560 }
4561
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004562 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004563 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004564 len_frms = 0;
4565 if (!cc && !MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004566 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004567
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004568 /* Initialize the length of the frames built below to <len>.
4569 * If any frame could be successfully built by qc_build_frms(),
4570 * we will have len_frms > len.
4571 */
4572 len_frms = len;
4573 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01004574 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4575 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004576 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004577
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004578 /* Length (of the remaining data). Must not fail because, the buffer size
4579 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4580 * for the encryption tag. It must be taken into an account for the length
4581 * of this packet.
4582 */
4583 if (len_frms)
4584 len = len_frms + QUIC_TLS_TAG_LEN;
4585 else
4586 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004587 /* CONNECTION_CLOSE frame */
4588 if (cc) {
4589 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004590
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004591 cc->error_code = conn->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004592 len += qc_frm_len(&cc_frm);
4593 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004594 add_ping_frm = 0;
4595 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004596 len_sz = quic_int_getsize(len);
4597 /* Add this packet size to <dglen> */
4598 dglen += head_len + len_sz + len;
4599 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4600 /* This is a maximum padding size */
4601 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4602 /* The length field value is of this packet is <len> + <padding_len>
4603 * the size of which may be greater than the initial computed size
4604 * <len_sz>. So, let's deduce the difference betwen these to packet
4605 * sizes from <padding_len>.
4606 */
4607 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4608 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004609 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004610 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004611 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004612 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004613 add_ping_frm = 1;
4614 len += 1;
4615 }
4616 /* 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 +01004617 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004618 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4619 }
4620
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004621 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4622 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004623
4624 /* Packet number field address. */
4625 *buf_pn = pos;
4626
4627 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004628 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4629 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004630
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004631 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn))
4632 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004633
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004634 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004635 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004636 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004637
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004638 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, conn->conn, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004639 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02004640 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004641 ssize_t room = end - pos;
4642 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4643 conn->conn, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004644 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004645 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004646 }
4647 }
4648
4649 /* Build a PING frame if needed. */
4650 if (add_ping_frm) {
4651 frm.type = QUIC_FT_PING;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004652 if (!qc_build_frm(&pos, end, &frm, pkt, conn))
4653 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004654 }
4655
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004656 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004657 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, conn))
4658 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004659
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004660 /* Build a PADDING frame if needed. */
4661 if (padding_len) {
4662 frm.type = QUIC_FT_PADDING;
4663 frm.padding.len = padding_len;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004664 if (!qc_build_frm(&pos, end, &frm, pkt, conn))
4665 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004666 }
4667
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004668 /* Always reset this variable as this function has no idea
4669 * if it was set. It is handle by the loss detection timer.
4670 */
4671 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004672 pkt->len = pos - beg;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004673
4674 return 1;
4675
4676 no_room:
4677 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, conn->conn);
4678 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004679}
4680
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004681static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004682{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004683 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004684 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004685 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01004686 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004687 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004688 pkt->next = NULL;
4689 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004690}
4691
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004692/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004693static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4694{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004695 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004696
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004697 if (!pkt)
4698 return;
4699
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004700 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004701 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004702 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004703 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004704 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004705}
4706
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004707/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4708 * type for <qc> QUIC connection from <qel> encryption level.
4709 * Return -2 if the packet could not be allocated or encrypted for any reason,
4710 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004711 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004712static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4713 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004714 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004715 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004716 int pkt_type, int ack, int nb_pto_dgrams, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004717{
4718 /* The pointer to the packet number field. */
4719 unsigned char *buf_pn;
4720 unsigned char *beg, *end, *payload;
4721 int64_t pn;
4722 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004723 struct quic_tls_ctx *tls_ctx;
4724 struct quic_tx_packet *pkt;
4725
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004726 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004727 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004728 pkt = pool_alloc(pool_head_quic_tx_packet);
4729 if (!pkt) {
4730 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004731 *err = -2;
4732 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004733 }
4734
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004735 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004736 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004737 pn_len = 0;
4738 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004739
4740 pn = qel->pktns->tx.next_pn + 1;
4741 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
4742 ack, padding, cc, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004743 *err = -1;
4744 goto err;
4745 }
4746
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004747 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004748 payload = buf_pn + pn_len;
4749 payload_len = end - payload;
4750 aad_len = payload - beg;
4751
4752 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004753 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4754 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004755 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004756 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004757
4758 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004759 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004760 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4761 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4762 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004763 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004764 goto err;
4765 }
4766
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004767 /* Consume a packet number */
4768 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01004769 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004770 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4771 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004772 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004773 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004774 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004775 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004776 pkt->in_flight_len = pkt->len;
4777 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004778 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004779 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004780 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4781
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004782 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004783
4784 err:
4785 free_quic_tx_packet(pkt);
4786 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004787 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004788}
4789
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004790/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4791 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004792 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004793static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4794 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004795{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004796 size_t try, done = 0;
4797
4798 if (!conn_ctrl_ready(conn))
4799 return 0;
4800
4801 if (!fd_recv_ready(conn->handle.fd))
4802 return 0;
4803
4804 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004805
4806 /* read the largest possible block. For this, we perform only one call
4807 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004808 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004809 */
4810 while (count > 0) {
4811 try = b_contig_space(buf);
4812 if (!try)
4813 break;
4814
4815 if (try > count)
4816 try = count;
4817
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004818 b_add(buf, try);
4819 done += try;
4820 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004821 }
4822
4823 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4824 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4825
4826 leave:
4827 return done;
4828
4829 read0:
4830 conn_sock_read0(conn);
4831 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4832
4833 /* Now a final check for a possible asynchronous low-level error
4834 * report. This can happen when a connection receives a reset
4835 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4836 * we might have come from there by just checking POLL_HUP instead
4837 * of recv()'s return value 0, so we have no way to tell there was
4838 * an error without checking.
4839 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004840 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004841 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4842 goto leave;
4843}
4844
4845
4846/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4847 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4848 * other pending data for example, but this flag is ignored at the moment.
4849 * Only one call to send() is performed, unless the buffer wraps, in which case
4850 * a second call may be performed. The connection's flags are updated with
4851 * whatever special event is detected (error, empty). The caller is responsible
4852 * for taking care of those events and avoiding the call if inappropriate. The
4853 * function does not call the connection's polling update function, so the caller
4854 * is responsible for this. It's up to the caller to update the buffer's contents
4855 * based on the return value.
4856 */
4857static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4858{
4859 ssize_t ret;
4860 size_t try, done;
4861 int send_flag;
4862
4863 if (!conn_ctrl_ready(conn))
4864 return 0;
4865
4866 if (!fd_send_ready(conn->handle.fd))
4867 return 0;
4868
4869 done = 0;
4870 /* send the largest possible block. For this we perform only one call
4871 * to send() unless the buffer wraps and we exactly fill the first hunk,
4872 * in which case we accept to do it once again.
4873 */
4874 while (count) {
4875 try = b_contig_data(buf, done);
4876 if (try > count)
4877 try = count;
4878
4879 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4880 if (try < count || flags & CO_SFL_MSG_MORE)
4881 send_flag |= MSG_MORE;
4882
4883 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4884 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4885 if (ret > 0) {
4886 count -= ret;
4887 done += ret;
4888
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004889 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004890 conn->flags |= CO_FL_WAIT_L4L6;
4891 /* if the system buffer is full, don't insist */
4892 if (ret < try)
4893 break;
4894 }
4895 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4896 /* nothing written, we need to poll for write first */
4897 fd_cant_send(conn->handle.fd);
4898 break;
4899 }
4900 else if (errno != EINTR) {
4901 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4902 break;
4903 }
4904 }
4905 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4906 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4907
4908 if (done > 0) {
4909 /* we count the total bytes sent, and the send rate for 32-byte
4910 * blocks. The reason for the latter is that freq_ctr are
4911 * limited to 4GB and that it's not enough per second.
4912 */
4913 _HA_ATOMIC_ADD(&global.out_bytes, done);
4914 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4915 }
4916 return done;
4917}
4918
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004919/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4920 * event subscriber <es> is not allowed to change from a previous call as long
4921 * as at least one event is still subscribed. The <event_type> must only be a
4922 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4923 */
4924static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4925{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004926 struct qcc *qcc = conn->qc->qcc;
4927
4928 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4929 BUG_ON(qcc->subs && qcc->subs != es);
4930
4931 es->events |= event_type;
4932 qcc->subs = es;
4933
4934 if (event_type & SUB_RETRY_RECV)
4935 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn, qcc);
4936
4937 if (event_type & SUB_RETRY_SEND)
4938 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn, qcc);
4939
4940 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004941}
4942
4943/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4944 * The <es> pointer is not allowed to differ from the one passed to the
4945 * subscribe() call. It always returns zero.
4946 */
4947static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4948{
4949 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4950}
4951
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004952/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4953 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4954 * parameters of this session.
4955 * This is the responsibility of the caller to check the validity of all the
4956 * pointers passed as parameter to this function.
4957 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4958 * CO_ER_SSL_NO_MEM.
4959 */
4960static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4961 unsigned char *params, size_t params_len)
4962{
4963 int retry;
4964
4965 retry = 1;
4966 retry:
4967 *ssl = SSL_new(ssl_ctx);
4968 if (!*ssl) {
4969 if (!retry--)
4970 goto err;
4971
4972 pool_gc(NULL);
4973 goto retry;
4974 }
4975
4976 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4977 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
4978 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
4979 goto err;
4980
4981 SSL_free(*ssl);
4982 *ssl = NULL;
4983 if (!retry--)
4984 goto err;
4985
4986 pool_gc(NULL);
4987 goto retry;
4988 }
4989
4990 return 0;
4991
4992 err:
4993 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4994 return -1;
4995}
4996
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004997/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4998 * connection with <xprt_ctx> as address of the xprt context.
4999 * Returns 1 if succeeded, 0 if not.
5000 */
5001static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5002{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005003 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005004
5005 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5006
5007 if (*xprt_ctx)
5008 goto out;
5009
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005010 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005011 if (!ctx) {
5012 conn->err_code = CO_ER_SYS_MEMLIM;
5013 goto err;
5014 }
5015
5016 ctx->wait_event.tasklet = tasklet_new();
5017 if (!ctx->wait_event.tasklet) {
5018 conn->err_code = CO_ER_SYS_MEMLIM;
5019 goto err;
5020 }
5021
5022 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5023 ctx->wait_event.tasklet->context = ctx;
5024 ctx->wait_event.events = 0;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005025 HA_ATOMIC_STORE(&ctx->conn, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005026 ctx->subs = NULL;
5027 ctx->xprt_ctx = NULL;
5028
5029 ctx->xprt = xprt_get(XPRT_QUIC);
5030 if (objt_server(conn->target)) {
5031 /* Server */
5032 struct server *srv = __objt_server(conn->target);
5033 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005034 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005035 int ssl_err, ipv4;
5036
5037 ssl_err = SSL_ERROR_NONE;
5038 if (RAND_bytes(dcid, sizeof dcid) != 1)
5039 goto err;
5040
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005041 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005042 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02005043 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005044 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005045 goto err;
5046
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005047 /* Insert our SCID, the connection ID for the QUIC client. */
5048 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
5049
5050 conn->qc = qc;
5051 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005052 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
5053 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005054 goto err;
5055
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005056 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005057 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005058 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
5059 qc->enc_params_len =
5060 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
5061 &qc->rx.params, 0);
5062 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005063 goto err;
5064
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005065 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
5066 qc->enc_params, qc->enc_params_len) == -1)
5067 goto err;
5068
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005069 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005070 SSL_set_connect_state(ctx->ssl);
5071 ssl_err = SSL_do_handshake(ctx->ssl);
5072 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005073 int st;
5074
5075 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005076 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
5077 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005078 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005079 }
5080 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005081 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005082 goto err;
5083 }
5084 }
5085 }
5086 else if (objt_listener(conn->target)) {
5087 /* Listener */
5088 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02005089 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005090
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01005091 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005092 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5093 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005094 goto err;
5095
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01005096 /* Enabling 0-RTT */
5097 if (bc->ssl_conf.early_data)
5098 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5099
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005100 SSL_set_accept_state(ctx->ssl);
5101 }
5102
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005103 HA_ATOMIC_STORE(xprt_ctx, ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005104
5105 /* Leave init state and start handshake */
5106 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005107
5108 out:
5109 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
5110
5111 return 0;
5112
5113 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02005114 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005115 tasklet_free(ctx->wait_event.tasklet);
5116 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005117 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005118 return -1;
5119}
5120
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005121/* Start the QUIC transport layer */
5122static int qc_xprt_start(struct connection *conn, void *ctx)
5123{
5124 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005125 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005126
5127 qc = conn->qc;
5128 if (!quic_conn_init_timer(qc)) {
5129 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
5130 return 0;
5131 }
5132
5133 tasklet_wakeup(qctx->wait_event.tasklet);
5134 return 1;
5135}
5136
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005137/* transport-layer operations for QUIC connections. */
5138static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005139 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005140 .snd_buf = quic_conn_from_buf,
5141 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005142 .subscribe = quic_conn_subscribe,
5143 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005144 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005145 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005146 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5147 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005148 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005149 .name = "QUIC",
5150};
5151
5152__attribute__((constructor))
5153static void __quic_conn_init(void)
5154{
5155 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5156 xprt_register(XPRT_QUIC, &ssl_quic);
5157}
5158
5159__attribute__((destructor))
5160static void __quic_conn_deinit(void)
5161{
5162 BIO_meth_free(ha_quic_meth);
5163}
5164
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005165/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5166 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005167 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005168 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005169static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005170 struct sockaddr_storage *saddr, qpkt_read_func *func)
5171{
5172 unsigned char *pos;
5173 const unsigned char *end;
5174 struct quic_dgram_ctx dgram_ctx = {
5175 .dcid_node = NULL,
5176 .owner = owner,
5177 };
5178
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005179 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005180 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005181 do {
5182 int ret;
5183 struct quic_rx_packet *pkt;
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005184 size_t pkt_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005185
Willy Tarreaue4498932021-03-22 21:13:05 +01005186 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005187 if (!pkt)
5188 goto err;
5189
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005190 quic_rx_packet_refinc(pkt);
5191 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005192 pkt_len = pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02005193 quic_rx_packet_refdec(pkt);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005194 if (ret == -1 && !pkt_len)
5195 /* If the packet length could not be found, we cannot continue. */
5196 break;
5197
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005198 } while (pos < end);
5199
5200 /* Increasing the received bytes counter by the UDP datagram length
5201 * if this datagram could be associated to a connection.
5202 */
5203 if (dgram_ctx.qc)
5204 dgram_ctx.qc->rx.bytes += len;
5205
5206 return pos - (unsigned char *)buf;
5207
5208 err:
5209 return -1;
5210}
5211
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005212ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005213 struct sockaddr_storage *saddr)
5214{
5215 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
5216}
5217
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005218/* Function to automatically activate QUIC traces on stdout.
5219 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5220 * Main use for now is in the docker image for QUIC interop testing.
5221 */
5222static void quic_init_stdout_traces(void)
5223{
5224#ifdef ENABLE_QUIC_STDOUT_TRACES
5225 trace_quic.sink = sink_find("stdout");
5226 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005227 trace_quic.state = TRACE_STATE_RUNNING;
5228#endif
5229}
5230INITCALL0(STG_INIT, quic_init_stdout_traces);
5231
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005232/*
5233 * Local variables:
5234 * c-indent-level: 8
5235 * c-basic-offset: 8
5236 * End:
5237 */