blob: 90761c9f2ce0579886f9139a356f9c08b1a8046a [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
25#include <haproxy/buf-t.h>
26#include <haproxy/compat.h>
27#include <haproxy/api.h>
28#include <haproxy/debug.h>
29#include <haproxy/tools.h>
30#include <haproxy/ticks.h>
31#include <haproxy/time.h>
32
33#include <haproxy/connection.h>
34#include <haproxy/fd.h>
35#include <haproxy/freq_ctr.h>
36#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037#include <haproxy/h3.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010038#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010039#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/pipe.h>
41#include <haproxy/proxy.h>
42#include <haproxy/quic_cc.h>
43#include <haproxy/quic_frame.h>
44#include <haproxy/quic_loss.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020045#include <haproxy/cbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010046#include <haproxy/quic_tls.h>
47#include <haproxy/ssl_sock.h>
48#include <haproxy/stream_interface.h>
49#include <haproxy/task.h>
50#include <haproxy/trace.h>
51#include <haproxy/xprt_quic.h>
52
53struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020054 .max_udp_payload_size = QUIC_DFLT_MAX_UDP_PAYLOAD_SIZE,
55 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
56 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010057};
58
59/* trace source and events */
60static void quic_trace(enum trace_level level, uint64_t mask, \
61 const struct trace_source *src,
62 const struct ist where, const struct ist func,
63 const void *a1, const void *a2, const void *a3, const void *a4);
64
65static const struct trace_event quic_trace_events[] = {
66 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
67 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
68 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
69 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
70 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
71 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
72 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050073 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010074 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
75 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
76 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
77 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
78 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
79 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
80 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
81 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
82 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
83 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
84 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
85 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
86 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
87 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
88 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
89 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
90 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
91 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
92 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010093 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
94 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
95 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
96 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
97 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
98 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +010099 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100100 { /* end */ }
101};
102
103static const struct name_desc quic_trace_lockon_args[4] = {
104 /* arg1 */ { /* already used by the connection */ },
105 /* arg2 */ { .name="quic", .desc="QUIC transport" },
106 /* arg3 */ { },
107 /* arg4 */ { }
108};
109
110static const struct name_desc quic_trace_decoding[] = {
111#define QUIC_VERB_CLEAN 1
112 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
113 { /* end */ }
114};
115
116
117struct trace_source trace_quic = {
118 .name = IST("quic"),
119 .desc = "QUIC xprt",
120 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
121 .default_cb = quic_trace,
122 .known_events = quic_trace_events,
123 .lockon_args = quic_trace_lockon_args,
124 .decoding = quic_trace_decoding,
125 .report_events = ~0, /* report everything by default */
126};
127
128#define TRACE_SOURCE &trace_quic
129INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
130
131static BIO_METHOD *ha_quic_meth;
132
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100133DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200134 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100135DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100136DECLARE_POOL(pool_head_quic_connection_id,
137 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100138DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100139DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100140DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100141DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100142DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200143DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100144DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100145
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +0200146static struct quic_tx_packet *qc_build_hdshk_pkt(unsigned char **pos, const unsigned char *buf_end,
147 struct quic_conn *qc, int pkt_type,
148 struct quic_enc_level *qel, int *err);
149int qc_prep_phdshk_pkts(struct qring *qr, struct quic_conn *qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100150
151/* Add traces to <buf> depending on <frm> TX frame type. */
152static inline void chunk_tx_frm_appendf(struct buffer *buf,
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200153 const struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100154{
155 switch (frm->type) {
156 case QUIC_FT_CRYPTO:
157 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
158 (unsigned long long)frm->crypto.offset,
159 (unsigned long long)frm->crypto.len);
160 break;
161 default:
162 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
163 }
164}
165
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100166/* Only for debug purpose */
167struct enc_debug_info {
168 unsigned char *payload;
169 size_t payload_len;
170 unsigned char *aad;
171 size_t aad_len;
172 uint64_t pn;
173};
174
175/* Initializes a enc_debug_info struct (only for debug purpose) */
176static inline void enc_debug_info_init(struct enc_debug_info *edi,
177 unsigned char *payload, size_t payload_len,
178 unsigned char *aad, size_t aad_len, uint64_t pn)
179{
180 edi->payload = payload;
181 edi->payload_len = payload_len;
182 edi->aad = aad;
183 edi->aad_len = aad_len;
184 edi->pn = pn;
185}
186
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100187/* Trace callback for QUIC.
188 * These traces always expect that arg1, if non-null, is of type connection.
189 */
190static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
191 const struct ist where, const struct ist func,
192 const void *a1, const void *a2, const void *a3, const void *a4)
193{
194 const struct connection *conn = a1;
195
196 if (conn) {
197 struct quic_tls_secrets *secs;
198 struct quic_conn *qc;
199
200 qc = conn->qc;
201 chunk_appendf(&trace_buf, " : conn@%p", conn);
202 if ((mask & QUIC_EV_CONN_INIT) && qc) {
203 chunk_appendf(&trace_buf, "\n odcid");
204 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100205 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100206 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100207 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100208 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100209 }
210
211 if (mask & QUIC_EV_CONN_ADDDATA) {
212 const enum ssl_encryption_level_t *level = a2;
213 const size_t *len = a3;
214
215 if (level) {
216 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
217
218 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
219 }
220 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100221 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100222 }
223 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
224 /* Initial read & write secrets. */
225 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
226 const unsigned char *rx_sec = a2;
227 const unsigned char *tx_sec = a3;
228
229 secs = &qc->els[level].tls_ctx.rx;
230 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
231 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
232 if (rx_sec)
233 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
234 quic_tls_keys_hexdump(&trace_buf, secs);
235 }
236 secs = &qc->els[level].tls_ctx.tx;
237 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
238 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
239 if (tx_sec)
240 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
241 quic_tls_keys_hexdump(&trace_buf, secs);
242 }
243 }
244 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
245 const enum ssl_encryption_level_t *level = a2;
246 const unsigned char *secret = a3;
247 const size_t *secret_len = a4;
248
249 if (level) {
250 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
251
252 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
253 if (secret && secret_len)
254 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
255 secs = &qc->els[lvl].tls_ctx.rx;
256 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
257 quic_tls_keys_hexdump(&trace_buf, secs);
258 }
259 }
260
261 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
262 const enum ssl_encryption_level_t *level = a2;
263 const unsigned char *secret = a3;
264 const size_t *secret_len = a4;
265
266 if (level) {
267 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
268
269 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
270 if (secret && secret_len)
271 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
272 secs = &qc->els[lvl].tls_ctx.tx;
273 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
274 quic_tls_keys_hexdump(&trace_buf, secs);
275 }
276
277 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100278
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100279 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100280 const struct quic_tx_packet *pkt = a2;
281 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100282 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100283
284 if (qel) {
285 struct quic_pktns *pktns;
286
287 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100288 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
289 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100291 (unsigned long long)qc->path->cwnd,
292 (unsigned long long)qc->path->prep_in_flight,
293 (unsigned long long)qc->path->in_flight,
294 (unsigned long long)pktns->tx.in_flight,
295 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100296 }
297 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200298 const struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100299 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
300 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
301 list_for_each_entry(frm, &pkt->frms, list)
302 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100303 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
304 }
305
306 if (room) {
307 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
308 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
309 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100310 }
311 }
312
313 if (mask & QUIC_EV_CONN_HDSHK) {
314 const enum quic_handshake_state *state = a2;
315 const int *err = a3;
316
317 if (state)
318 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
319 if (err)
320 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
321 }
322
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100323 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100324 const struct quic_rx_packet *pkt = a2;
325 const unsigned long *pktlen = a3;
326 const SSL *ssl = a4;
327
328 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100329 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100330 pkt, quic_packet_type_enc_level_char(pkt->type));
331 if (pkt->pnl)
332 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
333 (unsigned long long)pkt->pn);
334 if (pkt->token_len)
335 chunk_appendf(&trace_buf, " toklen=%llu",
336 (unsigned long long)pkt->token_len);
337 if (pkt->aad_len)
338 chunk_appendf(&trace_buf, " aadlen=%llu",
339 (unsigned long long)pkt->aad_len);
340 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
341 pkt->flags, (unsigned long long)pkt->len);
342 }
343 if (pktlen)
344 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
345 if (ssl) {
346 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
347 chunk_appendf(&trace_buf, " el=%c",
348 quic_enc_level_char(ssl_to_quic_enc_level(level)));
349 }
350 }
351
352 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
353 const struct quic_rx_packet *pkt = a2;
354 const struct quic_rx_crypto_frm *cf = a3;
355 const SSL *ssl = a4;
356
357 if (pkt)
358 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
359 quic_packet_type_enc_level_char(pkt->type),
360 (unsigned long long)pkt->pn);
361 if (cf)
362 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
363 (unsigned long long)cf->offset_node.key,
364 (unsigned long long)cf->len);
365 if (ssl) {
366 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
367 chunk_appendf(&trace_buf, " el=%c",
368 quic_enc_level_char(ssl_to_quic_enc_level(level)));
369 }
370 }
371
372 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
373 const struct quic_frame *frm = a2;
374
375 if (frm)
376 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
377 }
378
379 if (mask & QUIC_EV_CONN_PHPKTS) {
380 const struct quic_enc_level *qel = a2;
381
382 if (qel) {
383 struct quic_pktns *pktns;
384
385 pktns = qc->pktns;
386 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200387 " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u pdg=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100388 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200389 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100390 !!(pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100391 (unsigned long long)qc->path->cwnd,
392 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100393 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100394 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
395 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100396 }
397 }
398
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100399 if (mask & QUIC_EV_CONN_ENCPKT) {
400 const struct enc_debug_info *edi = a2;
401
402 if (edi)
403 chunk_appendf(&trace_buf,
404 " payload=@%p payload_len=%llu"
405 " aad=@%p aad_len=%llu pn=%llu",
406 edi->payload, (unsigned long long)edi->payload_len,
407 edi->aad, (unsigned long long)edi->aad_len,
408 (unsigned long long)edi->pn);
409 }
410
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100411 if (mask & QUIC_EV_CONN_RMHP) {
412 const struct quic_rx_packet *pkt = a2;
413
414 if (pkt) {
415 const int *ret = a3;
416
417 chunk_appendf(&trace_buf, " pkt@%p", pkt);
418 if (ret && *ret)
419 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
420 pkt->pnl, (unsigned long long)pkt->pn);
421 }
422 }
423
424 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200425 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100426 const unsigned long *val1 = a3;
427 const unsigned long *val2 = a4;
428
429 if (frm)
430 chunk_tx_frm_appendf(&trace_buf, frm);
431 if (val1)
432 chunk_appendf(&trace_buf, " %lu", *val1);
433 if (val2)
434 chunk_appendf(&trace_buf, "..%lu", *val2);
435 }
436
437 if (mask & QUIC_EV_CONN_RTTUPDT) {
438 const unsigned int *rtt_sample = a2;
439 const unsigned int *ack_delay = a3;
440 const struct quic_loss *ql = a4;
441
442 if (rtt_sample)
443 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
444 if (ack_delay)
445 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
446 if (ql)
447 chunk_appendf(&trace_buf,
448 " srtt=%ums rttvar=%ums min_rtt=%ums",
449 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
450 }
451 if (mask & QUIC_EV_CONN_CC) {
452 const struct quic_cc_event *ev = a2;
453 const struct quic_cc *cc = a3;
454
455 if (a2)
456 quic_cc_event_trace(&trace_buf, ev);
457 if (a3)
458 quic_cc_state_trace(&trace_buf, cc);
459 }
460
461 if (mask & QUIC_EV_CONN_PKTLOSS) {
462 const struct quic_pktns *pktns = a2;
463 const struct list *lost_pkts = a3;
464 struct quic_conn *qc = conn->qc;
465
466 if (pktns) {
467 chunk_appendf(&trace_buf, " pktns=%s",
468 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
469 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
470 if (pktns->tx.loss_time)
471 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100472 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100473 }
474 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
475 struct quic_tx_packet *pkt;
476
477 chunk_appendf(&trace_buf, " lost_pkts:");
478 list_for_each_entry(pkt, lost_pkts, list)
479 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
480 }
481 }
482
483 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
484 struct quic_conn *qc = conn->qc;
485 const struct quic_pktns *pktns = a2;
486 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100487 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100488
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100489 if (ifae_pkts)
490 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
491 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100492 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100493 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100495 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
496 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100497 if (mask & QUIC_EV_CONN_STIMER) {
498 if (pktns->tx.loss_time)
499 chunk_appendf(&trace_buf, " loss_time=%dms",
500 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
501 }
502 if (mask & QUIC_EV_CONN_SPTO) {
503 if (pktns->tx.time_of_last_eliciting)
504 chunk_appendf(&trace_buf, " tole=%dms",
505 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
506 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100507 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100508 }
509 }
510
511 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
512 chunk_appendf(&trace_buf,
513 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
514 }
515 }
516
517 if (mask & QUIC_EV_CONN_SPPKTS) {
518 const struct quic_tx_packet *pkt = a2;
519
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100520 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
521 (unsigned long long)qc->path->cwnd,
522 (unsigned long long)qc->path->prep_in_flight,
523 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100524 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100525 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100526 (unsigned long)pkt->pn_node.key,
527 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
528 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100529 (unsigned long long)pkt->in_flight_len,
530 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100531 }
532 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100533
534 if (mask & QUIC_EV_CONN_SSLALERT) {
535 const uint8_t *alert = a2;
536 const enum ssl_encryption_level_t *level = a3;
537
538 if (alert)
539 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
540 if (level)
541 chunk_appendf(&trace_buf, " el=%c",
542 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
543 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100544
545 if (mask & QUIC_EV_CONN_BCFRMS) {
546 const size_t *sz1 = a2;
547 const size_t *sz2 = a3;
548 const size_t *sz3 = a4;
549
550 if (sz1)
551 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
552 if (sz2)
553 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
554 if (sz3)
555 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
556 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100557
558 if (mask & QUIC_EV_CONN_PSTRM) {
559 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100560
561 if (a2) {
562 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100563
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100564 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
565 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
566 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
567 (unsigned long long)s->id,
568 (unsigned long long)s->offset,
569 (unsigned long long)s->len);
570 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100571 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100572 }
573 if (mask & QUIC_EV_CONN_LPKT) {
574 const struct quic_rx_packet *pkt = a2;
575
576 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200577 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100578 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200579 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
580 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100581 }
582
583}
584
585/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200586static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587{
588 struct quic_conn *qc;
589
590 qc = ctx->conn->qc;
591 if (objt_server(qc->conn->target))
592 return 1;
593
594 if ((qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
595 (qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +0200596 (qc->state & QUIC_HS_ST_COMPLETE))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100597 return 1;
598
599 return 0;
600}
601
602/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
603 * both loss detection and PTO and schedule the task assiated to this timer if needed.
604 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200605static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100606{
607 struct quic_conn *qc;
608 struct quic_pktns *pktns;
609 unsigned int pto;
610
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100611 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
612 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613 qc = ctx->conn->qc;
614 pktns = quic_loss_pktns(qc);
615 if (tick_isset(pktns->tx.loss_time)) {
616 qc->timer = pktns->tx.loss_time;
617 goto out;
618 }
619
620 /* XXX TODO: anti-amplification: the timer must be
621 * cancelled for a server which reached the anti-amplification limit.
622 */
623
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100624 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
625 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100626 /* Timer cancellation. */
627 qc->timer = TICK_ETERNITY;
628 goto out;
629 }
630
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +0200631 pktns = quic_pto_pktns(qc, qc->state & QUIC_HS_ST_COMPLETE, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100632 if (tick_isset(pto))
633 qc->timer = pto;
634 out:
635 task_schedule(qc->timer_task, qc->timer);
636 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
637}
638
639#ifndef OPENSSL_IS_BORINGSSL
640int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
641 const uint8_t *read_secret,
642 const uint8_t *write_secret, size_t secret_len)
643{
644 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
645 struct quic_tls_ctx *tls_ctx =
646 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
647 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
648
649 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
650 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
651 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
652 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
653
654 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
655 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
656 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
657 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
658 read_secret, secret_len)) {
659 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
660 return 0;
661 }
662
663 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
664 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
665 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
666 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
667 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
668 write_secret, secret_len)) {
669 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
670 return 0;
671 }
672
673 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
674 if (objt_server(conn->target) && level == ssl_encryption_application) {
675 const unsigned char *buf;
676 size_t buflen;
677
678 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
679 if (!buflen)
680 return 0;
681
682 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
683 return 0;
684 }
685 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
686
687 return 1;
688}
689#else
690/* ->set_read_secret callback to derive the RX secrets at <level> encryption
691 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500692 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100693 */
694int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
695 const SSL_CIPHER *cipher,
696 const uint8_t *secret, size_t secret_len)
697{
698 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
699 struct quic_tls_ctx *tls_ctx =
700 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
701
702 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
703 tls_ctx->rx.aead = tls_aead(cipher);
704 tls_ctx->rx.md = tls_md(cipher);
705 tls_ctx->rx.hp = tls_hp(cipher);
706
707 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
708 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
709 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
710 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
711 secret, secret_len)) {
712 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
713 goto err;
714 }
715
716 if (objt_server(conn->target) && level == ssl_encryption_application) {
717 const unsigned char *buf;
718 size_t buflen;
719
720 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
721 if (!buflen)
722 goto err;
723
724 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
725 goto err;
726 }
727
728 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
729 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
730
731 return 1;
732
733 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100734 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100735 return 0;
736}
737
738/* ->set_write_secret callback to derive the TX secrets at <level>
739 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500740 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100741 */
742int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
743 const SSL_CIPHER *cipher,
744 const uint8_t *secret, size_t secret_len)
745{
746 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
747 struct quic_tls_ctx *tls_ctx =
748 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
749
750 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
751 tls_ctx->tx.aead = tls_aead(cipher);
752 tls_ctx->tx.md = tls_md(cipher);
753 tls_ctx->tx.hp = tls_hp(cipher);
754
755 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
756 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
757 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
758 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
759 secret, secret_len)) {
760 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
761 goto err;
762 }
763
764 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
765 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
766
767 return 1;
768
769 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100770 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100771 return 0;
772}
773#endif
774
775/* This function copies the CRYPTO data provided by the TLS stack found at <data>
776 * with <len> as size in CRYPTO buffers dedicated to store the information about
777 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
778 * It fails only if it could not managed to allocate enough CRYPTO buffers to
779 * store all the data.
780 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
781 */
782static int quic_crypto_data_cpy(struct quic_enc_level *qel,
783 const unsigned char *data, size_t len)
784{
785 struct quic_crypto_buf **qcb;
786 /* The remaining byte to store in CRYPTO buffers. */
787 size_t cf_offset, cf_len, *nb_buf;
788 unsigned char *pos;
789
790 nb_buf = &qel->tx.crypto.nb_buf;
791 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
792 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
793 cf_len = len;
794
795 while (len) {
796 size_t to_copy, room;
797
798 pos = (*qcb)->data + (*qcb)->sz;
799 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
800 to_copy = len > room ? room : len;
801 if (to_copy) {
802 memcpy(pos, data, to_copy);
803 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
804 qel->tx.crypto.sz += to_copy;
805 (*qcb)->sz += to_copy;
806 pos += to_copy;
807 len -= to_copy;
808 data += to_copy;
809 }
810 else {
811 struct quic_crypto_buf **tmp;
812
813 tmp = realloc(qel->tx.crypto.bufs,
814 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
815 if (tmp) {
816 qel->tx.crypto.bufs = tmp;
817 qcb = &qel->tx.crypto.bufs[*nb_buf];
818 *qcb = pool_alloc(pool_head_quic_crypto_buf);
819 if (!*qcb)
820 return 0;
821
822 (*qcb)->sz = 0;
823 ++*nb_buf;
824 }
825 else {
826 break;
827 }
828 }
829 }
830
831 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
832 * have been buffered.
833 */
834 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200835 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100836
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200837 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100838 if (!frm)
839 return 0;
840
841 frm->type = QUIC_FT_CRYPTO;
842 frm->crypto.offset = cf_offset;
843 frm->crypto.len = cf_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +0200844 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100845 }
846
847 return len == 0;
848}
849
850
851/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
852 * wants to provide the QUIC layer with CRYPTO data.
853 * Returns 1 if succeeded, 0 if not.
854 */
855int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
856 const uint8_t *data, size_t len)
857{
858 struct connection *conn;
859 enum quic_tls_enc_level tel;
860 struct quic_enc_level *qel;
861
862 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
863 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
864 tel = ssl_to_quic_enc_level(level);
865 qel = &conn->qc->els[tel];
866
867 if (tel == -1) {
868 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
869 goto err;
870 }
871
872 if (!quic_crypto_data_cpy(qel, data, len)) {
873 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
874 goto err;
875 }
876
877 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
878 conn, &level, &len);
879
880 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
881 return 1;
882
883 err:
884 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
885 return 0;
886}
887
888int ha_quic_flush_flight(SSL *ssl)
889{
890 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
891
892 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
893 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
894
895 return 1;
896}
897
898int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
899{
900 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
901
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100902 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100903 return 1;
904}
905
906/* QUIC TLS methods */
907static SSL_QUIC_METHOD ha_quic_method = {
908#ifdef OPENSSL_IS_BORINGSSL
909 .set_read_secret = ha_set_rsec,
910 .set_write_secret = ha_set_wsec,
911#else
912 .set_encryption_secrets = ha_quic_set_encryption_secrets,
913#endif
914 .add_handshake_data = ha_quic_add_handshake_data,
915 .flush_flight = ha_quic_flush_flight,
916 .send_alert = ha_quic_send_alert,
917};
918
919/* Initialize the TLS context of a listener with <bind_conf> as configuration.
920 * Returns an error count.
921 */
922int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
923{
924 struct proxy *curproxy = bind_conf->frontend;
925 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
926 int cfgerr = 0;
927
928#if 0
929 /* XXX Did not manage to use this. */
930 const char *ciphers =
931 "TLS_AES_128_GCM_SHA256:"
932 "TLS_AES_256_GCM_SHA384:"
933 "TLS_CHACHA20_POLY1305_SHA256:"
934 "TLS_AES_128_CCM_SHA256";
935#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200936 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100937 long options =
938 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
939 SSL_OP_SINGLE_ECDH_USE |
940 SSL_OP_CIPHER_SERVER_PREFERENCE;
941 SSL_CTX *ctx;
942
943 ctx = SSL_CTX_new(TLS_server_method());
944 bind_conf->initial_ctx = ctx;
945
946 SSL_CTX_set_options(ctx, options);
947#if 0
948 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
949 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
950 "for bind '%s' at [%s:%d].\n",
951 curproxy->id, ciphers,
952 bind_conf->arg, bind_conf->file, bind_conf->line);
953 cfgerr++;
954 }
955#endif
956
957 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
958 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
959 "for bind '%s' at [%s:%d].\n",
960 curproxy->id, groups,
961 bind_conf->arg, bind_conf->file, bind_conf->line);
962 cfgerr++;
963 }
964
965 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
966 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
967 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
968 SSL_CTX_set_default_verify_paths(ctx);
969
970#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
971#ifdef OPENSSL_IS_BORINGSSL
972 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
973 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
974#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
975 if (bind_conf->ssl_conf.early_data) {
976 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
977 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
978 }
979 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
980 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
981#else
982 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
983#endif
984 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
985#endif
986 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
987
988 return cfgerr;
989}
990
991/* Decode an expected packet number from <truncated_on> its truncated value,
992 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
993 * the number of bits used to encode this packet number (its length in bytes * 8).
994 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
995 */
996static uint64_t decode_packet_number(uint64_t largest_pn,
997 uint32_t truncated_pn, unsigned int pn_nbits)
998{
999 uint64_t expected_pn = largest_pn + 1;
1000 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1001 uint64_t pn_hwin = pn_win / 2;
1002 uint64_t pn_mask = pn_win - 1;
1003 uint64_t candidate_pn;
1004
1005
1006 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1007 /* Note that <pn_win> > <pn_hwin>. */
1008 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1009 candidate_pn + pn_hwin <= expected_pn)
1010 return candidate_pn + pn_win;
1011
1012 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1013 return candidate_pn - pn_win;
1014
1015 return candidate_pn;
1016}
1017
1018/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1019 * cryptographic context.
1020 * <largest_pn> is the largest received packet number and <pn> the address of
1021 * the packet number field for this packet with <byte0> address of its first byte.
1022 * <end> points to one byte past the end of this packet.
1023 * Returns 1 if succeeded, 0 if not.
1024 */
1025static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1026 int64_t largest_pn, unsigned char *pn,
1027 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001028 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001029{
1030 int ret, outlen, i, pnlen;
1031 uint64_t packet_number;
1032 uint32_t truncated_pn = 0;
1033 unsigned char mask[5] = {0};
1034 unsigned char *sample;
1035 EVP_CIPHER_CTX *cctx;
1036 unsigned char *hp_key;
1037
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001038 /* Check there is enough data in this packet. */
1039 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1040 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1041 return 0;
1042 }
1043
1044 cctx = EVP_CIPHER_CTX_new();
1045 if (!cctx) {
1046 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1047 return 0;
1048 }
1049
1050 ret = 0;
1051 sample = pn + QUIC_PACKET_PN_MAXLEN;
1052
1053 hp_key = tls_ctx->rx.hp_key;
1054 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1055 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1056 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1057 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1058 goto out;
1059 }
1060
1061 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1062 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1063 for (i = 0; i < pnlen; i++) {
1064 pn[i] ^= mask[i + 1];
1065 truncated_pn = (truncated_pn << 8) | pn[i];
1066 }
1067
1068 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1069 /* Store remaining information for this unprotected header */
1070 pkt->pn = packet_number;
1071 pkt->pnl = pnlen;
1072
1073 ret = 1;
1074
1075 out:
1076 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001077
1078 return ret;
1079}
1080
1081/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1082 * address, with <payload_len> as payload length, <aad> as address of
1083 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1084 * context.
1085 * Returns 1 if succeeded, 0 if not.
1086 */
1087static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1088 unsigned char *aad, size_t aad_len, uint64_t pn,
1089 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1090{
1091 unsigned char iv[12];
1092 unsigned char *tx_iv = tls_ctx->tx.iv;
1093 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001094 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001095
1096 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1097 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001098 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099 }
1100
1101 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1102 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1103 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001104 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001105 }
1106
1107 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001108
1109 err:
1110 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1111 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1112 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001113}
1114
1115/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1116 * Returns 1 if succeeded, 0 if not.
1117 */
1118static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1119{
1120 int ret;
1121 unsigned char iv[12];
1122 unsigned char *rx_iv = tls_ctx->rx.iv;
1123 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1124
1125 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1126 return 0;
1127
1128 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1129 pkt->data, pkt->aad_len,
1130 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1131 if (!ret)
1132 return 0;
1133
1134 /* Update the packet length (required to parse the frames). */
1135 pkt->len = pkt->aad_len + ret;
1136
1137 return 1;
1138}
1139
1140/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001141static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001142 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001143{
1144 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001145 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001146 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001147}
1148
1149/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1150 * deallocating them, and their TX frames.
1151 * Returns the last node reached to be used for the next range.
1152 * May be NULL if <largest> node could not be found.
1153 */
1154static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1155 struct list *newly_acked_pkts,
1156 struct eb64_node *largest_node,
1157 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001158 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001159{
1160 struct eb64_node *node;
1161 struct quic_tx_packet *pkt;
1162
1163 if (largest_node)
1164 node = largest_node;
1165 else {
1166 node = eb64_lookup(pkts, largest);
1167 while (!node && largest > smallest) {
1168 node = eb64_lookup(pkts, --largest);
1169 }
1170 }
1171
1172 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001173 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001174
1175 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1176 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001177 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001178 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1179 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1180 qc_treat_acked_tx_frm(frm, ctx);
1181 node = eb64_prev(node);
1182 eb64_delete(&pkt->pn_node);
1183 }
1184
1185 return node;
1186}
1187
1188/* Treat <frm> frame whose packet it is attached to has just been detected as non
1189 * acknowledged.
1190 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001191static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001192 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001193 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001194{
1195 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001196 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001197 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001198}
1199
1200
1201/* Free the TX packets of <pkts> list */
1202static inline void free_quic_tx_pkts(struct list *pkts)
1203{
1204 struct quic_tx_packet *pkt, *tmp;
1205
1206 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001207 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001208 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001209 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001210 }
1211}
1212
1213/* Send a packet loss event nofification to the congestion controller
1214 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1215 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1216 * at <now_us> current time.
1217 * Always succeeds.
1218 */
1219static inline void qc_cc_loss_event(struct quic_conn *qc,
1220 unsigned int lost_bytes,
1221 unsigned int newest_time_sent,
1222 unsigned int period,
1223 unsigned int now_us)
1224{
1225 struct quic_cc_event ev = {
1226 .type = QUIC_CC_EVT_LOSS,
1227 .loss.now_ms = now_ms,
1228 .loss.max_ack_delay = qc->max_ack_delay,
1229 .loss.lost_bytes = lost_bytes,
1230 .loss.newest_time_sent = newest_time_sent,
1231 .loss.period = period,
1232 };
1233
1234 quic_cc_event(&qc->path->cc, &ev);
1235}
1236
1237/* Send a packet ack event nofication for each newly acked packet of
1238 * <newly_acked_pkts> list and free them.
1239 * Always succeeds.
1240 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001241static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001242 struct list *newly_acked_pkts)
1243{
1244 struct quic_conn *qc = ctx->conn->qc;
1245 struct quic_tx_packet *pkt, *tmp;
1246 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1247
1248 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1249 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001250 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001251 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001252 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001253 ev.ack.acked = pkt->in_flight_len;
1254 ev.ack.time_sent = pkt->time_sent;
1255 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001256 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001257 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001258 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001259 }
1260
1261}
1262
1263/* Handle <pkts> list of lost packets detected at <now_us> handling
1264 * their TX frames.
1265 * Send a packet loss event to the congestion controller if
1266 * in flight packet have been lost.
1267 * Also frees the packet in <pkts> list.
1268 * Never fails.
1269 */
1270static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001271 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001272 struct list *pkts,
1273 uint64_t now_us)
1274{
1275 struct quic_conn *qc = ctx->conn->qc;
1276 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001277 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001278 uint64_t lost_bytes;
1279
1280 lost_bytes = 0;
1281 oldest_lost = newest_lost = NULL;
1282 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1283 lost_bytes += pkt->in_flight_len;
1284 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001285 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001286 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001287 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001288 /* Treat the frames of this lost packet. */
1289 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1290 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001291 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001292 if (!oldest_lost) {
1293 oldest_lost = newest_lost = pkt;
1294 }
1295 else {
1296 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001297 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001298 newest_lost = pkt;
1299 }
1300 }
1301
1302 if (lost_bytes) {
1303 /* Sent a packet loss event to the congestion controller. */
1304 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1305 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001306 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001307 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001308 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001309 }
1310}
1311
1312/* Look for packet loss from sent packets for <qel> encryption level of a
1313 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1314 * their tree if deemed as lost or set the <loss_time> value the packet number
1315 * space if any not deemed lost.
1316 * Should be called after having received an ACK frame with newly acknowledged
1317 * packets or when the the loss detection timer has expired.
1318 * Always succeeds.
1319 */
1320static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1321 struct quic_conn *qc,
1322 struct list *lost_pkts)
1323{
1324 struct eb_root *pkts;
1325 struct eb64_node *node;
1326 struct quic_loss *ql;
1327 unsigned int loss_delay;
1328
1329 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1330 pkts = &pktns->tx.pkts;
1331 pktns->tx.loss_time = TICK_ETERNITY;
1332 if (eb_is_empty(pkts))
1333 goto out;
1334
1335 ql = &qc->path->loss;
1336 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1337 loss_delay += loss_delay >> 3;
1338 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1339
1340 node = eb64_first(pkts);
1341 while (node) {
1342 struct quic_tx_packet *pkt;
1343 int64_t largest_acked_pn;
1344 unsigned int loss_time_limit, time_sent;
1345
1346 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1347 largest_acked_pn = pktns->tx.largest_acked_pn;
1348 node = eb64_next(node);
1349 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1350 break;
1351
1352 time_sent = pkt->time_sent;
1353 loss_time_limit = tick_add(time_sent, loss_delay);
1354 if (tick_is_le(time_sent, now_ms) ||
1355 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1356 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001357 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001358 }
1359 else {
1360 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1361 }
1362 }
1363
1364 out:
1365 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1366}
1367
1368/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1369 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001370 * 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 +01001371 * acked ack-eliciting packet.
1372 * Return 1, if succeeded, 0 if not.
1373 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001374static 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 +01001375 struct quic_enc_level *qel,
1376 unsigned int *rtt_sample,
1377 const unsigned char **pos, const unsigned char *end)
1378{
1379 struct quic_ack *ack = &frm->ack;
1380 uint64_t smallest, largest;
1381 struct eb_root *pkts;
1382 struct eb64_node *largest_node;
1383 unsigned int time_sent, pkt_flags;
1384 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1385 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1386
1387 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1388 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1389 ctx->conn,, &ack->largest_ack);
1390 goto err;
1391 }
1392
1393 if (ack->first_ack_range > ack->largest_ack) {
1394 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1395 ctx->conn,, &ack->first_ack_range);
1396 goto err;
1397 }
1398
1399 largest = ack->largest_ack;
1400 smallest = largest - ack->first_ack_range;
1401 pkts = &qel->pktns->tx.pkts;
1402 pkt_flags = 0;
1403 largest_node = NULL;
1404 time_sent = 0;
1405
1406 if ((int64_t)ack->largest_ack > qel->pktns->tx.largest_acked_pn) {
1407 largest_node = eb64_lookup(pkts, largest);
1408 if (!largest_node) {
1409 TRACE_DEVEL("Largest acked packet not found",
1410 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1411 goto err;
1412 }
1413
1414 time_sent = eb64_entry(&largest_node->node,
1415 struct quic_tx_packet, pn_node)->time_sent;
1416 }
1417
1418 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1419 ctx->conn,, &largest, &smallest);
1420 do {
1421 uint64_t gap, ack_range;
1422
1423 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1424 largest_node, largest, smallest, ctx);
1425 if (!ack->ack_range_num--)
1426 break;
1427
1428 if (!quic_dec_int(&gap, pos, end))
1429 goto err;
1430
1431 if (smallest < gap + 2) {
1432 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1433 ctx->conn,, &gap, &smallest);
1434 goto err;
1435 }
1436
1437 largest = smallest - gap - 2;
1438 if (!quic_dec_int(&ack_range, pos, end))
1439 goto err;
1440
1441 if (largest < ack_range) {
1442 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1443 ctx->conn,, &largest, &ack_range);
1444 goto err;
1445 }
1446
1447 /* Do not use this node anymore. */
1448 largest_node = NULL;
1449 /* Next range */
1450 smallest = largest - ack_range;
1451
1452 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1453 ctx->conn,, &largest, &smallest);
1454 } while (1);
1455
1456 /* Flag this packet number space as having received an ACK. */
1457 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_RECEIVED;
1458
1459 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1460 *rtt_sample = tick_remain(time_sent, now_ms);
1461 qel->pktns->tx.largest_acked_pn = ack->largest_ack;
1462 }
1463
1464 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1465 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1466 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1467 if (!LIST_ISEMPTY(&lost_pkts))
1468 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1469 }
1470 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1471 if (quic_peer_validated_addr(ctx))
1472 ctx->conn->qc->path->loss.pto_count = 0;
1473 qc_set_timer(ctx);
1474 }
1475
1476
1477 return 1;
1478
1479 err:
1480 free_quic_tx_pkts(&newly_acked_pkts);
1481 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1482 return 0;
1483}
1484
1485/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1486 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001487 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001488 * Return 1 if succeeded, 0 if not.
1489 */
1490static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001491 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001492 const unsigned char *data, size_t len,
1493 struct quic_rx_packet *pkt,
1494 struct quic_rx_crypto_frm *cf)
1495{
1496 int ssl_err;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001497 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001498
1499 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1500 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001501 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001502 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1503 TRACE_PROTO("SSL_provide_quic_data() error",
1504 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1505 goto err;
1506 }
1507
1508 el->rx.crypto.offset += len;
1509 TRACE_PROTO("in order CRYPTO data",
1510 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1511
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001512 if (qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001513 ssl_err = SSL_do_handshake(ctx->ssl);
1514 if (ssl_err != 1) {
1515 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1516 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1517 TRACE_PROTO("SSL handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001518 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001519 goto out;
1520 }
1521
1522 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001523 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001524 goto err;
1525 }
1526
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001527 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001528 if (objt_listener(ctx->conn->target))
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001529 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001530 else
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001531 qc->state = QUIC_HS_ST_COMPLETE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001532 } else {
1533 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1534 if (ssl_err != 1) {
1535 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1536 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1537 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001538 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001539 goto out;
1540 }
1541
1542 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001543 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001544 goto err;
1545 }
1546
1547 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001548 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001549 }
1550
1551 out:
1552 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1553 return 1;
1554
1555 err:
1556 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1557 return 0;
1558}
1559
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001560/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1561 * <pkt> RX packet.
1562 * Return it if succeeded, NULL if not.
1563 */
1564static inline
1565struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1566 struct quic_rx_packet *pkt)
1567{
1568 struct quic_rx_strm_frm *frm;
1569
1570 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1571 if (frm) {
1572 frm->offset_node.key = stream_frm->offset;
1573 frm->len = stream_frm->len;
1574 frm->data = stream_frm->data;
1575 frm->pkt = pkt;
1576 }
1577
1578 return frm;
1579}
1580
1581/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1582 * several streams, depending on the already open onces.
1583 * Return this node if succeeded, NULL if not.
1584 */
1585static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1586{
1587 unsigned int strm_type;
1588 int64_t sub_id;
1589 struct eb64_node *strm_node;
1590
1591 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1592
1593 strm_type = id & QCS_ID_TYPE_MASK;
1594 sub_id = id >> QCS_ID_TYPE_SHIFT;
1595 strm_node = NULL;
1596 if (qc_local_stream_id(qcc, id)) {
1597 /* Local streams: this stream must be already opened. */
1598 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1599 if (!strm_node) {
1600 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1601 goto out;
1602 }
1603 }
1604 else {
1605 /* Remote streams. */
1606 struct eb_root *strms;
1607 uint64_t largest_id;
1608 enum qcs_type qcs_type;
1609
1610 strms = &qcc->streams_by_id;
1611 qcs_type = qcs_id_type(id);
1612 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1613 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1614 goto out;
1615 }
1616
1617 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1618 * correct value.
1619 */
1620 largest_id = qcc->strms[qcs_type].largest_id;
1621 if (sub_id > (int64_t)largest_id) {
1622 /* RFC: "A stream ID that is used out of order results in all streams
1623 * of that type with lower-numbered stream IDs also being opened".
1624 * So, let's "open" these streams.
1625 */
1626 int64_t i;
1627 struct qcs *qcs;
1628
1629 qcs = NULL;
1630 for (i = largest_id + 1; i <= sub_id; i++) {
1631 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1632 if (!qcs) {
1633 TRACE_PROTO("Could not allocate a new stream",
1634 QUIC_EV_CONN_PSTRM, qcc->conn);
1635 goto out;
1636 }
1637
1638 qcc->strms[qcs_type].largest_id = i;
1639 }
1640 if (qcs)
1641 strm_node = &qcs->by_id;
1642 }
1643 else {
1644 strm_node = eb64_lookup(strms, id);
1645 }
1646 }
1647
1648 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1649 return strm_node;
1650
1651 out:
1652 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1653 return NULL;
1654}
1655
1656/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1657 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1658 * to reflect the data which have been consumed.
1659 */
1660static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1661{
1662 size_t ret;
1663
1664 ret = 0;
1665 while (strm_frm->len) {
1666 size_t try;
1667
1668 try = b_contig_space(buf);
1669 if (!try)
1670 break;
1671
1672 if (try > strm_frm->len)
1673 try = strm_frm->len;
1674 memcpy(b_tail(buf), strm_frm->data, try);
1675 strm_frm->len -= try;
1676 strm_frm->offset += try;
1677 b_add(buf, try);
1678 ret += try;
1679 }
1680
1681 return ret;
1682}
1683
1684/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1685 * streams may be open. The data are copied to the stream RX buffer if possible.
1686 * If not, the STREAM frame is stored to be treated again later.
1687 * We rely on the flow control so that not to store too much STREAM frames.
1688 * Return 1 if succeeded, 0 if not.
1689 */
1690static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1691 struct quic_stream *strm_frm,
1692 struct quic_conn *qc)
1693{
1694 struct qcs *strm;
1695 struct eb64_node *strm_node, *frm_node;
1696 struct quic_rx_strm_frm *frm;
1697
1698 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1699 if (!strm_node) {
1700 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1701 return 0;
1702 }
1703
1704 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1705 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1706 /* FIXME: handle the case where this frame overlap others */
1707 if (frm_node) {
1708 TRACE_PROTO("Already existing stream data",
1709 QUIC_EV_CONN_PSTRM, qc->conn);
1710 goto out;
1711 }
1712
1713 if (strm_frm->offset == strm->rx.offset) {
1714 int ret;
1715
1716 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1717 goto store_frm;
1718
1719 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1720 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1721 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1722 return 0;
1723 }
1724
1725 strm->rx.offset += ret;
1726 }
1727
1728 if (!strm_frm->len)
1729 goto out;
1730
1731 store_frm:
1732 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1733 if (!frm) {
1734 TRACE_PROTO("Could not alloc RX STREAM frame",
1735 QUIC_EV_CONN_PSTRM, qc->conn);
1736 return 0;
1737 }
1738
1739 eb64_insert(&strm->frms, &frm->offset_node);
1740 quic_rx_packet_refinc(pkt);
1741
1742 out:
1743 return 1;
1744}
1745
1746/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1747 * streams may be open. The data are copied to the stream RX buffer if possible.
1748 * If not, the STREAM frame is stored to be treated again later.
1749 * We rely on the flow control so that not to store too much STREAM frames.
1750 * Return 1 if succeeded, 0 if not.
1751 */
1752static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1753 struct quic_stream *strm_frm,
1754 struct quic_conn *qc)
1755{
1756 struct qcs *strm;
1757 struct eb64_node *strm_node, *frm_node;
1758 struct quic_rx_strm_frm *frm;
1759 size_t strm_frm_len;
1760
1761 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1762 if (!strm_node) {
1763 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1764 return 0;
1765 }
1766
1767 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1768 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1769 /* FIXME: handle the case where this frame overlap others */
1770 if (frm_node) {
1771 TRACE_PROTO("Already existing stream data",
1772 QUIC_EV_CONN_PSTRM, qc->conn);
1773 goto out;
1774 }
1775
1776 strm_frm_len = strm_frm->len;
1777 if (strm_frm->offset == strm->rx.offset) {
1778 int ret;
1779
1780 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1781 goto store_frm;
1782
1783 /* qc_strm_cpy() will modify the offset, depending on the number
1784 * of bytes copied.
1785 */
1786 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1787 /* Inform the application of the arrival of this new stream */
1788 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1789 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1790 return 0;
1791 }
1792
1793 if (ret)
1794 ruqs_notify_recv(strm);
1795
1796 strm_frm->offset += ret;
1797 }
1798 /* Take this frame into an account for the stream flow control */
1799 strm->rx.offset += strm_frm_len;
1800 /* It all the data were provided to the application, there is no need to
1801 * store any more inforamtion for it.
1802 */
1803 if (!strm_frm->len)
1804 goto out;
1805
1806 store_frm:
1807 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1808 if (!frm) {
1809 TRACE_PROTO("Could not alloc RX STREAM frame",
1810 QUIC_EV_CONN_PSTRM, qc->conn);
1811 return 0;
1812 }
1813
1814 eb64_insert(&strm->frms, &frm->offset_node);
1815 quic_rx_packet_refinc(pkt);
1816
1817 out:
1818 return 1;
1819}
1820
1821static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1822 struct quic_stream *strm_frm,
1823 struct quic_conn *qc)
1824{
1825 if (strm_frm->id & QCS_ID_DIR_BIT)
1826 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1827 else
1828 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1829}
1830
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001831/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1832 * as I/O handler context and <qel> as encryption level.
1833 * Returns 1 if succeeded, 0 if failed.
1834 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001835static 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 +01001836 struct quic_enc_level *qel)
1837{
1838 struct quic_frame frm;
1839 const unsigned char *pos, *end;
1840 struct quic_conn *conn = ctx->conn->qc;
1841
1842 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1843 /* Skip the AAD */
1844 pos = pkt->data + pkt->aad_len;
1845 end = pkt->data + pkt->len;
1846
1847 while (pos < end) {
1848 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1849 goto err;
1850
1851 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001852 case QUIC_FT_PADDING:
1853 if (pos != end) {
1854 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1855 goto err;
1856 }
1857 break;
1858 case QUIC_FT_PING:
1859 break;
1860 case QUIC_FT_ACK:
1861 {
1862 unsigned int rtt_sample;
1863
1864 rtt_sample = 0;
1865 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1866 goto err;
1867
1868 if (rtt_sample) {
1869 unsigned int ack_delay;
1870
1871 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1872 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1873 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1874 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001875 break;
1876 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001877 case QUIC_FT_CRYPTO:
1878 if (frm.crypto.offset != qel->rx.crypto.offset) {
1879 struct quic_rx_crypto_frm *cf;
1880
1881 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1882 if (!cf) {
1883 TRACE_DEVEL("CRYPTO frame allocation failed",
1884 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1885 goto err;
1886 }
1887
1888 cf->offset_node.key = frm.crypto.offset;
1889 cf->len = frm.crypto.len;
1890 cf->data = frm.crypto.data;
1891 cf->pkt = pkt;
1892 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1893 quic_rx_packet_refinc(pkt);
1894 }
1895 else {
1896 /* XXX TO DO: <cf> is used only for the traces. */
1897 struct quic_rx_crypto_frm cf = { };
1898
1899 cf.offset_node.key = frm.crypto.offset;
1900 cf.len = frm.crypto.len;
1901 if (!qc_provide_cdata(qel, ctx,
1902 frm.crypto.data, frm.crypto.len,
1903 pkt, &cf))
1904 goto err;
1905 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001906 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001907 case QUIC_FT_STREAM_8:
1908 case QUIC_FT_STREAM_9:
1909 case QUIC_FT_STREAM_A:
1910 case QUIC_FT_STREAM_B:
1911 case QUIC_FT_STREAM_C:
1912 case QUIC_FT_STREAM_D:
1913 case QUIC_FT_STREAM_E:
1914 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001915 {
1916 struct quic_stream *stream = &frm.stream;
1917
1918 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
1919 if (objt_listener(ctx->conn->target)) {
1920 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
1921 goto err;
1922 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
1923 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001924
1925 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
1926 goto err;
1927
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001928 break;
1929 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001930 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001931 break;
1932 case QUIC_FT_CONNECTION_CLOSE:
1933 case QUIC_FT_CONNECTION_CLOSE_APP:
1934 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001935 case QUIC_FT_HANDSHAKE_DONE:
1936 if (objt_listener(ctx->conn->target))
1937 goto err;
1938
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001939 conn->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001940 break;
1941 default:
1942 goto err;
1943 }
1944 }
1945
1946 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
1947 * has successfully parse a Handshake packet. The Initial encryption must also
1948 * be discarded.
1949 */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001950 if (conn->state == QUIC_HS_ST_SERVER_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001951 pkt->type == QUIC_PACKET_TYPE_HANDSHAKE) {
1952 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
1953 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
1954 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001955 conn->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001956 }
1957
1958 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1959 return 1;
1960
1961 err:
1962 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
1963 return 0;
1964}
1965
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001966/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
1967 * buffer. This is the responsability of the caller to check there is enough
1968 * room in <cbuf>. Also increase the <cbuf> write index consequently.
1969 * This function must be called only after having built a correct datagram.
1970 * Always succeeds.
1971 */
1972static inline void qc_set_dg(struct cbuf *cbuf,
1973 uint16_t dglen, struct quic_tx_packet *pkt)
1974{
1975 write_u16(cb_wr(cbuf), dglen);
1976 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
1977 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
1978}
1979
1980/* Prepare as much as possible handshake packets into <qr> ring buffer for
1981 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
1982 * several packets in the same datagram. A header made of two fields is added
1983 * to each datagram: the datagram length followed by the address of the first
1984 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001985 * Returns 1 if succeeded, or 0 if something wrong happened.
1986 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001987static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001988{
1989 struct quic_conn *qc;
1990 enum quic_tls_enc_level tel, next_tel;
1991 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001992 struct cbuf *cbuf;
1993 unsigned char *end_buf, *end, *pos, *spos;
1994 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
1995 /* length of datagrams */
1996 uint16_t dglen;
1997 size_t total;
1998 /* Each datagram is prepended with its length followed by the
1999 * address of the first packet in the datagram.
2000 */
2001 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002002
2003 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2004 qc = ctx->conn->qc;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002005 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc->state)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002006 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002007 goto err;
2008 }
2009
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002010 start:
2011 total = 0;
2012 dglen = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002013 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002014 cbuf = qr->cbuf;
2015 spos = pos = cb_wr(cbuf);
2016 /* Leave at least <dglen> bytes at the end of this buffer
2017 * to ensure there is enough room to mark the end of prepared
2018 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002019 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002020 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2021 first_pkt = prv_pkt = NULL;
2022 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
2023 int err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002024 enum quic_pkt_type pkt_type;
2025
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002026 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002027 /* Do not build any more packet if the TX secrets are not available or
2028 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002029 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002030 * and if there is no more CRYPTO data available or in flight
2031 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002032 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002033 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2034 (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002035 !qc->tx.nb_pto_dgrams &&
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002036 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002037 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002038 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002039 /* Set the current datagram as prepared into <cbuf> if
2040 * the was already a correct packet which was previously written.
2041 */
2042 if (prv_pkt)
2043 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002044 break;
2045 }
2046
2047 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002048 if (!prv_pkt) {
2049 /* Leave room for the datagram header */
2050 pos += dg_headlen;
2051 end = pos + qc->path->mtu;
2052 }
2053
2054 cur_pkt = qc_build_hdshk_pkt(&pos, end, qc, pkt_type, qel, &err);
2055 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002056 case -2:
2057 goto err;
2058 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002059 /* If there was already a correct packet present, set the
2060 * current datagram as prepared into <cbuf>.
2061 */
2062 if (prv_pkt) {
2063 qc_set_dg(cbuf, dglen, first_pkt);
2064 goto stop_build;
2065 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002066 goto out;
2067 default:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002068 total += cur_pkt->len;
2069 /* keep trace of the first packet in the datagram */
2070 if (!first_pkt)
2071 first_pkt = cur_pkt;
2072 /* Attach the current one to the previous one */
2073 if (prv_pkt)
2074 prv_pkt->next = cur_pkt;
2075 /* Let's say we have to build a new dgram */
2076 prv_pkt = NULL;
2077 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002078 /* Discard the Initial encryption keys as soon as
2079 * a handshake packet could be built.
2080 */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002081 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002082 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2083 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2084 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2085 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002086 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002087 }
2088 /* Special case for Initial packets: when they have all
2089 * been sent, select the next level.
2090 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002091 if (tel == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002092 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) || qc->els[next_tel].pktns->tx.in_flight)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002093 tel = next_tel;
2094 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002095 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002096 /* If there is data for the next level, do not
2097 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002098 * which sends only one Initial packet, then wait
2099 * for additional CRYPTO data from the server to enter the
2100 * next level.
2101 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002102 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002103 }
2104 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002105 }
2106
2107 /* If we have to build a new datagram, set the current datagram as
2108 * prepared into <cbuf>.
2109 */
2110 if (!prv_pkt) {
2111 qc_set_dg(cbuf, dglen, first_pkt);
2112 first_pkt = NULL;
2113 dglen = 0;
2114 }
2115 }
2116
2117 stop_build:
2118 /* Reset <wr> writer index if in front of <rd> index */
2119 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2120 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2121
2122 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2123 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2124 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2125 /* Mark the end of contiguous data for the reader */
2126 write_u16(cb_wr(cbuf), 0);
2127 cb_add(cbuf, sizeof(uint16_t));
2128 }
2129 }
2130
2131 if (rd && rd <= cbuf->wr) {
2132 cb_wr_reset(cbuf);
2133 if (pos == spos) {
2134 /* Reuse the same buffer if nothing was built. */
2135 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002136 }
2137 }
2138 }
2139
2140 out:
2141 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002142 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002143
2144 err:
2145 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002146 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002147}
2148
2149/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002150 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002151 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002152int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002153{
2154 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002155 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002156
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002157 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002158 cbuf = qr->cbuf;
2159 while (cb_contig_data(cbuf)) {
2160 unsigned char *pos;
2161 struct buffer tmpbuf = { };
2162 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2163 uint16_t dglen;
2164 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002165 unsigned int time_sent;
2166
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002167 pos = cb_rd(cbuf);
2168 dglen = read_u16(pos);
2169 /* End of prepared datagrams.
2170 * Reset the reader index only if in front of the writer index.
2171 */
2172 if (!dglen) {
2173 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2174
2175 if (wr && wr < cbuf->rd) {
2176 cb_rd_reset(cbuf);
2177 continue;
2178 }
2179 break;
2180 }
2181
2182 pos += sizeof dglen;
2183 first_pkt = read_ptr(pos);
2184 pos += sizeof first_pkt;
2185 tmpbuf.area = (char *)pos;
2186 tmpbuf.size = tmpbuf.data = dglen;
2187
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002188 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002189 for (pkt = first_pkt; pkt; pkt = pkt->next)
2190 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002191 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002192 &tmpbuf, tmpbuf.data, 0) <= 0) {
2193 for (pkt = first_pkt; pkt; pkt = pkt->next)
2194 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002195 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002196 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002197
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002198 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002199 qc->tx.bytes += tmpbuf.data;
2200 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002201
2202 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2203 pkt->time_sent = time_sent;
2204 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2205 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002206 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002207 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002208 qc->path->in_flight += pkt->in_flight_len;
2209 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2210 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002211 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002212 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2213 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002214 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002215 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002216 }
2217 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002218
2219 return 1;
2220}
2221
2222/* Build all the frames which must be sent just after the handshake have succeeded.
2223 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2224 * a HANDSHAKE_DONE frame.
2225 * Return 1 if succeeded, 0 if not.
2226 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002227static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002228{
2229 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002230 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002231 struct quic_frame *frm;
2232
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002233 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002234 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002235 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002236 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002237 if (!frm)
2238 return 0;
2239
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002240 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002241 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002242 }
2243
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002244 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002245 struct quic_connection_id *cid;
2246
2247 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002248 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002249 if (!frm || !cid)
2250 goto err;
2251
2252 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002253 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002254 }
2255
2256 return 1;
2257
2258 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002259 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002260 return 0;
2261}
2262
2263/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002264void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002265{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002266 struct eb64_node *n;
2267 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002268
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002269 n = eb64_first(&arngs->root);
2270 while (n) {
2271 struct eb64_node *next;
2272
2273 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2274 next = eb64_next(n);
2275 eb64_delete(n);
2276 free(ar);
2277 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002278 }
2279}
2280
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002281/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2282 * descending order.
2283 */
2284static inline size_t sack_gap(struct quic_arng_node *p,
2285 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002286{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002287 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002288}
2289
2290
2291/* Remove the last elements of <ack_ranges> list of ack range updating its
2292 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002293 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002294 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002295static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
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 *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002298
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002299 last = eb64_last(&arngs->root);
2300 while (last && arngs->enc_sz > limit) {
2301 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002302
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002303 prev = eb64_prev(last);
2304 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002305 return 0;
2306
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002307 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2308 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2309 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2310 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2311 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2312 --arngs->sz;
2313 eb64_delete(last);
2314 pool_free(pool_head_quic_arng, last);
2315 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002316 }
2317
2318 return 1;
2319}
2320
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002321/* Set the encoded size of <arngs> QUIC ack ranges. */
2322static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2323{
2324 struct eb64_node *node, *next;
2325 struct quic_arng_node *ar, *ar_next;
2326
2327 node = eb64_last(&arngs->root);
2328 if (!node)
2329 return;
2330
2331 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2332 arngs->enc_sz = quic_int_getsize(ar->last) +
2333 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2334
2335 while ((next = eb64_prev(node))) {
2336 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2337 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2338 quic_int_getsize(ar_next->last - ar_next->first.key);
2339 node = next;
2340 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2341 }
2342}
2343
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002344/* Insert <ar> ack range into <argns> tree of ack ranges.
2345 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002346 */
2347static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002348struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2349 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002350{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002351 struct quic_arng_node *new_ar;
2352
2353 new_ar = pool_alloc(pool_head_quic_arng);
2354 if (new_ar) {
2355 new_ar->first.key = ar->first;
2356 new_ar->last = ar->last;
2357 eb64_insert(&arngs->root, &new_ar->first);
2358 arngs->sz++;
2359 }
2360
2361 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002362}
2363
2364/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002365 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002366 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002367 *
2368 * Descending order
2369 * ------------->
2370 * range1 range2
2371 * ..........|--------|..............|--------|
2372 * ^ ^ ^ ^
2373 * | | | |
2374 * last1 first1 last2 first2
2375 * ..........+--------+--------------+--------+......
2376 * diff1 gap12 diff2
2377 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002378 * To encode the previous list of ranges we must encode integers as follows in
2379 * descending order:
2380 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002381 * with diff1 = last1 - first1
2382 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002383 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002384 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002385 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002386int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2387 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002388{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002389 struct eb64_node *le;
2390 struct quic_arng_node *new_node;
2391 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002392
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002393 new = NULL;
2394 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002395 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002396 if (!new_node)
2397 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002398
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002399 goto out;
2400 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002401
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002402 le = eb64_lookup_le(&arngs->root, ar->first);
2403 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002404 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002405 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002406 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002407 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002408 else {
2409 struct quic_arng_node *le_ar =
2410 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002411
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002412 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002413 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002414 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002415
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002416 if (le_ar->last + 1 >= ar->first) {
2417 le_ar->last = ar->last;
2418 new = le;
2419 new_node = le_ar;
2420 }
2421 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002422 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002423 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002424 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002425
2426 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002427 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002428 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002429
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002430 /* Verify that the new inserted node does not overlap the nodes
2431 * which follow it.
2432 */
2433 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002434 struct eb64_node *next;
2435 struct quic_arng_node *next_node;
2436
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002437 while ((next = eb64_next(new))) {
2438 next_node =
2439 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002440 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002441 break;
2442
2443 if (next_node->last > new_node->last)
2444 new_node->last = next_node->last;
2445 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002446 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002447 /* Decrement the size of these ranges. */
2448 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002449 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002450 }
2451
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002452 quic_arngs_set_enc_sz(arngs);
2453
2454 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002455 return 1;
2456}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002457/* Remove the header protection of packets at <el> encryption level.
2458 * Always succeeds.
2459 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002460static 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 +01002461{
2462 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002463 struct quic_rx_packet *pqpkt;
2464 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002465 struct quic_enc_level *app_qel;
2466
2467 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2468 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2469 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002470 if (el == app_qel && objt_listener(ctx->conn->target) &&
2471 ctx->conn->qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002472 TRACE_PROTO("hp not removed (handshake not completed)",
2473 QUIC_EV_CONN_ELRMHP, ctx->conn);
2474 goto out;
2475 }
2476 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002477 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002478 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2479 pqpkt->data + pqpkt->pn_offset,
2480 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2481 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2482 /* XXX TO DO XXX */
2483 }
2484 else {
2485 /* The AAD includes the packet number field */
2486 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2487 /* Store the packet into the tree of packets to decrypt. */
2488 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002489 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002490 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002491 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002492 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2493 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002494 MT_LIST_DELETE_SAFE(pkttmp1);
2495 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002496 }
2497
2498 out:
2499 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2500}
2501
2502/* Process all the CRYPTO frame at <el> encryption level.
2503 * Return 1 if succeeded, 0 if not.
2504 */
2505static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002506 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002507{
2508 struct eb64_node *node;
2509
2510 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002511 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002512 node = eb64_first(&el->rx.crypto.frms);
2513 while (node) {
2514 struct quic_rx_crypto_frm *cf;
2515
2516 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2517 if (cf->offset_node.key != el->rx.crypto.offset)
2518 break;
2519
2520 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2521 goto err;
2522
2523 node = eb64_next(node);
2524 quic_rx_packet_refdec(cf->pkt);
2525 eb64_delete(&cf->offset_node);
2526 pool_free(pool_head_quic_rx_crypto_frm, cf);
2527 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002528 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002529 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2530 return 1;
2531
2532 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002533 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002534 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2535 return 0;
2536}
2537
2538/* Process all the packets at <el> encryption level.
2539 * Return 1 if succeeded, 0 if not.
2540 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002541int qc_treat_rx_pkts(struct quic_enc_level *el, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002542{
2543 struct quic_tls_ctx *tls_ctx;
2544 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002545 int64_t largest_pn = -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002546
2547 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2548 tls_ctx = &el->tls_ctx;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002549 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002550 node = eb64_first(&el->rx.pkts);
2551 while (node) {
2552 struct quic_rx_packet *pkt;
2553
2554 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
2555 if (!qc_pkt_decrypt(pkt, tls_ctx)) {
2556 /* Drop the packet */
2557 TRACE_PROTO("packet decryption failed -> dropped",
2558 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2559 }
2560 else {
Frédéric Lécaillec4b93ea2021-06-04 10:12:43 +02002561 if (!qc_parse_pkt_frms(pkt, ctx, el)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002562 /* Drop the packet */
2563 TRACE_PROTO("packet parsing failed -> dropped",
2564 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2565 }
2566 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002567 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2568
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002569 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
2570 !(HA_ATOMIC_ADD_FETCH(&el->pktns->rx.nb_ack_eliciting, 1) & 1))
2571 HA_ATOMIC_OR(&el->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002572
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002573 if (pkt->pn > largest_pn)
2574 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002575 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille654c6912021-06-04 10:27:23 +02002576 if (!quic_update_ack_ranges_list(&el->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002577 TRACE_DEVEL("Could not update ack range list",
2578 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002579 }
2580 }
2581 node = eb64_next(node);
2582 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002583 }
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002584 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002585
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002586 /* Update the largest packet number. */
2587 if (largest_pn != -1)
2588 HA_ATOMIC_UPDATE_MAX(&el->pktns->rx.largest_pn, largest_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002589 if (!qc_treat_rx_crypto_frms(el, ctx))
2590 goto err;
2591
2592 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2593 return 1;
2594
2595 err:
2596 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2597 return 0;
2598}
2599
2600/* Called during handshakes to parse and build Initial and Handshake packets for QUIC
2601 * connections with <ctx> as I/O handler context.
2602 * Returns 1 if succeeded, 0 if not.
2603 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002604int qc_do_hdshk(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002605{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002606 int ret, ssl_err;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002607 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002608 enum quic_tls_enc_level tel, next_tel;
2609 struct quic_enc_level *qel, *next_qel;
2610 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002611 struct qring *qr; // Tx ring
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002612
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002613 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002614 qr = NULL;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002615 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002616 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002617 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc->state))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002618 goto err;
2619
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002620 qel = &qc->els[tel];
2621 next_qel = &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002622
2623 next_level:
2624 tls_ctx = &qel->tls_ctx;
2625
2626 /* If the header protection key for this level has been derived,
2627 * remove the packet header protections.
2628 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002629 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002630 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2631 qc_rm_hp_pkts(qel, ctx);
2632
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002633 if (!qc_treat_rx_pkts(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002634 goto err;
2635
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002636 if (!qr)
2637 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2638 ret = qc_prep_hdshk_pkts(qr, ctx);
2639 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002640 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002641 else if (ret == 0)
2642 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002643
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002644 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002645 goto err;
2646
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002647 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002648 /* Check if there is something to do for the next level.
2649 */
2650 if ((next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002651 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002652 qel = next_qel;
2653 goto next_level;
2654 }
2655
2656 /* If the handshake has not been completed -> out! */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002657 if (qc->state < QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002658 goto out;
2659
2660 /* Discard the Handshake keys. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002661 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2662 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002663 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002664 if (!quic_build_post_handshake_frames(qc) ||
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002665 !qc_prep_phdshk_pkts(qr, qc) ||
2666 !qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002667 goto err;
2668
2669 out:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002670 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002671 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002672 return 1;
2673
2674 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002675 if (qr)
2676 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002677 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 +01002678 return 0;
2679}
2680
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002681/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002682static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2683{
2684 int i;
2685
2686 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2687 if (qel->tx.crypto.bufs[i]) {
2688 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2689 qel->tx.crypto.bufs[i] = NULL;
2690 }
2691 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002692 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002693}
2694
2695/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002696 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002697 * Returns 1 if succeeded, 0 if not.
2698 */
2699static int quic_conn_enc_level_init(struct quic_conn *qc,
2700 enum quic_tls_enc_level level)
2701{
2702 struct quic_enc_level *qel;
2703
2704 qel = &qc->els[level];
2705 qel->level = quic_to_ssl_enc_level(level);
2706 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2707 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2708 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2709 qel->tls_ctx.rx.flags = 0;
2710 qel->tls_ctx.tx.flags = 0;
2711
2712 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002713 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002714 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002715 qel->rx.crypto.offset = 0;
2716 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2717 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002718
2719 /* Allocate only one buffer. */
2720 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2721 if (!qel->tx.crypto.bufs)
2722 goto err;
2723
2724 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2725 if (!qel->tx.crypto.bufs[0])
2726 goto err;
2727
2728 qel->tx.crypto.bufs[0]->sz = 0;
2729 qel->tx.crypto.nb_buf = 1;
2730
2731 qel->tx.crypto.sz = 0;
2732 qel->tx.crypto.offset = 0;
2733
2734 return 1;
2735
2736 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002737 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002738 return 0;
2739}
2740
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002741/* Release all the memory allocated for <conn> QUIC connection. */
2742static void quic_conn_free(struct quic_conn *conn)
2743{
2744 int i;
2745
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002746 if (!conn)
2747 return;
2748
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002749 free_quic_conn_cids(conn);
2750 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2751 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002752 if (conn->timer_task)
2753 task_destroy(conn->timer_task);
2754 pool_free(pool_head_quic_conn, conn);
2755}
2756
2757/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002758static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002759{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002760 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002761 struct quic_conn *qc;
2762 struct quic_pktns *pktns;
2763
2764
2765 conn_ctx = task->context;
2766 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002767 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2768 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002769 task->expire = TICK_ETERNITY;
2770 pktns = quic_loss_pktns(qc);
2771 if (tick_isset(pktns->tx.loss_time)) {
2772 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2773
2774 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2775 if (!LIST_ISEMPTY(&lost_pkts))
2776 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2777 qc_set_timer(conn_ctx);
2778 goto out;
2779 }
2780
2781 if (qc->path->in_flight) {
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002782 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002783 pktns->tx.pto_probe = 1;
2784 }
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002785 else if (objt_server(qc->conn->target) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002786 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2787 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2788
2789 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2790 hel->pktns->tx.pto_probe = 1;
2791 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2792 iel->pktns->tx.pto_probe = 1;
2793 }
2794 qc->tx.nb_pto_dgrams = QUIC_MAX_NB_PTO_DGRAMS;
2795 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2796 qc->path->loss.pto_count++;
2797
2798 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002799 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002800
2801 return task;
2802}
2803
2804/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2805 * connections used to identify the first Initial packets of client connecting
2806 * to listeners. This parameter must be NULL for QUIC connections attached
2807 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2808 * <scid> is the source connection ID with <scid_len> as length.
2809 * Returns 1 if succeeded, 0 if not.
2810 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002811static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2812 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002813 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002814{
2815 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002816 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002817 /* Initial CID. */
2818 struct quic_connection_id *icid;
2819
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002820 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002821 qc = pool_zalloc(pool_head_quic_conn);
2822 if (!qc) {
2823 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2824 goto err;
2825 }
2826
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002827 qc->cids = EB_ROOT;
2828 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002829 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002830 struct listener *l = owner;
2831
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002832 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002833 /* Copy the initial DCID. */
2834 qc->odcid.len = dcid_len;
2835 if (qc->odcid.len)
2836 memcpy(qc->odcid.data, dcid, dcid_len);
2837
2838 /* Copy the SCID as our DCID for this connection. */
2839 if (scid_len)
2840 memcpy(qc->dcid.data, scid, scid_len);
2841 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002842 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002843 }
2844 /* QUIC Client (outgoing connection to servers) */
2845 else {
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002846 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002847 if (dcid_len)
2848 memcpy(qc->dcid.data, dcid, dcid_len);
2849 qc->dcid.len = dcid_len;
2850 }
2851
2852 /* Initialize the output buffer */
2853 qc->obuf.pos = qc->obuf.data;
2854
2855 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002856 if (!icid) {
2857 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
2858 goto err;
2859 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002860
2861 /* Select our SCID which is the first CID with 0 as sequence number. */
2862 qc->scid = icid->cid;
2863
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002864 /* Packet number spaces initialization. */
2865 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2866 quic_pktns_init(&qc->pktns[i]);
2867 /* QUIC encryption level context initialization. */
2868 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002869 if (!quic_conn_enc_level_init(qc, i)) {
2870 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002871 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002872 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002873 /* Initialize the packet number space. */
2874 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2875 }
2876
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02002877 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002878 /* TX part. */
2879 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002880 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2881 qc->tx.wbuf = qc->tx.rbuf = 0;
2882 qc->tx.bytes = 0;
2883 qc->tx.nb_pto_dgrams = 0;
2884 /* RX part. */
2885 qc->rx.bytes = 0;
2886
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002887 /* XXX TO DO: Only one path at this time. */
2888 qc->path = &qc->paths[0];
2889 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2890
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002891 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002892
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002893 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002894
2895 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002896 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002897 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002898 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002899}
2900
2901/* Initialize the timer task of <qc> QUIC connection.
2902 * Returns 1 if succeeded, 0 if not.
2903 */
2904static int quic_conn_init_timer(struct quic_conn *qc)
2905{
2906 qc->timer_task = task_new(MAX_THREADS_MASK);
2907 if (!qc->timer_task)
2908 return 0;
2909
2910 qc->timer = TICK_ETERNITY;
2911 qc->timer_task->process = process_timer;
2912 qc->timer_task->context = qc->conn->xprt_ctx;
2913
2914 return 1;
2915}
2916
2917/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
2918 * past one byte of this buffer.
2919 */
2920static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
2921 struct quic_rx_packet *pkt)
2922{
2923 unsigned char dcid_len, scid_len;
2924
2925 /* Version */
2926 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
2927 return 0;
2928
2929 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
2930
2931 /* Destination Connection ID Length */
2932 dcid_len = *(*buf)++;
2933 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
2934 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
2935 /* XXX MUST BE DROPPED */
2936 return 0;
2937
2938 if (dcid_len) {
2939 /* Check that the length of this received DCID matches the CID lengths
2940 * of our implementation for non Initials packets only.
2941 */
2942 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
2943 return 0;
2944
2945 memcpy(pkt->dcid.data, *buf, dcid_len);
2946 }
2947
2948 pkt->dcid.len = dcid_len;
2949 *buf += dcid_len;
2950
2951 /* Source Connection ID Length */
2952 scid_len = *(*buf)++;
2953 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
2954 /* XXX MUST BE DROPPED */
2955 return 0;
2956
2957 if (scid_len)
2958 memcpy(pkt->scid.data, *buf, scid_len);
2959 pkt->scid.len = scid_len;
2960 *buf += scid_len;
2961
2962 return 1;
2963}
2964
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002965/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
2966 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
2967 * encryption level matching with the packet type. <*qel> may be null if not found.
2968 * Note that <ctx> may be null (for Initial packets).
2969 */
2970static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002971 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002972 struct quic_enc_level **qel)
2973{
2974 enum quic_tls_enc_level tel;
2975
2976 /* Special case without connection context (firt Initial packets) */
2977 if (!ctx) {
2978 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2979 return 1;
2980 }
2981
2982 tel = quic_packet_type_enc_level(pkt->type);
2983 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
2984 *qel = NULL;
2985 return 0;
2986 }
2987
2988 *qel = &qc->els[tel];
2989 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
2990 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
2991 return 0;
2992 }
2993
2994 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002995 (tel != QUIC_TLS_ENC_LEVEL_APP || ctx->conn->qc->state >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002996 return 1;
2997
2998 return 0;
2999}
3000
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003001/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3002 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3003 * byte past the end of the buffer containing this packet and <beg> the address of
3004 * the packet first byte.
3005 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3006 * Returns 1 if succeeded, 0 if not.
3007 */
3008static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3009 unsigned char **buf, unsigned char *beg,
3010 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003011 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003012{
3013 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003014 struct quic_enc_level *qel;
3015 /* Only for traces. */
3016 struct quic_rx_packet *qpkt_trace;
3017
3018 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003019 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003020 /* The packet number is here. This is also the start minus
3021 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3022 * protection.
3023 */
3024 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003025 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003026 /* Note that the following function enables us to unprotect the packet
3027 * number and its length subsequently used to decrypt the entire
3028 * packets.
3029 */
3030 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3031 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003032 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003033 goto err;
3034 }
3035
3036 /* The AAD includes the packet number field found at <pn>. */
3037 pkt->aad_len = pn - beg + pkt->pnl;
3038 qpkt_trace = pkt;
3039 /* Store the packet */
3040 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003041 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003042 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003043 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003044 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003045 else if (qel) {
3046 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003047 pkt->pn_offset = pn - beg;
3048 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3049 }
3050
3051 memcpy(pkt->data, beg, pkt->len);
3052 /* Updtate the offset of <*buf> for the next QUIC packet. */
3053 *buf = beg + pkt->len;
3054
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003055 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003056 return 1;
3057
3058 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003059 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 +01003060 return 0;
3061}
3062
3063/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3064 * Also set <*long_header> to 1 if this form is long, 0 if not.
3065 */
3066static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3067 unsigned char byte0, int *long_header)
3068{
3069 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3070 pkt->type =
3071 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3072 *long_header = 1;
3073 }
3074 else {
3075 pkt->type = QUIC_PACKET_TYPE_SHORT;
3076 *long_header = 0;
3077 }
3078}
3079
3080static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3081 struct quic_rx_packet *pkt,
3082 struct quic_dgram_ctx *dgram_ctx,
3083 struct sockaddr_storage *saddr)
3084{
3085 unsigned char *beg;
3086 uint64_t len;
3087 struct quic_conn *qc;
3088 struct eb_root *cids;
3089 struct ebmb_node *node;
3090 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003091 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003092 int long_header;
3093
3094 qc = NULL;
3095 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3096 if (end <= *buf)
3097 goto err;
3098
3099 /* Fixed bit */
3100 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3101 /* XXX TO BE DISCARDED */
3102 goto err;
3103
3104 srv_conn = dgram_ctx->owner;
3105 beg = *buf;
3106 /* Header form */
3107 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3108 if (long_header) {
3109 size_t cid_lookup_len;
3110
3111 if (!quic_packet_read_long_header(buf, end, pkt))
3112 goto err;
3113
3114 /* For Initial packets, and for servers (QUIC clients connections),
3115 * there is no Initial connection IDs storage.
3116 */
3117 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3118 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3119 cid_lookup_len = pkt->dcid.len;
3120 }
3121 else {
3122 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3123 cid_lookup_len = QUIC_CID_LEN;
3124 }
3125
3126 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3127 if (!node)
3128 goto err;
3129
3130 qc = ebmb_entry(node, struct quic_conn, scid_node);
3131
3132 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3133 qc->dcid.len = pkt->scid.len;
3134 if (pkt->scid.len)
3135 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3136 }
3137
3138 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3139 uint64_t token_len;
3140
3141 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3142 goto err;
3143
3144 /* XXX TO DO XXX 0 value means "the token is not present".
3145 * A server which sends an Initial packet must not set the token.
3146 * So, a client which receives an Initial packet with a token
3147 * MUST discard the packet or generate a connection error with
3148 * PROTOCOL_VIOLATION as type.
3149 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3150 */
3151 pkt->token_len = token_len;
3152 }
3153 }
3154 else {
3155 /* XXX TO DO: Short header XXX */
3156 if (end - *buf < QUIC_CID_LEN)
3157 goto err;
3158
3159 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3160 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3161 if (!node)
3162 goto err;
3163
3164 qc = ebmb_entry(node, struct quic_conn, scid_node);
3165 *buf += QUIC_CID_LEN;
3166 }
3167 /* Store the DCID used for this packet to check the packet which
3168 * come in this UDP datagram match with it.
3169 */
3170 if (!dgram_ctx->dcid_node)
3171 dgram_ctx->dcid_node = node;
3172 /* Only packets packets with long headers and not RETRY or VERSION as type
3173 * have a length field.
3174 */
3175 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3176 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3177 goto err;
3178
3179 pkt->len = len;
3180 }
3181 else if (!long_header) {
3182 /* A short packet is the last one of an UDP datagram. */
3183 pkt->len = end - *buf;
3184 }
3185
3186 conn_ctx = qc->conn->xprt_ctx;
3187
3188 /* Increase the total length of this packet by the header length. */
3189 pkt->len += *buf - beg;
3190 /* Do not check the DCID node before the length. */
3191 if (dgram_ctx->dcid_node != node) {
3192 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3193 goto err;
3194 }
3195
3196 if (pkt->len > sizeof pkt->data) {
3197 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3198 goto err;
3199 }
3200
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003201 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003202 goto err;
3203
3204 /* Wake the tasklet of the QUIC connection packet handler. */
3205 if (conn_ctx)
3206 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3207
3208 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3209
3210 return pkt->len;
3211
3212 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003213 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003214 return -1;
3215}
3216
3217static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3218 struct quic_rx_packet *pkt,
3219 struct quic_dgram_ctx *dgram_ctx,
3220 struct sockaddr_storage *saddr)
3221{
3222 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003223 struct quic_conn *qc;
3224 struct eb_root *cids;
3225 struct ebmb_node *node;
3226 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003227 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003228 int long_header = 0;
3229
3230 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003231 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003232 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003233 if (end <= *buf)
3234 goto err;
3235
3236 /* Fixed bit */
3237 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3238 /* XXX TO BE DISCARDED */
3239 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3240 goto err;
3241 }
3242
3243 l = dgram_ctx->owner;
3244 beg = *buf;
3245 /* Header form */
3246 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3247 if (long_header) {
3248 unsigned char dcid_len;
3249
3250 if (!quic_packet_read_long_header(buf, end, pkt)) {
3251 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3252 goto err;
3253 }
3254
3255 dcid_len = pkt->dcid.len;
3256 /* For Initial packets, and for servers (QUIC clients connections),
3257 * there is no Initial connection IDs storage.
3258 */
3259 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003260 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003261 /* DCIDs of first packets coming from clients may have the same values.
3262 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3263 */
3264 quic_cid_saddr_cat(&pkt->dcid, saddr);
3265 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003266
3267 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3268 end - *buf < token_len) {
3269 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3270 goto err;
3271 }
3272
3273 /* XXX TO DO XXX 0 value means "the token is not present".
3274 * A server which sends an Initial packet must not set the token.
3275 * So, a client which receives an Initial packet with a token
3276 * MUST discard the packet or generate a connection error with
3277 * PROTOCOL_VIOLATION as type.
3278 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3279 */
3280 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003281 }
3282 else {
3283 if (pkt->dcid.len != QUIC_CID_LEN) {
3284 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3285 goto err;
3286 }
3287
3288 cids = &l->rx.cids;
3289 }
3290
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003291 /* Only packets packets with long headers and not RETRY or VERSION as type
3292 * have a length field.
3293 */
3294 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3295 uint64_t len;
3296
3297 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3298 end - *buf < len) {
3299 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3300 goto err;
3301 }
3302
3303 pkt->len = len;
3304 }
3305
3306
3307 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003308 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3309 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3310 cids == &l->rx.odcids) {
3311 /* Switch to the definitive tree ->cids containing the final CIDs. */
3312 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3313 if (node) {
3314 /* If found, signal this with NULL as special value for <cids>. */
3315 pkt->dcid.len = dcid_len;
3316 cids = NULL;
3317 }
3318 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003319 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003320
3321 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003322 int ipv4;
3323 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003324 struct ebmb_node *n = NULL;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003325
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003326 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3327 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3328 goto err;
3329 }
3330
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003331 pkt->saddr = *saddr;
3332 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3333 * of <saddr>.
3334 */
3335 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003336 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003337 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003338 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003339 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003340 goto err;
3341
3342 odcid = &qc->rx.params.original_destination_connection_id;
3343 /* Copy the transport parameters. */
3344 qc->rx.params = l->bind_conf->quic_params;
3345 /* Copy original_destination_connection_id transport parameter. */
3346 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3347 odcid->len = pkt->odcid_len;
3348 /* Copy the initial source connection ID. */
3349 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3350 qc->enc_params_len =
3351 quic_transport_params_encode(qc->enc_params,
3352 qc->enc_params + sizeof qc->enc_params,
3353 &qc->rx.params, 1);
3354 if (!qc->enc_params_len)
3355 goto err;
3356
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003357 /* NOTE: the socket address has been concatenated to the destination ID
3358 * chosen by the client for Initial packets.
3359 */
3360 if (!qc_new_isecs(qc, pkt->dcid.data, pkt->odcid_len, 1)) {
3361 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3362 goto err;
3363 }
3364
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003365 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003366 /* This is the DCID node sent in this packet by the client. */
3367 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003368 /* Enqueue this packet. */
3369 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3370 /* Try to accept a new connection. */
3371 listener_accept(l);
3372
3373 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3374 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3375 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3376 /* Insert our SCID, the connection ID for the QUIC client. */
3377 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3378 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3379 if (n != &qc->scid_node) {
3380 quic_conn_free(qc);
3381 qc = ebmb_entry(n, struct quic_conn, scid_node);
3382 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003383 }
3384 else {
3385 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3386 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3387 else
3388 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003389 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003390 }
3391 }
3392 else {
3393 if (end - *buf < QUIC_CID_LEN) {
3394 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3395 goto err;
3396 }
3397
3398 cids = &l->rx.cids;
3399 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3400 if (!node) {
3401 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3402 goto err;
3403 }
3404
3405 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003406 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003407 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003408 /* A short packet is the last one of an UDP datagram. */
3409 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003410 }
3411
3412 /* Store the DCID used for this packet to check the packet which
3413 * come in this UDP datagram match with it.
3414 */
3415 if (!dgram_ctx->dcid_node) {
3416 dgram_ctx->dcid_node = node;
3417 dgram_ctx->qc = qc;
3418 }
3419
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003420 /* Increase the total length of this packet by the header length. */
3421 pkt->len += *buf - beg;
3422 /* Do not check the DCID node before the length. */
3423 if (dgram_ctx->dcid_node != node) {
3424 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3425 goto err;
3426 }
3427
3428 if (pkt->len > sizeof pkt->data) {
3429 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3430 goto err;
3431 }
3432
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003433 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3434 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3435 goto err;
3436 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003437
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003438
3439 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003440 /* Wake up the connection packet handler task from here only if all
3441 * the contexts have been initialized, especially the mux context
3442 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3443 * will start it if these contexts for the connection are not already
3444 * initialized.
3445 */
3446 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003447 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003448
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003449 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3450
3451 return pkt->len;
3452
3453 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003454 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003455 qc ? qc->conn : NULL, pkt);
3456 return -1;
3457}
3458
3459/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3460 * in advance. This is the reponsability of the caller to check there is enough room in this
3461 * buffer to build a long header.
3462 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3463 */
3464static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3465 int type, size_t pn_len, struct quic_conn *conn)
3466{
3467 if (type > QUIC_PACKET_TYPE_RETRY)
3468 return 0;
3469
3470 /* #0 byte flags */
3471 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3472 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3473 /* Version */
3474 quic_write_uint32(buf, end, conn->version);
3475 *(*buf)++ = conn->dcid.len;
3476 /* Destination connection ID */
3477 if (conn->dcid.len) {
3478 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3479 *buf += conn->dcid.len;
3480 }
3481 /* Source connection ID */
3482 *(*buf)++ = conn->scid.len;
3483 if (conn->scid.len) {
3484 memcpy(*buf, conn->scid.data, conn->scid.len);
3485 *buf += conn->scid.len;
3486 }
3487
3488 return 1;
3489}
3490
3491/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3492 * in advance. This is the reponsability of the caller to check there is enough room in this
3493 * buffer to build a long header.
3494 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3495 */
3496static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3497 size_t pn_len, struct quic_conn *conn)
3498{
3499 /* #0 byte flags */
3500 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3501 /* Destination connection ID */
3502 if (conn->dcid.len) {
3503 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3504 *buf += conn->dcid.len;
3505 }
3506
3507 return 1;
3508}
3509
3510/* Apply QUIC header protection to the packet with <buf> as first byte address,
3511 * <pn> as address of the Packet number field, <pnlen> being this field length
3512 * with <aead> as AEAD cipher and <key> as secret key.
3513 * Returns 1 if succeeded or 0 if failed.
3514 */
3515static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3516 const EVP_CIPHER *aead, const unsigned char *key)
3517{
3518 int i, ret, outlen;
3519 EVP_CIPHER_CTX *ctx;
3520 /* We need an IV of at least 5 bytes: one byte for bytes #0
3521 * and at most 4 bytes for the packet number
3522 */
3523 unsigned char mask[5] = {0};
3524
3525 ret = 0;
3526 ctx = EVP_CIPHER_CTX_new();
3527 if (!ctx)
3528 return 0;
3529
3530 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3531 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3532 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3533 goto out;
3534
3535 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3536 for (i = 0; i < pnlen; i++)
3537 pn[i] ^= mask[i + 1];
3538
3539 ret = 1;
3540
3541 out:
3542 EVP_CIPHER_CTX_free(ctx);
3543
3544 return ret;
3545}
3546
3547/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3548 * ACK ranges if needed to a value below <limit> in bytes.
3549 * Return 1 if succeeded, 0 if not.
3550 */
3551static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3552{
3553 size_t room, ack_delay_sz;
3554
3555 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3556 /* A frame is made of 1 byte for the frame type. */
3557 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003558 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003559 return 0;
3560
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003561 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003562}
3563
3564/* Prepare as most as possible CRYPTO frames from prebuilt CRYPTO frames for <qel>
3565 * encryption level to be encoded in a buffer with <room> as available room,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003566 * and <*len> the packet Length field initialized with the number of bytes already present
3567 * in this buffer which must be taken into an account for the Length packet field value.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003568 * <headlen> is the number of bytes already present in this packet before building
Frédéric Lécailleea604992020-12-24 13:01:37 +01003569 * CRYPTO frames.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003570 * This is the responsibility of the caller to check that <*len> < <room> as this is
3571 * the responsibility to check that <headlen> < quic_path_prep_data(conn->path).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003572 * Update consequently <*len> to reflect the size of these CRYPTO frames built
3573 * by this function. Also attach these CRYPTO frames to <pkt> QUIC packet.
3574 * Return 1 if succeeded, 0 if not.
3575 */
3576static inline int qc_build_cfrms(struct quic_tx_packet *pkt,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003577 size_t room, size_t *len, size_t headlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003578 struct quic_enc_level *qel,
3579 struct quic_conn *conn)
3580{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003581 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003582 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003583 struct mt_list *tmp1, tmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003584
Frédéric Lécailleea604992020-12-24 13:01:37 +01003585 ret = 0;
3586 /* If we are not probing we must take into an account the congestion
3587 * control window.
3588 */
3589 if (!conn->tx.nb_pto_dgrams)
3590 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
3591 TRACE_PROTO("************** CRYPTO frames build (headlen)",
3592 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003593 mt_list_for_each_entry_safe(cf, &qel->pktns->tx.frms, mt_list, tmp1, tmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003594 /* header length, data length, frame length. */
3595 size_t hlen, dlen, cflen;
3596
Frédéric Lécailleea604992020-12-24 13:01:37 +01003597 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3598 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3599 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003600 break;
3601
3602 /* Compute the length of this CRYPTO frame header */
3603 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3604 /* Compute the data length of this CRyPTO frame. */
3605 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécailleea604992020-12-24 13:01:37 +01003606 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3607 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003608 if (!dlen)
3609 break;
3610
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003611 pkt->cdata_len += dlen;
3612 /* CRYPTO frame length. */
3613 cflen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003614 TRACE_PROTO(" CRYPTO frame length (cflen)",
3615 QUIC_EV_CONN_BCFRMS, conn->conn, &cflen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003616 /* Add the CRYPTO data length and its encoded length to the packet
3617 * length and the length of this length.
3618 */
3619 *len += cflen;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003620 room -= cflen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003621 if (dlen == cf->crypto.len) {
3622 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003623 MT_LIST_DELETE_SAFE(tmp1);
Willy Tarreau2b718102021-04-21 07:32:39 +02003624 LIST_APPEND(&pkt->frms, &cf->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003625 }
3626 else {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003627 struct quic_frame *new_cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003628
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003629 new_cf = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003630 if (!new_cf) {
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003631 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003632 return 0;
3633 }
3634
3635 new_cf->type = QUIC_FT_CRYPTO;
3636 new_cf->crypto.len = dlen;
3637 new_cf->crypto.offset = cf->crypto.offset;
Willy Tarreau2b718102021-04-21 07:32:39 +02003638 LIST_APPEND(&pkt->frms, &new_cf->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003639 /* Consume <dlen> bytes of the current frame. */
3640 cf->crypto.len -= dlen;
3641 cf->crypto.offset += dlen;
3642 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003643 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003644 }
3645
Frédéric Lécailleea604992020-12-24 13:01:37 +01003646 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003647}
3648
3649/* This function builds a clear handshake packet used during a QUIC TLS handshakes
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003650 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption level
3651 * for <conn> QUIC connection and <qel> as QUIC TLS encryption level, filling the buffer
3652 * with as much as CRYPTO.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003653 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003654 * reserved so that to ensure there is enough room to build this AEAD TAG after
3655 * having successfully returned from this function and to ensure the position
3656 * pointer <pos> may be safely incremented by QUIC_TLS_TAG_LEN.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003657 * This function also update the value of <buf_pn> pointer to point to the packet
3658 * number field in this packet. <pn_len> will also have the packet number
3659 * length as value.
3660 *
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003661 * Return 1 packet if succeeded or 0 if failed (not enough room in the buffer to build
3662 * this packet, QUIC_TLS_TAG_LEN bytes for the encryption TAG included).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003663 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003664static int qc_do_build_hdshk_pkt(unsigned char *pos, const unsigned char *end,
3665 struct quic_tx_packet *pkt, int pkt_type,
3666 int64_t pn, size_t *pn_len,
3667 unsigned char **buf_pn,
3668 struct quic_enc_level *qel,
3669 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003670{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003671 unsigned char *beg;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003672 size_t len, len_frms, token_fields_len, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003673 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3674 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3675 struct quic_crypto *crypto = &frm.crypto;
3676 size_t ack_frm_len;
3677 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003678 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003679
Frédéric Lécailleea604992020-12-24 13:01:37 +01003680 /* Length field value with CRYPTO frames if present. */
3681 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003682 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003683 /* When not probing and not acking, reduce the size of this buffer to respect
3684 * the congestion controller window.
3685 */
3686 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3687 size_t path_room;
3688
3689 path_room = quic_path_prep_data(conn->path);
3690 if (end - beg > path_room)
3691 end = beg + path_room;
3692 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003693
3694 /* For a server, the token field of an Initial packet is empty. */
3695 token_fields_len = pkt_type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3696
3697 /* Check there is enough room to build the header followed by a token. */
3698 if (end - pos < QUIC_LONG_PACKET_MINLEN + conn->dcid.len +
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003699 conn->scid.len + token_fields_len + QUIC_TLS_TAG_LEN) {
3700 ssize_t room = end - pos;
3701 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3702 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003703 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003704 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003705
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003706 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3707 /* packet number length */
3708 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3709
3710 quic_build_packet_long_header(&pos, end, pkt_type, *pn_len, conn);
3711
3712 /* Encode the token length (0) for an Initial packet. */
3713 if (pkt_type == QUIC_PACKET_TYPE_INITIAL)
3714 *pos++ = 0;
3715
3716 /* Build an ACK frame if required. */
3717 ack_frm_len = 0;
3718 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003719 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003720 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003721 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003722 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003723 if (!ack_frm_len) {
3724 ssize_t room = end - pos;
3725 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3726 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003727 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003728 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003729
3730 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3731 }
3732
3733 /* Length field value without the CRYPTO frames data length. */
3734 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003735 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003736 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003737
3738 len_frms = len + QUIC_TLS_TAG_LEN;
3739 if (!qc_build_cfrms(pkt, end - pos, &len_frms, pos - beg, qel, conn)) {
3740 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3741 conn->conn, NULL, NULL, &room);
3742 goto err;
3743 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003744 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003745
3746 add_ping_frm = 0;
3747 padding_len = 0;
3748 if (objt_server(conn->conn->target) &&
3749 pkt_type == QUIC_PACKET_TYPE_INITIAL &&
3750 len < QUIC_INITIAL_PACKET_MINLEN) {
3751 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
3752 }
3753 else if (LIST_ISEMPTY(&pkt->frms)) {
3754 if (qel->pktns->tx.pto_probe) {
3755 /* If we cannot send a CRYPTO frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003756 add_ping_frm = 1;
3757 len += 1;
3758 }
3759 /* If there is no frame at all to follow, add at least a PADDING frame. */
3760 if (!ack_frm_len)
3761 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
3762 }
3763
3764 /* Length (of the remaining data). Must not fail because, the buffer size
3765 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
3766 * for the encryption TAG. It must be taken into an account for the length
3767 * of this packet.
3768 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01003769 if (len_frms)
3770 len = len_frms;
3771 else
3772 len += QUIC_TLS_TAG_LEN;
3773 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003774
3775 /* Packet number field address. */
3776 *buf_pn = pos;
3777
3778 /* Packet number encoding. */
3779 quic_packet_number_encode(&pos, end, pn, *pn_len);
3780
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003781 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3782 ssize_t room = end - pos;
3783 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3784 conn->conn, NULL, NULL, &room);
3785 goto err;
3786 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003787
3788 /* Crypto frame */
3789 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003790 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003791
3792 list_for_each_entry(cf, &pkt->frms, list) {
3793 crypto->offset = cf->crypto.offset;
3794 crypto->len = cf->crypto.len;
3795 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003796 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3797 ssize_t room = end - pos;
3798 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3799 conn->conn, NULL, NULL, &room);
3800 goto err;
3801 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003802 }
3803 }
3804
3805 /* Build a PING frame if needed. */
3806 if (add_ping_frm) {
3807 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003808 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3809 ssize_t room = end - pos;
3810 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3811 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003812 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003813 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003814 }
3815
3816 /* Build a PADDING frame if needed. */
3817 if (padding_len) {
3818 frm.type = QUIC_FT_PADDING;
3819 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003820 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3821 ssize_t room = end - pos;
3822 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3823 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003824 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003825 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003826 }
3827
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003828 /* Always reset this variable as this function has no idea
3829 * if it was set. It is handle by the loss detection timer.
3830 */
3831 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003832 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003833
3834 out:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003835 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003836
3837 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003838 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003839}
3840
3841static inline void quic_tx_packet_init(struct quic_tx_packet *pkt)
3842{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003843 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003844 pkt->cdata_len = 0;
3845 pkt->in_flight_len = 0;
3846 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003847 pkt->next = NULL;
3848 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003849}
3850
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003851/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003852static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
3853{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003854 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003855
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003856 if (!pkt)
3857 return;
3858
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003859 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003860 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003861 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003862 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003863 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003864}
3865
3866/* Build a handshake packet into <buf> packet buffer with <pkt_type> as packet
3867 * type for <qc> QUIC connection from CRYPTO data stream at <*offset> offset to
3868 * be encrypted at <qel> encryption level.
3869 * Return -2 if the packet could not be encrypted for any reason, -1 if there was
3870 * not enough room in <buf> to build the packet, or the size of the built packet
3871 * if succeeded (may be zero if there is too much crypto data in flight to build the packet).
3872 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003873static struct quic_tx_packet *qc_build_hdshk_pkt(unsigned char **pos,
3874 const unsigned char *buf_end,
3875 struct quic_conn *qc, int pkt_type,
3876 struct quic_enc_level *qel, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003877{
3878 /* The pointer to the packet number field. */
3879 unsigned char *buf_pn;
3880 unsigned char *beg, *end, *payload;
3881 int64_t pn;
3882 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003883 struct quic_tls_ctx *tls_ctx;
3884 struct quic_tx_packet *pkt;
3885
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003886 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003887 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003888 pkt = pool_alloc(pool_head_quic_tx_packet);
3889 if (!pkt) {
3890 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003891 *err = -2;
3892 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003893 }
3894
3895 quic_tx_packet_init(pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003896 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003897 pn_len = 0;
3898 buf_pn = NULL;
3899 pn = qel->pktns->tx.next_pn + 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003900 if (!qc_do_build_hdshk_pkt(*pos, buf_end, pkt, pkt_type, pn, &pn_len, &buf_pn, qel, qc)) {
3901 *err = -1;
3902 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003903 }
3904
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003905 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003906 payload = buf_pn + pn_len;
3907 payload_len = end - payload;
3908 aad_len = payload - beg;
3909
3910 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003911 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
3912 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003913 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003914 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003915
3916 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003917 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003918 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
3919 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
3920 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003921 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003922 goto err;
3923 }
3924
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003925 /* Now that a correct packet is built, let us consume <*pos> buffer. */
3926 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003927 /* Consume a packet number. */
3928 ++qel->pktns->tx.next_pn;
3929 /* Attach the built packet to its tree. */
3930 pkt->pn_node.key = qel->pktns->tx.next_pn;
3931 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003932 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003933 pkt->in_flight_len = pkt->len;
3934 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003935 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003936 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003937 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
3938
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003939 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003940
3941 err:
3942 free_quic_tx_packet(pkt);
3943 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003944 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003945}
3946
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003947/* Prepare a clear post handhskake packet for <conn> QUIC connection.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003948 * Return the length of this packet if succeeded, -1 <wbuf> was full.
3949 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003950static int qc_do_build_phdshk_apkt(unsigned char *pos, const unsigned char *end,
3951 struct quic_tx_packet *pkt,
3952 int64_t pn, size_t *pn_len,
3953 unsigned char **buf_pn,
3954 struct quic_enc_level *qel,
3955 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003956{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003957 const unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003958 struct quic_frame *frm, *sfrm;
3959 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3960 size_t fake_len, ack_frm_len;
3961 int64_t largest_acked_pn;
3962
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003963 TRACE_ENTER(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003964 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003965 /* When not probing and not acking, reduce the size of this buffer to respect
3966 * the congestion controller window.
3967 */
3968 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3969 size_t path_room;
3970
3971 path_room = quic_path_prep_data(conn->path);
3972 if (end - beg > path_room)
3973 end = beg + path_room;
3974 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003975 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3976 /* Packet number length */
3977 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3978 /* Check there is enough room to build this packet (without payload). */
3979 if (end - pos < QUIC_SHORT_PACKET_MINLEN + sizeof_quic_cid(&conn->dcid) +
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003980 *pn_len + QUIC_TLS_TAG_LEN) {
3981 ssize_t room = end - pos;
3982 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
3983 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003984 goto err;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003985 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003986
3987 /* Reserve enough room at the end of the packet for the AEAD TAG. */
3988 end -= QUIC_TLS_TAG_LEN;
3989 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3990 /* Packet number field. */
3991 *buf_pn = pos;
3992 /* Packet number encoding. */
3993 quic_packet_number_encode(&pos, end, pn, *pn_len);
3994
3995 /* Build an ACK frame if required. */
3996 ack_frm_len = 0;
3997 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003998 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003999 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004000 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004001 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
4002 if (!ack_frm_len)
4003 goto err;
4004
4005 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
4006 }
4007
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004008 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4009 ssize_t room = end - pos;
4010 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
4011 conn->conn, NULL, NULL, &room);
4012 goto err;
4013 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004014
4015 fake_len = ack_frm_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004016 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms) &&
Frédéric Lécailleea604992020-12-24 13:01:37 +01004017 !qc_build_cfrms(pkt, end - pos, &fake_len, pos - beg, qel, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004018 ssize_t room = end - pos;
4019 TRACE_PROTO("some CRYPTO frames could not be built",
4020 QUIC_EV_CONN_PAPKT, conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004021 goto err;
4022 }
4023
4024 /* Crypto frame */
4025 if (!LIST_ISEMPTY(&pkt->frms)) {
4026 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4027 struct quic_crypto *crypto = &frm.crypto;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004028 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004029
4030 list_for_each_entry(cf, &pkt->frms, list) {
4031 crypto->offset = cf->crypto.offset;
4032 crypto->len = cf->crypto.len;
4033 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004034 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4035 ssize_t room = end - pos;
4036 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
4037 conn->conn, NULL, NULL, &room);
4038 goto err;
4039 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004040 }
4041 }
4042
4043 /* Encode a maximum of frames. */
4044 list_for_each_entry_safe(frm, sfrm, &conn->tx.frms_to_send, list) {
4045 unsigned char *ppos;
4046
4047 ppos = pos;
4048 if (!qc_build_frm(&ppos, end, frm, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004049 TRACE_DEVEL("Frames not built", QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004050 break;
4051 }
4052
Willy Tarreau2b718102021-04-21 07:32:39 +02004053 LIST_DELETE(&frm->list);
4054 LIST_APPEND(&pkt->frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004055 pos = ppos;
4056 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004057 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004058
4059 out:
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004060 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004061 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004062
4063 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004064 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004065}
4066
4067/* Prepare a post handhskake packet at Application encryption level for <conn>
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004068 * QUIC connection.
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004069 * Return the length if succeeded, -1 if <wbuf> was full, -2 in case of major error
4070 * (allocation or encryption failures).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004071 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004072static struct quic_tx_packet *qc_build_phdshk_apkt(unsigned char **pos,
4073 const unsigned char *buf_end,
4074 struct quic_conn *qc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004075{
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004076 /* A pointer to the packet number field in <buf> */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004077 unsigned char *buf_pn;
4078 unsigned char *beg, *end, *payload;
4079 int64_t pn;
4080 size_t pn_len, aad_len, payload_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004081 struct quic_tls_ctx *tls_ctx;
4082 struct quic_enc_level *qel;
4083 struct quic_tx_packet *pkt;
4084
4085 TRACE_ENTER(QUIC_EV_CONN_PAPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004086 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004087 pkt = pool_alloc(pool_head_quic_tx_packet);
4088 if (!pkt) {
4089 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_PAPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004090 *err = -2;
4091 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004092 }
4093
4094 quic_tx_packet_init(pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004095 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004096 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4097 pn_len = 0;
4098 buf_pn = NULL;
4099 pn = qel->pktns->tx.next_pn + 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004100 if (!qc_do_build_phdshk_apkt(*pos, buf_end, pkt, pn, &pn_len, &buf_pn, qel, qc)) {
4101 *err = -1;
4102 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004103 }
4104
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004105 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004106 payload = buf_pn + pn_len;
4107 payload_len = end - payload;
4108 aad_len = payload - beg;
4109
4110 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004111 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4112 *err = -2;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004113 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004114 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004115
4116 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004117 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004118 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004119 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4120 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_PAPKT, qc->conn);
4121 *err = -2;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004122 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004123 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004124
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004125 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4126 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004127 /* Consume a packet number. */
4128 ++qel->pktns->tx.next_pn;
4129 /* Attach the built packet to its tree. */
4130 pkt->pn_node.key = qel->pktns->tx.next_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004131 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004132 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004133 pkt->in_flight_len = pkt->len;
4134 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004135 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004136 pkt->pktns = qel->pktns;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004137 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, qc->conn, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004138
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004139 return pkt;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004140
4141 err:
4142 free_quic_tx_packet(pkt);
4143 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PAPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004144 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004145}
4146
4147/* Prepare a maximum of QUIC Application level packets from <ctx> QUIC
4148 * connection I/O handler context.
4149 * Returns 1 if succeeded, 0 if not.
4150 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004151int qc_prep_phdshk_pkts(struct qring *qr, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004152{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004153 struct cbuf *cbuf;
4154 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004155 struct quic_enc_level *qel;
4156
4157 TRACE_ENTER(QUIC_EV_CONN_PAPKTS, qc->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004158 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004159 cbuf = qr->cbuf;
4160 pos = cb_wr(cbuf);
4161 end = end_buf = pos + cb_contig_space(cbuf);
4162 while (pos < end_buf) {
4163 int err;
4164 uint16_t dglen;
4165 struct quic_tx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004166
4167 if (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004168 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004169 qc->path->prep_in_flight >= qc->path->cwnd)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004170 TRACE_DEVEL("nothing more to do",
4171 QUIC_EV_CONN_PAPKTS, qc->conn);
4172 break;
4173 }
4174
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004175 /* Leave room for the datagram header */
4176 pos += sizeof dglen + sizeof pkt;
4177 if (end - pos > qc->path->mtu)
4178 end = pos + qc->path->mtu;
4179 pkt = qc_build_phdshk_apkt(&pos, end, qc, &err);
4180 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004181 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004182 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004183 case -2:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004184 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004185 default:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004186 dglen = pkt->len;
4187 qc_set_dg(cbuf, dglen, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004188 }
4189 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004190 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004191 TRACE_LEAVE(QUIC_EV_CONN_PAPKTS, qc->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004192 return 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004193
4194 err:
4195 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PAPKTS, qc->conn);
4196 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004197}
4198
4199/* QUIC connection packet handler task. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004200struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004201{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004202 struct ssl_sock_ctx *ctx = context;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004203
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004204 if (ctx->conn->qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004205 qc_do_hdshk(ctx);
4206 }
4207 else {
4208 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004209 struct qring *qr;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004210
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004211 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004212 /* XXX TO DO: may fail!!! XXX */
4213 qc_treat_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_APP], ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004214 qc_prep_phdshk_pkts(qr, qc);
4215 qc_send_ppkts(qr, ctx);
4216 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004217 }
4218
Willy Tarreau74163142021-03-13 11:30:19 +01004219 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004220}
4221
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004222/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4223 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004224 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004225static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4226 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004227{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004228 size_t try, done = 0;
4229
4230 if (!conn_ctrl_ready(conn))
4231 return 0;
4232
4233 if (!fd_recv_ready(conn->handle.fd))
4234 return 0;
4235
4236 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004237
4238 /* read the largest possible block. For this, we perform only one call
4239 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004240 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004241 */
4242 while (count > 0) {
4243 try = b_contig_space(buf);
4244 if (!try)
4245 break;
4246
4247 if (try > count)
4248 try = count;
4249
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004250 b_add(buf, try);
4251 done += try;
4252 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004253 }
4254
4255 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4256 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4257
4258 leave:
4259 return done;
4260
4261 read0:
4262 conn_sock_read0(conn);
4263 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4264
4265 /* Now a final check for a possible asynchronous low-level error
4266 * report. This can happen when a connection receives a reset
4267 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4268 * we might have come from there by just checking POLL_HUP instead
4269 * of recv()'s return value 0, so we have no way to tell there was
4270 * an error without checking.
4271 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004272 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004273 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4274 goto leave;
4275}
4276
4277
4278/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4279 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4280 * other pending data for example, but this flag is ignored at the moment.
4281 * Only one call to send() is performed, unless the buffer wraps, in which case
4282 * a second call may be performed. The connection's flags are updated with
4283 * whatever special event is detected (error, empty). The caller is responsible
4284 * for taking care of those events and avoiding the call if inappropriate. The
4285 * function does not call the connection's polling update function, so the caller
4286 * is responsible for this. It's up to the caller to update the buffer's contents
4287 * based on the return value.
4288 */
4289static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4290{
4291 ssize_t ret;
4292 size_t try, done;
4293 int send_flag;
4294
4295 if (!conn_ctrl_ready(conn))
4296 return 0;
4297
4298 if (!fd_send_ready(conn->handle.fd))
4299 return 0;
4300
4301 done = 0;
4302 /* send the largest possible block. For this we perform only one call
4303 * to send() unless the buffer wraps and we exactly fill the first hunk,
4304 * in which case we accept to do it once again.
4305 */
4306 while (count) {
4307 try = b_contig_data(buf, done);
4308 if (try > count)
4309 try = count;
4310
4311 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4312 if (try < count || flags & CO_SFL_MSG_MORE)
4313 send_flag |= MSG_MORE;
4314
4315 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4316 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4317 if (ret > 0) {
4318 count -= ret;
4319 done += ret;
4320
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004321 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004322 conn->flags |= CO_FL_WAIT_L4L6;
4323 /* if the system buffer is full, don't insist */
4324 if (ret < try)
4325 break;
4326 }
4327 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4328 /* nothing written, we need to poll for write first */
4329 fd_cant_send(conn->handle.fd);
4330 break;
4331 }
4332 else if (errno != EINTR) {
4333 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4334 break;
4335 }
4336 }
4337 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4338 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4339
4340 if (done > 0) {
4341 /* we count the total bytes sent, and the send rate for 32-byte
4342 * blocks. The reason for the latter is that freq_ctr are
4343 * limited to 4GB and that it's not enough per second.
4344 */
4345 _HA_ATOMIC_ADD(&global.out_bytes, done);
4346 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4347 }
4348 return done;
4349}
4350
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004351/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4352 * event subscriber <es> is not allowed to change from a previous call as long
4353 * as at least one event is still subscribed. The <event_type> must only be a
4354 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4355 */
4356static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4357{
4358 return conn_subscribe(conn, xprt_ctx, event_type, es);
4359}
4360
4361/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4362 * The <es> pointer is not allowed to differ from the one passed to the
4363 * subscribe() call. It always returns zero.
4364 */
4365static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4366{
4367 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4368}
4369
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004370/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4371 * connection with <xprt_ctx> as address of the xprt context.
4372 * Returns 1 if succeeded, 0 if not.
4373 */
4374static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4375{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004376 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004377
4378 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4379
4380 if (*xprt_ctx)
4381 goto out;
4382
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004383 ctx = pool_alloc(pool_head_quic_conn_ctx);
4384 if (!ctx) {
4385 conn->err_code = CO_ER_SYS_MEMLIM;
4386 goto err;
4387 }
4388
4389 ctx->wait_event.tasklet = tasklet_new();
4390 if (!ctx->wait_event.tasklet) {
4391 conn->err_code = CO_ER_SYS_MEMLIM;
4392 goto err;
4393 }
4394
4395 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4396 ctx->wait_event.tasklet->context = ctx;
4397 ctx->wait_event.events = 0;
4398 ctx->conn = conn;
4399 ctx->subs = NULL;
4400 ctx->xprt_ctx = NULL;
4401
4402 ctx->xprt = xprt_get(XPRT_QUIC);
4403 if (objt_server(conn->target)) {
4404 /* Server */
4405 struct server *srv = __objt_server(conn->target);
4406 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004407 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004408 int ssl_err, ipv4;
4409
4410 ssl_err = SSL_ERROR_NONE;
4411 if (RAND_bytes(dcid, sizeof dcid) != 1)
4412 goto err;
4413
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004414 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004415 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004416 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004417 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004418 goto err;
4419
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004420 /* Insert our SCID, the connection ID for the QUIC client. */
4421 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4422
4423 conn->qc = qc;
4424 qc->conn = conn;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004425 if (!qc_new_isecs(qc, dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004426 goto err;
4427
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004428 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4429 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4430 goto err;
4431
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004432 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004433 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004434 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4435 qc->enc_params_len =
4436 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4437 &qc->rx.params, 0);
4438 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004439 goto err;
4440
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004441 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004442 SSL_set_connect_state(ctx->ssl);
4443 ssl_err = SSL_do_handshake(ctx->ssl);
4444 if (ssl_err != 1) {
4445 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4446 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
4447 TRACE_PROTO("SSL handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004448 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004449 }
4450 else {
4451 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004452 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004453 goto err;
4454 }
4455 }
4456 }
4457 else if (objt_listener(conn->target)) {
4458 /* Listener */
4459 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004460 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004461
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004462 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4463 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4464 goto err;
4465
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004466 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004467 SSL_set_accept_state(ctx->ssl);
4468 }
4469
4470 *xprt_ctx = ctx;
4471
4472 /* Leave init state and start handshake */
4473 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004474
4475 out:
4476 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4477
4478 return 0;
4479
4480 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004481 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004482 tasklet_free(ctx->wait_event.tasklet);
4483 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004484 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004485 return -1;
4486}
4487
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004488/* Start the QUIC transport layer */
4489static int qc_xprt_start(struct connection *conn, void *ctx)
4490{
4491 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004492 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004493
4494 qc = conn->qc;
4495 if (!quic_conn_init_timer(qc)) {
4496 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4497 return 0;
4498 }
4499
4500 tasklet_wakeup(qctx->wait_event.tasklet);
4501 return 1;
4502}
4503
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004504/* transport-layer operations for QUIC connections. */
4505static struct xprt_ops ssl_quic = {
4506 .snd_buf = quic_conn_from_buf,
4507 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004508 .subscribe = quic_conn_subscribe,
4509 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004510 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004511 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004512 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4513 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4514 .name = "QUIC",
4515};
4516
4517__attribute__((constructor))
4518static void __quic_conn_init(void)
4519{
4520 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4521 xprt_register(XPRT_QUIC, &ssl_quic);
4522}
4523
4524__attribute__((destructor))
4525static void __quic_conn_deinit(void)
4526{
4527 BIO_meth_free(ha_quic_meth);
4528}
4529
4530/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4531 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4532 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004533 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004534 */
4535static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4536 struct sockaddr_storage *saddr, qpkt_read_func *func)
4537{
4538 unsigned char *pos;
4539 const unsigned char *end;
4540 struct quic_dgram_ctx dgram_ctx = {
4541 .dcid_node = NULL,
4542 .owner = owner,
4543 };
4544
4545 pos = (unsigned char *)buf;
4546 end = pos + len;
4547
4548 do {
4549 int ret;
4550 struct quic_rx_packet *pkt;
4551
Willy Tarreaue4498932021-03-22 21:13:05 +01004552 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004553 if (!pkt)
4554 goto err;
4555
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004556 quic_rx_packet_refinc(pkt);
4557 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4558 if (ret == -1) {
4559 size_t pkt_len;
4560
4561 pkt_len = pkt->len;
4562 free_quic_rx_packet(pkt);
4563 /* If the packet length could not be found, we cannot continue. */
4564 if (!pkt_len)
4565 break;
4566 }
4567 } while (pos < end);
4568
4569 /* Increasing the received bytes counter by the UDP datagram length
4570 * if this datagram could be associated to a connection.
4571 */
4572 if (dgram_ctx.qc)
4573 dgram_ctx.qc->rx.bytes += len;
4574
4575 return pos - (unsigned char *)buf;
4576
4577 err:
4578 return -1;
4579}
4580
4581ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4582 struct sockaddr_storage *saddr)
4583{
4584 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4585}
4586
4587ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4588 struct sockaddr_storage *saddr)
4589{
4590 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4591}
4592
4593/* QUIC I/O handler for connection to local listeners or remove servers
4594 * depending on <listener> boolean value, with <fd> as socket file
4595 * descriptor and <ctx> as context.
4596 */
4597static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4598{
4599 ssize_t ret;
4600 size_t done = 0;
4601 struct buffer *buf = get_trash_chunk();
4602 /* Source address */
4603 struct sockaddr_storage saddr = {0};
4604 socklen_t saddrlen = sizeof saddr;
4605
4606 if (!fd_recv_ready(fd))
4607 return 0;
4608
4609 do {
4610 ret = recvfrom(fd, buf->area, buf->size, 0,
4611 (struct sockaddr *)&saddr, &saddrlen);
4612 if (ret < 0) {
4613 if (errno == EINTR)
4614 continue;
4615 if (errno == EAGAIN)
4616 fd_cant_recv(fd);
4617 goto out;
4618 }
4619 } while (0);
4620
4621 done = buf->data = ret;
4622 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4623
4624 out:
4625 return done;
4626}
4627
4628/* QUIC I/O handler for connections to local listeners with <fd> as socket
4629 * file descriptor.
4630 */
4631void quic_fd_handler(int fd)
4632{
Willy Tarreauf5090652021-04-06 17:23:40 +02004633 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004634 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4635}
4636
4637/* QUIC I/O handler for connections to remote servers with <fd> as socket
4638 * file descriptor.
4639 */
4640void quic_conn_fd_handler(int fd)
4641{
Willy Tarreauf5090652021-04-06 17:23:40 +02004642 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004643 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4644}
4645
4646/*
4647 * Local variables:
4648 * c-indent-level: 8
4649 * c-basic-offset: 8
4650 * End:
4651 */