blob: 1a458cd7c5298e37d9eb69571848dab7d1480be4 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
25#include <haproxy/buf-t.h>
26#include <haproxy/compat.h>
27#include <haproxy/api.h>
28#include <haproxy/debug.h>
29#include <haproxy/tools.h>
30#include <haproxy/ticks.h>
31#include <haproxy/time.h>
32
33#include <haproxy/connection.h>
34#include <haproxy/fd.h>
35#include <haproxy/freq_ctr.h>
36#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037#include <haproxy/h3.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010038#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010039#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/pipe.h>
41#include <haproxy/proxy.h>
42#include <haproxy/quic_cc.h>
43#include <haproxy/quic_frame.h>
44#include <haproxy/quic_loss.h>
45#include <haproxy/quic_tls.h>
46#include <haproxy/ssl_sock.h>
47#include <haproxy/stream_interface.h>
48#include <haproxy/task.h>
49#include <haproxy/trace.h>
50#include <haproxy/xprt_quic.h>
51
52struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020053 .max_udp_payload_size = QUIC_DFLT_MAX_UDP_PAYLOAD_SIZE,
54 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
55 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010056};
57
58/* trace source and events */
59static void quic_trace(enum trace_level level, uint64_t mask, \
60 const struct trace_source *src,
61 const struct ist where, const struct ist func,
62 const void *a1, const void *a2, const void *a3, const void *a4);
63
64static const struct trace_event quic_trace_events[] = {
65 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
66 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
67 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
68 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
69 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
70 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
71 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050072 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010073 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
74 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
75 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
76 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
77 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
78 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
79 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
80 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
81 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
82 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
83 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
84 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
85 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
86 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
87 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
88 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
89 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
90 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
91 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010092 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
93 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
94 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
95 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
96 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
97 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +010098 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010099 { /* end */ }
100};
101
102static const struct name_desc quic_trace_lockon_args[4] = {
103 /* arg1 */ { /* already used by the connection */ },
104 /* arg2 */ { .name="quic", .desc="QUIC transport" },
105 /* arg3 */ { },
106 /* arg4 */ { }
107};
108
109static const struct name_desc quic_trace_decoding[] = {
110#define QUIC_VERB_CLEAN 1
111 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
112 { /* end */ }
113};
114
115
116struct trace_source trace_quic = {
117 .name = IST("quic"),
118 .desc = "QUIC xprt",
119 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
120 .default_cb = quic_trace,
121 .known_events = quic_trace_events,
122 .lockon_args = quic_trace_lockon_args,
123 .decoding = quic_trace_decoding,
124 .report_events = ~0, /* report everything by default */
125};
126
127#define TRACE_SOURCE &trace_quic
128INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
129
130static BIO_METHOD *ha_quic_meth;
131
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100132DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200133 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100134
135DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
136
137DECLARE_POOL(pool_head_quic_connection_id,
138 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
139
140DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
141
142DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
143
144DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
145
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100146DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
147
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100148DECLARE_POOL(pool_head_quic_tx_frm, "quic_tx_frm_pool", sizeof(struct quic_tx_frm));
149
150DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
151
152DECLARE_STATIC_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
153
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100154DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100155
156static ssize_t qc_build_hdshk_pkt(struct q_buf *buf, struct quic_conn *qc, int pkt_type,
157 struct quic_enc_level *qel);
158
159int qc_prep_phdshk_pkts(struct quic_conn *qc);
160
161/* Add traces to <buf> depending on <frm> TX frame type. */
162static inline void chunk_tx_frm_appendf(struct buffer *buf,
163 const struct quic_tx_frm *frm)
164{
165 switch (frm->type) {
166 case QUIC_FT_CRYPTO:
167 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
168 (unsigned long long)frm->crypto.offset,
169 (unsigned long long)frm->crypto.len);
170 break;
171 default:
172 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
173 }
174}
175
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100176/* Only for debug purpose */
177struct enc_debug_info {
178 unsigned char *payload;
179 size_t payload_len;
180 unsigned char *aad;
181 size_t aad_len;
182 uint64_t pn;
183};
184
185/* Initializes a enc_debug_info struct (only for debug purpose) */
186static inline void enc_debug_info_init(struct enc_debug_info *edi,
187 unsigned char *payload, size_t payload_len,
188 unsigned char *aad, size_t aad_len, uint64_t pn)
189{
190 edi->payload = payload;
191 edi->payload_len = payload_len;
192 edi->aad = aad;
193 edi->aad_len = aad_len;
194 edi->pn = pn;
195}
196
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100197/* Trace callback for QUIC.
198 * These traces always expect that arg1, if non-null, is of type connection.
199 */
200static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
201 const struct ist where, const struct ist func,
202 const void *a1, const void *a2, const void *a3, const void *a4)
203{
204 const struct connection *conn = a1;
205
206 if (conn) {
207 struct quic_tls_secrets *secs;
208 struct quic_conn *qc;
209
210 qc = conn->qc;
211 chunk_appendf(&trace_buf, " : conn@%p", conn);
212 if ((mask & QUIC_EV_CONN_INIT) && qc) {
213 chunk_appendf(&trace_buf, "\n odcid");
214 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100215 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100216 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100217 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100218 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100219 }
220
221 if (mask & QUIC_EV_CONN_ADDDATA) {
222 const enum ssl_encryption_level_t *level = a2;
223 const size_t *len = a3;
224
225 if (level) {
226 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
227
228 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
229 }
230 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100231 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100232 }
233 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
234 /* Initial read & write secrets. */
235 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
236 const unsigned char *rx_sec = a2;
237 const unsigned char *tx_sec = a3;
238
239 secs = &qc->els[level].tls_ctx.rx;
240 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
241 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
242 if (rx_sec)
243 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
244 quic_tls_keys_hexdump(&trace_buf, secs);
245 }
246 secs = &qc->els[level].tls_ctx.tx;
247 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
248 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
249 if (tx_sec)
250 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
251 quic_tls_keys_hexdump(&trace_buf, secs);
252 }
253 }
254 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
255 const enum ssl_encryption_level_t *level = a2;
256 const unsigned char *secret = a3;
257 const size_t *secret_len = a4;
258
259 if (level) {
260 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
261
262 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
263 if (secret && secret_len)
264 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
265 secs = &qc->els[lvl].tls_ctx.rx;
266 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
267 quic_tls_keys_hexdump(&trace_buf, secs);
268 }
269 }
270
271 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
272 const enum ssl_encryption_level_t *level = a2;
273 const unsigned char *secret = a3;
274 const size_t *secret_len = a4;
275
276 if (level) {
277 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
278
279 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
280 if (secret && secret_len)
281 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
282 secs = &qc->els[lvl].tls_ctx.tx;
283 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
284 quic_tls_keys_hexdump(&trace_buf, secs);
285 }
286
287 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100288
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100289 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290 const struct quic_tx_packet *pkt = a2;
291 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100292 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100293
294 if (qel) {
295 struct quic_pktns *pktns;
296
297 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100298 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
299 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100301 (unsigned long long)qc->path->cwnd,
302 (unsigned long long)qc->path->prep_in_flight,
303 (unsigned long long)qc->path->in_flight,
304 (unsigned long long)pktns->tx.in_flight,
305 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100306 }
307 if (pkt) {
308 const struct quic_tx_frm *frm;
309 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
310 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
311 list_for_each_entry(frm, &pkt->frms, list)
312 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100313 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
314 }
315
316 if (room) {
317 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
318 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
319 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100320 }
321 }
322
323 if (mask & QUIC_EV_CONN_HDSHK) {
324 const enum quic_handshake_state *state = a2;
325 const int *err = a3;
326
327 if (state)
328 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
329 if (err)
330 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
331 }
332
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100333 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100334 const struct quic_rx_packet *pkt = a2;
335 const unsigned long *pktlen = a3;
336 const SSL *ssl = a4;
337
338 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100339 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100340 pkt, quic_packet_type_enc_level_char(pkt->type));
341 if (pkt->pnl)
342 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
343 (unsigned long long)pkt->pn);
344 if (pkt->token_len)
345 chunk_appendf(&trace_buf, " toklen=%llu",
346 (unsigned long long)pkt->token_len);
347 if (pkt->aad_len)
348 chunk_appendf(&trace_buf, " aadlen=%llu",
349 (unsigned long long)pkt->aad_len);
350 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
351 pkt->flags, (unsigned long long)pkt->len);
352 }
353 if (pktlen)
354 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
355 if (ssl) {
356 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
357 chunk_appendf(&trace_buf, " el=%c",
358 quic_enc_level_char(ssl_to_quic_enc_level(level)));
359 }
360 }
361
362 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
363 const struct quic_rx_packet *pkt = a2;
364 const struct quic_rx_crypto_frm *cf = a3;
365 const SSL *ssl = a4;
366
367 if (pkt)
368 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
369 quic_packet_type_enc_level_char(pkt->type),
370 (unsigned long long)pkt->pn);
371 if (cf)
372 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
373 (unsigned long long)cf->offset_node.key,
374 (unsigned long long)cf->len);
375 if (ssl) {
376 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
377 chunk_appendf(&trace_buf, " el=%c",
378 quic_enc_level_char(ssl_to_quic_enc_level(level)));
379 }
380 }
381
382 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
383 const struct quic_frame *frm = a2;
384
385 if (frm)
386 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
387 }
388
389 if (mask & QUIC_EV_CONN_PHPKTS) {
390 const struct quic_enc_level *qel = a2;
391
392 if (qel) {
393 struct quic_pktns *pktns;
394
395 pktns = qc->pktns;
396 chunk_appendf(&trace_buf,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100397 " qel=%c ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u pdg=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100398 quic_enc_level_char_from_qel(qel, qc),
399 !!(pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100400 (unsigned long long)qc->path->cwnd,
401 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100402 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100403 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
404 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100405 }
406 }
407
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100408 if (mask & QUIC_EV_CONN_ENCPKT) {
409 const struct enc_debug_info *edi = a2;
410
411 if (edi)
412 chunk_appendf(&trace_buf,
413 " payload=@%p payload_len=%llu"
414 " aad=@%p aad_len=%llu pn=%llu",
415 edi->payload, (unsigned long long)edi->payload_len,
416 edi->aad, (unsigned long long)edi->aad_len,
417 (unsigned long long)edi->pn);
418 }
419
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100420 if (mask & QUIC_EV_CONN_RMHP) {
421 const struct quic_rx_packet *pkt = a2;
422
423 if (pkt) {
424 const int *ret = a3;
425
426 chunk_appendf(&trace_buf, " pkt@%p", pkt);
427 if (ret && *ret)
428 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
429 pkt->pnl, (unsigned long long)pkt->pn);
430 }
431 }
432
433 if (mask & QUIC_EV_CONN_PRSAFRM) {
434 const struct quic_tx_frm *frm = a2;
435 const unsigned long *val1 = a3;
436 const unsigned long *val2 = a4;
437
438 if (frm)
439 chunk_tx_frm_appendf(&trace_buf, frm);
440 if (val1)
441 chunk_appendf(&trace_buf, " %lu", *val1);
442 if (val2)
443 chunk_appendf(&trace_buf, "..%lu", *val2);
444 }
445
446 if (mask & QUIC_EV_CONN_RTTUPDT) {
447 const unsigned int *rtt_sample = a2;
448 const unsigned int *ack_delay = a3;
449 const struct quic_loss *ql = a4;
450
451 if (rtt_sample)
452 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
453 if (ack_delay)
454 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
455 if (ql)
456 chunk_appendf(&trace_buf,
457 " srtt=%ums rttvar=%ums min_rtt=%ums",
458 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
459 }
460 if (mask & QUIC_EV_CONN_CC) {
461 const struct quic_cc_event *ev = a2;
462 const struct quic_cc *cc = a3;
463
464 if (a2)
465 quic_cc_event_trace(&trace_buf, ev);
466 if (a3)
467 quic_cc_state_trace(&trace_buf, cc);
468 }
469
470 if (mask & QUIC_EV_CONN_PKTLOSS) {
471 const struct quic_pktns *pktns = a2;
472 const struct list *lost_pkts = a3;
473 struct quic_conn *qc = conn->qc;
474
475 if (pktns) {
476 chunk_appendf(&trace_buf, " pktns=%s",
477 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
478 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
479 if (pktns->tx.loss_time)
480 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100481 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100482 }
483 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
484 struct quic_tx_packet *pkt;
485
486 chunk_appendf(&trace_buf, " lost_pkts:");
487 list_for_each_entry(pkt, lost_pkts, list)
488 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
489 }
490 }
491
492 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
493 struct quic_conn *qc = conn->qc;
494 const struct quic_pktns *pktns = a2;
495 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100496 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100497
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100498 if (ifae_pkts)
499 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
500 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100501 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100502 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100503 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100504 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
505 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100506 if (mask & QUIC_EV_CONN_STIMER) {
507 if (pktns->tx.loss_time)
508 chunk_appendf(&trace_buf, " loss_time=%dms",
509 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
510 }
511 if (mask & QUIC_EV_CONN_SPTO) {
512 if (pktns->tx.time_of_last_eliciting)
513 chunk_appendf(&trace_buf, " tole=%dms",
514 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
515 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100516 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100517 }
518 }
519
520 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
521 chunk_appendf(&trace_buf,
522 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
523 }
524 }
525
526 if (mask & QUIC_EV_CONN_SPPKTS) {
527 const struct quic_tx_packet *pkt = a2;
528
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100529 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
530 (unsigned long long)qc->path->cwnd,
531 (unsigned long long)qc->path->prep_in_flight,
532 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100533 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100534 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100535 (unsigned long)pkt->pn_node.key,
536 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
537 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100538 (unsigned long long)pkt->in_flight_len,
539 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100540 }
541 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100542
543 if (mask & QUIC_EV_CONN_SSLALERT) {
544 const uint8_t *alert = a2;
545 const enum ssl_encryption_level_t *level = a3;
546
547 if (alert)
548 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
549 if (level)
550 chunk_appendf(&trace_buf, " el=%c",
551 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
552 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100553
554 if (mask & QUIC_EV_CONN_BCFRMS) {
555 const size_t *sz1 = a2;
556 const size_t *sz2 = a3;
557 const size_t *sz3 = a4;
558
559 if (sz1)
560 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
561 if (sz2)
562 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
563 if (sz3)
564 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
565 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100566
567 if (mask & QUIC_EV_CONN_PSTRM) {
568 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100569
570 if (a2) {
571 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100572
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100573 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
574 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
575 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
576 (unsigned long long)s->id,
577 (unsigned long long)s->offset,
578 (unsigned long long)s->len);
579 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100580 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100581 }
582 if (mask & QUIC_EV_CONN_LPKT) {
583 const struct quic_rx_packet *pkt = a2;
584
585 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200586 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200588 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
589 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100590 }
591
592}
593
594/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200595static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100596{
597 struct quic_conn *qc;
598
599 qc = ctx->conn->qc;
600 if (objt_server(qc->conn->target))
601 return 1;
602
603 if ((qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
604 (qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +0200605 (qc->state & QUIC_HS_ST_COMPLETE))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100606 return 1;
607
608 return 0;
609}
610
611/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
612 * both loss detection and PTO and schedule the task assiated to this timer if needed.
613 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200614static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615{
616 struct quic_conn *qc;
617 struct quic_pktns *pktns;
618 unsigned int pto;
619
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100620 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
621 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100622 qc = ctx->conn->qc;
623 pktns = quic_loss_pktns(qc);
624 if (tick_isset(pktns->tx.loss_time)) {
625 qc->timer = pktns->tx.loss_time;
626 goto out;
627 }
628
629 /* XXX TODO: anti-amplification: the timer must be
630 * cancelled for a server which reached the anti-amplification limit.
631 */
632
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100633 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
634 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100635 /* Timer cancellation. */
636 qc->timer = TICK_ETERNITY;
637 goto out;
638 }
639
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +0200640 pktns = quic_pto_pktns(qc, qc->state & QUIC_HS_ST_COMPLETE, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100641 if (tick_isset(pto))
642 qc->timer = pto;
643 out:
644 task_schedule(qc->timer_task, qc->timer);
645 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
646}
647
648#ifndef OPENSSL_IS_BORINGSSL
649int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
650 const uint8_t *read_secret,
651 const uint8_t *write_secret, size_t secret_len)
652{
653 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
654 struct quic_tls_ctx *tls_ctx =
655 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
656 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
657
658 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
659 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
660 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
661 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
662
663 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
664 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
665 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
666 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
667 read_secret, secret_len)) {
668 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
669 return 0;
670 }
671
672 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
673 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
674 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
675 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
676 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
677 write_secret, secret_len)) {
678 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
679 return 0;
680 }
681
682 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
683 if (objt_server(conn->target) && level == ssl_encryption_application) {
684 const unsigned char *buf;
685 size_t buflen;
686
687 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
688 if (!buflen)
689 return 0;
690
691 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
692 return 0;
693 }
694 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
695
696 return 1;
697}
698#else
699/* ->set_read_secret callback to derive the RX secrets at <level> encryption
700 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500701 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100702 */
703int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
704 const SSL_CIPHER *cipher,
705 const uint8_t *secret, size_t secret_len)
706{
707 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
708 struct quic_tls_ctx *tls_ctx =
709 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
710
711 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
712 tls_ctx->rx.aead = tls_aead(cipher);
713 tls_ctx->rx.md = tls_md(cipher);
714 tls_ctx->rx.hp = tls_hp(cipher);
715
716 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
717 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
718 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
719 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
720 secret, secret_len)) {
721 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
722 goto err;
723 }
724
725 if (objt_server(conn->target) && level == ssl_encryption_application) {
726 const unsigned char *buf;
727 size_t buflen;
728
729 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
730 if (!buflen)
731 goto err;
732
733 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
734 goto err;
735 }
736
737 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
738 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
739
740 return 1;
741
742 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100743 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100744 return 0;
745}
746
747/* ->set_write_secret callback to derive the TX secrets at <level>
748 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500749 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100750 */
751int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
752 const SSL_CIPHER *cipher,
753 const uint8_t *secret, size_t secret_len)
754{
755 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
756 struct quic_tls_ctx *tls_ctx =
757 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
758
759 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
760 tls_ctx->tx.aead = tls_aead(cipher);
761 tls_ctx->tx.md = tls_md(cipher);
762 tls_ctx->tx.hp = tls_hp(cipher);
763
764 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
765 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
766 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
767 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
768 secret, secret_len)) {
769 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
770 goto err;
771 }
772
773 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
774 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
775
776 return 1;
777
778 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100779 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100780 return 0;
781}
782#endif
783
784/* This function copies the CRYPTO data provided by the TLS stack found at <data>
785 * with <len> as size in CRYPTO buffers dedicated to store the information about
786 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
787 * It fails only if it could not managed to allocate enough CRYPTO buffers to
788 * store all the data.
789 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
790 */
791static int quic_crypto_data_cpy(struct quic_enc_level *qel,
792 const unsigned char *data, size_t len)
793{
794 struct quic_crypto_buf **qcb;
795 /* The remaining byte to store in CRYPTO buffers. */
796 size_t cf_offset, cf_len, *nb_buf;
797 unsigned char *pos;
798
799 nb_buf = &qel->tx.crypto.nb_buf;
800 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
801 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
802 cf_len = len;
803
804 while (len) {
805 size_t to_copy, room;
806
807 pos = (*qcb)->data + (*qcb)->sz;
808 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
809 to_copy = len > room ? room : len;
810 if (to_copy) {
811 memcpy(pos, data, to_copy);
812 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
813 qel->tx.crypto.sz += to_copy;
814 (*qcb)->sz += to_copy;
815 pos += to_copy;
816 len -= to_copy;
817 data += to_copy;
818 }
819 else {
820 struct quic_crypto_buf **tmp;
821
822 tmp = realloc(qel->tx.crypto.bufs,
823 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
824 if (tmp) {
825 qel->tx.crypto.bufs = tmp;
826 qcb = &qel->tx.crypto.bufs[*nb_buf];
827 *qcb = pool_alloc(pool_head_quic_crypto_buf);
828 if (!*qcb)
829 return 0;
830
831 (*qcb)->sz = 0;
832 ++*nb_buf;
833 }
834 else {
835 break;
836 }
837 }
838 }
839
840 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
841 * have been buffered.
842 */
843 if (!len) {
844 struct quic_tx_frm *frm;
845
846 frm = pool_alloc(pool_head_quic_tx_frm);
847 if (!frm)
848 return 0;
849
850 frm->type = QUIC_FT_CRYPTO;
851 frm->crypto.offset = cf_offset;
852 frm->crypto.len = cf_len;
Willy Tarreau2b718102021-04-21 07:32:39 +0200853 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100854 }
855
856 return len == 0;
857}
858
859
860/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
861 * wants to provide the QUIC layer with CRYPTO data.
862 * Returns 1 if succeeded, 0 if not.
863 */
864int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
865 const uint8_t *data, size_t len)
866{
867 struct connection *conn;
868 enum quic_tls_enc_level tel;
869 struct quic_enc_level *qel;
870
871 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
872 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
873 tel = ssl_to_quic_enc_level(level);
874 qel = &conn->qc->els[tel];
875
876 if (tel == -1) {
877 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
878 goto err;
879 }
880
881 if (!quic_crypto_data_cpy(qel, data, len)) {
882 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
883 goto err;
884 }
885
886 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
887 conn, &level, &len);
888
889 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
890 return 1;
891
892 err:
893 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
894 return 0;
895}
896
897int ha_quic_flush_flight(SSL *ssl)
898{
899 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
900
901 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
902 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
903
904 return 1;
905}
906
907int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
908{
909 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
910
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100911 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100912 return 1;
913}
914
915/* QUIC TLS methods */
916static SSL_QUIC_METHOD ha_quic_method = {
917#ifdef OPENSSL_IS_BORINGSSL
918 .set_read_secret = ha_set_rsec,
919 .set_write_secret = ha_set_wsec,
920#else
921 .set_encryption_secrets = ha_quic_set_encryption_secrets,
922#endif
923 .add_handshake_data = ha_quic_add_handshake_data,
924 .flush_flight = ha_quic_flush_flight,
925 .send_alert = ha_quic_send_alert,
926};
927
928/* Initialize the TLS context of a listener with <bind_conf> as configuration.
929 * Returns an error count.
930 */
931int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
932{
933 struct proxy *curproxy = bind_conf->frontend;
934 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
935 int cfgerr = 0;
936
937#if 0
938 /* XXX Did not manage to use this. */
939 const char *ciphers =
940 "TLS_AES_128_GCM_SHA256:"
941 "TLS_AES_256_GCM_SHA384:"
942 "TLS_CHACHA20_POLY1305_SHA256:"
943 "TLS_AES_128_CCM_SHA256";
944#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200945 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100946 long options =
947 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
948 SSL_OP_SINGLE_ECDH_USE |
949 SSL_OP_CIPHER_SERVER_PREFERENCE;
950 SSL_CTX *ctx;
951
952 ctx = SSL_CTX_new(TLS_server_method());
953 bind_conf->initial_ctx = ctx;
954
955 SSL_CTX_set_options(ctx, options);
956#if 0
957 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
958 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
959 "for bind '%s' at [%s:%d].\n",
960 curproxy->id, ciphers,
961 bind_conf->arg, bind_conf->file, bind_conf->line);
962 cfgerr++;
963 }
964#endif
965
966 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
967 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
968 "for bind '%s' at [%s:%d].\n",
969 curproxy->id, groups,
970 bind_conf->arg, bind_conf->file, bind_conf->line);
971 cfgerr++;
972 }
973
974 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
975 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
976 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
977 SSL_CTX_set_default_verify_paths(ctx);
978
979#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
980#ifdef OPENSSL_IS_BORINGSSL
981 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
982 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
983#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
984 if (bind_conf->ssl_conf.early_data) {
985 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
986 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
987 }
988 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
989 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
990#else
991 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
992#endif
993 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
994#endif
995 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
996
997 return cfgerr;
998}
999
1000/* Decode an expected packet number from <truncated_on> its truncated value,
1001 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1002 * the number of bits used to encode this packet number (its length in bytes * 8).
1003 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1004 */
1005static uint64_t decode_packet_number(uint64_t largest_pn,
1006 uint32_t truncated_pn, unsigned int pn_nbits)
1007{
1008 uint64_t expected_pn = largest_pn + 1;
1009 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1010 uint64_t pn_hwin = pn_win / 2;
1011 uint64_t pn_mask = pn_win - 1;
1012 uint64_t candidate_pn;
1013
1014
1015 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1016 /* Note that <pn_win> > <pn_hwin>. */
1017 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1018 candidate_pn + pn_hwin <= expected_pn)
1019 return candidate_pn + pn_win;
1020
1021 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1022 return candidate_pn - pn_win;
1023
1024 return candidate_pn;
1025}
1026
1027/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1028 * cryptographic context.
1029 * <largest_pn> is the largest received packet number and <pn> the address of
1030 * the packet number field for this packet with <byte0> address of its first byte.
1031 * <end> points to one byte past the end of this packet.
1032 * Returns 1 if succeeded, 0 if not.
1033 */
1034static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1035 int64_t largest_pn, unsigned char *pn,
1036 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001037 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001038{
1039 int ret, outlen, i, pnlen;
1040 uint64_t packet_number;
1041 uint32_t truncated_pn = 0;
1042 unsigned char mask[5] = {0};
1043 unsigned char *sample;
1044 EVP_CIPHER_CTX *cctx;
1045 unsigned char *hp_key;
1046
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001047 /* Check there is enough data in this packet. */
1048 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1049 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1050 return 0;
1051 }
1052
1053 cctx = EVP_CIPHER_CTX_new();
1054 if (!cctx) {
1055 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1056 return 0;
1057 }
1058
1059 ret = 0;
1060 sample = pn + QUIC_PACKET_PN_MAXLEN;
1061
1062 hp_key = tls_ctx->rx.hp_key;
1063 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1064 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1065 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1066 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1067 goto out;
1068 }
1069
1070 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1071 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1072 for (i = 0; i < pnlen; i++) {
1073 pn[i] ^= mask[i + 1];
1074 truncated_pn = (truncated_pn << 8) | pn[i];
1075 }
1076
1077 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1078 /* Store remaining information for this unprotected header */
1079 pkt->pn = packet_number;
1080 pkt->pnl = pnlen;
1081
1082 ret = 1;
1083
1084 out:
1085 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001086
1087 return ret;
1088}
1089
1090/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1091 * address, with <payload_len> as payload length, <aad> as address of
1092 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1093 * context.
1094 * Returns 1 if succeeded, 0 if not.
1095 */
1096static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1097 unsigned char *aad, size_t aad_len, uint64_t pn,
1098 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1099{
1100 unsigned char iv[12];
1101 unsigned char *tx_iv = tls_ctx->tx.iv;
1102 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001103 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001104
1105 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1106 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001107 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001108 }
1109
1110 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1111 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1112 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001113 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001114 }
1115
1116 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001117
1118 err:
1119 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1120 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1121 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001122}
1123
1124/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1125 * Returns 1 if succeeded, 0 if not.
1126 */
1127static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1128{
1129 int ret;
1130 unsigned char iv[12];
1131 unsigned char *rx_iv = tls_ctx->rx.iv;
1132 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1133
1134 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1135 return 0;
1136
1137 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1138 pkt->data, pkt->aad_len,
1139 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1140 if (!ret)
1141 return 0;
1142
1143 /* Update the packet length (required to parse the frames). */
1144 pkt->len = pkt->aad_len + ret;
1145
1146 return 1;
1147}
1148
1149/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
1150static inline void qc_treat_acked_tx_frm(struct quic_tx_frm *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001151 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001152{
1153 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001154 LIST_DELETE(&frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001155 pool_free(pool_head_quic_tx_frm, frm);
1156}
1157
1158/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1159 * deallocating them, and their TX frames.
1160 * Returns the last node reached to be used for the next range.
1161 * May be NULL if <largest> node could not be found.
1162 */
1163static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1164 struct list *newly_acked_pkts,
1165 struct eb64_node *largest_node,
1166 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001167 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001168{
1169 struct eb64_node *node;
1170 struct quic_tx_packet *pkt;
1171
1172 if (largest_node)
1173 node = largest_node;
1174 else {
1175 node = eb64_lookup(pkts, largest);
1176 while (!node && largest > smallest) {
1177 node = eb64_lookup(pkts, --largest);
1178 }
1179 }
1180
1181 while (node && node->key >= smallest) {
1182 struct quic_tx_frm *frm, *frmbak;
1183
1184 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1185 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001186 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001187 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1188 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1189 qc_treat_acked_tx_frm(frm, ctx);
1190 node = eb64_prev(node);
1191 eb64_delete(&pkt->pn_node);
1192 }
1193
1194 return node;
1195}
1196
1197/* Treat <frm> frame whose packet it is attached to has just been detected as non
1198 * acknowledged.
1199 */
1200static inline void qc_treat_nacked_tx_frm(struct quic_tx_frm *frm,
1201 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001202 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001203{
1204 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001205 LIST_DELETE(&frm->list);
1206 LIST_INSERT(&pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001207}
1208
1209
1210/* Free the TX packets of <pkts> list */
1211static inline void free_quic_tx_pkts(struct list *pkts)
1212{
1213 struct quic_tx_packet *pkt, *tmp;
1214
1215 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001216 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001217 eb64_delete(&pkt->pn_node);
1218 pool_free(pool_head_quic_tx_packet, pkt);
1219 }
1220}
1221
1222/* Send a packet loss event nofification to the congestion controller
1223 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1224 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1225 * at <now_us> current time.
1226 * Always succeeds.
1227 */
1228static inline void qc_cc_loss_event(struct quic_conn *qc,
1229 unsigned int lost_bytes,
1230 unsigned int newest_time_sent,
1231 unsigned int period,
1232 unsigned int now_us)
1233{
1234 struct quic_cc_event ev = {
1235 .type = QUIC_CC_EVT_LOSS,
1236 .loss.now_ms = now_ms,
1237 .loss.max_ack_delay = qc->max_ack_delay,
1238 .loss.lost_bytes = lost_bytes,
1239 .loss.newest_time_sent = newest_time_sent,
1240 .loss.period = period,
1241 };
1242
1243 quic_cc_event(&qc->path->cc, &ev);
1244}
1245
1246/* Send a packet ack event nofication for each newly acked packet of
1247 * <newly_acked_pkts> list and free them.
1248 * Always succeeds.
1249 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001250static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001251 struct list *newly_acked_pkts)
1252{
1253 struct quic_conn *qc = ctx->conn->qc;
1254 struct quic_tx_packet *pkt, *tmp;
1255 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1256
1257 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1258 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001259 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001260 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001261 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001262 ev.ack.acked = pkt->in_flight_len;
1263 ev.ack.time_sent = pkt->time_sent;
1264 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001265 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001266 eb64_delete(&pkt->pn_node);
1267 pool_free(pool_head_quic_tx_packet, pkt);
1268 }
1269
1270}
1271
1272/* Handle <pkts> list of lost packets detected at <now_us> handling
1273 * their TX frames.
1274 * Send a packet loss event to the congestion controller if
1275 * in flight packet have been lost.
1276 * Also frees the packet in <pkts> list.
1277 * Never fails.
1278 */
1279static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001280 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001281 struct list *pkts,
1282 uint64_t now_us)
1283{
1284 struct quic_conn *qc = ctx->conn->qc;
1285 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
1286 struct quic_tx_frm *frm, *frmbak;
1287 uint64_t lost_bytes;
1288
1289 lost_bytes = 0;
1290 oldest_lost = newest_lost = NULL;
1291 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1292 lost_bytes += pkt->in_flight_len;
1293 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001294 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001295 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001296 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001297 /* Treat the frames of this lost packet. */
1298 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1299 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001300 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001301 if (!oldest_lost) {
1302 oldest_lost = newest_lost = pkt;
1303 }
1304 else {
1305 if (newest_lost != oldest_lost)
1306 pool_free(pool_head_quic_tx_packet, newest_lost);
1307 newest_lost = pkt;
1308 }
1309 }
1310
1311 if (lost_bytes) {
1312 /* Sent a packet loss event to the congestion controller. */
1313 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1314 newest_lost->time_sent - oldest_lost->time_sent, now_us);
1315 pool_free(pool_head_quic_tx_packet, oldest_lost);
1316 if (newest_lost != oldest_lost)
1317 pool_free(pool_head_quic_tx_packet, newest_lost);
1318 }
1319}
1320
1321/* Look for packet loss from sent packets for <qel> encryption level of a
1322 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1323 * their tree if deemed as lost or set the <loss_time> value the packet number
1324 * space if any not deemed lost.
1325 * Should be called after having received an ACK frame with newly acknowledged
1326 * packets or when the the loss detection timer has expired.
1327 * Always succeeds.
1328 */
1329static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1330 struct quic_conn *qc,
1331 struct list *lost_pkts)
1332{
1333 struct eb_root *pkts;
1334 struct eb64_node *node;
1335 struct quic_loss *ql;
1336 unsigned int loss_delay;
1337
1338 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1339 pkts = &pktns->tx.pkts;
1340 pktns->tx.loss_time = TICK_ETERNITY;
1341 if (eb_is_empty(pkts))
1342 goto out;
1343
1344 ql = &qc->path->loss;
1345 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1346 loss_delay += loss_delay >> 3;
1347 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1348
1349 node = eb64_first(pkts);
1350 while (node) {
1351 struct quic_tx_packet *pkt;
1352 int64_t largest_acked_pn;
1353 unsigned int loss_time_limit, time_sent;
1354
1355 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1356 largest_acked_pn = pktns->tx.largest_acked_pn;
1357 node = eb64_next(node);
1358 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1359 break;
1360
1361 time_sent = pkt->time_sent;
1362 loss_time_limit = tick_add(time_sent, loss_delay);
1363 if (tick_is_le(time_sent, now_ms) ||
1364 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1365 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001366 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001367 }
1368 else {
1369 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1370 }
1371 }
1372
1373 out:
1374 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1375}
1376
1377/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1378 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001379 * 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 +01001380 * acked ack-eliciting packet.
1381 * Return 1, if succeeded, 0 if not.
1382 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001383static 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 +01001384 struct quic_enc_level *qel,
1385 unsigned int *rtt_sample,
1386 const unsigned char **pos, const unsigned char *end)
1387{
1388 struct quic_ack *ack = &frm->ack;
1389 uint64_t smallest, largest;
1390 struct eb_root *pkts;
1391 struct eb64_node *largest_node;
1392 unsigned int time_sent, pkt_flags;
1393 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1394 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1395
1396 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1397 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1398 ctx->conn,, &ack->largest_ack);
1399 goto err;
1400 }
1401
1402 if (ack->first_ack_range > ack->largest_ack) {
1403 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1404 ctx->conn,, &ack->first_ack_range);
1405 goto err;
1406 }
1407
1408 largest = ack->largest_ack;
1409 smallest = largest - ack->first_ack_range;
1410 pkts = &qel->pktns->tx.pkts;
1411 pkt_flags = 0;
1412 largest_node = NULL;
1413 time_sent = 0;
1414
1415 if ((int64_t)ack->largest_ack > qel->pktns->tx.largest_acked_pn) {
1416 largest_node = eb64_lookup(pkts, largest);
1417 if (!largest_node) {
1418 TRACE_DEVEL("Largest acked packet not found",
1419 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1420 goto err;
1421 }
1422
1423 time_sent = eb64_entry(&largest_node->node,
1424 struct quic_tx_packet, pn_node)->time_sent;
1425 }
1426
1427 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1428 ctx->conn,, &largest, &smallest);
1429 do {
1430 uint64_t gap, ack_range;
1431
1432 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1433 largest_node, largest, smallest, ctx);
1434 if (!ack->ack_range_num--)
1435 break;
1436
1437 if (!quic_dec_int(&gap, pos, end))
1438 goto err;
1439
1440 if (smallest < gap + 2) {
1441 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1442 ctx->conn,, &gap, &smallest);
1443 goto err;
1444 }
1445
1446 largest = smallest - gap - 2;
1447 if (!quic_dec_int(&ack_range, pos, end))
1448 goto err;
1449
1450 if (largest < ack_range) {
1451 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1452 ctx->conn,, &largest, &ack_range);
1453 goto err;
1454 }
1455
1456 /* Do not use this node anymore. */
1457 largest_node = NULL;
1458 /* Next range */
1459 smallest = largest - ack_range;
1460
1461 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1462 ctx->conn,, &largest, &smallest);
1463 } while (1);
1464
1465 /* Flag this packet number space as having received an ACK. */
1466 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_RECEIVED;
1467
1468 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1469 *rtt_sample = tick_remain(time_sent, now_ms);
1470 qel->pktns->tx.largest_acked_pn = ack->largest_ack;
1471 }
1472
1473 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1474 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1475 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1476 if (!LIST_ISEMPTY(&lost_pkts))
1477 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1478 }
1479 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1480 if (quic_peer_validated_addr(ctx))
1481 ctx->conn->qc->path->loss.pto_count = 0;
1482 qc_set_timer(ctx);
1483 }
1484
1485
1486 return 1;
1487
1488 err:
1489 free_quic_tx_pkts(&newly_acked_pkts);
1490 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1491 return 0;
1492}
1493
1494/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1495 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001496 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001497 * Return 1 if succeeded, 0 if not.
1498 */
1499static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001500 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001501 const unsigned char *data, size_t len,
1502 struct quic_rx_packet *pkt,
1503 struct quic_rx_crypto_frm *cf)
1504{
1505 int ssl_err;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001506 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001507
1508 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1509 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001510 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001511 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1512 TRACE_PROTO("SSL_provide_quic_data() error",
1513 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1514 goto err;
1515 }
1516
1517 el->rx.crypto.offset += len;
1518 TRACE_PROTO("in order CRYPTO data",
1519 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1520
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001521 if (qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001522 ssl_err = SSL_do_handshake(ctx->ssl);
1523 if (ssl_err != 1) {
1524 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1525 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1526 TRACE_PROTO("SSL handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001527 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001528 goto out;
1529 }
1530
1531 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001532 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001533 goto err;
1534 }
1535
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001536 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001537 if (objt_listener(ctx->conn->target))
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001538 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001539 else
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001540 qc->state = QUIC_HS_ST_COMPLETE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001541 } else {
1542 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1543 if (ssl_err != 1) {
1544 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1545 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1546 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001547 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001548 goto out;
1549 }
1550
1551 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001552 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001553 goto err;
1554 }
1555
1556 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001557 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001558 }
1559
1560 out:
1561 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1562 return 1;
1563
1564 err:
1565 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1566 return 0;
1567}
1568
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001569/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1570 * <pkt> RX packet.
1571 * Return it if succeeded, NULL if not.
1572 */
1573static inline
1574struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1575 struct quic_rx_packet *pkt)
1576{
1577 struct quic_rx_strm_frm *frm;
1578
1579 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1580 if (frm) {
1581 frm->offset_node.key = stream_frm->offset;
1582 frm->len = stream_frm->len;
1583 frm->data = stream_frm->data;
1584 frm->pkt = pkt;
1585 }
1586
1587 return frm;
1588}
1589
1590/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1591 * several streams, depending on the already open onces.
1592 * Return this node if succeeded, NULL if not.
1593 */
1594static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1595{
1596 unsigned int strm_type;
1597 int64_t sub_id;
1598 struct eb64_node *strm_node;
1599
1600 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1601
1602 strm_type = id & QCS_ID_TYPE_MASK;
1603 sub_id = id >> QCS_ID_TYPE_SHIFT;
1604 strm_node = NULL;
1605 if (qc_local_stream_id(qcc, id)) {
1606 /* Local streams: this stream must be already opened. */
1607 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1608 if (!strm_node) {
1609 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1610 goto out;
1611 }
1612 }
1613 else {
1614 /* Remote streams. */
1615 struct eb_root *strms;
1616 uint64_t largest_id;
1617 enum qcs_type qcs_type;
1618
1619 strms = &qcc->streams_by_id;
1620 qcs_type = qcs_id_type(id);
1621 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1622 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1623 goto out;
1624 }
1625
1626 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1627 * correct value.
1628 */
1629 largest_id = qcc->strms[qcs_type].largest_id;
1630 if (sub_id > (int64_t)largest_id) {
1631 /* RFC: "A stream ID that is used out of order results in all streams
1632 * of that type with lower-numbered stream IDs also being opened".
1633 * So, let's "open" these streams.
1634 */
1635 int64_t i;
1636 struct qcs *qcs;
1637
1638 qcs = NULL;
1639 for (i = largest_id + 1; i <= sub_id; i++) {
1640 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1641 if (!qcs) {
1642 TRACE_PROTO("Could not allocate a new stream",
1643 QUIC_EV_CONN_PSTRM, qcc->conn);
1644 goto out;
1645 }
1646
1647 qcc->strms[qcs_type].largest_id = i;
1648 }
1649 if (qcs)
1650 strm_node = &qcs->by_id;
1651 }
1652 else {
1653 strm_node = eb64_lookup(strms, id);
1654 }
1655 }
1656
1657 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1658 return strm_node;
1659
1660 out:
1661 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1662 return NULL;
1663}
1664
1665/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1666 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1667 * to reflect the data which have been consumed.
1668 */
1669static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1670{
1671 size_t ret;
1672
1673 ret = 0;
1674 while (strm_frm->len) {
1675 size_t try;
1676
1677 try = b_contig_space(buf);
1678 if (!try)
1679 break;
1680
1681 if (try > strm_frm->len)
1682 try = strm_frm->len;
1683 memcpy(b_tail(buf), strm_frm->data, try);
1684 strm_frm->len -= try;
1685 strm_frm->offset += try;
1686 b_add(buf, try);
1687 ret += try;
1688 }
1689
1690 return ret;
1691}
1692
1693/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1694 * streams may be open. The data are copied to the stream RX buffer if possible.
1695 * If not, the STREAM frame is stored to be treated again later.
1696 * We rely on the flow control so that not to store too much STREAM frames.
1697 * Return 1 if succeeded, 0 if not.
1698 */
1699static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1700 struct quic_stream *strm_frm,
1701 struct quic_conn *qc)
1702{
1703 struct qcs *strm;
1704 struct eb64_node *strm_node, *frm_node;
1705 struct quic_rx_strm_frm *frm;
1706
1707 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1708 if (!strm_node) {
1709 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1710 return 0;
1711 }
1712
1713 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1714 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1715 /* FIXME: handle the case where this frame overlap others */
1716 if (frm_node) {
1717 TRACE_PROTO("Already existing stream data",
1718 QUIC_EV_CONN_PSTRM, qc->conn);
1719 goto out;
1720 }
1721
1722 if (strm_frm->offset == strm->rx.offset) {
1723 int ret;
1724
1725 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1726 goto store_frm;
1727
1728 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1729 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1730 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1731 return 0;
1732 }
1733
1734 strm->rx.offset += ret;
1735 }
1736
1737 if (!strm_frm->len)
1738 goto out;
1739
1740 store_frm:
1741 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1742 if (!frm) {
1743 TRACE_PROTO("Could not alloc RX STREAM frame",
1744 QUIC_EV_CONN_PSTRM, qc->conn);
1745 return 0;
1746 }
1747
1748 eb64_insert(&strm->frms, &frm->offset_node);
1749 quic_rx_packet_refinc(pkt);
1750
1751 out:
1752 return 1;
1753}
1754
1755/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1756 * streams may be open. The data are copied to the stream RX buffer if possible.
1757 * If not, the STREAM frame is stored to be treated again later.
1758 * We rely on the flow control so that not to store too much STREAM frames.
1759 * Return 1 if succeeded, 0 if not.
1760 */
1761static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1762 struct quic_stream *strm_frm,
1763 struct quic_conn *qc)
1764{
1765 struct qcs *strm;
1766 struct eb64_node *strm_node, *frm_node;
1767 struct quic_rx_strm_frm *frm;
1768 size_t strm_frm_len;
1769
1770 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1771 if (!strm_node) {
1772 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1773 return 0;
1774 }
1775
1776 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1777 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1778 /* FIXME: handle the case where this frame overlap others */
1779 if (frm_node) {
1780 TRACE_PROTO("Already existing stream data",
1781 QUIC_EV_CONN_PSTRM, qc->conn);
1782 goto out;
1783 }
1784
1785 strm_frm_len = strm_frm->len;
1786 if (strm_frm->offset == strm->rx.offset) {
1787 int ret;
1788
1789 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1790 goto store_frm;
1791
1792 /* qc_strm_cpy() will modify the offset, depending on the number
1793 * of bytes copied.
1794 */
1795 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1796 /* Inform the application of the arrival of this new stream */
1797 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1798 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1799 return 0;
1800 }
1801
1802 if (ret)
1803 ruqs_notify_recv(strm);
1804
1805 strm_frm->offset += ret;
1806 }
1807 /* Take this frame into an account for the stream flow control */
1808 strm->rx.offset += strm_frm_len;
1809 /* It all the data were provided to the application, there is no need to
1810 * store any more inforamtion for it.
1811 */
1812 if (!strm_frm->len)
1813 goto out;
1814
1815 store_frm:
1816 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1817 if (!frm) {
1818 TRACE_PROTO("Could not alloc RX STREAM frame",
1819 QUIC_EV_CONN_PSTRM, qc->conn);
1820 return 0;
1821 }
1822
1823 eb64_insert(&strm->frms, &frm->offset_node);
1824 quic_rx_packet_refinc(pkt);
1825
1826 out:
1827 return 1;
1828}
1829
1830static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1831 struct quic_stream *strm_frm,
1832 struct quic_conn *qc)
1833{
1834 if (strm_frm->id & QCS_ID_DIR_BIT)
1835 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1836 else
1837 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1838}
1839
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001840/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1841 * as I/O handler context and <qel> as encryption level.
1842 * Returns 1 if succeeded, 0 if failed.
1843 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001844static 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 +01001845 struct quic_enc_level *qel)
1846{
1847 struct quic_frame frm;
1848 const unsigned char *pos, *end;
1849 struct quic_conn *conn = ctx->conn->qc;
1850
1851 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1852 /* Skip the AAD */
1853 pos = pkt->data + pkt->aad_len;
1854 end = pkt->data + pkt->len;
1855
1856 while (pos < end) {
1857 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1858 goto err;
1859
1860 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001861 case QUIC_FT_PADDING:
1862 if (pos != end) {
1863 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1864 goto err;
1865 }
1866 break;
1867 case QUIC_FT_PING:
1868 break;
1869 case QUIC_FT_ACK:
1870 {
1871 unsigned int rtt_sample;
1872
1873 rtt_sample = 0;
1874 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1875 goto err;
1876
1877 if (rtt_sample) {
1878 unsigned int ack_delay;
1879
1880 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1881 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1882 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1883 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001884 break;
1885 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001886 case QUIC_FT_CRYPTO:
1887 if (frm.crypto.offset != qel->rx.crypto.offset) {
1888 struct quic_rx_crypto_frm *cf;
1889
1890 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1891 if (!cf) {
1892 TRACE_DEVEL("CRYPTO frame allocation failed",
1893 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1894 goto err;
1895 }
1896
1897 cf->offset_node.key = frm.crypto.offset;
1898 cf->len = frm.crypto.len;
1899 cf->data = frm.crypto.data;
1900 cf->pkt = pkt;
1901 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1902 quic_rx_packet_refinc(pkt);
1903 }
1904 else {
1905 /* XXX TO DO: <cf> is used only for the traces. */
1906 struct quic_rx_crypto_frm cf = { };
1907
1908 cf.offset_node.key = frm.crypto.offset;
1909 cf.len = frm.crypto.len;
1910 if (!qc_provide_cdata(qel, ctx,
1911 frm.crypto.data, frm.crypto.len,
1912 pkt, &cf))
1913 goto err;
1914 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001915 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001916 case QUIC_FT_STREAM_8:
1917 case QUIC_FT_STREAM_9:
1918 case QUIC_FT_STREAM_A:
1919 case QUIC_FT_STREAM_B:
1920 case QUIC_FT_STREAM_C:
1921 case QUIC_FT_STREAM_D:
1922 case QUIC_FT_STREAM_E:
1923 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001924 {
1925 struct quic_stream *stream = &frm.stream;
1926
1927 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
1928 if (objt_listener(ctx->conn->target)) {
1929 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
1930 goto err;
1931 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
1932 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001933
1934 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
1935 goto err;
1936
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001937 break;
1938 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001939 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001940 break;
1941 case QUIC_FT_CONNECTION_CLOSE:
1942 case QUIC_FT_CONNECTION_CLOSE_APP:
1943 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001944 case QUIC_FT_HANDSHAKE_DONE:
1945 if (objt_listener(ctx->conn->target))
1946 goto err;
1947
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001948 conn->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001949 break;
1950 default:
1951 goto err;
1952 }
1953 }
1954
1955 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
1956 * has successfully parse a Handshake packet. The Initial encryption must also
1957 * be discarded.
1958 */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001959 if (conn->state == QUIC_HS_ST_SERVER_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001960 pkt->type == QUIC_PACKET_TYPE_HANDSHAKE) {
1961 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
1962 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
1963 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001964 conn->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001965 }
1966
1967 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1968 return 1;
1969
1970 err:
1971 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
1972 return 0;
1973}
1974
1975/* Prepare as much as possible handshake packets for the QUIC connection
1976 * with <ctx> as I/O handler context.
1977 * Returns 1 if succeeded, or 0 if something wrong happened.
1978 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001979static int qc_prep_hdshk_pkts(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001980{
1981 struct quic_conn *qc;
1982 enum quic_tls_enc_level tel, next_tel;
1983 struct quic_enc_level *qel;
1984 struct q_buf *wbuf;
1985 /* A boolean to flag <wbuf> as reusable, even if not empty. */
1986 int reuse_wbuf;
1987
1988 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
1989 qc = ctx->conn->qc;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001990 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc->state)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001991 TRACE_DEVEL("unknown enc. levels",
1992 QUIC_EV_CONN_PHPKTS, ctx->conn);
1993 goto err;
1994 }
1995
1996 reuse_wbuf = 0;
1997 wbuf = q_wbuf(qc);
1998 qel = &qc->els[tel];
Ilya Shipitsin01881082021-08-07 14:41:56 +05001999 /* When entering this function, the writer buffer must be empty.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002000 * Most of the time it points to the reader buffer.
2001 */
2002 while ((q_buf_empty(wbuf) || reuse_wbuf)) {
2003 ssize_t ret;
2004 enum quic_pkt_type pkt_type;
2005
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002006 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002007 /* Do not build any more packet f the TX secrets are not available or
2008 * f there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002009 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002010 * and if there is no more CRYPTO data available or in flight
2011 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002012 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002013 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2014 (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002015 !qc->tx.nb_pto_dgrams &&
2016 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002017 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002018 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
2019 /* Consume the buffer if we were supposed to reuse it. */
2020 if (reuse_wbuf)
2021 wbuf = q_next_wbuf(qc);
2022 break;
2023 }
2024
2025 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002026 ret = qc_build_hdshk_pkt(wbuf, qc, pkt_type, qel);
2027 switch (ret) {
2028 case -2:
2029 goto err;
2030 case -1:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002031 if (!reuse_wbuf)
2032 goto out;
2033
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002034 /* Not enough room in <wbuf>. */
2035 wbuf = q_next_wbuf(qc);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002036 reuse_wbuf = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002037 continue;
2038 case 0:
2039 goto out;
2040 default:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002041 reuse_wbuf = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002042 /* Discard the Initial encryption keys as soon as
2043 * a handshake packet could be built.
2044 */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002045 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002046 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2047 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2048 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2049 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002050 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002051 }
2052 /* Special case for Initial packets: when they have all
2053 * been sent, select the next level.
2054 */
2055 if ((LIST_ISEMPTY(&qel->pktns->tx.frms) || qc->els[next_tel].pktns->tx.in_flight) &&
2056 tel == QUIC_TLS_ENC_LEVEL_INITIAL) {
2057 tel = next_tel;
2058 qel = &qc->els[tel];
2059 if (LIST_ISEMPTY(&qel->pktns->tx.frms)) {
2060 /* If there is no more data for the next level, let's
2061 * consume a buffer. This is the case for a client
2062 * which sends only one Initial packet, then wait
2063 * for additional CRYPTO data from the server to enter the
2064 * next level.
2065 */
2066 wbuf = q_next_wbuf(qc);
2067 }
2068 else {
2069 /* Let's try to reuse this buffer. */
2070 reuse_wbuf = 1;
2071 }
2072 }
2073 else {
2074 wbuf = q_next_wbuf(qc);
2075 }
2076 }
2077 }
2078
2079 out:
2080 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
2081 return 1;
2082
2083 err:
2084 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
2085 return 0;
2086}
2087
2088/* Send the QUIC packets which have been prepared for QUIC connections
2089 * with <ctx> as I/O handler context.
2090 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002091int qc_send_ppkts(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002092{
2093 struct quic_conn *qc;
2094 struct buffer tmpbuf = { };
2095 struct q_buf *rbuf;
2096
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002097 qc = ctx->conn->qc;
2098 for (rbuf = q_rbuf(qc); !q_buf_empty(rbuf) ; rbuf = q_next_rbuf(qc)) {
2099 struct quic_tx_packet *p, *q;
2100 unsigned int time_sent;
2101
2102 tmpbuf.area = (char *)rbuf->area;
2103 tmpbuf.size = tmpbuf.data = rbuf->data;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002104 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002105 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
2106 &tmpbuf, tmpbuf.data, 0) <= 0)
2107 break;
2108
2109 qc->tx.bytes += tmpbuf.data;
2110 time_sent = now_ms;
2111 /* Reset this buffer to make it available for the next packet to prepare. */
2112 q_buf_reset(rbuf);
2113 /* Remove from <rbuf> the packets which have just been sent. */
2114 list_for_each_entry_safe(p, q, &rbuf->pkts, list) {
2115 p->time_sent = time_sent;
2116 if (p->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2117 p->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002118 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002119 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002120 qc->path->in_flight += p->in_flight_len;
2121 p->pktns->tx.in_flight += p->in_flight_len;
2122 if (p->in_flight_len)
2123 qc_set_timer(ctx);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002124 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, p);
Willy Tarreau2b718102021-04-21 07:32:39 +02002125 LIST_DELETE(&p->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002126 }
2127 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002128
2129 return 1;
2130}
2131
2132/* Build all the frames which must be sent just after the handshake have succeeded.
2133 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2134 * a HANDSHAKE_DONE frame.
2135 * Return 1 if succeeded, 0 if not.
2136 */
2137static int quic_build_post_handshake_frames(struct quic_conn *conn)
2138{
2139 int i;
2140 struct quic_frame *frm;
2141
2142 /* Only servers must send a HANDSHAKE_DONE frame. */
2143 if (!objt_server(conn->conn->target)) {
2144 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002145 if (!frm)
2146 return 0;
2147
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002148 frm->type = QUIC_FT_HANDSHAKE_DONE;
Willy Tarreau2b718102021-04-21 07:32:39 +02002149 LIST_APPEND(&conn->tx.frms_to_send, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002150 }
2151
Frédéric Lécaille5aa41432021-01-28 16:22:52 +01002152 for (i = 1; i < conn->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002153 struct quic_connection_id *cid;
2154
2155 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002156 cid = new_quic_cid(&conn->cids, i);
2157 if (!frm || !cid)
2158 goto err;
2159
2160 quic_connection_id_to_frm_cpy(frm, cid);
Willy Tarreau2b718102021-04-21 07:32:39 +02002161 LIST_APPEND(&conn->tx.frms_to_send, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002162 }
2163
2164 return 1;
2165
2166 err:
2167 free_quic_conn_cids(conn);
2168 return 0;
2169}
2170
2171/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002172void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002173{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002174 struct eb64_node *n;
2175 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002176
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002177 n = eb64_first(&arngs->root);
2178 while (n) {
2179 struct eb64_node *next;
2180
2181 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2182 next = eb64_next(n);
2183 eb64_delete(n);
2184 free(ar);
2185 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002186 }
2187}
2188
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002189/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2190 * descending order.
2191 */
2192static inline size_t sack_gap(struct quic_arng_node *p,
2193 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002194{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002195 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002196}
2197
2198
2199/* Remove the last elements of <ack_ranges> list of ack range updating its
2200 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002201 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002202 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002203static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002204{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002205 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002206
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002207 last = eb64_last(&arngs->root);
2208 while (last && arngs->enc_sz > limit) {
2209 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002210
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002211 prev = eb64_prev(last);
2212 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002213 return 0;
2214
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002215 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2216 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2217 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2218 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2219 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2220 --arngs->sz;
2221 eb64_delete(last);
2222 pool_free(pool_head_quic_arng, last);
2223 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002224 }
2225
2226 return 1;
2227}
2228
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002229/* Set the encoded size of <arngs> QUIC ack ranges. */
2230static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2231{
2232 struct eb64_node *node, *next;
2233 struct quic_arng_node *ar, *ar_next;
2234
2235 node = eb64_last(&arngs->root);
2236 if (!node)
2237 return;
2238
2239 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2240 arngs->enc_sz = quic_int_getsize(ar->last) +
2241 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2242
2243 while ((next = eb64_prev(node))) {
2244 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2245 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2246 quic_int_getsize(ar_next->last - ar_next->first.key);
2247 node = next;
2248 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2249 }
2250}
2251
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002252/* Insert <ar> ack range into <argns> tree of ack ranges.
2253 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002254 */
2255static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002256struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2257 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002258{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002259 struct quic_arng_node *new_ar;
2260
2261 new_ar = pool_alloc(pool_head_quic_arng);
2262 if (new_ar) {
2263 new_ar->first.key = ar->first;
2264 new_ar->last = ar->last;
2265 eb64_insert(&arngs->root, &new_ar->first);
2266 arngs->sz++;
2267 }
2268
2269 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002270}
2271
2272/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002273 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002274 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002275 *
2276 * Descending order
2277 * ------------->
2278 * range1 range2
2279 * ..........|--------|..............|--------|
2280 * ^ ^ ^ ^
2281 * | | | |
2282 * last1 first1 last2 first2
2283 * ..........+--------+--------------+--------+......
2284 * diff1 gap12 diff2
2285 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002286 * To encode the previous list of ranges we must encode integers as follows in
2287 * descending order:
2288 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002289 * with diff1 = last1 - first1
2290 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002291 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002292 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002293 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002294int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2295 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002296{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002297 struct eb64_node *le;
2298 struct quic_arng_node *new_node;
2299 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002300
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002301 new = NULL;
2302 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002303 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002304 if (!new_node)
2305 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002306
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002307 goto out;
2308 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002309
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002310 le = eb64_lookup_le(&arngs->root, ar->first);
2311 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002312 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002313 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002314 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002315 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002316 else {
2317 struct quic_arng_node *le_ar =
2318 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002319
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002320 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002321 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002322 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002323
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002324 if (le_ar->last + 1 >= ar->first) {
2325 le_ar->last = ar->last;
2326 new = le;
2327 new_node = le_ar;
2328 }
2329 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002330 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002331 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002332 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002333
2334 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002335 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002336 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002337
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002338 /* Verify that the new inserted node does not overlap the nodes
2339 * which follow it.
2340 */
2341 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002342 struct eb64_node *next;
2343 struct quic_arng_node *next_node;
2344
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002345 while ((next = eb64_next(new))) {
2346 next_node =
2347 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002348 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002349 break;
2350
2351 if (next_node->last > new_node->last)
2352 new_node->last = next_node->last;
2353 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002354 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002355 /* Decrement the size of these ranges. */
2356 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002357 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002358 }
2359
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002360 quic_arngs_set_enc_sz(arngs);
2361
2362 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002363 return 1;
2364}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002365/* Remove the header protection of packets at <el> encryption level.
2366 * Always succeeds.
2367 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002368static 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 +01002369{
2370 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002371 struct quic_rx_packet *pqpkt;
2372 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002373 struct quic_enc_level *app_qel;
2374
2375 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2376 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2377 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002378 if (el == app_qel && objt_listener(ctx->conn->target) &&
2379 ctx->conn->qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002380 TRACE_PROTO("hp not removed (handshake not completed)",
2381 QUIC_EV_CONN_ELRMHP, ctx->conn);
2382 goto out;
2383 }
2384 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002385 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002386 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2387 pqpkt->data + pqpkt->pn_offset,
2388 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2389 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2390 /* XXX TO DO XXX */
2391 }
2392 else {
2393 /* The AAD includes the packet number field */
2394 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2395 /* Store the packet into the tree of packets to decrypt. */
2396 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002397 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002398 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002399 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002400 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2401 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002402 MT_LIST_DELETE_SAFE(pkttmp1);
2403 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002404 }
2405
2406 out:
2407 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2408}
2409
2410/* Process all the CRYPTO frame at <el> encryption level.
2411 * Return 1 if succeeded, 0 if not.
2412 */
2413static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002414 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002415{
2416 struct eb64_node *node;
2417
2418 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
2419 node = eb64_first(&el->rx.crypto.frms);
2420 while (node) {
2421 struct quic_rx_crypto_frm *cf;
2422
2423 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2424 if (cf->offset_node.key != el->rx.crypto.offset)
2425 break;
2426
2427 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2428 goto err;
2429
2430 node = eb64_next(node);
2431 quic_rx_packet_refdec(cf->pkt);
2432 eb64_delete(&cf->offset_node);
2433 pool_free(pool_head_quic_rx_crypto_frm, cf);
2434 }
2435
2436 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2437 return 1;
2438
2439 err:
2440 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2441 return 0;
2442}
2443
2444/* Process all the packets at <el> encryption level.
2445 * Return 1 if succeeded, 0 if not.
2446 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002447int qc_treat_rx_pkts(struct quic_enc_level *el, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002448{
2449 struct quic_tls_ctx *tls_ctx;
2450 struct eb64_node *node;
2451
2452 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2453 tls_ctx = &el->tls_ctx;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002454 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002455 node = eb64_first(&el->rx.pkts);
2456 while (node) {
2457 struct quic_rx_packet *pkt;
2458
2459 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
2460 if (!qc_pkt_decrypt(pkt, tls_ctx)) {
2461 /* Drop the packet */
2462 TRACE_PROTO("packet decryption failed -> dropped",
2463 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2464 }
2465 else {
Frédéric Lécaillec4b93ea2021-06-04 10:12:43 +02002466 if (!qc_parse_pkt_frms(pkt, ctx, el)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002467 /* Drop the packet */
2468 TRACE_PROTO("packet parsing failed -> dropped",
2469 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2470 }
2471 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002472 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2473
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002474 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
2475 el->pktns->rx.nb_ack_eliciting++;
2476 if (!(el->pktns->rx.nb_ack_eliciting & 1))
2477 el->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
2478 }
2479
2480 /* Update the largest packet number. */
2481 if (pkt->pn > el->pktns->rx.largest_pn)
2482 el->pktns->rx.largest_pn = pkt->pn;
2483
2484 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille654c6912021-06-04 10:27:23 +02002485 if (!quic_update_ack_ranges_list(&el->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002486 TRACE_DEVEL("Could not update ack range list",
2487 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002488 }
2489 }
2490 node = eb64_next(node);
2491 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002492 }
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002493 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002494
2495 if (!qc_treat_rx_crypto_frms(el, ctx))
2496 goto err;
2497
2498 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2499 return 1;
2500
2501 err:
2502 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2503 return 0;
2504}
2505
2506/* Called during handshakes to parse and build Initial and Handshake packets for QUIC
2507 * connections with <ctx> as I/O handler context.
2508 * Returns 1 if succeeded, 0 if not.
2509 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002510int qc_do_hdshk(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002511{
2512 int ssl_err;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002513 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002514 enum quic_tls_enc_level tel, next_tel;
2515 struct quic_enc_level *qel, *next_qel;
2516 struct quic_tls_ctx *tls_ctx;
2517
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002518 qc = ctx->conn->qc;
2519 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002520 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002521 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc->state))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002522 goto err;
2523
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002524 qel = &qc->els[tel];
2525 next_qel = &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002526
2527 next_level:
2528 tls_ctx = &qel->tls_ctx;
2529
2530 /* If the header protection key for this level has been derived,
2531 * remove the packet header protections.
2532 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002533 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002534 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2535 qc_rm_hp_pkts(qel, ctx);
2536
2537 if (!eb_is_empty(&qel->rx.pkts) &&
2538 !qc_treat_rx_pkts(qel, ctx))
2539 goto err;
2540
2541 if (!qc_prep_hdshk_pkts(ctx))
2542 goto err;
2543
2544 if (!qc_send_ppkts(ctx))
2545 goto err;
2546
2547 /* Check if there is something to do for the next level.
2548 */
2549 if ((next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002550 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002551 qel = next_qel;
2552 goto next_level;
2553 }
2554
2555 /* If the handshake has not been completed -> out! */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002556 if (qc->state < QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002557 goto out;
2558
2559 /* Discard the Handshake keys. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002560 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2561 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002562 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002563 if (!quic_build_post_handshake_frames(qc) ||
2564 !qc_prep_phdshk_pkts(qc) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002565 !qc_send_ppkts(ctx))
2566 goto err;
2567
2568 out:
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002569 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002570 return 1;
2571
2572 err:
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002573 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002574 return 0;
2575}
2576
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002577/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002578static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2579{
2580 int i;
2581
2582 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2583 if (qel->tx.crypto.bufs[i]) {
2584 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2585 qel->tx.crypto.bufs[i] = NULL;
2586 }
2587 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002588 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002589}
2590
2591/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002592 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002593 * Returns 1 if succeeded, 0 if not.
2594 */
2595static int quic_conn_enc_level_init(struct quic_conn *qc,
2596 enum quic_tls_enc_level level)
2597{
2598 struct quic_enc_level *qel;
2599
2600 qel = &qc->els[level];
2601 qel->level = quic_to_ssl_enc_level(level);
2602 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2603 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2604 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2605 qel->tls_ctx.rx.flags = 0;
2606 qel->tls_ctx.tx.flags = 0;
2607
2608 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002609 HA_RWLOCK_INIT(&qel->rx.rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002610 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002611
2612 /* Allocate only one buffer. */
2613 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2614 if (!qel->tx.crypto.bufs)
2615 goto err;
2616
2617 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2618 if (!qel->tx.crypto.bufs[0])
2619 goto err;
2620
2621 qel->tx.crypto.bufs[0]->sz = 0;
2622 qel->tx.crypto.nb_buf = 1;
2623
2624 qel->tx.crypto.sz = 0;
2625 qel->tx.crypto.offset = 0;
2626
2627 return 1;
2628
2629 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002630 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002631 return 0;
2632}
2633
2634/* Release the memory allocated for <buf> array of buffers, with <nb> as size.
2635 * Never fails.
2636 */
2637static inline void free_quic_conn_tx_bufs(struct q_buf **bufs, size_t nb)
2638{
2639 struct q_buf **p;
2640
2641 if (!bufs)
2642 return;
2643
2644 p = bufs;
2645 while (--nb) {
2646 if (!*p) {
2647 p++;
2648 continue;
2649 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002650 ha_free(&(*p)->area);
2651 ha_free(p);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002652 p++;
2653 }
2654 free(bufs);
2655}
2656
2657/* Allocate an array or <nb> buffers of <sz> bytes each.
2658 * Return this array if succeeded, NULL if failed.
2659 */
2660static inline struct q_buf **quic_conn_tx_bufs_alloc(size_t nb, size_t sz)
2661{
2662 int i;
2663 struct q_buf **bufs, **p;
2664
2665 bufs = calloc(nb, sizeof *bufs);
2666 if (!bufs)
2667 return NULL;
2668
2669 i = 0;
2670 p = bufs;
2671 while (i++ < nb) {
2672 *p = calloc(1, sizeof **p);
2673 if (!*p)
2674 goto err;
2675
2676 (*p)->area = malloc(sz);
2677 if (!(*p)->area)
2678 goto err;
2679
2680 (*p)->pos = (*p)->area;
2681 (*p)->end = (*p)->area + sz;
2682 (*p)->data = 0;
2683 LIST_INIT(&(*p)->pkts);
2684 p++;
2685 }
2686
2687 return bufs;
2688
2689 err:
2690 free_quic_conn_tx_bufs(bufs, nb);
2691 return NULL;
2692}
2693
2694/* Release all the memory allocated for <conn> QUIC connection. */
2695static void quic_conn_free(struct quic_conn *conn)
2696{
2697 int i;
2698
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002699 if (!conn)
2700 return;
2701
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002702 free_quic_conn_cids(conn);
2703 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2704 quic_conn_enc_level_uninit(&conn->els[i]);
2705 free_quic_conn_tx_bufs(conn->tx.bufs, conn->tx.nb_buf);
2706 if (conn->timer_task)
2707 task_destroy(conn->timer_task);
2708 pool_free(pool_head_quic_conn, conn);
2709}
2710
2711/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002712static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002713{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002714 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002715 struct quic_conn *qc;
2716 struct quic_pktns *pktns;
2717
2718
2719 conn_ctx = task->context;
2720 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002721 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2722 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002723 task->expire = TICK_ETERNITY;
2724 pktns = quic_loss_pktns(qc);
2725 if (tick_isset(pktns->tx.loss_time)) {
2726 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2727
2728 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2729 if (!LIST_ISEMPTY(&lost_pkts))
2730 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2731 qc_set_timer(conn_ctx);
2732 goto out;
2733 }
2734
2735 if (qc->path->in_flight) {
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002736 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002737 pktns->tx.pto_probe = 1;
2738 }
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002739 else if (objt_server(qc->conn->target) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002740 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2741 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2742
2743 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2744 hel->pktns->tx.pto_probe = 1;
2745 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2746 iel->pktns->tx.pto_probe = 1;
2747 }
2748 qc->tx.nb_pto_dgrams = QUIC_MAX_NB_PTO_DGRAMS;
2749 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2750 qc->path->loss.pto_count++;
2751
2752 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002753 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002754
2755 return task;
2756}
2757
2758/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2759 * connections used to identify the first Initial packets of client connecting
2760 * to listeners. This parameter must be NULL for QUIC connections attached
2761 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2762 * <scid> is the source connection ID with <scid_len> as length.
2763 * Returns 1 if succeeded, 0 if not.
2764 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002765static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2766 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002767 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002768{
2769 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002770 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002771 /* Initial CID. */
2772 struct quic_connection_id *icid;
2773
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002774 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002775 qc = pool_zalloc(pool_head_quic_conn);
2776 if (!qc) {
2777 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2778 goto err;
2779 }
2780
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002781 qc->cids = EB_ROOT;
2782 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002783 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002784 struct listener *l = owner;
2785
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002786 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002787 /* Copy the initial DCID. */
2788 qc->odcid.len = dcid_len;
2789 if (qc->odcid.len)
2790 memcpy(qc->odcid.data, dcid, dcid_len);
2791
2792 /* Copy the SCID as our DCID for this connection. */
2793 if (scid_len)
2794 memcpy(qc->dcid.data, scid, scid_len);
2795 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002796 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002797 }
2798 /* QUIC Client (outgoing connection to servers) */
2799 else {
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002800 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002801 if (dcid_len)
2802 memcpy(qc->dcid.data, dcid, dcid_len);
2803 qc->dcid.len = dcid_len;
2804 }
2805
2806 /* Initialize the output buffer */
2807 qc->obuf.pos = qc->obuf.data;
2808
2809 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002810 if (!icid) {
2811 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
2812 goto err;
2813 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002814
2815 /* Select our SCID which is the first CID with 0 as sequence number. */
2816 qc->scid = icid->cid;
2817
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002818 /* Packet number spaces initialization. */
2819 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2820 quic_pktns_init(&qc->pktns[i]);
2821 /* QUIC encryption level context initialization. */
2822 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002823 if (!quic_conn_enc_level_init(qc, i)) {
2824 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002825 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002826 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002827 /* Initialize the packet number space. */
2828 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2829 }
2830
2831 /* TX part. */
2832 LIST_INIT(&qc->tx.frms_to_send);
2833 qc->tx.bufs = quic_conn_tx_bufs_alloc(QUIC_CONN_TX_BUFS_NB, QUIC_CONN_TX_BUF_SZ);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002834 if (!qc->tx.bufs) {
2835 TRACE_PROTO("Could not allocate TX bufs", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002836 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002837 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002838
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002839 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002840 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2841 qc->tx.wbuf = qc->tx.rbuf = 0;
2842 qc->tx.bytes = 0;
2843 qc->tx.nb_pto_dgrams = 0;
2844 /* RX part. */
2845 qc->rx.bytes = 0;
2846
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002847 /* XXX TO DO: Only one path at this time. */
2848 qc->path = &qc->paths[0];
2849 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2850
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002851 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002852
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002853 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002854
2855 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002856 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002857 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002858 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002859}
2860
2861/* Initialize the timer task of <qc> QUIC connection.
2862 * Returns 1 if succeeded, 0 if not.
2863 */
2864static int quic_conn_init_timer(struct quic_conn *qc)
2865{
2866 qc->timer_task = task_new(MAX_THREADS_MASK);
2867 if (!qc->timer_task)
2868 return 0;
2869
2870 qc->timer = TICK_ETERNITY;
2871 qc->timer_task->process = process_timer;
2872 qc->timer_task->context = qc->conn->xprt_ctx;
2873
2874 return 1;
2875}
2876
2877/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
2878 * past one byte of this buffer.
2879 */
2880static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
2881 struct quic_rx_packet *pkt)
2882{
2883 unsigned char dcid_len, scid_len;
2884
2885 /* Version */
2886 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
2887 return 0;
2888
2889 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
2890
2891 /* Destination Connection ID Length */
2892 dcid_len = *(*buf)++;
2893 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
2894 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
2895 /* XXX MUST BE DROPPED */
2896 return 0;
2897
2898 if (dcid_len) {
2899 /* Check that the length of this received DCID matches the CID lengths
2900 * of our implementation for non Initials packets only.
2901 */
2902 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
2903 return 0;
2904
2905 memcpy(pkt->dcid.data, *buf, dcid_len);
2906 }
2907
2908 pkt->dcid.len = dcid_len;
2909 *buf += dcid_len;
2910
2911 /* Source Connection ID Length */
2912 scid_len = *(*buf)++;
2913 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
2914 /* XXX MUST BE DROPPED */
2915 return 0;
2916
2917 if (scid_len)
2918 memcpy(pkt->scid.data, *buf, scid_len);
2919 pkt->scid.len = scid_len;
2920 *buf += scid_len;
2921
2922 return 1;
2923}
2924
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002925/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
2926 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
2927 * encryption level matching with the packet type. <*qel> may be null if not found.
2928 * Note that <ctx> may be null (for Initial packets).
2929 */
2930static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002931 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002932 struct quic_enc_level **qel)
2933{
2934 enum quic_tls_enc_level tel;
2935
2936 /* Special case without connection context (firt Initial packets) */
2937 if (!ctx) {
2938 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2939 return 1;
2940 }
2941
2942 tel = quic_packet_type_enc_level(pkt->type);
2943 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
2944 *qel = NULL;
2945 return 0;
2946 }
2947
2948 *qel = &qc->els[tel];
2949 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
2950 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
2951 return 0;
2952 }
2953
2954 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002955 (tel != QUIC_TLS_ENC_LEVEL_APP || ctx->conn->qc->state >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002956 return 1;
2957
2958 return 0;
2959}
2960
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002961/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
2962 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
2963 * byte past the end of the buffer containing this packet and <beg> the address of
2964 * the packet first byte.
2965 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
2966 * Returns 1 if succeeded, 0 if not.
2967 */
2968static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
2969 unsigned char **buf, unsigned char *beg,
2970 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002971 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002972{
2973 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002974 struct quic_enc_level *qel;
2975 /* Only for traces. */
2976 struct quic_rx_packet *qpkt_trace;
2977
2978 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002979 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002980 /* The packet number is here. This is also the start minus
2981 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
2982 * protection.
2983 */
2984 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002985 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002986 /* Note that the following function enables us to unprotect the packet
2987 * number and its length subsequently used to decrypt the entire
2988 * packets.
2989 */
2990 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
2991 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002992 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002993 goto err;
2994 }
2995
2996 /* The AAD includes the packet number field found at <pn>. */
2997 pkt->aad_len = pn - beg + pkt->pnl;
2998 qpkt_trace = pkt;
2999 /* Store the packet */
3000 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02003001 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003002 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02003003 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003004 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003005 else if (qel) {
3006 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003007 pkt->pn_offset = pn - beg;
3008 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3009 }
3010
3011 memcpy(pkt->data, beg, pkt->len);
3012 /* Updtate the offset of <*buf> for the next QUIC packet. */
3013 *buf = beg + pkt->len;
3014
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003015 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003016 return 1;
3017
3018 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003019 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 +01003020 return 0;
3021}
3022
3023/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3024 * Also set <*long_header> to 1 if this form is long, 0 if not.
3025 */
3026static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3027 unsigned char byte0, int *long_header)
3028{
3029 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3030 pkt->type =
3031 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3032 *long_header = 1;
3033 }
3034 else {
3035 pkt->type = QUIC_PACKET_TYPE_SHORT;
3036 *long_header = 0;
3037 }
3038}
3039
3040static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3041 struct quic_rx_packet *pkt,
3042 struct quic_dgram_ctx *dgram_ctx,
3043 struct sockaddr_storage *saddr)
3044{
3045 unsigned char *beg;
3046 uint64_t len;
3047 struct quic_conn *qc;
3048 struct eb_root *cids;
3049 struct ebmb_node *node;
3050 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003051 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003052 int long_header;
3053
3054 qc = NULL;
3055 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3056 if (end <= *buf)
3057 goto err;
3058
3059 /* Fixed bit */
3060 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3061 /* XXX TO BE DISCARDED */
3062 goto err;
3063
3064 srv_conn = dgram_ctx->owner;
3065 beg = *buf;
3066 /* Header form */
3067 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3068 if (long_header) {
3069 size_t cid_lookup_len;
3070
3071 if (!quic_packet_read_long_header(buf, end, pkt))
3072 goto err;
3073
3074 /* For Initial packets, and for servers (QUIC clients connections),
3075 * there is no Initial connection IDs storage.
3076 */
3077 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3078 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3079 cid_lookup_len = pkt->dcid.len;
3080 }
3081 else {
3082 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3083 cid_lookup_len = QUIC_CID_LEN;
3084 }
3085
3086 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3087 if (!node)
3088 goto err;
3089
3090 qc = ebmb_entry(node, struct quic_conn, scid_node);
3091
3092 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3093 qc->dcid.len = pkt->scid.len;
3094 if (pkt->scid.len)
3095 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3096 }
3097
3098 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3099 uint64_t token_len;
3100
3101 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3102 goto err;
3103
3104 /* XXX TO DO XXX 0 value means "the token is not present".
3105 * A server which sends an Initial packet must not set the token.
3106 * So, a client which receives an Initial packet with a token
3107 * MUST discard the packet or generate a connection error with
3108 * PROTOCOL_VIOLATION as type.
3109 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3110 */
3111 pkt->token_len = token_len;
3112 }
3113 }
3114 else {
3115 /* XXX TO DO: Short header XXX */
3116 if (end - *buf < QUIC_CID_LEN)
3117 goto err;
3118
3119 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3120 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3121 if (!node)
3122 goto err;
3123
3124 qc = ebmb_entry(node, struct quic_conn, scid_node);
3125 *buf += QUIC_CID_LEN;
3126 }
3127 /* Store the DCID used for this packet to check the packet which
3128 * come in this UDP datagram match with it.
3129 */
3130 if (!dgram_ctx->dcid_node)
3131 dgram_ctx->dcid_node = node;
3132 /* Only packets packets with long headers and not RETRY or VERSION as type
3133 * have a length field.
3134 */
3135 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3136 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3137 goto err;
3138
3139 pkt->len = len;
3140 }
3141 else if (!long_header) {
3142 /* A short packet is the last one of an UDP datagram. */
3143 pkt->len = end - *buf;
3144 }
3145
3146 conn_ctx = qc->conn->xprt_ctx;
3147
3148 /* Increase the total length of this packet by the header length. */
3149 pkt->len += *buf - beg;
3150 /* Do not check the DCID node before the length. */
3151 if (dgram_ctx->dcid_node != node) {
3152 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3153 goto err;
3154 }
3155
3156 if (pkt->len > sizeof pkt->data) {
3157 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3158 goto err;
3159 }
3160
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003161 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003162 goto err;
3163
3164 /* Wake the tasklet of the QUIC connection packet handler. */
3165 if (conn_ctx)
3166 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3167
3168 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3169
3170 return pkt->len;
3171
3172 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003173 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003174 return -1;
3175}
3176
3177static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3178 struct quic_rx_packet *pkt,
3179 struct quic_dgram_ctx *dgram_ctx,
3180 struct sockaddr_storage *saddr)
3181{
3182 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003183 struct quic_conn *qc;
3184 struct eb_root *cids;
3185 struct ebmb_node *node;
3186 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003187 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003188 int long_header = 0;
3189
3190 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003191 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003192 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003193 if (end <= *buf)
3194 goto err;
3195
3196 /* Fixed bit */
3197 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3198 /* XXX TO BE DISCARDED */
3199 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3200 goto err;
3201 }
3202
3203 l = dgram_ctx->owner;
3204 beg = *buf;
3205 /* Header form */
3206 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3207 if (long_header) {
3208 unsigned char dcid_len;
3209
3210 if (!quic_packet_read_long_header(buf, end, pkt)) {
3211 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3212 goto err;
3213 }
3214
3215 dcid_len = pkt->dcid.len;
3216 /* For Initial packets, and for servers (QUIC clients connections),
3217 * there is no Initial connection IDs storage.
3218 */
3219 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003220 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003221 /* DCIDs of first packets coming from clients may have the same values.
3222 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3223 */
3224 quic_cid_saddr_cat(&pkt->dcid, saddr);
3225 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003226
3227 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3228 end - *buf < token_len) {
3229 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3230 goto err;
3231 }
3232
3233 /* XXX TO DO XXX 0 value means "the token is not present".
3234 * A server which sends an Initial packet must not set the token.
3235 * So, a client which receives an Initial packet with a token
3236 * MUST discard the packet or generate a connection error with
3237 * PROTOCOL_VIOLATION as type.
3238 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3239 */
3240 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003241 }
3242 else {
3243 if (pkt->dcid.len != QUIC_CID_LEN) {
3244 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3245 goto err;
3246 }
3247
3248 cids = &l->rx.cids;
3249 }
3250
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003251 /* Only packets packets with long headers and not RETRY or VERSION as type
3252 * have a length field.
3253 */
3254 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3255 uint64_t len;
3256
3257 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3258 end - *buf < len) {
3259 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3260 goto err;
3261 }
3262
3263 pkt->len = len;
3264 }
3265
3266
3267 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003268 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3269 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3270 cids == &l->rx.odcids) {
3271 /* Switch to the definitive tree ->cids containing the final CIDs. */
3272 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3273 if (node) {
3274 /* If found, signal this with NULL as special value for <cids>. */
3275 pkt->dcid.len = dcid_len;
3276 cids = NULL;
3277 }
3278 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003279 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003280
3281 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003282 int ipv4;
3283 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003284 struct ebmb_node *n = NULL;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003285
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003286 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3287 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3288 goto err;
3289 }
3290
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003291 pkt->saddr = *saddr;
3292 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3293 * of <saddr>.
3294 */
3295 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003296 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003297 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003298 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003299 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003300 goto err;
3301
3302 odcid = &qc->rx.params.original_destination_connection_id;
3303 /* Copy the transport parameters. */
3304 qc->rx.params = l->bind_conf->quic_params;
3305 /* Copy original_destination_connection_id transport parameter. */
3306 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3307 odcid->len = pkt->odcid_len;
3308 /* Copy the initial source connection ID. */
3309 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3310 qc->enc_params_len =
3311 quic_transport_params_encode(qc->enc_params,
3312 qc->enc_params + sizeof qc->enc_params,
3313 &qc->rx.params, 1);
3314 if (!qc->enc_params_len)
3315 goto err;
3316
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003317 /* NOTE: the socket address has been concatenated to the destination ID
3318 * chosen by the client for Initial packets.
3319 */
3320 if (!qc_new_isecs(qc, pkt->dcid.data, pkt->odcid_len, 1)) {
3321 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3322 goto err;
3323 }
3324
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003325 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003326 /* This is the DCID node sent in this packet by the client. */
3327 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003328 /* Enqueue this packet. */
3329 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3330 /* Try to accept a new connection. */
3331 listener_accept(l);
3332
3333 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3334 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3335 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3336 /* Insert our SCID, the connection ID for the QUIC client. */
3337 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3338 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3339 if (n != &qc->scid_node) {
3340 quic_conn_free(qc);
3341 qc = ebmb_entry(n, struct quic_conn, scid_node);
3342 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003343 }
3344 else {
3345 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3346 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3347 else
3348 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003349 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003350 }
3351 }
3352 else {
3353 if (end - *buf < QUIC_CID_LEN) {
3354 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3355 goto err;
3356 }
3357
3358 cids = &l->rx.cids;
3359 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3360 if (!node) {
3361 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3362 goto err;
3363 }
3364
3365 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003366 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003367 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003368 /* A short packet is the last one of an UDP datagram. */
3369 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003370 }
3371
3372 /* Store the DCID used for this packet to check the packet which
3373 * come in this UDP datagram match with it.
3374 */
3375 if (!dgram_ctx->dcid_node) {
3376 dgram_ctx->dcid_node = node;
3377 dgram_ctx->qc = qc;
3378 }
3379
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003380 /* Increase the total length of this packet by the header length. */
3381 pkt->len += *buf - beg;
3382 /* Do not check the DCID node before the length. */
3383 if (dgram_ctx->dcid_node != node) {
3384 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3385 goto err;
3386 }
3387
3388 if (pkt->len > sizeof pkt->data) {
3389 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3390 goto err;
3391 }
3392
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003393 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3394 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3395 goto err;
3396 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003397
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003398
3399 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003400 if (conn_ctx)
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003401 /* Wake the tasklet of the QUIC connection packet handler. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003402 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003403
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003404 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3405
3406 return pkt->len;
3407
3408 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003409 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003410 qc ? qc->conn : NULL, pkt);
3411 return -1;
3412}
3413
3414/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3415 * in advance. This is the reponsability of the caller to check there is enough room in this
3416 * buffer to build a long header.
3417 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3418 */
3419static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3420 int type, size_t pn_len, struct quic_conn *conn)
3421{
3422 if (type > QUIC_PACKET_TYPE_RETRY)
3423 return 0;
3424
3425 /* #0 byte flags */
3426 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3427 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3428 /* Version */
3429 quic_write_uint32(buf, end, conn->version);
3430 *(*buf)++ = conn->dcid.len;
3431 /* Destination connection ID */
3432 if (conn->dcid.len) {
3433 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3434 *buf += conn->dcid.len;
3435 }
3436 /* Source connection ID */
3437 *(*buf)++ = conn->scid.len;
3438 if (conn->scid.len) {
3439 memcpy(*buf, conn->scid.data, conn->scid.len);
3440 *buf += conn->scid.len;
3441 }
3442
3443 return 1;
3444}
3445
3446/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3447 * in advance. This is the reponsability of the caller to check there is enough room in this
3448 * buffer to build a long header.
3449 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3450 */
3451static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3452 size_t pn_len, struct quic_conn *conn)
3453{
3454 /* #0 byte flags */
3455 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3456 /* Destination connection ID */
3457 if (conn->dcid.len) {
3458 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3459 *buf += conn->dcid.len;
3460 }
3461
3462 return 1;
3463}
3464
3465/* Apply QUIC header protection to the packet with <buf> as first byte address,
3466 * <pn> as address of the Packet number field, <pnlen> being this field length
3467 * with <aead> as AEAD cipher and <key> as secret key.
3468 * Returns 1 if succeeded or 0 if failed.
3469 */
3470static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3471 const EVP_CIPHER *aead, const unsigned char *key)
3472{
3473 int i, ret, outlen;
3474 EVP_CIPHER_CTX *ctx;
3475 /* We need an IV of at least 5 bytes: one byte for bytes #0
3476 * and at most 4 bytes for the packet number
3477 */
3478 unsigned char mask[5] = {0};
3479
3480 ret = 0;
3481 ctx = EVP_CIPHER_CTX_new();
3482 if (!ctx)
3483 return 0;
3484
3485 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3486 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3487 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3488 goto out;
3489
3490 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3491 for (i = 0; i < pnlen; i++)
3492 pn[i] ^= mask[i + 1];
3493
3494 ret = 1;
3495
3496 out:
3497 EVP_CIPHER_CTX_free(ctx);
3498
3499 return ret;
3500}
3501
3502/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3503 * ACK ranges if needed to a value below <limit> in bytes.
3504 * Return 1 if succeeded, 0 if not.
3505 */
3506static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3507{
3508 size_t room, ack_delay_sz;
3509
3510 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3511 /* A frame is made of 1 byte for the frame type. */
3512 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003513 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003514 return 0;
3515
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003516 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003517}
3518
3519/* Prepare as most as possible CRYPTO frames from prebuilt CRYPTO frames for <qel>
3520 * encryption level to be encoded in a buffer with <room> as available room,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003521 * and <*len> the packet Length field initialized with the number of bytes already present
3522 * in this buffer which must be taken into an account for the Length packet field value.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003523 * <headlen> is the number of bytes already present in this packet before building
Frédéric Lécailleea604992020-12-24 13:01:37 +01003524 * CRYPTO frames.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003525 * This is the responsibility of the caller to check that <*len> < <room> as this is
3526 * the responsibility to check that <headlen> < quic_path_prep_data(conn->path).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003527 * Update consequently <*len> to reflect the size of these CRYPTO frames built
3528 * by this function. Also attach these CRYPTO frames to <pkt> QUIC packet.
3529 * Return 1 if succeeded, 0 if not.
3530 */
3531static inline int qc_build_cfrms(struct quic_tx_packet *pkt,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003532 size_t room, size_t *len, size_t headlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003533 struct quic_enc_level *qel,
3534 struct quic_conn *conn)
3535{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003536 int ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003537 struct quic_tx_frm *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003538
Frédéric Lécailleea604992020-12-24 13:01:37 +01003539 ret = 0;
3540 /* If we are not probing we must take into an account the congestion
3541 * control window.
3542 */
3543 if (!conn->tx.nb_pto_dgrams)
3544 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
3545 TRACE_PROTO("************** CRYPTO frames build (headlen)",
3546 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003547 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
3548 /* header length, data length, frame length. */
3549 size_t hlen, dlen, cflen;
3550
Frédéric Lécailleea604992020-12-24 13:01:37 +01003551 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3552 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3553 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003554 break;
3555
3556 /* Compute the length of this CRYPTO frame header */
3557 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3558 /* Compute the data length of this CRyPTO frame. */
3559 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécailleea604992020-12-24 13:01:37 +01003560 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3561 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003562 if (!dlen)
3563 break;
3564
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003565 pkt->cdata_len += dlen;
3566 /* CRYPTO frame length. */
3567 cflen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003568 TRACE_PROTO(" CRYPTO frame length (cflen)",
3569 QUIC_EV_CONN_BCFRMS, conn->conn, &cflen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003570 /* Add the CRYPTO data length and its encoded length to the packet
3571 * length and the length of this length.
3572 */
3573 *len += cflen;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003574 room -= cflen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003575 if (dlen == cf->crypto.len) {
3576 /* <cf> CRYPTO data have been consumed. */
Willy Tarreau2b718102021-04-21 07:32:39 +02003577 LIST_DELETE(&cf->list);
3578 LIST_APPEND(&pkt->frms, &cf->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003579 }
3580 else {
3581 struct quic_tx_frm *new_cf;
3582
3583 new_cf = pool_alloc(pool_head_quic_tx_frm);
3584 if (!new_cf) {
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003585 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003586 return 0;
3587 }
3588
3589 new_cf->type = QUIC_FT_CRYPTO;
3590 new_cf->crypto.len = dlen;
3591 new_cf->crypto.offset = cf->crypto.offset;
Willy Tarreau2b718102021-04-21 07:32:39 +02003592 LIST_APPEND(&pkt->frms, &new_cf->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003593 /* Consume <dlen> bytes of the current frame. */
3594 cf->crypto.len -= dlen;
3595 cf->crypto.offset += dlen;
3596 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003597 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003598 }
3599
Frédéric Lécailleea604992020-12-24 13:01:37 +01003600 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003601}
3602
3603/* This function builds a clear handshake packet used during a QUIC TLS handshakes
3604 * into <wbuf> the current <wbuf> for <conn> QUIC connection with <qel> as QUIC
3605 * TLS encryption level for outgoing packets filling it with as much as CRYPTO
3606 * data as possible from <offset> offset in the CRYPTO data stream. Note that
3607 * this offset value is updated by the length of the CRYPTO frame used to embed
3608 * the CRYPTO data if this packet and only if the packet is successfully built.
3609 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
3610 * reserved so that to be sure there is enough room to build this AEAD TAG after
3611 * having successfully returned from this function and to be sure the position
3612 * pointer of <wbuf> may be safely incremented by QUIC_TLS_TAG_LEN. After having
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003613 * returned from this function, <wbuf> position will point one past the last
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003614 * byte of the payload with the confidence there is at least QUIC_TLS_TAG_LEN bytes
3615 * available packet to encrypt this packet.
3616 * This function also update the value of <buf_pn> pointer to point to the packet
3617 * number field in this packet. <pn_len> will also have the packet number
3618 * length as value.
3619 *
3620 * Return the length of the packet if succeeded minus QUIC_TLS_TAG_LEN, or -1 if
3621 * failed (not enough room in <wbuf> to build this packet plus QUIC_TLS_TAG_LEN
3622 * bytes), -2 if there are too much CRYPTO data in flight to build a packet.
3623 */
3624static ssize_t qc_do_build_hdshk_pkt(struct q_buf *wbuf,
3625 struct quic_tx_packet *pkt, int pkt_type,
3626 int64_t pn, size_t *pn_len,
3627 unsigned char **buf_pn,
3628 struct quic_enc_level *qel,
3629 struct quic_conn *conn)
3630{
3631 unsigned char *beg, *pos;
3632 const unsigned char *end;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003633 size_t len, len_frms, token_fields_len, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003634 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3635 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3636 struct quic_crypto *crypto = &frm.crypto;
3637 size_t ack_frm_len;
3638 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003639 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003640
Frédéric Lécailleea604992020-12-24 13:01:37 +01003641 /* Length field value with CRYPTO frames if present. */
3642 len_frms = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003643 beg = pos = q_buf_getpos(wbuf);
3644 end = q_buf_end(wbuf);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003645 /* When not probing and not acking, reduce the size of this buffer to respect
3646 * the congestion controller window.
3647 */
3648 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3649 size_t path_room;
3650
3651 path_room = quic_path_prep_data(conn->path);
3652 if (end - beg > path_room)
3653 end = beg + path_room;
3654 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003655
3656 /* For a server, the token field of an Initial packet is empty. */
3657 token_fields_len = pkt_type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3658
3659 /* Check there is enough room to build the header followed by a token. */
3660 if (end - pos < QUIC_LONG_PACKET_MINLEN + conn->dcid.len +
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003661 conn->scid.len + token_fields_len + QUIC_TLS_TAG_LEN) {
3662 ssize_t room = end - pos;
3663 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3664 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003665 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003666 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003667
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003668 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3669 /* packet number length */
3670 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3671
3672 quic_build_packet_long_header(&pos, end, pkt_type, *pn_len, conn);
3673
3674 /* Encode the token length (0) for an Initial packet. */
3675 if (pkt_type == QUIC_PACKET_TYPE_INITIAL)
3676 *pos++ = 0;
3677
3678 /* Build an ACK frame if required. */
3679 ack_frm_len = 0;
3680 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003681 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003682 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003683 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003684 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003685 if (!ack_frm_len) {
3686 ssize_t room = end - pos;
3687 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3688 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003689 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003690 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003691
3692 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3693 }
3694
3695 /* Length field value without the CRYPTO frames data length. */
3696 len = ack_frm_len + *pn_len;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003697 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003698 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003699
3700 len_frms = len + QUIC_TLS_TAG_LEN;
3701 if (!qc_build_cfrms(pkt, end - pos, &len_frms, pos - beg, qel, conn)) {
3702 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3703 conn->conn, NULL, NULL, &room);
3704 goto err;
3705 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003706 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003707
3708 add_ping_frm = 0;
3709 padding_len = 0;
3710 if (objt_server(conn->conn->target) &&
3711 pkt_type == QUIC_PACKET_TYPE_INITIAL &&
3712 len < QUIC_INITIAL_PACKET_MINLEN) {
3713 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
3714 }
3715 else if (LIST_ISEMPTY(&pkt->frms)) {
3716 if (qel->pktns->tx.pto_probe) {
3717 /* If we cannot send a CRYPTO frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003718 add_ping_frm = 1;
3719 len += 1;
3720 }
3721 /* If there is no frame at all to follow, add at least a PADDING frame. */
3722 if (!ack_frm_len)
3723 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
3724 }
3725
3726 /* Length (of the remaining data). Must not fail because, the buffer size
3727 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
3728 * for the encryption TAG. It must be taken into an account for the length
3729 * of this packet.
3730 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01003731 if (len_frms)
3732 len = len_frms;
3733 else
3734 len += QUIC_TLS_TAG_LEN;
3735 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003736
3737 /* Packet number field address. */
3738 *buf_pn = pos;
3739
3740 /* Packet number encoding. */
3741 quic_packet_number_encode(&pos, end, pn, *pn_len);
3742
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003743 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3744 ssize_t room = end - pos;
3745 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3746 conn->conn, NULL, NULL, &room);
3747 goto err;
3748 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003749
3750 /* Crypto frame */
3751 if (!LIST_ISEMPTY(&pkt->frms)) {
3752 struct quic_tx_frm *cf;
3753
3754 list_for_each_entry(cf, &pkt->frms, list) {
3755 crypto->offset = cf->crypto.offset;
3756 crypto->len = cf->crypto.len;
3757 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003758 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3759 ssize_t room = end - pos;
3760 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3761 conn->conn, NULL, NULL, &room);
3762 goto err;
3763 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003764 }
3765 }
3766
3767 /* Build a PING frame if needed. */
3768 if (add_ping_frm) {
3769 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003770 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3771 ssize_t room = end - pos;
3772 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3773 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003774 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003775 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003776 }
3777
3778 /* Build a PADDING frame if needed. */
3779 if (padding_len) {
3780 frm.type = QUIC_FT_PADDING;
3781 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003782 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3783 ssize_t room = end - pos;
3784 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3785 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003786 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003787 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003788 }
3789
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003790 /* Always reset this variable as this function has no idea
3791 * if it was set. It is handle by the loss detection timer.
3792 */
3793 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003794
3795 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003796 return pos - beg;
3797
3798 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003799 return -1;
3800}
3801
3802static inline void quic_tx_packet_init(struct quic_tx_packet *pkt)
3803{
3804 pkt->cdata_len = 0;
3805 pkt->in_flight_len = 0;
3806 LIST_INIT(&pkt->frms);
3807}
3808
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003809/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003810static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
3811{
3812 struct quic_tx_frm *frm, *frmbak;
3813
3814 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003815 LIST_DELETE(&frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003816 pool_free(pool_head_quic_tx_frm, frm);
3817 }
3818 pool_free(pool_head_quic_tx_packet, pkt);
3819}
3820
3821/* Build a handshake packet into <buf> packet buffer with <pkt_type> as packet
3822 * type for <qc> QUIC connection from CRYPTO data stream at <*offset> offset to
3823 * be encrypted at <qel> encryption level.
3824 * Return -2 if the packet could not be encrypted for any reason, -1 if there was
3825 * not enough room in <buf> to build the packet, or the size of the built packet
3826 * if succeeded (may be zero if there is too much crypto data in flight to build the packet).
3827 */
3828static ssize_t qc_build_hdshk_pkt(struct q_buf *buf, struct quic_conn *qc, int pkt_type,
3829 struct quic_enc_level *qel)
3830{
3831 /* The pointer to the packet number field. */
3832 unsigned char *buf_pn;
3833 unsigned char *beg, *end, *payload;
3834 int64_t pn;
3835 size_t pn_len, payload_len, aad_len;
3836 ssize_t pkt_len;
3837 struct quic_tls_ctx *tls_ctx;
3838 struct quic_tx_packet *pkt;
3839
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003840 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003841 pkt = pool_alloc(pool_head_quic_tx_packet);
3842 if (!pkt) {
3843 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc->conn);
3844 return -2;
3845 }
3846
3847 quic_tx_packet_init(pkt);
3848 beg = q_buf_getpos(buf);
3849 pn_len = 0;
3850 buf_pn = NULL;
3851 pn = qel->pktns->tx.next_pn + 1;
3852 pkt_len = qc_do_build_hdshk_pkt(buf, pkt, pkt_type, pn, &pn_len, &buf_pn, qel, qc);
3853 if (pkt_len <= 0) {
3854 free_quic_tx_packet(pkt);
3855 return pkt_len;
3856 }
3857
3858 end = beg + pkt_len;
3859 payload = buf_pn + pn_len;
3860 payload_len = end - payload;
3861 aad_len = payload - beg;
3862
3863 tls_ctx = &qel->tls_ctx;
3864 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn))
3865 goto err;
3866
3867 end += QUIC_TLS_TAG_LEN;
3868 pkt_len += QUIC_TLS_TAG_LEN;
3869 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
3870 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
3871 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
3872 goto err;
3873 }
3874
3875 /* Now that a correct packet is built, let us set the position pointer of
3876 * <buf> buffer for the next packet.
3877 */
3878 q_buf_setpos(buf, end);
3879 /* Consume a packet number. */
3880 ++qel->pktns->tx.next_pn;
3881 /* Attach the built packet to its tree. */
3882 pkt->pn_node.key = qel->pktns->tx.next_pn;
3883 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003884 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003885 pkt->in_flight_len = pkt_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003886 qc->path->prep_in_flight += pkt_len;
3887 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003888 pkt->pktns = qel->pktns;
3889 eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node);
3890 /* Increment the number of bytes in <buf> buffer by the length of this packet. */
3891 buf->data += pkt_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003892 /* Attach this packet to <buf>. */
Willy Tarreau2b718102021-04-21 07:32:39 +02003893 LIST_APPEND(&buf->pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003894 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
3895
3896 return pkt_len;
3897
3898 err:
3899 free_quic_tx_packet(pkt);
3900 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
3901 return -2;
3902}
3903
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003904/* Prepare a clear post handhskake packet for <conn> QUIC connection.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003905 * Return the length of this packet if succeeded, -1 <wbuf> was full.
3906 */
3907static ssize_t qc_do_build_phdshk_apkt(struct q_buf *wbuf,
3908 struct quic_tx_packet *pkt,
3909 int64_t pn, size_t *pn_len,
3910 unsigned char **buf_pn, struct quic_enc_level *qel,
3911 struct quic_conn *conn)
3912{
3913 const unsigned char *beg, *end;
3914 unsigned char *pos;
3915 struct quic_frame *frm, *sfrm;
3916 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3917 size_t fake_len, ack_frm_len;
3918 int64_t largest_acked_pn;
3919
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003920 TRACE_ENTER(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003921 beg = pos = q_buf_getpos(wbuf);
3922 end = q_buf_end(wbuf);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003923 /* When not probing and not acking, reduce the size of this buffer to respect
3924 * the congestion controller window.
3925 */
3926 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3927 size_t path_room;
3928
3929 path_room = quic_path_prep_data(conn->path);
3930 if (end - beg > path_room)
3931 end = beg + path_room;
3932 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003933 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3934 /* Packet number length */
3935 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3936 /* Check there is enough room to build this packet (without payload). */
3937 if (end - pos < QUIC_SHORT_PACKET_MINLEN + sizeof_quic_cid(&conn->dcid) +
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003938 *pn_len + QUIC_TLS_TAG_LEN) {
3939 ssize_t room = end - pos;
3940 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
3941 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003942 goto err;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003943 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003944
3945 /* Reserve enough room at the end of the packet for the AEAD TAG. */
3946 end -= QUIC_TLS_TAG_LEN;
3947 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3948 /* Packet number field. */
3949 *buf_pn = pos;
3950 /* Packet number encoding. */
3951 quic_packet_number_encode(&pos, end, pn, *pn_len);
3952
3953 /* Build an ACK frame if required. */
3954 ack_frm_len = 0;
3955 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003956 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003957 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003958 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003959 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
3960 if (!ack_frm_len)
3961 goto err;
3962
3963 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3964 }
3965
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003966 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3967 ssize_t room = end - pos;
3968 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
3969 conn->conn, NULL, NULL, &room);
3970 goto err;
3971 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003972
3973 fake_len = ack_frm_len;
3974 if (!LIST_ISEMPTY(&qel->pktns->tx.frms) &&
Frédéric Lécailleea604992020-12-24 13:01:37 +01003975 !qc_build_cfrms(pkt, end - pos, &fake_len, pos - beg, qel, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003976 ssize_t room = end - pos;
3977 TRACE_PROTO("some CRYPTO frames could not be built",
3978 QUIC_EV_CONN_PAPKT, conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003979 goto err;
3980 }
3981
3982 /* Crypto frame */
3983 if (!LIST_ISEMPTY(&pkt->frms)) {
3984 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3985 struct quic_crypto *crypto = &frm.crypto;
3986 struct quic_tx_frm *cf;
3987
3988 list_for_each_entry(cf, &pkt->frms, list) {
3989 crypto->offset = cf->crypto.offset;
3990 crypto->len = cf->crypto.len;
3991 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003992 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3993 ssize_t room = end - pos;
3994 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
3995 conn->conn, NULL, NULL, &room);
3996 goto err;
3997 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003998 }
3999 }
4000
4001 /* Encode a maximum of frames. */
4002 list_for_each_entry_safe(frm, sfrm, &conn->tx.frms_to_send, list) {
4003 unsigned char *ppos;
4004
4005 ppos = pos;
4006 if (!qc_build_frm(&ppos, end, frm, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004007 TRACE_DEVEL("Frames not built", QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004008 break;
4009 }
4010
Willy Tarreau2b718102021-04-21 07:32:39 +02004011 LIST_DELETE(&frm->list);
4012 LIST_APPEND(&pkt->frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004013 pos = ppos;
4014 }
4015
4016 out:
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004017 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004018 return pos - beg;
4019
4020 err:
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004021 TRACE_DEVEL("leaving in error (buffer full)", QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004022 return -1;
4023}
4024
4025/* Prepare a post handhskake packet at Application encryption level for <conn>
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004026 * QUIC connection.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004027 * Return the length of this packet if succeeded, -1 if <wbuf> was full,
4028 * -2 in case of major error (encryption failure).
4029 */
4030static ssize_t qc_build_phdshk_apkt(struct q_buf *wbuf, struct quic_conn *qc)
4031{
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004032 /* A pointer to the packet number field in <buf> */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004033 unsigned char *buf_pn;
4034 unsigned char *beg, *end, *payload;
4035 int64_t pn;
4036 size_t pn_len, aad_len, payload_len;
4037 ssize_t pkt_len;
4038 struct quic_tls_ctx *tls_ctx;
4039 struct quic_enc_level *qel;
4040 struct quic_tx_packet *pkt;
4041
4042 TRACE_ENTER(QUIC_EV_CONN_PAPKT, qc->conn);
4043 pkt = pool_alloc(pool_head_quic_tx_packet);
4044 if (!pkt) {
4045 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_PAPKT, qc->conn);
4046 return -2;
4047 }
4048
4049 quic_tx_packet_init(pkt);
4050 beg = q_buf_getpos(wbuf);
4051 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4052 pn_len = 0;
4053 buf_pn = NULL;
4054 pn = qel->pktns->tx.next_pn + 1;
4055 pkt_len = qc_do_build_phdshk_apkt(wbuf, pkt, pn, &pn_len, &buf_pn, qel, qc);
4056 if (pkt_len <= 0) {
4057 free_quic_tx_packet(pkt);
4058 return pkt_len;
4059 }
4060
4061 end = beg + pkt_len;
4062 payload = buf_pn + pn_len;
4063 payload_len = end - payload;
4064 aad_len = payload - beg;
4065
4066 tls_ctx = &qel->tls_ctx;
4067 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn))
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004068 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004069
4070 end += QUIC_TLS_TAG_LEN;
4071 pkt_len += QUIC_TLS_TAG_LEN;
4072 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4073 tls_ctx->tx.hp, tls_ctx->tx.hp_key))
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004074 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004075
4076 q_buf_setpos(wbuf, end);
4077 /* Consume a packet number. */
4078 ++qel->pktns->tx.next_pn;
4079 /* Attach the built packet to its tree. */
4080 pkt->pn_node.key = qel->pktns->tx.next_pn;
4081 eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node);
4082 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004083 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004084 pkt->in_flight_len = pkt_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004085 qc->path->prep_in_flight += pkt_len;
4086 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004087 pkt->pktns = qel->pktns;
4088 /* Increment the number of bytes in <buf> buffer by the length of this packet. */
4089 wbuf->data += pkt_len;
4090 /* Attach this packet to <buf>. */
Willy Tarreau2b718102021-04-21 07:32:39 +02004091 LIST_APPEND(&wbuf->pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004092
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004093 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, qc->conn, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004094
4095 return pkt_len;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004096
4097 err:
4098 free_quic_tx_packet(pkt);
4099 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PAPKT, qc->conn);
4100 return -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004101}
4102
4103/* Prepare a maximum of QUIC Application level packets from <ctx> QUIC
4104 * connection I/O handler context.
4105 * Returns 1 if succeeded, 0 if not.
4106 */
4107int qc_prep_phdshk_pkts(struct quic_conn *qc)
4108{
4109 struct q_buf *wbuf;
4110 struct quic_enc_level *qel;
4111
4112 TRACE_ENTER(QUIC_EV_CONN_PAPKTS, qc->conn);
4113 wbuf = q_wbuf(qc);
4114 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4115 while (q_buf_empty(wbuf)) {
4116 ssize_t ret;
4117
4118 if (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
4119 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004120 qc->path->prep_in_flight >= qc->path->cwnd)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004121 TRACE_DEVEL("nothing more to do",
4122 QUIC_EV_CONN_PAPKTS, qc->conn);
4123 break;
4124 }
4125
4126 ret = qc_build_phdshk_apkt(wbuf, qc);
4127 switch (ret) {
4128 case -1:
4129 /* Not enough room left in <wbuf>. */
4130 wbuf = q_next_wbuf(qc);
4131 continue;
4132 case -2:
4133 return 0;
4134 default:
4135 /* XXX TO CHECK: consume a buffer. */
4136 wbuf = q_next_wbuf(qc);
4137 continue;
4138 }
4139 }
4140 TRACE_LEAVE(QUIC_EV_CONN_PAPKTS, qc->conn);
4141
4142 return 1;
4143}
4144
4145/* QUIC connection packet handler task. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004146struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004147{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004148 struct ssl_sock_ctx *ctx = context;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004149
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004150 if (ctx->conn->qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004151 qc_do_hdshk(ctx);
4152 }
4153 else {
4154 struct quic_conn *qc = ctx->conn->qc;
4155
4156 /* XXX TO DO: may fail!!! XXX */
4157 qc_treat_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_APP], ctx);
4158 qc_prep_phdshk_pkts(qc);
4159 qc_send_ppkts(ctx);
4160 }
4161
Willy Tarreau74163142021-03-13 11:30:19 +01004162 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004163}
4164
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004165/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4166 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004167 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004168static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4169 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004170{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004171 size_t try, done = 0;
4172
4173 if (!conn_ctrl_ready(conn))
4174 return 0;
4175
4176 if (!fd_recv_ready(conn->handle.fd))
4177 return 0;
4178
4179 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004180
4181 /* read the largest possible block. For this, we perform only one call
4182 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004183 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004184 */
4185 while (count > 0) {
4186 try = b_contig_space(buf);
4187 if (!try)
4188 break;
4189
4190 if (try > count)
4191 try = count;
4192
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004193 b_add(buf, try);
4194 done += try;
4195 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004196 }
4197
4198 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4199 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4200
4201 leave:
4202 return done;
4203
4204 read0:
4205 conn_sock_read0(conn);
4206 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4207
4208 /* Now a final check for a possible asynchronous low-level error
4209 * report. This can happen when a connection receives a reset
4210 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4211 * we might have come from there by just checking POLL_HUP instead
4212 * of recv()'s return value 0, so we have no way to tell there was
4213 * an error without checking.
4214 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004215 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004216 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4217 goto leave;
4218}
4219
4220
4221/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4222 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4223 * other pending data for example, but this flag is ignored at the moment.
4224 * Only one call to send() is performed, unless the buffer wraps, in which case
4225 * a second call may be performed. The connection's flags are updated with
4226 * whatever special event is detected (error, empty). The caller is responsible
4227 * for taking care of those events and avoiding the call if inappropriate. The
4228 * function does not call the connection's polling update function, so the caller
4229 * is responsible for this. It's up to the caller to update the buffer's contents
4230 * based on the return value.
4231 */
4232static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4233{
4234 ssize_t ret;
4235 size_t try, done;
4236 int send_flag;
4237
4238 if (!conn_ctrl_ready(conn))
4239 return 0;
4240
4241 if (!fd_send_ready(conn->handle.fd))
4242 return 0;
4243
4244 done = 0;
4245 /* send the largest possible block. For this we perform only one call
4246 * to send() unless the buffer wraps and we exactly fill the first hunk,
4247 * in which case we accept to do it once again.
4248 */
4249 while (count) {
4250 try = b_contig_data(buf, done);
4251 if (try > count)
4252 try = count;
4253
4254 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4255 if (try < count || flags & CO_SFL_MSG_MORE)
4256 send_flag |= MSG_MORE;
4257
4258 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4259 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4260 if (ret > 0) {
4261 count -= ret;
4262 done += ret;
4263
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004264 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004265 conn->flags |= CO_FL_WAIT_L4L6;
4266 /* if the system buffer is full, don't insist */
4267 if (ret < try)
4268 break;
4269 }
4270 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4271 /* nothing written, we need to poll for write first */
4272 fd_cant_send(conn->handle.fd);
4273 break;
4274 }
4275 else if (errno != EINTR) {
4276 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4277 break;
4278 }
4279 }
4280 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4281 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4282
4283 if (done > 0) {
4284 /* we count the total bytes sent, and the send rate for 32-byte
4285 * blocks. The reason for the latter is that freq_ctr are
4286 * limited to 4GB and that it's not enough per second.
4287 */
4288 _HA_ATOMIC_ADD(&global.out_bytes, done);
4289 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4290 }
4291 return done;
4292}
4293
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004294/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4295 * event subscriber <es> is not allowed to change from a previous call as long
4296 * as at least one event is still subscribed. The <event_type> must only be a
4297 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4298 */
4299static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4300{
4301 return conn_subscribe(conn, xprt_ctx, event_type, es);
4302}
4303
4304/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4305 * The <es> pointer is not allowed to differ from the one passed to the
4306 * subscribe() call. It always returns zero.
4307 */
4308static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4309{
4310 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4311}
4312
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004313/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4314 * connection with <xprt_ctx> as address of the xprt context.
4315 * Returns 1 if succeeded, 0 if not.
4316 */
4317static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4318{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004319 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004320
4321 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4322
4323 if (*xprt_ctx)
4324 goto out;
4325
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004326 ctx = pool_alloc(pool_head_quic_conn_ctx);
4327 if (!ctx) {
4328 conn->err_code = CO_ER_SYS_MEMLIM;
4329 goto err;
4330 }
4331
4332 ctx->wait_event.tasklet = tasklet_new();
4333 if (!ctx->wait_event.tasklet) {
4334 conn->err_code = CO_ER_SYS_MEMLIM;
4335 goto err;
4336 }
4337
4338 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4339 ctx->wait_event.tasklet->context = ctx;
4340 ctx->wait_event.events = 0;
4341 ctx->conn = conn;
4342 ctx->subs = NULL;
4343 ctx->xprt_ctx = NULL;
4344
4345 ctx->xprt = xprt_get(XPRT_QUIC);
4346 if (objt_server(conn->target)) {
4347 /* Server */
4348 struct server *srv = __objt_server(conn->target);
4349 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004350 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004351 int ssl_err, ipv4;
4352
4353 ssl_err = SSL_ERROR_NONE;
4354 if (RAND_bytes(dcid, sizeof dcid) != 1)
4355 goto err;
4356
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004357 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004358 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004359 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004360 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004361 goto err;
4362
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004363 /* Insert our SCID, the connection ID for the QUIC client. */
4364 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4365
4366 conn->qc = qc;
4367 qc->conn = conn;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004368 if (!qc_new_isecs(qc, dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004369 goto err;
4370
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004371 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4372 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4373 goto err;
4374
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004375 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004376 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004377 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4378 qc->enc_params_len =
4379 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4380 &qc->rx.params, 0);
4381 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004382 goto err;
4383
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004384 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004385 SSL_set_connect_state(ctx->ssl);
4386 ssl_err = SSL_do_handshake(ctx->ssl);
4387 if (ssl_err != 1) {
4388 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4389 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
4390 TRACE_PROTO("SSL handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004391 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004392 }
4393 else {
4394 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004395 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004396 goto err;
4397 }
4398 }
4399 }
4400 else if (objt_listener(conn->target)) {
4401 /* Listener */
4402 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004403 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004404
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004405 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4406 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4407 goto err;
4408
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004409 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004410 SSL_set_accept_state(ctx->ssl);
4411 }
4412
4413 *xprt_ctx = ctx;
4414
4415 /* Leave init state and start handshake */
4416 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004417
4418 out:
4419 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4420
4421 return 0;
4422
4423 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004424 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004425 tasklet_free(ctx->wait_event.tasklet);
4426 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004427 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004428 return -1;
4429}
4430
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004431/* Start the QUIC transport layer */
4432static int qc_xprt_start(struct connection *conn, void *ctx)
4433{
4434 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004435 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004436
4437 qc = conn->qc;
4438 if (!quic_conn_init_timer(qc)) {
4439 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4440 return 0;
4441 }
4442
4443 tasklet_wakeup(qctx->wait_event.tasklet);
4444 return 1;
4445}
4446
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004447/* transport-layer operations for QUIC connections. */
4448static struct xprt_ops ssl_quic = {
4449 .snd_buf = quic_conn_from_buf,
4450 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004451 .subscribe = quic_conn_subscribe,
4452 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004453 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004454 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004455 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4456 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4457 .name = "QUIC",
4458};
4459
4460__attribute__((constructor))
4461static void __quic_conn_init(void)
4462{
4463 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4464 xprt_register(XPRT_QUIC, &ssl_quic);
4465}
4466
4467__attribute__((destructor))
4468static void __quic_conn_deinit(void)
4469{
4470 BIO_meth_free(ha_quic_meth);
4471}
4472
4473/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4474 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4475 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004476 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004477 */
4478static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4479 struct sockaddr_storage *saddr, qpkt_read_func *func)
4480{
4481 unsigned char *pos;
4482 const unsigned char *end;
4483 struct quic_dgram_ctx dgram_ctx = {
4484 .dcid_node = NULL,
4485 .owner = owner,
4486 };
4487
4488 pos = (unsigned char *)buf;
4489 end = pos + len;
4490
4491 do {
4492 int ret;
4493 struct quic_rx_packet *pkt;
4494
Willy Tarreaue4498932021-03-22 21:13:05 +01004495 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004496 if (!pkt)
4497 goto err;
4498
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004499 quic_rx_packet_refinc(pkt);
4500 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4501 if (ret == -1) {
4502 size_t pkt_len;
4503
4504 pkt_len = pkt->len;
4505 free_quic_rx_packet(pkt);
4506 /* If the packet length could not be found, we cannot continue. */
4507 if (!pkt_len)
4508 break;
4509 }
4510 } while (pos < end);
4511
4512 /* Increasing the received bytes counter by the UDP datagram length
4513 * if this datagram could be associated to a connection.
4514 */
4515 if (dgram_ctx.qc)
4516 dgram_ctx.qc->rx.bytes += len;
4517
4518 return pos - (unsigned char *)buf;
4519
4520 err:
4521 return -1;
4522}
4523
4524ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4525 struct sockaddr_storage *saddr)
4526{
4527 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4528}
4529
4530ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4531 struct sockaddr_storage *saddr)
4532{
4533 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4534}
4535
4536/* QUIC I/O handler for connection to local listeners or remove servers
4537 * depending on <listener> boolean value, with <fd> as socket file
4538 * descriptor and <ctx> as context.
4539 */
4540static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4541{
4542 ssize_t ret;
4543 size_t done = 0;
4544 struct buffer *buf = get_trash_chunk();
4545 /* Source address */
4546 struct sockaddr_storage saddr = {0};
4547 socklen_t saddrlen = sizeof saddr;
4548
4549 if (!fd_recv_ready(fd))
4550 return 0;
4551
4552 do {
4553 ret = recvfrom(fd, buf->area, buf->size, 0,
4554 (struct sockaddr *)&saddr, &saddrlen);
4555 if (ret < 0) {
4556 if (errno == EINTR)
4557 continue;
4558 if (errno == EAGAIN)
4559 fd_cant_recv(fd);
4560 goto out;
4561 }
4562 } while (0);
4563
4564 done = buf->data = ret;
4565 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4566
4567 out:
4568 return done;
4569}
4570
4571/* QUIC I/O handler for connections to local listeners with <fd> as socket
4572 * file descriptor.
4573 */
4574void quic_fd_handler(int fd)
4575{
Willy Tarreauf5090652021-04-06 17:23:40 +02004576 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004577 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4578}
4579
4580/* QUIC I/O handler for connections to remote servers with <fd> as socket
4581 * file descriptor.
4582 */
4583void quic_conn_fd_handler(int fd)
4584{
Willy Tarreauf5090652021-04-06 17:23:40 +02004585 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004586 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4587}
4588
4589/*
4590 * Local variables:
4591 * c-indent-level: 8
4592 * c-basic-offset: 8
4593 * End:
4594 */