Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QUIC transport layer over SOCK_DGRAM sockets. |
| 3 | * |
| 4 | * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define _GNU_SOURCE |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <netinet/tcp.h> |
| 24 | |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 25 | #include <import/ebmbtree.h> |
| 26 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 27 | #include <haproxy/buf-t.h> |
| 28 | #include <haproxy/compat.h> |
| 29 | #include <haproxy/api.h> |
| 30 | #include <haproxy/debug.h> |
| 31 | #include <haproxy/tools.h> |
| 32 | #include <haproxy/ticks.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 33 | |
| 34 | #include <haproxy/connection.h> |
| 35 | #include <haproxy/fd.h> |
| 36 | #include <haproxy/freq_ctr.h> |
| 37 | #include <haproxy/global.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 38 | #include <haproxy/h3.h> |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 39 | #include <haproxy/hq_interop.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 40 | #include <haproxy/log.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 41 | #include <haproxy/mux_quic.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 42 | #include <haproxy/pipe.h> |
| 43 | #include <haproxy/proxy.h> |
| 44 | #include <haproxy/quic_cc.h> |
| 45 | #include <haproxy/quic_frame.h> |
| 46 | #include <haproxy/quic_loss.h> |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 47 | #include <haproxy/quic_sock.h> |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 48 | #include <haproxy/cbuf.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 49 | #include <haproxy/quic_tls.h> |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 50 | #include <haproxy/sink.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 51 | #include <haproxy/ssl_sock.h> |
| 52 | #include <haproxy/stream_interface.h> |
| 53 | #include <haproxy/task.h> |
| 54 | #include <haproxy/trace.h> |
| 55 | #include <haproxy/xprt_quic.h> |
| 56 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 57 | /* list of supported QUIC versions by this implementation */ |
| 58 | static int quic_supported_version[] = { |
| 59 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 60 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 61 | |
| 62 | /* placeholder, do not add entry after this */ |
| 63 | 0x0 |
| 64 | }; |
| 65 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 66 | /* This is the values of some QUIC transport parameters when absent. |
| 67 | * Should be used to initialize any transport parameters (local or remote) |
| 68 | * before updating them with customized values. |
| 69 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 70 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 71 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 72 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 73 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 74 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /* trace source and events */ |
| 78 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 79 | const struct trace_source *src, |
| 80 | const struct ist where, const struct ist func, |
| 81 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 82 | |
| 83 | static const struct trace_event quic_trace_events[] = { |
| 84 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 85 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 86 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 87 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 89 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 90 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 91 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 92 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 93 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 94 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 95 | { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" }, |
| 96 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 97 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 100 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 101 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 102 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 103 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 104 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 105 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 106 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 107 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 108 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 109 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 110 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 111 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 112 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 113 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 114 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 115 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 116 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 117 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 118 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 119 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 120 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 121 | { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 122 | { /* end */ } |
| 123 | }; |
| 124 | |
| 125 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 126 | /* arg1 */ { /* already used by the connection */ }, |
| 127 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 128 | /* arg3 */ { }, |
| 129 | /* arg4 */ { } |
| 130 | }; |
| 131 | |
| 132 | static const struct name_desc quic_trace_decoding[] = { |
| 133 | #define QUIC_VERB_CLEAN 1 |
| 134 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 135 | { /* end */ } |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | struct trace_source trace_quic = { |
| 140 | .name = IST("quic"), |
| 141 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 142 | .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 143 | .default_cb = quic_trace, |
| 144 | .known_events = quic_trace_events, |
| 145 | .lockon_args = quic_trace_lockon_args, |
| 146 | .decoding = quic_trace_decoding, |
| 147 | .report_events = ~0, /* report everything by default */ |
| 148 | }; |
| 149 | |
| 150 | #define TRACE_SOURCE &trace_quic |
| 151 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 152 | |
| 153 | static BIO_METHOD *ha_quic_meth; |
| 154 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 155 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ); |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 156 | DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 157 | DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 158 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 159 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 160 | DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 161 | DECLARE_POOL(pool_head_quic_connection_id, |
| 162 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 163 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 164 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 165 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 166 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm)); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 167 | DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 168 | DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf)); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 169 | DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame)); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 170 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 171 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 172 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 173 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 174 | struct quic_conn *qc, size_t dglen, int pkt_type, |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 175 | int padding, int ack, int probe, int cc, int *err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 176 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 177 | /* Only for debug purpose */ |
| 178 | struct enc_debug_info { |
| 179 | unsigned char *payload; |
| 180 | size_t payload_len; |
| 181 | unsigned char *aad; |
| 182 | size_t aad_len; |
| 183 | uint64_t pn; |
| 184 | }; |
| 185 | |
| 186 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 187 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 188 | unsigned char *payload, size_t payload_len, |
| 189 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 190 | { |
| 191 | edi->payload = payload; |
| 192 | edi->payload_len = payload_len; |
| 193 | edi->aad = aad; |
| 194 | edi->aad_len = aad_len; |
| 195 | edi->pn = pn; |
| 196 | } |
| 197 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 198 | /* Trace callback for QUIC. |
| 199 | * These traces always expect that arg1, if non-null, is of type connection. |
| 200 | */ |
| 201 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 202 | const struct ist where, const struct ist func, |
| 203 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 204 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 205 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 206 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 207 | if (qc) { |
| 208 | const struct quic_tls_secrets *secs; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 209 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 210 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 211 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 212 | chunk_appendf(&trace_buf, "\n odcid"); |
| 213 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 214 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 215 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 216 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 217 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 221 | const enum ssl_encryption_level_t *level = a2; |
| 222 | const size_t *len = a3; |
| 223 | |
| 224 | if (level) { |
| 225 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 226 | |
| 227 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 228 | } |
| 229 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 230 | chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 231 | } |
| 232 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 233 | /* Initial read & write secrets. */ |
| 234 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 235 | const unsigned char *rx_sec = a2; |
| 236 | const unsigned char *tx_sec = a3; |
| 237 | |
| 238 | secs = &qc->els[level].tls_ctx.rx; |
| 239 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 240 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 241 | if (rx_sec) |
| 242 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 243 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 244 | } |
| 245 | secs = &qc->els[level].tls_ctx.tx; |
| 246 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 247 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 248 | if (tx_sec) |
| 249 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 250 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 251 | } |
| 252 | } |
| 253 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 254 | const enum ssl_encryption_level_t *level = a2; |
| 255 | const unsigned char *secret = a3; |
| 256 | const size_t *secret_len = a4; |
| 257 | |
| 258 | if (level) { |
| 259 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 260 | |
| 261 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 262 | if (secret && secret_len) |
| 263 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 264 | secs = &qc->els[lvl].tls_ctx.rx; |
| 265 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 266 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 271 | const enum ssl_encryption_level_t *level = a2; |
| 272 | const unsigned char *secret = a3; |
| 273 | const size_t *secret_len = a4; |
| 274 | |
| 275 | if (level) { |
| 276 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 277 | |
| 278 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 279 | if (secret && secret_len) |
| 280 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 281 | secs = &qc->els[lvl].tls_ctx.tx; |
| 282 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 283 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 284 | } |
| 285 | |
| 286 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 287 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 288 | if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 289 | const struct quic_tx_packet *pkt = a2; |
| 290 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 291 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 292 | |
| 293 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 294 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 295 | chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu " |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 296 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 297 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 298 | (unsigned long long)qc->path->cwnd, |
| 299 | (unsigned long long)qc->path->prep_in_flight, |
| 300 | (unsigned long long)qc->path->in_flight, |
| 301 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 302 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 303 | } |
| 304 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 305 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 306 | if (pkt->pn_node.key != (uint64_t)-1) |
| 307 | chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 308 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 309 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 310 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 311 | (unsigned long long)qc->rx.bytes, |
| 312 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | if (room) { |
| 316 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 317 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 318 | (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | if (mask & QUIC_EV_CONN_HDSHK) { |
| 323 | const enum quic_handshake_state *state = a2; |
| 324 | const int *err = a3; |
| 325 | |
| 326 | if (state) |
| 327 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 328 | if (err) |
| 329 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 330 | } |
| 331 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 332 | if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 333 | const struct quic_rx_packet *pkt = a2; |
| 334 | const unsigned long *pktlen = a3; |
| 335 | const SSL *ssl = a4; |
| 336 | |
| 337 | if (pkt) { |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 338 | chunk_appendf(&trace_buf, " pkt@%p el=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 339 | pkt, quic_packet_type_enc_level_char(pkt->type)); |
| 340 | if (pkt->pnl) |
| 341 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 342 | (unsigned long long)pkt->pn); |
| 343 | if (pkt->token_len) |
| 344 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 345 | (unsigned long long)pkt->token_len); |
| 346 | if (pkt->aad_len) |
| 347 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 348 | (unsigned long long)pkt->aad_len); |
| 349 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 350 | pkt->flags, (unsigned long long)pkt->len); |
| 351 | } |
| 352 | if (pktlen) |
| 353 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 354 | if (ssl) { |
| 355 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 356 | chunk_appendf(&trace_buf, " el=%c", |
| 357 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 362 | const struct quic_rx_packet *pkt = a2; |
| 363 | const struct quic_rx_crypto_frm *cf = a3; |
| 364 | const SSL *ssl = a4; |
| 365 | |
| 366 | if (pkt) |
| 367 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 368 | quic_packet_type_enc_level_char(pkt->type), |
| 369 | (unsigned long long)pkt->pn); |
| 370 | if (cf) |
| 371 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 372 | (unsigned long long)cf->offset_node.key, |
| 373 | (unsigned long long)cf->len); |
| 374 | if (ssl) { |
| 375 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
Frédéric Lécaille | 57e6e9e | 2021-09-23 18:10:56 +0200 | [diff] [blame] | 376 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 377 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 378 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 379 | |
| 380 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 381 | chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 385 | const struct quic_frame *frm = a2; |
| 386 | |
| 387 | if (frm) |
| 388 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 389 | } |
| 390 | |
| 391 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 392 | const struct quic_enc_level *qel = a2; |
| 393 | |
| 394 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 395 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 396 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 397 | " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 398 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 546186b | 2021-08-03 14:25:36 +0200 | [diff] [blame] | 399 | quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)), |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 400 | !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 401 | (unsigned long long)qc->path->cwnd, |
| 402 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 403 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 404 | (unsigned long long)pktns->tx.in_flight, |
| 405 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 409 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 410 | const struct enc_debug_info *edi = a2; |
| 411 | |
| 412 | if (edi) |
| 413 | chunk_appendf(&trace_buf, |
| 414 | " payload=@%p payload_len=%llu" |
| 415 | " aad=@%p aad_len=%llu pn=%llu", |
| 416 | edi->payload, (unsigned long long)edi->payload_len, |
| 417 | edi->aad, (unsigned long long)edi->aad_len, |
| 418 | (unsigned long long)edi->pn); |
| 419 | } |
| 420 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 421 | if (mask & QUIC_EV_CONN_RMHP) { |
| 422 | const struct quic_rx_packet *pkt = a2; |
| 423 | |
| 424 | if (pkt) { |
| 425 | const int *ret = a3; |
| 426 | |
| 427 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 428 | if (ret && *ret) |
| 429 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 430 | pkt->pnl, (unsigned long long)pkt->pn); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 435 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 436 | const unsigned long *val1 = a3; |
| 437 | const unsigned long *val2 = a4; |
| 438 | |
| 439 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 440 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 441 | if (val1) |
| 442 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 443 | if (val2) |
| 444 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 445 | } |
| 446 | |
| 447 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 448 | const unsigned int *rtt_sample = a2; |
| 449 | const unsigned int *ack_delay = a3; |
| 450 | const struct quic_loss *ql = a4; |
| 451 | |
| 452 | if (rtt_sample) |
| 453 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 454 | if (ack_delay) |
| 455 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 456 | if (ql) |
| 457 | chunk_appendf(&trace_buf, |
| 458 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 459 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 460 | } |
| 461 | if (mask & QUIC_EV_CONN_CC) { |
| 462 | const struct quic_cc_event *ev = a2; |
| 463 | const struct quic_cc *cc = a3; |
| 464 | |
| 465 | if (a2) |
| 466 | quic_cc_event_trace(&trace_buf, ev); |
| 467 | if (a3) |
| 468 | quic_cc_state_trace(&trace_buf, cc); |
| 469 | } |
| 470 | |
| 471 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 472 | const struct quic_pktns *pktns = a2; |
| 473 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 474 | |
| 475 | if (pktns) { |
| 476 | chunk_appendf(&trace_buf, " pktns=%s", |
| 477 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 478 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 479 | if (pktns->tx.loss_time) |
| 480 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 481 | TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time))); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 482 | } |
| 483 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 484 | struct quic_tx_packet *pkt; |
| 485 | |
| 486 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 487 | list_for_each_entry(pkt, lost_pkts, list) |
| 488 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 493 | const struct quic_pktns *pktns = a2; |
| 494 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 495 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 496 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 497 | if (ifae_pkts) |
| 498 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 499 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 500 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 501 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 502 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 503 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 504 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 505 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 506 | if (pktns->tx.in_flight) |
| 507 | chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 508 | if (pktns->tx.loss_time) |
| 509 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 510 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 511 | } |
| 512 | if (mask & QUIC_EV_CONN_SPTO) { |
| 513 | if (pktns->tx.time_of_last_eliciting) |
| 514 | chunk_appendf(&trace_buf, " tole=%dms", |
| 515 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 516 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 517 | chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) { |
| 522 | chunk_appendf(&trace_buf, |
| 523 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 528 | const struct quic_tx_packet *pkt = a2; |
| 529 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 530 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 531 | (unsigned long long)qc->path->cwnd, |
| 532 | (unsigned long long)qc->path->prep_in_flight, |
| 533 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 534 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 535 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 536 | (unsigned long)pkt->pn_node.key, |
| 537 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 538 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 539 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 540 | } |
| 541 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 542 | |
| 543 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 544 | const uint8_t *alert = a2; |
| 545 | const enum ssl_encryption_level_t *level = a3; |
| 546 | |
| 547 | if (alert) |
| 548 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 549 | if (level) |
| 550 | chunk_appendf(&trace_buf, " el=%c", |
| 551 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 552 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 553 | |
| 554 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 555 | const size_t *sz1 = a2; |
| 556 | const size_t *sz2 = a3; |
| 557 | const size_t *sz3 = a4; |
| 558 | |
| 559 | if (sz1) |
| 560 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 561 | if (sz2) |
| 562 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 563 | if (sz3) |
| 564 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 565 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 566 | |
| 567 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 568 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 569 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 570 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 571 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 572 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 573 | } |
| 574 | if (mask & QUIC_EV_CONN_LPKT) { |
| 575 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 576 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 577 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 578 | if (pkt) { |
| 579 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 580 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 581 | if (pkt->pn_node.key != (uint64_t)-1) |
| 582 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 583 | } |
| 584 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 585 | if (len) |
| 586 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | } |
| 590 | |
| 591 | /* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 592 | static inline int quic_peer_validated_addr(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 593 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 594 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 595 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 596 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 597 | return 1; |
| 598 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 599 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 600 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 601 | if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) || |
| 602 | (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) || |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 603 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 604 | return 1; |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 610 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 611 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 612 | static inline void qc_set_timer(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 613 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 614 | struct quic_pktns *pktns; |
| 615 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 616 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 617 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 618 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 619 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 620 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 621 | pktns = quic_loss_pktns(qc); |
| 622 | if (tick_isset(pktns->tx.loss_time)) { |
| 623 | qc->timer = pktns->tx.loss_time; |
| 624 | goto out; |
| 625 | } |
| 626 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 627 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 628 | * cancelled for a server which reached the anti-amplification limit. |
| 629 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 630 | if (!quic_peer_validated_addr(qc) && |
| 631 | (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 632 | TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 633 | qc->timer = TICK_ETERNITY; |
| 634 | goto out; |
| 635 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 636 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 637 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 638 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 639 | /* Timer cancellation. */ |
| 640 | qc->timer = TICK_ETERNITY; |
| 641 | goto out; |
| 642 | } |
| 643 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 644 | handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE; |
| 645 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 646 | if (tick_isset(pto)) |
| 647 | qc->timer = pto; |
| 648 | out: |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 649 | if (qc->timer_task && qc->timer != TICK_ETERNITY) |
Amaury Denoyelle | 336f6fd | 2021-10-05 14:42:25 +0200 | [diff] [blame] | 650 | task_schedule(qc->timer_task, qc->timer); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 651 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 654 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 655 | * connection. |
| 656 | * Return 1 if succeeded, 0 if not. |
| 657 | */ |
| 658 | static int quic_tls_key_update(struct quic_conn *qc) |
| 659 | { |
| 660 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 661 | struct quic_tls_secrets *rx, *tx; |
| 662 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 663 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 664 | |
| 665 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 666 | rx = &tls_ctx->rx; |
| 667 | tx = &tls_ctx->tx; |
| 668 | nxt_rx = &qc->ku.nxt_rx; |
| 669 | nxt_tx = &qc->ku.nxt_tx; |
| 670 | |
| 671 | /* Prepare new RX secrets */ |
| 672 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 673 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 674 | TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 679 | nxt_rx->key, nxt_rx->keylen, |
| 680 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 681 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 682 | TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | /* Prepare new TX secrets */ |
| 687 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 688 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 689 | TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 694 | nxt_tx->key, nxt_tx->keylen, |
| 695 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 696 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 697 | TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | return 1; |
| 702 | } |
| 703 | |
| 704 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 705 | * Must be used after having updated them. |
| 706 | * Always succeeds. |
| 707 | */ |
| 708 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 709 | { |
| 710 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 711 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 712 | |
| 713 | /* Rotate the RX secrets */ |
| 714 | curr_secret = tls_ctx->rx.secret; |
| 715 | curr_iv = tls_ctx->rx.iv; |
| 716 | curr_key = tls_ctx->rx.key; |
| 717 | |
| 718 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 719 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 720 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 721 | |
| 722 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 723 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 724 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 725 | |
| 726 | qc->ku.prv_rx.secret = curr_secret; |
| 727 | qc->ku.prv_rx.iv = curr_iv; |
| 728 | qc->ku.prv_rx.key = curr_key; |
| 729 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 730 | |
| 731 | /* Update the TX secrets */ |
| 732 | curr_secret = tls_ctx->tx.secret; |
| 733 | curr_iv = tls_ctx->tx.iv; |
| 734 | curr_key = tls_ctx->tx.key; |
| 735 | |
| 736 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 737 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 738 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 739 | |
| 740 | qc->ku.nxt_tx.secret = curr_secret; |
| 741 | qc->ku.nxt_tx.iv = curr_iv; |
| 742 | qc->ku.nxt_tx.key = curr_key; |
| 743 | } |
| 744 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 745 | #ifndef OPENSSL_IS_BORINGSSL |
| 746 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 747 | const uint8_t *read_secret, |
| 748 | const uint8_t *write_secret, size_t secret_len) |
| 749 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 750 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 751 | struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 752 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 753 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 754 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 755 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 756 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 757 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 758 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 759 | goto out; |
| 760 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 761 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 762 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 763 | TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 764 | goto err; |
| 765 | } |
| 766 | |
| 767 | rx = &tls_ctx->rx; |
| 768 | tx = &tls_ctx->tx; |
| 769 | |
| 770 | rx->aead = tx->aead = tls_aead(cipher); |
| 771 | rx->md = tx->md = tls_md(cipher); |
| 772 | rx->hp = tx->hp = tls_hp(cipher); |
| 773 | |
| 774 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 775 | rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 776 | read_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 777 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 778 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 781 | rx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 782 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 783 | * listener. Note that a listener derives only RX secrets for this |
| 784 | * level. |
| 785 | */ |
| 786 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) |
| 787 | quic_accept_push_qc(qc); |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 788 | |
| 789 | if (!write_secret) |
| 790 | goto tp; |
| 791 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 792 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 793 | tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 794 | write_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 795 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 796 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 799 | tx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 800 | tp: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 801 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 802 | const unsigned char *buf; |
| 803 | size_t buflen; |
| 804 | |
| 805 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 806 | if (!buflen) |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 807 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 808 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 809 | if (!quic_transport_params_store(qc, 1, buf, buf + buflen)) |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 810 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 811 | } |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 812 | |
| 813 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 814 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 815 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 816 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 817 | |
| 818 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 819 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 820 | TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 821 | goto err; |
| 822 | } |
| 823 | |
| 824 | memcpy(rx->secret, read_secret, secret_len); |
| 825 | rx->secretlen = secret_len; |
| 826 | memcpy(tx->secret, write_secret, secret_len); |
| 827 | tx->secretlen = secret_len; |
| 828 | /* Initialize all the secret keys lengths */ |
| 829 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 830 | /* Prepare the next key update */ |
| 831 | if (!quic_tls_key_update(qc)) |
| 832 | goto err; |
| 833 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 834 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 835 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 836 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 837 | |
| 838 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 839 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 840 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 841 | } |
| 842 | #else |
| 843 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 844 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 845 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 846 | */ |
| 847 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 848 | const SSL_CIPHER *cipher, |
| 849 | const uint8_t *secret, size_t secret_len) |
| 850 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 851 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 852 | struct quic_tls_ctx *tls_ctx = |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 853 | &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 854 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 855 | TRACE_ENTER(QUIC_EV_CONN_RSEC, qc); |
| 856 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 857 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 858 | goto out; |
| 859 | } |
| 860 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 861 | tls_ctx->rx.aead = tls_aead(cipher); |
| 862 | tls_ctx->rx.md = tls_md(cipher); |
| 863 | tls_ctx->rx.hp = tls_hp(cipher); |
| 864 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 865 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 866 | goto err; |
| 867 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 868 | if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md, |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 869 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 870 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 871 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 872 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 873 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 874 | goto err; |
| 875 | } |
| 876 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 877 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 878 | const unsigned char *buf; |
| 879 | size_t buflen; |
| 880 | |
| 881 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 882 | if (!buflen) |
| 883 | goto err; |
| 884 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 885 | if (!quic_transport_params_store(qc, 1, buf, buf + buflen)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 886 | goto err; |
| 887 | } |
| 888 | |
| 889 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 890 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 891 | TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 892 | |
| 893 | return 1; |
| 894 | |
| 895 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 896 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 901 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 902 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 903 | */ |
| 904 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 905 | const SSL_CIPHER *cipher, |
| 906 | const uint8_t *secret, size_t secret_len) |
| 907 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 908 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 909 | struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 910 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 911 | TRACE_ENTER(QUIC_EV_CONN_WSEC, qc); |
| 912 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 913 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 914 | goto out; |
| 915 | } |
| 916 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 917 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 918 | goto err; |
| 919 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 920 | tls_ctx->tx.aead = tls_aead(cipher); |
| 921 | tls_ctx->tx.md = tls_md(cipher); |
| 922 | tls_ctx->tx.hp = tls_hp(cipher); |
| 923 | |
| 924 | if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md, |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 925 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 926 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 927 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 928 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 929 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 930 | goto err; |
| 931 | } |
| 932 | |
| 933 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 934 | TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 935 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 936 | return 1; |
| 937 | |
| 938 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 939 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 940 | return 0; |
| 941 | } |
| 942 | #endif |
| 943 | |
| 944 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 945 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 946 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 947 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 948 | * store all the data. |
| 949 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 950 | */ |
| 951 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 952 | const unsigned char *data, size_t len) |
| 953 | { |
| 954 | struct quic_crypto_buf **qcb; |
| 955 | /* The remaining byte to store in CRYPTO buffers. */ |
| 956 | size_t cf_offset, cf_len, *nb_buf; |
| 957 | unsigned char *pos; |
| 958 | |
| 959 | nb_buf = &qel->tx.crypto.nb_buf; |
| 960 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 961 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 962 | cf_len = len; |
| 963 | |
| 964 | while (len) { |
| 965 | size_t to_copy, room; |
| 966 | |
| 967 | pos = (*qcb)->data + (*qcb)->sz; |
| 968 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 969 | to_copy = len > room ? room : len; |
| 970 | if (to_copy) { |
| 971 | memcpy(pos, data, to_copy); |
| 972 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 973 | qel->tx.crypto.sz += to_copy; |
| 974 | (*qcb)->sz += to_copy; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 975 | len -= to_copy; |
| 976 | data += to_copy; |
| 977 | } |
| 978 | else { |
| 979 | struct quic_crypto_buf **tmp; |
| 980 | |
| 981 | tmp = realloc(qel->tx.crypto.bufs, |
| 982 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 983 | if (tmp) { |
| 984 | qel->tx.crypto.bufs = tmp; |
| 985 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 986 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 987 | if (!*qcb) |
| 988 | return 0; |
| 989 | |
| 990 | (*qcb)->sz = 0; |
| 991 | ++*nb_buf; |
| 992 | } |
| 993 | else { |
| 994 | break; |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1000 | * have been buffered. |
| 1001 | */ |
| 1002 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1003 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1004 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1005 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1006 | /* There is at most one CRYPTO frame in this packet number |
| 1007 | * space. Let's look for it. |
| 1008 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1009 | list_for_each_entry(frm, &qel->pktns->tx.frms, list) { |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1010 | if (frm->type != QUIC_FT_CRYPTO) |
| 1011 | continue; |
| 1012 | |
| 1013 | /* Found */ |
| 1014 | found = frm; |
| 1015 | break; |
| 1016 | } |
| 1017 | |
| 1018 | if (found) { |
| 1019 | found->crypto.len += cf_len; |
| 1020 | } |
| 1021 | else { |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1022 | frm = pool_alloc(pool_head_quic_frame); |
| 1023 | if (!frm) |
| 1024 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1025 | |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1026 | frm->type = QUIC_FT_CRYPTO; |
| 1027 | frm->crypto.offset = cf_offset; |
| 1028 | frm->crypto.len = cf_len; |
| 1029 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1030 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1031 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | return len == 0; |
| 1035 | } |
| 1036 | |
| 1037 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1038 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1039 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1040 | { |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1041 | HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1042 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1043 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1046 | /* Set the application for <qc> QUIC connection. |
| 1047 | * Return 1 if succeeded, 0 if not. |
| 1048 | */ |
| 1049 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1050 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1051 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1052 | qc->app_ops = &h3_ops; |
| 1053 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1054 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1055 | else |
| 1056 | return 0; |
| 1057 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1058 | return 1; |
| 1059 | } |
| 1060 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1061 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1062 | * wants to provide the QUIC layer with CRYPTO data. |
| 1063 | * Returns 1 if succeeded, 0 if not. |
| 1064 | */ |
| 1065 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1066 | const uint8_t *data, size_t len) |
| 1067 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1068 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1069 | enum quic_tls_enc_level tel; |
| 1070 | struct quic_enc_level *qel; |
| 1071 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1072 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1073 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1074 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 1075 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1076 | goto out; |
| 1077 | } |
| 1078 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1079 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1080 | if (tel == -1) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1081 | TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1082 | goto err; |
| 1083 | } |
| 1084 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1085 | qel = &qc->els[tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1086 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1087 | TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1088 | goto err; |
| 1089 | } |
| 1090 | |
| 1091 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1092 | qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1093 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1094 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1095 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1096 | return 1; |
| 1097 | |
| 1098 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1099 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | int ha_quic_flush_flight(SSL *ssl) |
| 1104 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1105 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1106 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1107 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1108 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1109 | |
| 1110 | return 1; |
| 1111 | } |
| 1112 | |
| 1113 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1114 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1115 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1116 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1117 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1118 | quic_set_tls_alert(qc, alert); |
| 1119 | HA_ATOMIC_STORE(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1120 | return 1; |
| 1121 | } |
| 1122 | |
| 1123 | /* QUIC TLS methods */ |
| 1124 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1125 | #ifdef OPENSSL_IS_BORINGSSL |
| 1126 | .set_read_secret = ha_set_rsec, |
| 1127 | .set_write_secret = ha_set_wsec, |
| 1128 | #else |
| 1129 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1130 | #endif |
| 1131 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1132 | .flush_flight = ha_quic_flush_flight, |
| 1133 | .send_alert = ha_quic_send_alert, |
| 1134 | }; |
| 1135 | |
| 1136 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1137 | * Returns an error count. |
| 1138 | */ |
| 1139 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1140 | { |
| 1141 | struct proxy *curproxy = bind_conf->frontend; |
| 1142 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1143 | int cfgerr = 0; |
| 1144 | |
| 1145 | #if 0 |
| 1146 | /* XXX Did not manage to use this. */ |
| 1147 | const char *ciphers = |
| 1148 | "TLS_AES_128_GCM_SHA256:" |
| 1149 | "TLS_AES_256_GCM_SHA384:" |
| 1150 | "TLS_CHACHA20_POLY1305_SHA256:" |
| 1151 | "TLS_AES_128_CCM_SHA256"; |
| 1152 | #endif |
Frédéric Lécaille | 4b1fddc | 2021-07-01 17:09:05 +0200 | [diff] [blame] | 1153 | const char *groups = "X25519:P-256:P-384:P-521"; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1154 | long options = |
| 1155 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1156 | SSL_OP_SINGLE_ECDH_USE | |
| 1157 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1158 | SSL_CTX *ctx; |
| 1159 | |
| 1160 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1161 | bind_conf->initial_ctx = ctx; |
| 1162 | |
| 1163 | SSL_CTX_set_options(ctx, options); |
| 1164 | #if 0 |
| 1165 | if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) { |
| 1166 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' " |
| 1167 | "for bind '%s' at [%s:%d].\n", |
| 1168 | curproxy->id, ciphers, |
| 1169 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1170 | cfgerr++; |
| 1171 | } |
| 1172 | #endif |
| 1173 | |
| 1174 | if (SSL_CTX_set1_curves_list(ctx, groups) != 1) { |
| 1175 | ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' " |
| 1176 | "for bind '%s' at [%s:%d].\n", |
| 1177 | curproxy->id, groups, |
| 1178 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1179 | cfgerr++; |
| 1180 | } |
| 1181 | |
| 1182 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1183 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1184 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1185 | SSL_CTX_set_default_verify_paths(ctx); |
| 1186 | |
| 1187 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1188 | #ifdef OPENSSL_IS_BORINGSSL |
| 1189 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1190 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1191 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1192 | if (bind_conf->ssl_conf.early_data) { |
| 1193 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Frédéric Lécaille | ad3c07a | 2021-12-14 19:23:43 +0100 | [diff] [blame] | 1194 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1195 | } |
| 1196 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1197 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1198 | #else |
| 1199 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1200 | #endif |
| 1201 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1202 | #endif |
| 1203 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1204 | |
| 1205 | return cfgerr; |
| 1206 | } |
| 1207 | |
| 1208 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1209 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1210 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1211 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1212 | */ |
| 1213 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1214 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1215 | { |
| 1216 | uint64_t expected_pn = largest_pn + 1; |
| 1217 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1218 | uint64_t pn_hwin = pn_win / 2; |
| 1219 | uint64_t pn_mask = pn_win - 1; |
| 1220 | uint64_t candidate_pn; |
| 1221 | |
| 1222 | |
| 1223 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1224 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1225 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1226 | candidate_pn + pn_hwin <= expected_pn) |
| 1227 | return candidate_pn + pn_win; |
| 1228 | |
| 1229 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1230 | return candidate_pn - pn_win; |
| 1231 | |
| 1232 | return candidate_pn; |
| 1233 | } |
| 1234 | |
| 1235 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1236 | * cryptographic context. |
| 1237 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1238 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1239 | * <end> points to one byte past the end of this packet. |
| 1240 | * Returns 1 if succeeded, 0 if not. |
| 1241 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1242 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1243 | struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1244 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1245 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1246 | { |
| 1247 | int ret, outlen, i, pnlen; |
| 1248 | uint64_t packet_number; |
| 1249 | uint32_t truncated_pn = 0; |
| 1250 | unsigned char mask[5] = {0}; |
| 1251 | unsigned char *sample; |
| 1252 | EVP_CIPHER_CTX *cctx; |
| 1253 | unsigned char *hp_key; |
| 1254 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1255 | /* Check there is enough data in this packet. */ |
| 1256 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1257 | TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | cctx = EVP_CIPHER_CTX_new(); |
| 1262 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1263 | TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | ret = 0; |
| 1268 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1269 | |
| 1270 | hp_key = tls_ctx->rx.hp_key; |
| 1271 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1272 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1273 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1274 | TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1275 | goto out; |
| 1276 | } |
| 1277 | |
| 1278 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1279 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1280 | for (i = 0; i < pnlen; i++) { |
| 1281 | pn[i] ^= mask[i + 1]; |
| 1282 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1283 | } |
| 1284 | |
| 1285 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1286 | /* Store remaining information for this unprotected header */ |
| 1287 | pkt->pn = packet_number; |
| 1288 | pkt->pnl = pnlen; |
| 1289 | |
| 1290 | ret = 1; |
| 1291 | |
| 1292 | out: |
| 1293 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1294 | |
| 1295 | return ret; |
| 1296 | } |
| 1297 | |
| 1298 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1299 | * address, with <payload_len> as payload length, <aad> as address of |
| 1300 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1301 | * context. |
| 1302 | * Returns 1 if succeeded, 0 if not. |
| 1303 | */ |
| 1304 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1305 | unsigned char *aad, size_t aad_len, uint64_t pn, |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1306 | struct quic_tls_ctx *tls_ctx, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1307 | { |
| 1308 | unsigned char iv[12]; |
| 1309 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1310 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1311 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1312 | |
| 1313 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1314 | TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1315 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1319 | tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1320 | TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1321 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1325 | |
| 1326 | err: |
| 1327 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1328 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1329 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1333 | * Returns 1 if succeeded, 0 if not. |
| 1334 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1335 | static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1336 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1337 | int ret, kp_changed; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1338 | unsigned char iv[12]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1339 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1340 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1341 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1342 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1343 | |
| 1344 | kp_changed = 0; |
| 1345 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1346 | /* The two tested bits are not at the same position, |
| 1347 | * this is why they are first both inversed. |
| 1348 | */ |
| 1349 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1350 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1351 | /* The lowest packet number of a previous key phase |
| 1352 | * cannot be null if it really stores previous key phase |
| 1353 | * secrets. |
| 1354 | */ |
| 1355 | if (!pkt->qc->ku.prv_rx.pn) |
| 1356 | return 0; |
| 1357 | |
| 1358 | rx_iv = pkt->qc->ku.prv_rx.iv; |
| 1359 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1360 | } |
| 1361 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1362 | /* Next key phase */ |
| 1363 | kp_changed = 1; |
| 1364 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
| 1365 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1366 | } |
| 1367 | } |
| 1368 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1369 | |
| 1370 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1371 | return 0; |
| 1372 | |
| 1373 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1374 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1375 | tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1376 | if (!ret) |
| 1377 | return 0; |
| 1378 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1379 | /* Update the keys only if the packet decryption succeeded. */ |
| 1380 | if (kp_changed) { |
| 1381 | quic_tls_rotate_keys(pkt->qc); |
| 1382 | /* Toggle the Key Phase bit */ |
| 1383 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1384 | /* Store the lowest packet number received for the current key phase */ |
| 1385 | tls_ctx->rx.pn = pkt->pn; |
| 1386 | /* Prepare the next key update */ |
| 1387 | if (!quic_tls_key_update(pkt->qc)) |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1391 | /* Update the packet length (required to parse the frames). */ |
| 1392 | pkt->len = pkt->aad_len + ret; |
| 1393 | |
| 1394 | return 1; |
| 1395 | } |
| 1396 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1397 | /* Remove from <qcs> stream the acknowledged frames. |
| 1398 | * Never fails. |
| 1399 | */ |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1400 | static int qcs_try_to_consume(struct qcs *qcs) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1401 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1402 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1403 | struct eb64_node *frm_node; |
| 1404 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1405 | ret = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1406 | frm_node = eb64_first(&qcs->tx.acked_frms); |
| 1407 | while (frm_node) { |
| 1408 | struct quic_stream *strm; |
| 1409 | |
| 1410 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
| 1411 | if (strm->offset.key != qcs->tx.ack_offset) |
| 1412 | break; |
| 1413 | |
| 1414 | b_del(strm->buf, strm->len); |
| 1415 | qcs->tx.ack_offset += strm->len; |
| 1416 | frm_node = eb64_next(frm_node); |
| 1417 | eb64_delete(&strm->offset); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1418 | ret = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1419 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1420 | |
| 1421 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1422 | } |
| 1423 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1424 | /* Treat <frm> frame whose packet it is attached to has just been acknowledged. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1425 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1426 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1427 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1428 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1429 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1430 | TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1431 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1432 | switch (frm->type) { |
| 1433 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1434 | { |
| 1435 | struct qcs *qcs = frm->stream.qcs; |
| 1436 | struct quic_stream *strm = &frm->stream; |
| 1437 | |
| 1438 | if (qcs->tx.ack_offset == strm->offset.key) { |
| 1439 | b_del(strm->buf, strm->len); |
| 1440 | qcs->tx.ack_offset += strm->len; |
| 1441 | LIST_DELETE(&frm->list); |
| 1442 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1443 | stream_acked = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1444 | } |
| 1445 | else { |
| 1446 | eb64_insert(&qcs->tx.acked_frms, &strm->offset); |
| 1447 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1448 | stream_acked |= qcs_try_to_consume(qcs); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1449 | } |
| 1450 | break; |
| 1451 | default: |
| 1452 | LIST_DELETE(&frm->list); |
| 1453 | pool_free(pool_head_quic_frame, frm); |
| 1454 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1455 | |
| 1456 | if (stream_acked) { |
| 1457 | struct qcc *qcc = qc->qcc; |
| 1458 | |
| 1459 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1460 | tasklet_wakeup(qcc->subs->tasklet); |
| 1461 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1462 | if (!qcc->subs->events) |
| 1463 | qcc->subs = NULL; |
| 1464 | } |
| 1465 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1469 | * deallocating them, and their TX frames. |
| 1470 | * Returns the last node reached to be used for the next range. |
| 1471 | * May be NULL if <largest> node could not be found. |
| 1472 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1473 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1474 | struct eb_root *pkts, |
| 1475 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1476 | struct list *newly_acked_pkts, |
| 1477 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1478 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1479 | { |
| 1480 | struct eb64_node *node; |
| 1481 | struct quic_tx_packet *pkt; |
| 1482 | |
| 1483 | if (largest_node) |
| 1484 | node = largest_node; |
| 1485 | else { |
| 1486 | node = eb64_lookup(pkts, largest); |
| 1487 | while (!node && largest > smallest) { |
| 1488 | node = eb64_lookup(pkts, --largest); |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1493 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1494 | |
| 1495 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1496 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1497 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1498 | TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1499 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1500 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1501 | node = eb64_prev(node); |
| 1502 | eb64_delete(&pkt->pn_node); |
| 1503 | } |
| 1504 | |
| 1505 | return node; |
| 1506 | } |
| 1507 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1508 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1509 | * same order they have been sent into <pktns_frm_list>. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1510 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1511 | static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1512 | struct list *pkt_frm_list, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1513 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1514 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1515 | struct quic_frame *frm, *frmbak; |
| 1516 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1517 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1518 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1519 | LIST_DELETE(&frm->list); |
| 1520 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1521 | LIST_APPEND(&tmp, &frm->list); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1522 | } |
| 1523 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1524 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1525 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1526 | |
| 1527 | /* Free the TX packets of <pkts> list */ |
| 1528 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1529 | { |
| 1530 | struct quic_tx_packet *pkt, *tmp; |
| 1531 | |
| 1532 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1533 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1534 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1535 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | /* Send a packet loss event nofification to the congestion controller |
| 1540 | * attached to <qc> connection with <lost_bytes> the number of lost bytes, |
| 1541 | * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet |
| 1542 | * at <now_us> current time. |
| 1543 | * Always succeeds. |
| 1544 | */ |
| 1545 | static inline void qc_cc_loss_event(struct quic_conn *qc, |
| 1546 | unsigned int lost_bytes, |
| 1547 | unsigned int newest_time_sent, |
| 1548 | unsigned int period, |
| 1549 | unsigned int now_us) |
| 1550 | { |
| 1551 | struct quic_cc_event ev = { |
| 1552 | .type = QUIC_CC_EVT_LOSS, |
| 1553 | .loss.now_ms = now_ms, |
| 1554 | .loss.max_ack_delay = qc->max_ack_delay, |
| 1555 | .loss.lost_bytes = lost_bytes, |
| 1556 | .loss.newest_time_sent = newest_time_sent, |
| 1557 | .loss.period = period, |
| 1558 | }; |
| 1559 | |
| 1560 | quic_cc_event(&qc->path->cc, &ev); |
| 1561 | } |
| 1562 | |
| 1563 | /* Send a packet ack event nofication for each newly acked packet of |
| 1564 | * <newly_acked_pkts> list and free them. |
| 1565 | * Always succeeds. |
| 1566 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1567 | static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1568 | struct list *newly_acked_pkts) |
| 1569 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1570 | struct quic_tx_packet *pkt, *tmp; |
| 1571 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1572 | |
| 1573 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1574 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1575 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1576 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1577 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1578 | ev.ack.acked = pkt->in_flight_len; |
| 1579 | ev.ack.time_sent = pkt->time_sent; |
| 1580 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1581 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1582 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1583 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | } |
| 1587 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1588 | /* Release all the frames attached to <pktns> packet number space */ |
| 1589 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1590 | { |
| 1591 | struct quic_frame *frm, *frmbak; |
| 1592 | |
| 1593 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1594 | LIST_DELETE(&frm->list); |
| 1595 | pool_free(pool_head_quic_frame, frm); |
| 1596 | } |
| 1597 | } |
| 1598 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1599 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1600 | * their TX frames. |
| 1601 | * Send a packet loss event to the congestion controller if |
| 1602 | * in flight packet have been lost. |
| 1603 | * Also frees the packet in <pkts> list. |
| 1604 | * Never fails. |
| 1605 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1606 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1607 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1608 | struct list *pkts, |
| 1609 | uint64_t now_us) |
| 1610 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1611 | struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1612 | uint64_t lost_bytes; |
| 1613 | |
| 1614 | lost_bytes = 0; |
| 1615 | oldest_lost = newest_lost = NULL; |
| 1616 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1617 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1618 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1619 | lost_bytes += pkt->in_flight_len; |
| 1620 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1621 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1622 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1623 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1624 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1625 | qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1626 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1627 | if (!oldest_lost) { |
| 1628 | oldest_lost = newest_lost = pkt; |
| 1629 | } |
| 1630 | else { |
| 1631 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1632 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1633 | newest_lost = pkt; |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | if (lost_bytes) { |
| 1638 | /* Sent a packet loss event to the congestion controller. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1639 | qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1640 | newest_lost->time_sent - oldest_lost->time_sent, now_us); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1641 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1642 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1643 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1648 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1649 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1650 | * space if any not deemed lost. |
| 1651 | * Should be called after having received an ACK frame with newly acknowledged |
| 1652 | * packets or when the the loss detection timer has expired. |
| 1653 | * Always succeeds. |
| 1654 | */ |
| 1655 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1656 | struct quic_conn *qc, |
| 1657 | struct list *lost_pkts) |
| 1658 | { |
| 1659 | struct eb_root *pkts; |
| 1660 | struct eb64_node *node; |
| 1661 | struct quic_loss *ql; |
| 1662 | unsigned int loss_delay; |
| 1663 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1664 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1665 | pkts = &pktns->tx.pkts; |
| 1666 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1667 | if (eb_is_empty(pkts)) |
| 1668 | goto out; |
| 1669 | |
| 1670 | ql = &qc->path->loss; |
| 1671 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
| 1672 | loss_delay += loss_delay >> 3; |
| 1673 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1674 | |
| 1675 | node = eb64_first(pkts); |
| 1676 | while (node) { |
| 1677 | struct quic_tx_packet *pkt; |
| 1678 | int64_t largest_acked_pn; |
| 1679 | unsigned int loss_time_limit, time_sent; |
| 1680 | |
| 1681 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1682 | largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1683 | node = eb64_next(node); |
| 1684 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1685 | break; |
| 1686 | |
| 1687 | time_sent = pkt->time_sent; |
| 1688 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1689 | if (tick_is_le(time_sent, now_ms) || |
| 1690 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1691 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1692 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1693 | } |
| 1694 | else { |
Frédéric Lécaille | dc90c07 | 2021-12-27 18:15:27 +0100 | [diff] [blame] | 1695 | if (tick_isset(pktns->tx.loss_time)) |
| 1696 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1697 | else |
| 1698 | pktns->tx.loss_time = loss_time_limit; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1703 | TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1707 | * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1708 | * if the largest acked packet was newly acked and if there was at least one newly |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1709 | * acked ack-eliciting packet. |
| 1710 | * Return 1, if succeeded, 0 if not. |
| 1711 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1712 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1713 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1714 | struct quic_enc_level *qel, |
| 1715 | unsigned int *rtt_sample, |
| 1716 | const unsigned char **pos, const unsigned char *end) |
| 1717 | { |
| 1718 | struct quic_ack *ack = &frm->ack; |
| 1719 | uint64_t smallest, largest; |
| 1720 | struct eb_root *pkts; |
| 1721 | struct eb64_node *largest_node; |
| 1722 | unsigned int time_sent, pkt_flags; |
| 1723 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1724 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1725 | |
| 1726 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1727 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1728 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1729 | goto err; |
| 1730 | } |
| 1731 | |
| 1732 | if (ack->first_ack_range > ack->largest_ack) { |
| 1733 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1734 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1735 | goto err; |
| 1736 | } |
| 1737 | |
| 1738 | largest = ack->largest_ack; |
| 1739 | smallest = largest - ack->first_ack_range; |
| 1740 | pkts = &qel->pktns->tx.pkts; |
| 1741 | pkt_flags = 0; |
| 1742 | largest_node = NULL; |
| 1743 | time_sent = 0; |
| 1744 | |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1745 | if ((int64_t)ack->largest_ack > HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1746 | largest_node = eb64_lookup(pkts, largest); |
| 1747 | if (!largest_node) { |
| 1748 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1749 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1750 | } |
| 1751 | else { |
| 1752 | time_sent = eb64_entry(&largest_node->node, |
| 1753 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1754 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1758 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1759 | do { |
| 1760 | uint64_t gap, ack_range; |
| 1761 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1762 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1763 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1764 | if (!ack->ack_range_num--) |
| 1765 | break; |
| 1766 | |
| 1767 | if (!quic_dec_int(&gap, pos, end)) |
| 1768 | goto err; |
| 1769 | |
| 1770 | if (smallest < gap + 2) { |
| 1771 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1772 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1773 | goto err; |
| 1774 | } |
| 1775 | |
| 1776 | largest = smallest - gap - 2; |
| 1777 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1778 | goto err; |
| 1779 | |
| 1780 | if (largest < ack_range) { |
| 1781 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1782 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1783 | goto err; |
| 1784 | } |
| 1785 | |
| 1786 | /* Do not use this node anymore. */ |
| 1787 | largest_node = NULL; |
| 1788 | /* Next range */ |
| 1789 | smallest = largest - ack_range; |
| 1790 | |
| 1791 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1792 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1793 | } while (1); |
| 1794 | |
| 1795 | /* Flag this packet number space as having received an ACK. */ |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 1796 | HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1797 | |
| 1798 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1799 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1800 | HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1804 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1805 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1806 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1807 | qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1808 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1809 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 1810 | if (quic_peer_validated_addr(qc)) |
| 1811 | qc->path->loss.pto_count = 0; |
| 1812 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | |
| 1816 | return 1; |
| 1817 | |
| 1818 | err: |
| 1819 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1820 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1821 | return 0; |
| 1822 | } |
| 1823 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 1824 | /* This function gives the detail of the SSL error. It is used only |
| 1825 | * if the debug mode and the verbose mode are activated. It dump all |
| 1826 | * the SSL error until the stack was empty. |
| 1827 | */ |
| 1828 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 1829 | { |
| 1830 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 1831 | while (1) { |
| 1832 | unsigned long ret; |
| 1833 | |
| 1834 | ret = ERR_get_error(); |
| 1835 | if (!ret) |
| 1836 | return; |
| 1837 | |
| 1838 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 1839 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 1840 | } |
| 1841 | } |
| 1842 | } |
| 1843 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1844 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 1845 | const char **str, int *len); |
| 1846 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1847 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 1848 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1849 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1850 | * Return 1 if succeeded, 0 if not. |
| 1851 | */ |
| 1852 | static inline int qc_provide_cdata(struct quic_enc_level *el, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1853 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1854 | const unsigned char *data, size_t len, |
| 1855 | struct quic_rx_packet *pkt, |
| 1856 | struct quic_rx_crypto_frm *cf) |
| 1857 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1858 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 1859 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1860 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1861 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 1862 | qc = ctx->qc; |
| 1863 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1864 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 1865 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1866 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 1867 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1868 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1869 | goto err; |
| 1870 | } |
| 1871 | |
| 1872 | el->rx.crypto.offset += len; |
| 1873 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 1874 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1875 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1876 | state = HA_ATOMIC_LOAD(&qc->state); |
| 1877 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1878 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 1879 | if (ssl_err != 1) { |
| 1880 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1881 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1882 | TRACE_PROTO("SSL handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1883 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1884 | goto out; |
| 1885 | } |
| 1886 | |
| 1887 | TRACE_DEVEL("SSL handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1888 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 1889 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 1890 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1891 | goto err; |
| 1892 | } |
| 1893 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1894 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 1895 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1896 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 1897 | /* The connection is ready to be accepted. */ |
| 1898 | quic_accept_push_qc(qc); |
| 1899 | } |
| 1900 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1901 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 1902 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1903 | } else { |
| 1904 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 1905 | if (ssl_err != 1) { |
| 1906 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1907 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1908 | TRACE_DEVEL("SSL post handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1909 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1910 | goto out; |
| 1911 | } |
| 1912 | |
| 1913 | TRACE_DEVEL("SSL post handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1914 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1915 | goto err; |
| 1916 | } |
| 1917 | |
| 1918 | TRACE_PROTO("SSL post handshake succeeded", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1919 | QUIC_EV_CONN_HDSHK, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1920 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 1921 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1922 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1923 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1924 | return 1; |
| 1925 | |
| 1926 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1927 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1928 | return 0; |
| 1929 | } |
| 1930 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1931 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 1932 | * <pkt> RX packet. |
| 1933 | * Return it if succeeded, NULL if not. |
| 1934 | */ |
| 1935 | static inline |
| 1936 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 1937 | struct quic_rx_packet *pkt) |
| 1938 | { |
| 1939 | struct quic_rx_strm_frm *frm; |
| 1940 | |
| 1941 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 1942 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1943 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1944 | frm->len = stream_frm->len; |
| 1945 | frm->data = stream_frm->data; |
| 1946 | frm->pkt = pkt; |
| 1947 | } |
| 1948 | |
| 1949 | return frm; |
| 1950 | } |
| 1951 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1952 | /* Copy as most as possible STREAM data from <strm_frm> into <strm> stream. |
Frédéric Lécaille | 3fe7df8 | 2021-12-15 15:32:55 +0100 | [diff] [blame] | 1953 | * Also update <strm_frm> frame to reflect the data which have been consumed. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1954 | */ |
| 1955 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 1956 | { |
| 1957 | size_t ret; |
| 1958 | |
| 1959 | ret = 0; |
| 1960 | while (strm_frm->len) { |
| 1961 | size_t try; |
| 1962 | |
| 1963 | try = b_contig_space(buf); |
| 1964 | if (!try) |
| 1965 | break; |
| 1966 | |
| 1967 | if (try > strm_frm->len) |
| 1968 | try = strm_frm->len; |
| 1969 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1970 | strm_frm->len -= try; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1971 | strm_frm->offset.key += try; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1972 | b_add(buf, try); |
| 1973 | ret += try; |
| 1974 | } |
| 1975 | |
| 1976 | return ret; |
| 1977 | } |
| 1978 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 1979 | /* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer. |
| 1980 | * Also update <strm_frm> frame to reflect the data which have been consumed. |
| 1981 | */ |
| 1982 | static size_t qc_rx_strm_frm_cpy(struct buffer *buf, |
| 1983 | struct quic_rx_strm_frm *strm_frm) |
| 1984 | { |
| 1985 | size_t ret; |
| 1986 | |
| 1987 | ret = 0; |
| 1988 | while (strm_frm->len) { |
| 1989 | size_t try; |
| 1990 | |
| 1991 | try = b_contig_space(buf); |
| 1992 | if (!try) |
| 1993 | break; |
| 1994 | |
| 1995 | if (try > strm_frm->len) |
| 1996 | try = strm_frm->len; |
| 1997 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1998 | strm_frm->len -= try; |
| 1999 | strm_frm->offset_node.key += try; |
| 2000 | b_add(buf, try); |
| 2001 | ret += try; |
| 2002 | } |
| 2003 | |
| 2004 | return ret; |
| 2005 | } |
| 2006 | |
| 2007 | /* Process as much as possible RX STREAM frames received for <qcs> */ |
| 2008 | static size_t qc_treat_rx_strm_frms(struct qcs *qcs) |
| 2009 | { |
| 2010 | int total; |
| 2011 | struct eb64_node *frm_node; |
| 2012 | |
| 2013 | total = 0; |
| 2014 | frm_node = eb64_first(&qcs->rx.frms); |
| 2015 | while (frm_node) { |
| 2016 | int ret; |
| 2017 | struct quic_rx_strm_frm *frm; |
| 2018 | |
| 2019 | frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node); |
| 2020 | if (frm->offset_node.key != qcs->rx.offset) |
| 2021 | break; |
| 2022 | |
| 2023 | ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm); |
| 2024 | qcs->rx.offset += ret; |
| 2025 | total += ret; |
| 2026 | if (frm->len) { |
| 2027 | /* If there is remaining data in this frame |
| 2028 | * this is because the destination buffer is full. |
| 2029 | */ |
| 2030 | break; |
| 2031 | } |
| 2032 | |
| 2033 | frm_node = eb64_next(frm_node); |
| 2034 | quic_rx_packet_refdec(frm->pkt); |
| 2035 | eb64_delete(&frm->offset_node); |
| 2036 | pool_free(pool_head_quic_rx_strm_frm, frm); |
| 2037 | } |
| 2038 | |
| 2039 | return total; |
| 2040 | } |
| 2041 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2042 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2043 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2044 | * If not, the STREAM frame is stored to be treated again later. |
| 2045 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2046 | * Return 1 if succeeded, 0 if not. |
| 2047 | */ |
| 2048 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2049 | struct quic_stream *strm_frm, |
| 2050 | struct quic_conn *qc) |
| 2051 | { |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2052 | int total; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2053 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2054 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2055 | struct quic_rx_strm_frm *frm; |
| 2056 | |
| 2057 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2058 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2059 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2060 | return 0; |
| 2061 | } |
| 2062 | |
| 2063 | strm = eb64_entry(&strm_node->node, struct qcs, by_id); |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2064 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2065 | size_t diff; |
| 2066 | |
| 2067 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2068 | TRACE_PROTO("Already received STREAM data", |
| 2069 | QUIC_EV_CONN_PSTRM, qc); |
| 2070 | goto out; |
| 2071 | } |
| 2072 | |
| 2073 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2074 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2075 | strm_frm->offset.key = strm->rx.offset; |
| 2076 | strm_frm->len -= diff; |
| 2077 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2078 | } |
| 2079 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2080 | total = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2081 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2082 | int ret; |
| 2083 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2084 | if (!qc_get_buf(strm, &strm->rx.buf)) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2085 | goto store_frm; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2086 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2087 | |
| 2088 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2089 | total += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2090 | strm->rx.offset += ret; |
| 2091 | } |
| 2092 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2093 | total += qc_treat_rx_strm_frms(strm); |
| 2094 | if (total && qc->qcc->app_ops->decode_qcs(strm, strm_frm->fin, qc->qcc->ctx) < 0) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2095 | TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2096 | return 0; |
| 2097 | } |
| 2098 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2099 | if (!strm_frm->len) |
| 2100 | goto out; |
| 2101 | |
| 2102 | store_frm: |
| 2103 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2104 | if (!frm) { |
| 2105 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2106 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2107 | return 0; |
| 2108 | } |
| 2109 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2110 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2111 | quic_rx_packet_refinc(pkt); |
| 2112 | |
| 2113 | out: |
| 2114 | return 1; |
| 2115 | } |
| 2116 | |
| 2117 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2118 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2119 | * If not, the STREAM frame is stored to be treated again later. |
| 2120 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2121 | * Return 1 if succeeded, 0 if not. |
| 2122 | */ |
| 2123 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2124 | struct quic_stream *strm_frm, |
| 2125 | struct quic_conn *qc) |
| 2126 | { |
| 2127 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2128 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2129 | struct quic_rx_strm_frm *frm; |
| 2130 | size_t strm_frm_len; |
| 2131 | |
| 2132 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2133 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2134 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2135 | return 0; |
| 2136 | } |
| 2137 | |
| 2138 | strm = eb64_entry(&strm_node->node, struct qcs, by_id); |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2139 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2140 | size_t diff; |
| 2141 | |
| 2142 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2143 | TRACE_PROTO("Already received STREAM data", |
| 2144 | QUIC_EV_CONN_PSTRM, qc); |
| 2145 | goto out; |
| 2146 | } |
| 2147 | |
| 2148 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2149 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2150 | strm_frm->offset.key = strm->rx.offset; |
| 2151 | strm_frm->len -= diff; |
| 2152 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2156 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2157 | int ret; |
| 2158 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 2159 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2160 | goto store_frm; |
| 2161 | |
| 2162 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 2163 | * of bytes copied. |
| 2164 | */ |
| 2165 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 2166 | /* Inform the application of the arrival of this new stream */ |
| 2167 | if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2168 | TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2169 | return 0; |
| 2170 | } |
| 2171 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2172 | if (ret) |
| 2173 | qcs_notify_recv(strm); |
| 2174 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2175 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2176 | } |
| 2177 | /* Take this frame into an account for the stream flow control */ |
| 2178 | strm->rx.offset += strm_frm_len; |
| 2179 | /* It all the data were provided to the application, there is no need to |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2180 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2181 | */ |
| 2182 | if (!strm_frm->len) |
| 2183 | goto out; |
| 2184 | |
| 2185 | store_frm: |
| 2186 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2187 | if (!frm) { |
| 2188 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2189 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2190 | return 0; |
| 2191 | } |
| 2192 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2193 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2194 | quic_rx_packet_refinc(pkt); |
| 2195 | |
| 2196 | out: |
| 2197 | return 1; |
| 2198 | } |
| 2199 | |
| 2200 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2201 | struct quic_stream *strm_frm, |
| 2202 | struct quic_conn *qc) |
| 2203 | { |
| 2204 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2205 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2206 | else |
| 2207 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2208 | } |
| 2209 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2210 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2211 | static void qc_prep_fast_retrans(struct quic_enc_level *qel, |
| 2212 | struct quic_conn *qc) |
| 2213 | { |
| 2214 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2215 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2216 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2217 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2218 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2219 | pkts = &qel->pktns->tx.pkts; |
| 2220 | node = eb64_first(pkts); |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2221 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2222 | while (node) { |
| 2223 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2224 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2225 | break; |
| 2226 | node = eb64_next(node); |
| 2227 | } |
| 2228 | |
| 2229 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2230 | return; |
| 2231 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 2232 | qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | /* Prepare a fast retransmission during handshake after a client |
| 2236 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2237 | * up to two datagrams of Initial packets if did not receive all Initial packets |
| 2238 | * and resend them coalescing with others (Handshake here). |
| 2239 | * (Listener only). |
| 2240 | */ |
| 2241 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc) |
| 2242 | { |
| 2243 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2244 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2245 | struct quic_frame *frm, *frmbak; |
| 2246 | |
| 2247 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2248 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2249 | struct quic_enc_level *qel = iqel; |
| 2250 | struct eb_root *pkts; |
| 2251 | struct eb64_node *node; |
| 2252 | struct quic_tx_packet *pkt; |
| 2253 | struct list *tmp = &itmp; |
| 2254 | |
| 2255 | start: |
| 2256 | pkt = NULL; |
| 2257 | pkts = &qel->pktns->tx.pkts; |
| 2258 | node = eb64_first(pkts); |
| 2259 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2260 | while (node) { |
| 2261 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2262 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2263 | break; |
| 2264 | node = eb64_next(node); |
| 2265 | } |
| 2266 | |
| 2267 | if (!pkt) |
| 2268 | goto end; |
| 2269 | |
| 2270 | qel->pktns->tx.pto_probe += 1; |
| 2271 | requeue: |
| 2272 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 2273 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2274 | LIST_DELETE(&frm->list); |
| 2275 | LIST_APPEND(tmp, &frm->list); |
| 2276 | } |
| 2277 | |
| 2278 | if (qel == iqel) { |
| 2279 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2280 | pkt = pkt->next; |
| 2281 | tmp = &htmp; |
| 2282 | hqel->pktns->tx.pto_probe += 1; |
| 2283 | goto requeue; |
| 2284 | } |
| 2285 | |
| 2286 | qel = hqel; |
| 2287 | tmp = &htmp; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2288 | goto start; |
| 2289 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2290 | |
| 2291 | end: |
| 2292 | LIST_SPLICE(&iqel->pktns->tx.frms, &itmp); |
| 2293 | LIST_SPLICE(&hqel->pktns->tx.frms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2294 | } |
| 2295 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2296 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2297 | * as I/O handler context and <qel> as encryption level. |
| 2298 | * Returns 1 if succeeded, 0 if failed. |
| 2299 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2300 | static int qc_parse_pkt_frms(struct quic_rx_packet *pkt, struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2301 | struct quic_enc_level *qel) |
| 2302 | { |
| 2303 | struct quic_frame frm; |
| 2304 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2305 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2306 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2307 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2308 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2309 | /* Skip the AAD */ |
| 2310 | pos = pkt->data + pkt->aad_len; |
| 2311 | end = pkt->data + pkt->len; |
| 2312 | |
| 2313 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2314 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2315 | goto err; |
| 2316 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2317 | TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2318 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2319 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2320 | break; |
| 2321 | case QUIC_FT_PING: |
| 2322 | break; |
| 2323 | case QUIC_FT_ACK: |
| 2324 | { |
| 2325 | unsigned int rtt_sample; |
| 2326 | |
| 2327 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2328 | if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2329 | goto err; |
| 2330 | |
| 2331 | if (rtt_sample) { |
| 2332 | unsigned int ack_delay; |
| 2333 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2334 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2335 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ? |
| 2336 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2337 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2338 | quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc); |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2339 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2340 | break; |
| 2341 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2342 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2343 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2344 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2345 | { |
| 2346 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2347 | |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 2348 | if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) { |
| 2349 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2350 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2351 | |
| 2352 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2353 | cfdebug.len = frm.crypto.len; |
| 2354 | TRACE_PROTO("CRYPTO data discarded", |
| 2355 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2356 | break; |
| 2357 | } |
| 2358 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2359 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2360 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2361 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2362 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2363 | |
| 2364 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2365 | cfdebug.len = frm.crypto.len; |
| 2366 | /* Nothing to do */ |
| 2367 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2368 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2369 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2370 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2371 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2372 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2373 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2374 | else { |
| 2375 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2376 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2377 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2378 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2379 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2380 | cfdebug.len = frm.crypto.len; |
| 2381 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2382 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2383 | frm.crypto.len -= diff; |
| 2384 | frm.crypto.data += diff; |
| 2385 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2386 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2387 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2388 | |
| 2389 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2390 | /* XXX TO DO: <cf> is used only for the traces. */ |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2391 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2392 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2393 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2394 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2395 | if (!qc_provide_cdata(qel, ctx, |
| 2396 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2397 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2398 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2399 | |
| 2400 | break; |
| 2401 | } |
| 2402 | |
| 2403 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2404 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2405 | if (!cf) { |
| 2406 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2407 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2408 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2409 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2410 | |
| 2411 | cf->offset_node.key = frm.crypto.offset; |
| 2412 | cf->len = frm.crypto.len; |
| 2413 | cf->data = frm.crypto.data; |
| 2414 | cf->pkt = pkt; |
| 2415 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2416 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2417 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2418 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2419 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2420 | { |
| 2421 | struct quic_stream *stream = &frm.stream; |
| 2422 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2423 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2424 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2425 | goto err; |
| 2426 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2427 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2428 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2429 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2430 | goto err; |
| 2431 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2432 | break; |
| 2433 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2434 | case QUIC_FT_MAX_DATA: |
| 2435 | case QUIC_FT_MAX_STREAM_DATA: |
| 2436 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2437 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2438 | case QUIC_FT_DATA_BLOCKED: |
| 2439 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2440 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2441 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2442 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2443 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2444 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2445 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2446 | break; |
| 2447 | case QUIC_FT_CONNECTION_CLOSE: |
| 2448 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Amaury Denoyelle | 5154e7a | 2021-12-08 14:51:04 +0100 | [diff] [blame] | 2449 | /* warn the mux to close the connection */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2450 | qc->qcc->flags |= QC_CF_CC_RECV; |
| 2451 | tasklet_wakeup(qc->qcc->wait_event.tasklet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2452 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2453 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2454 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2455 | goto err; |
| 2456 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2457 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2458 | break; |
| 2459 | default: |
| 2460 | goto err; |
| 2461 | } |
| 2462 | } |
| 2463 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2464 | if (fast_retrans) |
| 2465 | qc_prep_hdshk_fast_retrans(qc); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2466 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2467 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2468 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2469 | * be discarded. |
| 2470 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2471 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2472 | int state = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2473 | |
| 2474 | if (state >= QUIC_HS_ST_SERVER_INITIAL) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2475 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2476 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2477 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2478 | qc_set_timer(ctx->qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2479 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 2480 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2481 | if (state < QUIC_HS_ST_SERVER_HANDSHAKE) |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2482 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2483 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2484 | } |
| 2485 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2486 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2487 | return 1; |
| 2488 | |
| 2489 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2490 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2491 | return 0; |
| 2492 | } |
| 2493 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2494 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2495 | * buffer. This is the responsibility of the caller to check there is enough |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2496 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2497 | * This function must be called only after having built a correct datagram. |
| 2498 | * Always succeeds. |
| 2499 | */ |
| 2500 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2501 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2502 | { |
| 2503 | write_u16(cb_wr(cbuf), dglen); |
| 2504 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2505 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2506 | } |
| 2507 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2508 | /* Prepare as much as possible packets into <qr> ring buffer for |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2509 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2510 | * several packets in the same datagram. A header made of two fields is added |
| 2511 | * to each datagram: the datagram length followed by the address of the first |
| 2512 | * packet in this datagram. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2513 | * Returns 1 if succeeded, or 0 if something wrong happened. |
| 2514 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 2515 | static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr, |
| 2516 | enum quic_tls_enc_level tel, |
| 2517 | enum quic_tls_enc_level next_tel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2518 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2519 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2520 | struct cbuf *cbuf; |
| 2521 | unsigned char *end_buf, *end, *pos, *spos; |
| 2522 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2523 | /* length of datagrams */ |
| 2524 | uint16_t dglen; |
| 2525 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2526 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2527 | /* Each datagram is prepended with its length followed by the |
| 2528 | * address of the first packet in the datagram. |
| 2529 | */ |
| 2530 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2531 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2532 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2533 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2534 | total = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2535 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2536 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2537 | padding = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2538 | qel = &qc->els[tel]; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2539 | cbuf = qr->cbuf; |
| 2540 | spos = pos = cb_wr(cbuf); |
| 2541 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2542 | * to ensure there is enough room to mark the end of prepared |
| 2543 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2544 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2545 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2546 | first_pkt = prv_pkt = NULL; |
| 2547 | while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2548 | int err, probe, ack, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2549 | enum quic_pkt_type pkt_type; |
| 2550 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2551 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2552 | probe = ack = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 2553 | cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 2554 | if (!cc) { |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 2555 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2556 | ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2557 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2558 | /* Do not build any more packet if the TX secrets are not available or |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2559 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2560 | * and if there is no more packets to send upon PTO expiration |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2561 | * and if there is no more CRYPTO data available or in flight |
| 2562 | * congestion control limit is reached for prepared data |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2563 | */ |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2564 | if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) || |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2565 | (!cc && !ack && !probe && |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2566 | (LIST_ISEMPTY(&qel->pktns->tx.frms) || |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2567 | qc->path->prep_in_flight >= qc->path->cwnd))) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2568 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2569 | /* Set the current datagram as prepared into <cbuf> if |
| 2570 | * the was already a correct packet which was previously written. |
| 2571 | */ |
| 2572 | if (prv_pkt) |
| 2573 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2574 | /* Let's select the next encryption level */ |
| 2575 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 2576 | tel = next_tel; |
| 2577 | qel = &qc->els[tel]; |
| 2578 | /* Build a new datagram */ |
| 2579 | prv_pkt = NULL; |
| 2580 | continue; |
| 2581 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2582 | break; |
| 2583 | } |
| 2584 | |
| 2585 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2586 | if (!prv_pkt) { |
| 2587 | /* Leave room for the datagram header */ |
| 2588 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2589 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 2590 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2591 | } |
| 2592 | else { |
| 2593 | end = pos + qc->path->mtu; |
| 2594 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2595 | } |
| 2596 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2597 | cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2598 | pkt_type, ack, probe, cc, &err); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2599 | /* Restore the PTO dgrams counter if a packet could not be built */ |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2600 | if (err < 0) { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2601 | if (ack) |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2602 | HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2603 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2604 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2605 | case -2: |
| 2606 | goto err; |
| 2607 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2608 | /* If there was already a correct packet present, set the |
| 2609 | * current datagram as prepared into <cbuf>. |
| 2610 | */ |
| 2611 | if (prv_pkt) { |
| 2612 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2613 | goto stop_build; |
| 2614 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2615 | goto out; |
| 2616 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2617 | break; |
| 2618 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2619 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2620 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2621 | if (!cur_pkt) |
| 2622 | goto err; |
| 2623 | |
| 2624 | total += cur_pkt->len; |
| 2625 | /* keep trace of the first packet in the datagram */ |
| 2626 | if (!first_pkt) |
| 2627 | first_pkt = cur_pkt; |
| 2628 | /* Attach the current one to the previous one */ |
| 2629 | if (prv_pkt) |
| 2630 | prv_pkt->next = cur_pkt; |
| 2631 | /* Let's say we have to build a new dgram */ |
| 2632 | prv_pkt = NULL; |
| 2633 | dglen += cur_pkt->len; |
| 2634 | /* Client: discard the Initial encryption keys as soon as |
| 2635 | * a handshake packet could be built. |
| 2636 | */ |
| 2637 | if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL && |
| 2638 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2639 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2640 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 2641 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2642 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2643 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 2644 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2645 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE); |
| 2646 | } |
| 2647 | /* If the data for the current encryption level have all been sent, |
| 2648 | * select the next level. |
| 2649 | */ |
| 2650 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2651 | (LIST_ISEMPTY(&qel->pktns->tx.frms))) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2652 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2653 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2654 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 2655 | tel = next_tel; |
| 2656 | qel = &qc->els[tel]; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2657 | if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2658 | /* If there is data for the next level, do not |
| 2659 | * consume a datagram. |
| 2660 | */ |
| 2661 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2662 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2663 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2664 | /* If we have to build a new datagram, set the current datagram as |
| 2665 | * prepared into <cbuf>. |
| 2666 | */ |
| 2667 | if (!prv_pkt) { |
| 2668 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2669 | first_pkt = NULL; |
| 2670 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2671 | padding = 0; |
| 2672 | } |
| 2673 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 2674 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2675 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2676 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2677 | } |
| 2678 | } |
| 2679 | |
| 2680 | stop_build: |
| 2681 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2682 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
| 2683 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2684 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2685 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2686 | if (cb_contig_space(cbuf) >= sizeof(uint16_t)) { |
| 2687 | if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) { |
| 2688 | /* Mark the end of contiguous data for the reader */ |
| 2689 | write_u16(cb_wr(cbuf), 0); |
| 2690 | cb_add(cbuf, sizeof(uint16_t)); |
| 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | if (rd && rd <= cbuf->wr) { |
| 2695 | cb_wr_reset(cbuf); |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2696 | /* Let's try to reuse this buffer */ |
| 2697 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2698 | } |
| 2699 | } |
| 2700 | |
| 2701 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2702 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2703 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2704 | |
| 2705 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2706 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2707 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | /* Send the QUIC packets which have been prepared for QUIC connections |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2711 | * from <qr> ring buffer with <ctx> as I/O handler context. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2712 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2713 | int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2714 | { |
| 2715 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2716 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2717 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2718 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2719 | cbuf = qr->cbuf; |
| 2720 | while (cb_contig_data(cbuf)) { |
| 2721 | unsigned char *pos; |
| 2722 | struct buffer tmpbuf = { }; |
| 2723 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 2724 | uint16_t dglen; |
| 2725 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2726 | unsigned int time_sent; |
| 2727 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2728 | pos = cb_rd(cbuf); |
| 2729 | dglen = read_u16(pos); |
| 2730 | /* End of prepared datagrams. |
| 2731 | * Reset the reader index only if in front of the writer index. |
| 2732 | */ |
| 2733 | if (!dglen) { |
| 2734 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 2735 | |
| 2736 | if (wr && wr < cbuf->rd) { |
| 2737 | cb_rd_reset(cbuf); |
| 2738 | continue; |
| 2739 | } |
| 2740 | break; |
| 2741 | } |
| 2742 | |
| 2743 | pos += sizeof dglen; |
| 2744 | first_pkt = read_ptr(pos); |
| 2745 | pos += sizeof first_pkt; |
| 2746 | tmpbuf.area = (char *)pos; |
| 2747 | tmpbuf.size = tmpbuf.data = dglen; |
| 2748 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2749 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2750 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2751 | quic_tx_packet_refinc(pkt); |
Amaury Denoyelle | 58a7704 | 2022-02-09 15:43:07 +0100 | [diff] [blame] | 2752 | if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2753 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2754 | quic_tx_packet_refdec(pkt); |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 2755 | break; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2756 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2757 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2758 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2759 | qc->tx.bytes += tmpbuf.data; |
| 2760 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2761 | |
| 2762 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 2763 | pkt->time_sent = time_sent; |
| 2764 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 2765 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 2766 | qc->path->ifae_pkts++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2767 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2768 | qc->path->in_flight += pkt->in_flight_len; |
| 2769 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 2770 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2771 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2772 | TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2773 | next_pkt = pkt->next; |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 2774 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2775 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2776 | } |
| 2777 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2778 | |
| 2779 | return 1; |
| 2780 | } |
| 2781 | |
| 2782 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 2783 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 2784 | * a HANDSHAKE_DONE frame. |
| 2785 | * Return 1 if succeeded, 0 if not. |
| 2786 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2787 | static int quic_build_post_handshake_frames(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2788 | { |
| 2789 | int i; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2790 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2791 | struct quic_frame *frm; |
| 2792 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2793 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2794 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 2795 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2796 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 2797 | if (!frm) |
| 2798 | return 0; |
| 2799 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2800 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2801 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2802 | } |
| 2803 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2804 | for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2805 | struct quic_connection_id *cid; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 2806 | struct listener *l = qc->li; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2807 | |
| 2808 | frm = pool_alloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2809 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2810 | goto err; |
| 2811 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 2812 | cid = new_quic_cid(&qc->cids, qc, i); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2813 | if (!cid) |
| 2814 | goto err; |
| 2815 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 2816 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 2817 | ebmb_insert(&l->rx.dghdlrs[tid]->cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2818 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2819 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2820 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2821 | } |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 2822 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2823 | |
| 2824 | return 1; |
| 2825 | |
| 2826 | err: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2827 | return 0; |
| 2828 | } |
| 2829 | |
| 2830 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2831 | void free_quic_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2832 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2833 | struct eb64_node *n; |
| 2834 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2835 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2836 | n = eb64_first(&arngs->root); |
| 2837 | while (n) { |
| 2838 | struct eb64_node *next; |
| 2839 | |
| 2840 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 2841 | next = eb64_next(n); |
| 2842 | eb64_delete(n); |
| 2843 | free(ar); |
| 2844 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2845 | } |
| 2846 | } |
| 2847 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2848 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 2849 | * descending order. |
| 2850 | */ |
| 2851 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 2852 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2853 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2854 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | |
| 2858 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 2859 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2860 | * Returns 1 if succeeded, 0 if not (no more element to remove). |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2861 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2862 | static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2863 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2864 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2865 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2866 | last = eb64_last(&arngs->root); |
| 2867 | while (last && arngs->enc_sz > limit) { |
| 2868 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2869 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2870 | prev = eb64_prev(last); |
| 2871 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2872 | return 0; |
| 2873 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2874 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 2875 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 2876 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 2877 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 2878 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 2879 | --arngs->sz; |
| 2880 | eb64_delete(last); |
| 2881 | pool_free(pool_head_quic_arng, last); |
| 2882 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | return 1; |
| 2886 | } |
| 2887 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2888 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 2889 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 2890 | { |
| 2891 | struct eb64_node *node, *next; |
| 2892 | struct quic_arng_node *ar, *ar_next; |
| 2893 | |
| 2894 | node = eb64_last(&arngs->root); |
| 2895 | if (!node) |
| 2896 | return; |
| 2897 | |
| 2898 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2899 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 2900 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 2901 | |
| 2902 | while ((next = eb64_prev(node))) { |
| 2903 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 2904 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 2905 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 2906 | node = next; |
| 2907 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2908 | } |
| 2909 | } |
| 2910 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2911 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 2912 | * Returns the ack range node which has been inserted if succeeded, NULL if not. |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2913 | */ |
| 2914 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2915 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 2916 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2917 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2918 | struct quic_arng_node *new_ar; |
| 2919 | |
| 2920 | new_ar = pool_alloc(pool_head_quic_arng); |
| 2921 | if (new_ar) { |
| 2922 | new_ar->first.key = ar->first; |
| 2923 | new_ar->last = ar->last; |
| 2924 | eb64_insert(&arngs->root, &new_ar->first); |
| 2925 | arngs->sz++; |
| 2926 | } |
| 2927 | |
| 2928 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | /* Update <arngs> tree of ACK ranges with <ar> as new ACK range value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2932 | * Note that this function computes the number of bytes required to encode |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2933 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2934 | * |
| 2935 | * Descending order |
| 2936 | * -------------> |
| 2937 | * range1 range2 |
| 2938 | * ..........|--------|..............|--------| |
| 2939 | * ^ ^ ^ ^ |
| 2940 | * | | | | |
| 2941 | * last1 first1 last2 first2 |
| 2942 | * ..........+--------+--------------+--------+...... |
| 2943 | * diff1 gap12 diff2 |
| 2944 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2945 | * To encode the previous list of ranges we must encode integers as follows in |
| 2946 | * descending order: |
| 2947 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2948 | * with diff1 = last1 - first1 |
| 2949 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2950 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2951 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2952 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2953 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 2954 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2955 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2956 | struct eb64_node *le; |
| 2957 | struct quic_arng_node *new_node; |
| 2958 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2959 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2960 | new = NULL; |
| 2961 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2962 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2963 | if (!new_node) |
| 2964 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2965 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2966 | goto out; |
| 2967 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2968 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2969 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 2970 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2971 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2972 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2973 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 2974 | |
| 2975 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2976 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2977 | else { |
| 2978 | struct quic_arng_node *le_ar = |
| 2979 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2980 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2981 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 2982 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2983 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2984 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2985 | if (le_ar->last + 1 >= ar->first) { |
| 2986 | le_ar->last = ar->last; |
| 2987 | new = le; |
| 2988 | new_node = le_ar; |
| 2989 | } |
| 2990 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2991 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2992 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2993 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 2994 | |
| 2995 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2996 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2997 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2998 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2999 | /* Verify that the new inserted node does not overlap the nodes |
| 3000 | * which follow it. |
| 3001 | */ |
| 3002 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3003 | struct eb64_node *next; |
| 3004 | struct quic_arng_node *next_node; |
| 3005 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3006 | while ((next = eb64_next(new))) { |
| 3007 | next_node = |
| 3008 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3009 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3010 | break; |
| 3011 | |
| 3012 | if (next_node->last > new_node->last) |
| 3013 | new_node->last = next_node->last; |
| 3014 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3015 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3016 | /* Decrement the size of these ranges. */ |
| 3017 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3018 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3019 | } |
| 3020 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3021 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3022 | quic_arngs_set_enc_sz(arngs); |
| 3023 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3024 | return 1; |
| 3025 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3026 | /* Remove the header protection of packets at <el> encryption level. |
| 3027 | * Always succeeds. |
| 3028 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3029 | static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3030 | { |
| 3031 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3032 | struct quic_rx_packet *pqpkt; |
| 3033 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3034 | struct quic_enc_level *app_qel; |
| 3035 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3036 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3037 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3038 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 3039 | if (el == app_qel && qc_is_listener(qc) && |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3040 | HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3041 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3042 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3043 | goto out; |
| 3044 | } |
| 3045 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3046 | mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3047 | if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3048 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3049 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3050 | TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3051 | /* XXX TO DO XXX */ |
| 3052 | } |
| 3053 | else { |
| 3054 | /* The AAD includes the packet number field */ |
| 3055 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3056 | /* Store the packet into the tree of packets to decrypt. */ |
| 3057 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3058 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3059 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3060 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3061 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3062 | TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3063 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3064 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3065 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3069 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3073 | * Return 1 if succeeded, 0 if not. |
| 3074 | */ |
| 3075 | static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3076 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3077 | { |
| 3078 | struct eb64_node *node; |
| 3079 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3080 | node = eb64_first(&el->rx.crypto.frms); |
| 3081 | while (node) { |
| 3082 | struct quic_rx_crypto_frm *cf; |
| 3083 | |
| 3084 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 3085 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3086 | break; |
| 3087 | |
| 3088 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3089 | goto err; |
| 3090 | |
| 3091 | node = eb64_next(node); |
| 3092 | quic_rx_packet_refdec(cf->pkt); |
| 3093 | eb64_delete(&cf->offset_node); |
| 3094 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3095 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3096 | return 1; |
| 3097 | |
| 3098 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3099 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3100 | return 0; |
| 3101 | } |
| 3102 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3103 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3104 | * This is the caller responsibility to check that <cur_el> is different of <next_el> |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3105 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3106 | * Return 1 if succeeded, 0 if not. |
| 3107 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3108 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3109 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3110 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3111 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3112 | int64_t largest_pn = -1; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3113 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3114 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3115 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3116 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3117 | qel = cur_el; |
| 3118 | next_tel: |
| 3119 | if (!qel) |
| 3120 | goto out; |
| 3121 | |
| 3122 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3123 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3124 | while (node) { |
| 3125 | struct quic_rx_packet *pkt; |
| 3126 | |
| 3127 | pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3128 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3129 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3130 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3131 | /* Drop the packet */ |
| 3132 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3133 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3134 | } |
| 3135 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3136 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3137 | /* Drop the packet */ |
| 3138 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3139 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3140 | } |
| 3141 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3142 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3143 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3144 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING && |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3145 | (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack)) |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 3146 | HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3147 | if (pkt->pn > largest_pn) |
| 3148 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3149 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3150 | if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3151 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3152 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3153 | } |
| 3154 | } |
| 3155 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3156 | eb64_delete(&pkt->pn_node); |
| 3157 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3158 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3159 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3160 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3161 | /* Update the largest packet number. */ |
| 3162 | if (largest_pn != -1) |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3163 | HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn); |
| 3164 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3165 | goto err; |
| 3166 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3167 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3168 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3169 | qel = next_el; |
| 3170 | goto next_tel; |
| 3171 | } |
| 3172 | |
| 3173 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3174 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3175 | return 1; |
| 3176 | |
| 3177 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3178 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3179 | return 0; |
| 3180 | } |
| 3181 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3182 | /* Check if it's possible to remove header protection for packets related to |
| 3183 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3184 | * |
| 3185 | * Return true if the operation is possible else false. |
| 3186 | */ |
| 3187 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3188 | { |
| 3189 | enum quic_tls_enc_level tel; |
| 3190 | |
| 3191 | if (!qel) |
| 3192 | return 0; |
| 3193 | |
| 3194 | tel = ssl_to_quic_enc_level(qel->level); |
| 3195 | |
| 3196 | /* check if tls secrets are available */ |
| 3197 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) |
| 3198 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
| 3199 | |
| 3200 | if (!(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) |
| 3201 | return 0; |
| 3202 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3203 | /* check if the connection layer is ready before using app level */ |
Frédéric Lécaille | 298931d | 2022-01-28 21:41:06 +0100 | [diff] [blame] | 3204 | if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) && |
| 3205 | qc->mux_state != QC_MUX_READY) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3206 | return 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3207 | |
| 3208 | return 1; |
| 3209 | } |
| 3210 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3211 | /* QUIC connection packet handler task. */ |
| 3212 | struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3213 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3214 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3215 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3216 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3217 | enum quic_tls_enc_level tel, next_tel; |
| 3218 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3219 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3220 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3221 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3222 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3223 | qc = ctx->qc; |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3224 | TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3225 | qr = NULL; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3226 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3227 | TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3228 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3229 | HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT); |
| 3230 | /* The I/O handler has been woken up by the dgram listener |
| 3231 | * after the anti-amplification was reached. |
| 3232 | */ |
| 3233 | qc_set_timer(qc); |
| 3234 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3235 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3236 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3237 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3238 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3239 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3240 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA])); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3241 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3242 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3243 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3244 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3245 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3246 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3247 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3248 | } |
| 3249 | else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3250 | goto err; |
| 3251 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3252 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3253 | next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3254 | |
| 3255 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3256 | /* Treat packets waiting for header packet protection decryption */ |
| 3257 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3258 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3259 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3260 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3261 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3262 | if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3263 | goto err; |
| 3264 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3265 | if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) && |
| 3266 | (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3267 | qel = next_qel; |
| 3268 | next_qel = NULL; |
| 3269 | goto next_level; |
| 3270 | } |
| 3271 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3272 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3273 | if (st >= QUIC_HS_ST_COMPLETE) { |
| 3274 | if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) && |
| 3275 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3276 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3277 | |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3278 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags & |
| 3279 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3280 | /* Discard the Handshake keys. */ |
| 3281 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3282 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3283 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3284 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3285 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 3286 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 3290 | /* There may be remaining handshake to build (acks) */ |
| 3291 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3292 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3293 | } |
| 3294 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3295 | if (!qr) |
| 3296 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
Frédéric Lécaille | bec186d | 2022-01-12 15:32:55 +0100 | [diff] [blame] | 3297 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 3298 | * be considered. |
| 3299 | */ |
| 3300 | if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0)) |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 3301 | goto err; |
| 3302 | ret = qc_prep_pkts(qc, qr, tel, next_tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3303 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3304 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3305 | else if (ret == 0) |
| 3306 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3307 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3308 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3309 | goto err; |
| 3310 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3311 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3312 | /* Check if there is something to do for the next level. |
| 3313 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3314 | if (next_qel && next_qel != qel && |
| 3315 | (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) && |
Frédéric Lécaille | 7d807c9 | 2021-12-06 08:56:38 +0100 | [diff] [blame] | 3316 | (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3317 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3318 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3319 | goto next_level; |
| 3320 | } |
| 3321 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3322 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3323 | TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3324 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3325 | |
| 3326 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3327 | if (qr) |
| 3328 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3329 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3330 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3331 | } |
| 3332 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3333 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3334 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 3335 | { |
| 3336 | int i; |
| 3337 | |
| 3338 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 3339 | if (qel->tx.crypto.bufs[i]) { |
| 3340 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 3341 | qel->tx.crypto.bufs[i] = NULL; |
| 3342 | } |
| 3343 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3344 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3348 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3349 | * Returns 1 if succeeded, 0 if not. |
| 3350 | */ |
| 3351 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 3352 | enum quic_tls_enc_level level) |
| 3353 | { |
| 3354 | struct quic_enc_level *qel; |
| 3355 | |
| 3356 | qel = &qc->els[level]; |
| 3357 | qel->level = quic_to_ssl_enc_level(level); |
| 3358 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 3359 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 3360 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 3361 | qel->tls_ctx.rx.flags = 0; |
| 3362 | qel->tls_ctx.tx.flags = 0; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 3363 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3364 | |
| 3365 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3366 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3367 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 3368 | qel->rx.crypto.offset = 0; |
| 3369 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3370 | |
| 3371 | /* Allocate only one buffer. */ |
| 3372 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 3373 | if (!qel->tx.crypto.bufs) |
| 3374 | goto err; |
| 3375 | |
| 3376 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 3377 | if (!qel->tx.crypto.bufs[0]) |
| 3378 | goto err; |
| 3379 | |
| 3380 | qel->tx.crypto.bufs[0]->sz = 0; |
| 3381 | qel->tx.crypto.nb_buf = 1; |
| 3382 | |
| 3383 | qel->tx.crypto.sz = 0; |
| 3384 | qel->tx.crypto.offset = 0; |
| 3385 | |
| 3386 | return 1; |
| 3387 | |
| 3388 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3389 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3390 | return 0; |
| 3391 | } |
| 3392 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3393 | /* Increment the <qc> refcount. |
| 3394 | * |
| 3395 | * This operation must be conducted when manipulating the quic_conn outside of |
| 3396 | * the connection pinned thread. These threads can only retrieve the connection |
| 3397 | * in the CID tree, so this function must be conducted under the CID lock. |
| 3398 | */ |
| 3399 | static inline void quic_conn_take(struct quic_conn *qc) |
| 3400 | { |
| 3401 | HA_ATOMIC_INC(&qc->refcount); |
| 3402 | } |
| 3403 | |
| 3404 | /* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the |
Ilya Shipitsin | 37d3e38 | 2022-01-07 14:46:15 +0500 | [diff] [blame] | 3405 | * subtraction, the quic_conn is freed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3406 | */ |
| 3407 | static void quic_conn_drop(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3408 | { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3409 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3410 | int i; |
| 3411 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3412 | if (!qc) |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3413 | return; |
| 3414 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3415 | if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1)) |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3416 | return; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3417 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3418 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3419 | if (conn_ctx) { |
| 3420 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3421 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3422 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3423 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3424 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3425 | quic_conn_enc_level_uninit(&qc->els[i]); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3426 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3427 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 3428 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3429 | TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3430 | } |
| 3431 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3432 | /* Release the quic_conn <qc>. It will decrement its refcount so that the |
| 3433 | * connection will be freed once all threads have finished to work with it. The |
| 3434 | * connection is removed from the CIDs tree and thus cannot be found by other |
Amaury Denoyelle | 760da3b | 2022-01-20 17:43:02 +0100 | [diff] [blame] | 3435 | * threads after it. The connection tasklet is killed. |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3436 | * |
| 3437 | * Do not use <qc> after it as it may be freed. This function must only be |
| 3438 | * called by the thread responsible of the quic_conn tasklet. |
| 3439 | */ |
| 3440 | static void quic_conn_release(struct quic_conn *qc) |
| 3441 | { |
Amaury Denoyelle | 760da3b | 2022-01-20 17:43:02 +0100 | [diff] [blame] | 3442 | struct ssl_sock_ctx *conn_ctx; |
| 3443 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3444 | /* remove the connection from receiver cids trees */ |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3445 | ebmb_delete(&qc->odcid_node); |
| 3446 | ebmb_delete(&qc->scid_node); |
| 3447 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3448 | |
Amaury Denoyelle | 760da3b | 2022-01-20 17:43:02 +0100 | [diff] [blame] | 3449 | /* Kill the tasklet. Do not use tasklet_free as this is not thread safe |
| 3450 | * as other threads may call tasklet_wakeup after this. |
| 3451 | */ |
| 3452 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
| 3453 | if (conn_ctx) |
| 3454 | tasklet_kill(conn_ctx->wait_event.tasklet); |
| 3455 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3456 | quic_conn_drop(qc); |
| 3457 | } |
| 3458 | |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3459 | void quic_close(struct connection *conn, void *xprt_ctx) |
| 3460 | { |
| 3461 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3462 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3463 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3464 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3465 | /* This task must be deleted by the connection-pinned thread. */ |
| 3466 | if (qc->timer_task) { |
| 3467 | task_destroy(qc->timer_task); |
| 3468 | qc->timer_task = NULL; |
| 3469 | } |
| 3470 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3471 | /* Next application data can be dropped. */ |
| 3472 | qc->mux_state = QC_MUX_RELEASED; |
| 3473 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3474 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 9c4da93 | 2022-01-21 14:54:58 +0100 | [diff] [blame] | 3475 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3476 | /* TODO for now release the quic_conn on notification by the upper |
| 3477 | * layer. It could be useful to delay it if there is remaining data to |
| 3478 | * send or data to be acked. |
| 3479 | */ |
| 3480 | quic_conn_release(qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3481 | } |
| 3482 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3483 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3484 | static struct task *process_timer(struct task *task, void *ctx, unsigned int state) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3485 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3486 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3487 | struct quic_conn *qc; |
| 3488 | struct quic_pktns *pktns; |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3489 | int i, st; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3490 | |
| 3491 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3492 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3493 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3494 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3495 | task->expire = TICK_ETERNITY; |
| 3496 | pktns = quic_loss_pktns(qc); |
| 3497 | if (tick_isset(pktns->tx.loss_time)) { |
| 3498 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 3499 | |
| 3500 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 3501 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3502 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 3503 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3504 | goto out; |
| 3505 | } |
| 3506 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3507 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3508 | if (qc->path->in_flight) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3509 | pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL); |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 3510 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 3511 | pktns->tx.pto_probe = 1; |
| 3512 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
| 3513 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1; |
| 3514 | } |
| 3515 | else { |
| 3516 | pktns->tx.pto_probe = 2; |
| 3517 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3518 | } |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3519 | else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3520 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3521 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3522 | |
| 3523 | if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3524 | hel->pktns->tx.pto_probe = 1; |
| 3525 | if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3526 | iel->pktns->tx.pto_probe = 1; |
| 3527 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3528 | |
| 3529 | for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 3530 | int j; |
| 3531 | |
| 3532 | if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc)) |
| 3533 | continue; |
| 3534 | |
| 3535 | for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++) |
| 3536 | qc_prep_fast_retrans(&qc->els[i], qc); |
| 3537 | } |
| 3538 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3539 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3540 | qc->path->loss.pto_count++; |
| 3541 | |
| 3542 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3543 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3544 | |
| 3545 | return task; |
| 3546 | } |
| 3547 | |
| 3548 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 3549 | * connections used to identify the first Initial packets of client connecting |
| 3550 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 3551 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 3552 | * <scid> is the source connection ID with <scid_len> as length. |
| 3553 | * Returns 1 if succeeded, 0 if not. |
| 3554 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3555 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3556 | unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3557 | unsigned char *scid, size_t scid_len, int server, void *owner) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3558 | { |
| 3559 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3560 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3561 | /* Initial CID. */ |
| 3562 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 3563 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3564 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3565 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3566 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3567 | qc = pool_zalloc(pool_head_quic_conn); |
| 3568 | if (!qc) { |
| 3569 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 3570 | goto err; |
| 3571 | } |
| 3572 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3573 | HA_ATOMIC_STORE(&qc->refcount, 0); |
| 3574 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3575 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 3576 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3577 | TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3578 | goto err; |
| 3579 | } |
| 3580 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3581 | qc->cids = EB_ROOT; |
| 3582 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3583 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3584 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3585 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 3586 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3587 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL); |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3588 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3589 | qc->odcid.len = dcid_len; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3590 | qc->odcid.addrlen = dcid_addr_len; |
| 3591 | memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3592 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 3593 | /* copy the packet SCID to reuse it as DCID for sending */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3594 | if (scid_len) |
| 3595 | memcpy(qc->dcid.data, scid, scid_len); |
| 3596 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 3597 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3598 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3599 | } |
| 3600 | /* QUIC Client (outgoing connection to servers) */ |
| 3601 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3602 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3603 | if (dcid_len) |
| 3604 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 3605 | qc->dcid.len = dcid_len; |
| 3606 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3607 | qc->mux_state = QC_MUX_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3608 | |
| 3609 | /* Initialize the output buffer */ |
| 3610 | qc->obuf.pos = qc->obuf.data; |
| 3611 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3612 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3613 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3614 | TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3615 | goto err; |
| 3616 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3617 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3618 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 3619 | if (server) |
| 3620 | ebmb_insert(&l->rx.dghdlrs[tid]->cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3621 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3622 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 3623 | qc->scid = icid->cid; |
| 3624 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3625 | /* Packet number spaces initialization. */ |
| 3626 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 3627 | quic_pktns_init(&qc->pktns[i]); |
| 3628 | /* QUIC encryption level context initialization. */ |
| 3629 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3630 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3631 | TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3632 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3633 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3634 | /* Initialize the packet number space. */ |
| 3635 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 3636 | } |
| 3637 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 3638 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 3639 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 3640 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 3641 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3642 | /* TX part. */ |
| 3643 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3644 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 3645 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 3646 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3647 | /* RX part. */ |
| 3648 | qc->rx.bytes = 0; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3649 | qc->rx.nb_ack_eliciting = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3650 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3651 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 3652 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3653 | TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 3654 | goto err; |
| 3655 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3656 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3657 | /* XXX TO DO: Only one path at this time. */ |
| 3658 | qc->path = &qc->paths[0]; |
| 3659 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 3660 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 3661 | /* required to use MTLIST_IN_LIST */ |
| 3662 | MT_LIST_INIT(&qc->accept_list); |
| 3663 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3664 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3665 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3666 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3667 | |
| 3668 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3669 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL); |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 3670 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 3671 | if (qc) |
| 3672 | qc->rx.buf.area = NULL; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3673 | quic_conn_drop(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3674 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | /* Initialize the timer task of <qc> QUIC connection. |
| 3678 | * Returns 1 if succeeded, 0 if not. |
| 3679 | */ |
| 3680 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 3681 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 3682 | /* Attach this task to the same thread ID used for the connection */ |
| 3683 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3684 | if (!qc->timer_task) |
| 3685 | return 0; |
| 3686 | |
| 3687 | qc->timer = TICK_ETERNITY; |
| 3688 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 3689 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3690 | |
| 3691 | return 1; |
| 3692 | } |
| 3693 | |
| 3694 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 3695 | * past one byte of this buffer. |
| 3696 | */ |
| 3697 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 3698 | struct quic_rx_packet *pkt) |
| 3699 | { |
| 3700 | unsigned char dcid_len, scid_len; |
| 3701 | |
| 3702 | /* Version */ |
| 3703 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 3704 | return 0; |
| 3705 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3706 | /* Destination Connection ID Length */ |
| 3707 | dcid_len = *(*buf)++; |
| 3708 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 3709 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 3710 | /* XXX MUST BE DROPPED */ |
| 3711 | return 0; |
| 3712 | |
| 3713 | if (dcid_len) { |
| 3714 | /* Check that the length of this received DCID matches the CID lengths |
| 3715 | * of our implementation for non Initials packets only. |
| 3716 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3717 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 3718 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3719 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3720 | return 0; |
| 3721 | |
| 3722 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 3723 | } |
| 3724 | |
| 3725 | pkt->dcid.len = dcid_len; |
| 3726 | *buf += dcid_len; |
| 3727 | |
| 3728 | /* Source Connection ID Length */ |
| 3729 | scid_len = *(*buf)++; |
| 3730 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 3731 | /* XXX MUST BE DROPPED */ |
| 3732 | return 0; |
| 3733 | |
| 3734 | if (scid_len) |
| 3735 | memcpy(pkt->scid.data, *buf, scid_len); |
| 3736 | pkt->scid.len = scid_len; |
| 3737 | *buf += scid_len; |
| 3738 | |
| 3739 | return 1; |
| 3740 | } |
| 3741 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3742 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 3743 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 3744 | { |
| 3745 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 3746 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3747 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3748 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 3749 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | /* Try to remove the header protection of <pkt> QUIC packet attached to <qc> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3753 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 3754 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 3755 | * the packet first byte. |
| 3756 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 3757 | * Returns 1 if succeeded, 0 if not. |
| 3758 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3759 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 3760 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3761 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3762 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3763 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3764 | { |
| 3765 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3766 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3767 | struct quic_enc_level *qel; |
| 3768 | /* Only for traces. */ |
| 3769 | struct quic_rx_packet *qpkt_trace; |
| 3770 | |
| 3771 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3772 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3773 | /* The packet number is here. This is also the start minus |
| 3774 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 3775 | * protection. |
| 3776 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3777 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3778 | |
| 3779 | tel = quic_packet_type_enc_level(pkt->type); |
| 3780 | qel = &qc->els[tel]; |
| 3781 | |
| 3782 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3783 | /* Note that the following function enables us to unprotect the packet |
| 3784 | * number and its length subsequently used to decrypt the entire |
| 3785 | * packets. |
| 3786 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3787 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 3788 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 3789 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3790 | goto err; |
| 3791 | } |
| 3792 | |
| 3793 | /* The AAD includes the packet number field found at <pn>. */ |
| 3794 | pkt->aad_len = pn - beg + pkt->pnl; |
| 3795 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3796 | } |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3797 | else if (qel) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3798 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 3799 | /* If the packet number space has been discarded, this packet |
| 3800 | * will be not parsed. |
| 3801 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3802 | TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3803 | goto out; |
| 3804 | } |
| 3805 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3806 | TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3807 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3808 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 3809 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3810 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3811 | else { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3812 | TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3813 | goto err; |
| 3814 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3815 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3816 | *el = qel; |
| 3817 | /* No reference counter incrementation here!!! */ |
| 3818 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 3819 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 3820 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 3821 | b_add(&qc->rx.buf, pkt->len); |
| 3822 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3823 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3824 | return 1; |
| 3825 | |
| 3826 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3827 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3828 | return 0; |
| 3829 | } |
| 3830 | |
| 3831 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 3832 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 3833 | */ |
| 3834 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 3835 | unsigned char byte0, int *long_header) |
| 3836 | { |
| 3837 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 3838 | pkt->type = |
| 3839 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 3840 | *long_header = 1; |
| 3841 | } |
| 3842 | else { |
| 3843 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 3844 | *long_header = 0; |
| 3845 | } |
| 3846 | } |
| 3847 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3848 | /* |
| 3849 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 3850 | */ |
| 3851 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 3852 | { |
| 3853 | int j = 0, version; |
| 3854 | |
| 3855 | do { |
| 3856 | version = quic_supported_version[j]; |
| 3857 | if (version == pkt->version) |
| 3858 | return 1; |
| 3859 | |
| 3860 | version = quic_supported_version[++j]; |
| 3861 | } while(version); |
| 3862 | |
| 3863 | return 0; |
| 3864 | } |
| 3865 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3866 | /* |
| 3867 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 3868 | * address <addr>. |
| 3869 | * Implementation of RFC9000 6. Version Negotiation |
| 3870 | * |
| 3871 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 3872 | * |
| 3873 | * Returns 0 on success else non-zero |
| 3874 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 3875 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 3876 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3877 | { |
| 3878 | char buf[256]; |
| 3879 | int i = 0, j, version; |
| 3880 | const socklen_t addrlen = get_addr_len(addr); |
| 3881 | |
| 3882 | /* |
| 3883 | * header form |
| 3884 | * long header, fixed bit to 0 for Version Negotiation |
| 3885 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3886 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 3887 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3888 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3889 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3890 | /* null version for Version Negotiation */ |
| 3891 | buf[i++] = '\x00'; |
| 3892 | buf[i++] = '\x00'; |
| 3893 | buf[i++] = '\x00'; |
| 3894 | buf[i++] = '\x00'; |
| 3895 | |
| 3896 | /* source connection id */ |
| 3897 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3898 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3899 | i += pkt->scid.len; |
| 3900 | |
| 3901 | /* destination connection id */ |
| 3902 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3903 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3904 | i += pkt->dcid.len; |
| 3905 | |
| 3906 | /* supported version */ |
| 3907 | j = 0; |
| 3908 | do { |
| 3909 | version = htonl(quic_supported_version[j]); |
| 3910 | memcpy(&buf[i], &version, sizeof(version)); |
| 3911 | i += sizeof(version); |
| 3912 | |
| 3913 | version = quic_supported_version[++j]; |
| 3914 | } while (version); |
| 3915 | |
| 3916 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 3917 | return 1; |
| 3918 | |
| 3919 | return 0; |
| 3920 | } |
| 3921 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 3922 | /* Generate the token to be used in Retry packets. The token is written to |
| 3923 | * <buf> which is expected to be <len> bytes. |
| 3924 | * |
| 3925 | * Various parameters are expected to be encoded in the token. For now, only |
| 3926 | * the DCID from <pkt> is stored. This is useful to implement a stateless Retry |
| 3927 | * as this CID must be repeated by the server in the transport parameters. |
| 3928 | * |
| 3929 | * TODO add the client address to validate the token origin. |
| 3930 | * |
| 3931 | * Returns the length of the encoded token or 0 on error. |
| 3932 | */ |
| 3933 | static int generate_retry_token(unsigned char *buf, unsigned char len, |
| 3934 | struct quic_rx_packet *pkt) |
| 3935 | { |
| 3936 | const size_t token_len = 1 + pkt->dcid.len; |
| 3937 | unsigned char i = 0; |
| 3938 | |
| 3939 | if (token_len > len) |
| 3940 | return 0; |
| 3941 | |
| 3942 | buf[i++] = pkt->dcid.len; |
| 3943 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 3944 | i += pkt->dcid.len; |
| 3945 | |
| 3946 | return i; |
| 3947 | } |
| 3948 | |
| 3949 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 3950 | * the Initial <pkt> packet. |
| 3951 | * |
| 3952 | * Returns 0 on success else non-zero. |
| 3953 | */ |
| 3954 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 3955 | struct quic_rx_packet *pkt) |
| 3956 | { |
| 3957 | unsigned char buf[128]; |
| 3958 | int i = 0, token_len; |
| 3959 | const socklen_t addrlen = get_addr_len(addr); |
| 3960 | struct quic_cid scid; |
| 3961 | |
| 3962 | /* long header + fixed bit + packet type 0x3 */ |
| 3963 | buf[i++] = 0xf0; |
| 3964 | /* version */ |
| 3965 | buf[i++] = 0x00; |
| 3966 | buf[i++] = 0x00; |
| 3967 | buf[i++] = 0x00; |
| 3968 | buf[i++] = 0x01; |
| 3969 | |
| 3970 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 3971 | buf[i++] = pkt->scid.len; |
| 3972 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 3973 | i += pkt->scid.len; |
| 3974 | |
| 3975 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 3976 | scid.len = QUIC_HAP_CID_LEN; |
| 3977 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 3978 | return 1; |
| 3979 | |
| 3980 | buf[i++] = scid.len; |
| 3981 | memcpy(&buf[i], scid.data, scid.len); |
| 3982 | i += scid.len; |
| 3983 | |
| 3984 | /* token */ |
| 3985 | if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt))) |
| 3986 | return 1; |
| 3987 | i += token_len; |
| 3988 | |
| 3989 | /* token integrity tag */ |
| 3990 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 3991 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 3992 | pkt->dcid.len, buf, i)) { |
| 3993 | return 1; |
| 3994 | } |
| 3995 | |
| 3996 | i += QUIC_TLS_TAG_LEN; |
| 3997 | |
| 3998 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 3999 | return 1; |
| 4000 | |
| 4001 | return 0; |
| 4002 | } |
| 4003 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4004 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 4005 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 4006 | * concatenated to the <pkt> DCID field. |
| 4007 | * |
| 4008 | * Returns the instance or NULL if not found. |
| 4009 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4010 | static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4011 | struct listener *l, |
| 4012 | struct sockaddr_storage *saddr) |
| 4013 | { |
| 4014 | struct quic_conn *qc = NULL; |
| 4015 | struct ebmb_node *node; |
| 4016 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4017 | /* set if the quic_conn is found in the second DCID tree */ |
| 4018 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4019 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4020 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 4021 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 4022 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 4023 | /* DCIDs of first packets coming from multiple clients may have |
| 4024 | * the same values. Let's distinguish them by concatenating the |
| 4025 | * socket addresses. |
| 4026 | */ |
| 4027 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4028 | node = ebmb_lookup(&l->rx.dghdlrs[tid]->odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4029 | pkt->dcid.len + pkt->dcid.addrlen); |
| 4030 | if (node) { |
| 4031 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 4032 | goto end; |
| 4033 | } |
| 4034 | } |
| 4035 | |
| 4036 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 4037 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 4038 | * used. |
| 4039 | */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4040 | node = ebmb_lookup(&l->rx.dghdlrs[tid]->cids, pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4041 | if (!node) |
| 4042 | goto end; |
| 4043 | |
| 4044 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 4045 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4046 | found_in_dcid = 1; |
| 4047 | |
| 4048 | end: |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4049 | if (qc) |
| 4050 | quic_conn_take(qc); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4051 | |
| 4052 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 4053 | * If already done, this is a noop. |
| 4054 | */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4055 | if (qc && found_in_dcid) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4056 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4057 | |
| 4058 | return qc; |
| 4059 | } |
| 4060 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4061 | /* Parse the Retry token from buffer <token> whose size is <token_len>. This |
| 4062 | * will extract the parameters stored in the token : <odcid>. |
| 4063 | * |
| 4064 | * Returns 0 on success else non-zero. |
| 4065 | */ |
| 4066 | static int parse_retry_token(const unsigned char *token, uint64_t token_len, |
| 4067 | struct quic_cid *odcid) |
| 4068 | { |
| 4069 | uint64_t odcid_len; |
| 4070 | |
| 4071 | if (!quic_dec_int(&odcid_len, &token, token + token_len)) |
| 4072 | return 1; |
| 4073 | |
| 4074 | memcpy(odcid->data, token, odcid_len); |
| 4075 | odcid->len = odcid_len; |
| 4076 | |
| 4077 | return 0; |
| 4078 | } |
| 4079 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4080 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 4081 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 4082 | * parameters of this session. |
| 4083 | * This is the responsibility of the caller to check the validity of all the |
| 4084 | * pointers passed as parameter to this function. |
| 4085 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 4086 | * CO_ER_SSL_NO_MEM. |
| 4087 | */ |
| 4088 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 4089 | unsigned char *params, size_t params_len) |
| 4090 | { |
| 4091 | int retry; |
| 4092 | |
| 4093 | retry = 1; |
| 4094 | retry: |
| 4095 | *ssl = SSL_new(ssl_ctx); |
| 4096 | if (!*ssl) { |
| 4097 | if (!retry--) |
| 4098 | goto err; |
| 4099 | |
| 4100 | pool_gc(NULL); |
| 4101 | goto retry; |
| 4102 | } |
| 4103 | |
| 4104 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 4105 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) || |
| 4106 | !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) { |
| 4107 | goto err; |
| 4108 | |
| 4109 | SSL_free(*ssl); |
| 4110 | *ssl = NULL; |
| 4111 | if (!retry--) |
| 4112 | goto err; |
| 4113 | |
| 4114 | pool_gc(NULL); |
| 4115 | goto retry; |
| 4116 | } |
| 4117 | |
| 4118 | return 0; |
| 4119 | |
| 4120 | err: |
| 4121 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 4122 | return -1; |
| 4123 | } |
| 4124 | |
| 4125 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 4126 | * used to process <qc> received packets. The allocated context is stored in |
| 4127 | * <qc.xprt_ctx>. |
| 4128 | * |
| 4129 | * Returns 0 on success else non-zero. |
| 4130 | */ |
| 4131 | int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 4132 | { |
| 4133 | struct bind_conf *bc = qc->li->bind_conf; |
| 4134 | struct ssl_sock_ctx *ctx = NULL; |
| 4135 | |
| 4136 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 4137 | if (!ctx) |
| 4138 | goto err; |
| 4139 | |
| 4140 | ctx->wait_event.tasklet = tasklet_new(); |
| 4141 | if (!ctx->wait_event.tasklet) |
| 4142 | goto err; |
| 4143 | |
| 4144 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 4145 | ctx->wait_event.tasklet->context = ctx; |
| 4146 | ctx->wait_event.events = 0; |
| 4147 | ctx->subs = NULL; |
| 4148 | ctx->xprt_ctx = NULL; |
| 4149 | ctx->qc = qc; |
| 4150 | |
| 4151 | /* Set tasklet tid based on the SCID selected by us for this |
| 4152 | * connection. The upper layer will also be binded on the same thread. |
| 4153 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 4154 | qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data); |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4155 | |
| 4156 | if (qc_is_listener(qc)) { |
| 4157 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 4158 | qc->enc_params, qc->enc_params_len) == -1) { |
| 4159 | goto err; |
| 4160 | } |
| 4161 | |
| 4162 | /* Enabling 0-RTT */ |
| 4163 | if (bc->ssl_conf.early_data) |
| 4164 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 4165 | |
| 4166 | SSL_set_accept_state(ctx->ssl); |
| 4167 | } |
| 4168 | |
| 4169 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 4170 | |
| 4171 | /* Store the allocated context in <qc>. */ |
| 4172 | HA_ATOMIC_STORE(&qc->xprt_ctx, ctx); |
| 4173 | |
| 4174 | return 0; |
| 4175 | |
| 4176 | err: |
| 4177 | if (ctx && ctx->wait_event.tasklet) |
| 4178 | tasklet_free(ctx->wait_event.tasklet); |
| 4179 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 4180 | |
| 4181 | return 1; |
| 4182 | } |
| 4183 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4184 | static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4185 | struct quic_rx_packet *pkt, int first_pkt, |
| 4186 | struct quic_dgram *dgram) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4187 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4188 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4189 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4190 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4191 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4192 | int long_header = 0, io_cb_wakeup = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4193 | size_t b_cspace; |
| 4194 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4195 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4196 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4197 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4198 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 4199 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 4200 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 4201 | /* This ist only to please to traces and distinguish the |
| 4202 | * packet with parsed packet number from others. |
| 4203 | */ |
| 4204 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4205 | if (end <= buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4206 | goto err; |
| 4207 | |
| 4208 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4209 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4210 | /* XXX TO BE DISCARDED */ |
| 4211 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4212 | goto err; |
| 4213 | } |
| 4214 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4215 | l = dgram->owner; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4216 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4217 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4218 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4219 | uint64_t len; |
| 4220 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4221 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4222 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4223 | goto err; |
| 4224 | } |
| 4225 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4226 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4227 | * they must have the same DCID. |
| 4228 | */ |
| 4229 | if (!first_pkt && |
| 4230 | (pkt->dcid.len != dgram->dcid_len || |
| 4231 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 4232 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 4233 | goto err; |
| 4234 | } |
| 4235 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4236 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 4237 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 4238 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4239 | goto err; |
| 4240 | } |
| 4241 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4242 | /* RFC9000 6. Version Negotiation */ |
| 4243 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4244 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4245 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4246 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 4247 | goto err; |
| 4248 | } |
| 4249 | |
| 4250 | TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4251 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4252 | } |
| 4253 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4254 | /* For Initial packets, and for servers (QUIC clients connections), |
| 4255 | * there is no Initial connection IDs storage. |
| 4256 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4257 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4258 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4259 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4260 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 4261 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4262 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4263 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4264 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4265 | |
Frédéric Lécaille | 055ee6c | 2022-01-25 21:21:56 +0100 | [diff] [blame] | 4266 | /* The token may be provided in a Retry packet or NEW_TOKEN frame |
| 4267 | * only by the QUIC server. |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4268 | */ |
| 4269 | pkt->token_len = token_len; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4270 | |
| 4271 | /* TODO Retry should be automatically activated if |
| 4272 | * suspect network usage is detected. |
| 4273 | */ |
| 4274 | if (!token_len && l->bind_conf->quic_force_retry) { |
| 4275 | TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4276 | if (send_retry(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4277 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 4278 | goto err; |
| 4279 | } |
| 4280 | |
| 4281 | goto err; |
| 4282 | } |
| 4283 | else { |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4284 | pkt->token = buf; |
| 4285 | buf += pkt->token_len; |
| 4286 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4287 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4288 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4289 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4290 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4291 | goto err; |
| 4292 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4293 | } |
| 4294 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4295 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 4296 | end - buf < len) { |
| 4297 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4298 | goto err; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4299 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4300 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4301 | payload = buf; |
| 4302 | pkt->len = len + payload - beg; |
| 4303 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4304 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4305 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4306 | int ipv4; |
| 4307 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4308 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4309 | const unsigned char *salt = initial_salt_v1; |
| 4310 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4311 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4312 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 4313 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4314 | goto err; |
| 4315 | } |
| 4316 | |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 4317 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 4318 | TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT); |
| 4319 | goto err; |
| 4320 | } |
| 4321 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4322 | pkt->saddr = dgram->saddr; |
| 4323 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4324 | qc = qc_new_conn(pkt->version, ipv4, |
| 4325 | pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4326 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4327 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4328 | goto err; |
| 4329 | |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 4330 | memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr)); |
| 4331 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4332 | odcid = &qc->rx.params.original_destination_connection_id; |
| 4333 | /* Copy the transport parameters. */ |
| 4334 | qc->rx.params = l->bind_conf->quic_params; |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4335 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4336 | /* Copy original_destination_connection_id transport parameter. */ |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4337 | if (pkt->token_len) { |
| 4338 | if (parse_retry_token(pkt->token, pkt->token_len, odcid)) { |
| 4339 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 4340 | goto err; |
| 4341 | } |
Amaury Denoyelle | c3b6f4d | 2022-01-11 12:03:09 +0100 | [diff] [blame] | 4342 | /* Copy retry_source_connection_id transport parameter. */ |
| 4343 | quic_cid_cpy(&qc->rx.params.retry_source_connection_id, |
| 4344 | &pkt->dcid); |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4345 | } |
| 4346 | else { |
| 4347 | memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len); |
| 4348 | odcid->len = pkt->dcid.len; |
| 4349 | } |
| 4350 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4351 | /* Copy the initial source connection ID. */ |
| 4352 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4353 | qc->enc_params_len = |
| 4354 | quic_transport_params_encode(qc->enc_params, |
| 4355 | qc->enc_params + sizeof qc->enc_params, |
| 4356 | &qc->rx.params, 1); |
| 4357 | if (!qc->enc_params_len) |
| 4358 | goto err; |
| 4359 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4360 | if (qc_conn_alloc_ssl_ctx(qc)) |
| 4361 | goto err; |
| 4362 | |
Frédéric Lécaille | 789413c | 2022-01-31 10:16:18 +0100 | [diff] [blame] | 4363 | if (!quic_conn_init_timer(qc)) |
| 4364 | goto err; |
| 4365 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4366 | /* NOTE: the socket address has been concatenated to the destination ID |
| 4367 | * chosen by the client for Initial packets. |
| 4368 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4369 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4370 | salt = initial_salt_draft_29; |
| 4371 | salt_len = sizeof initial_salt_draft_29; |
| 4372 | } |
| 4373 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4374 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4375 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4376 | goto err; |
| 4377 | } |
| 4378 | |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4379 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4380 | n = ebmb_insert(&l->rx.dghdlrs[tid]->odcids, &qc->odcid_node, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4381 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4382 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4383 | /* If the insertion failed, it means that another |
| 4384 | * thread has already allocated a QUIC connection for |
| 4385 | * the same CID. Liberate our allocated connection. |
| 4386 | */ |
| 4387 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4388 | qc_to_purge = qc; |
| 4389 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4390 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 4391 | pkt->qc = qc; |
| 4392 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4393 | |
| 4394 | quic_conn_take(qc); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4395 | |
| 4396 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4397 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 4398 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4399 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4400 | else { |
| 4401 | quic_conn_drop(qc_to_purge); |
| 4402 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4403 | } |
| 4404 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4405 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4406 | } |
| 4407 | } |
| 4408 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4409 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4410 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4411 | goto err; |
| 4412 | } |
| 4413 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4414 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4415 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4416 | |
| 4417 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4418 | * they must have the same DCID. |
| 4419 | */ |
| 4420 | if (!first_pkt && |
| 4421 | (pkt->dcid.len != dgram->dcid_len || |
| 4422 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 4423 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 4424 | goto err; |
| 4425 | } |
| 4426 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4427 | buf += QUIC_HAP_CID_LEN; |
| 4428 | |
| 4429 | /* A short packet is the last one of a UDP datagram. */ |
| 4430 | payload = buf; |
| 4431 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4432 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4433 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4434 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4435 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4436 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
| 4437 | goto err; |
| 4438 | } |
| 4439 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4440 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4441 | } |
| 4442 | |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4443 | |
| 4444 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4445 | * they must have the same DCID. |
| 4446 | * |
| 4447 | * This check must be done after the final update to pkt.len to |
| 4448 | * properly drop the packet on failure. |
| 4449 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4450 | if (first_pkt && !quic_peer_validated_addr(qc) && |
| 4451 | HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
| 4452 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 4453 | QUIC_EV_CONN_LPKT, qc); |
| 4454 | /* Reset the anti-amplification bit. It will be set again |
| 4455 | * when sending the next packet if reached again. |
| 4456 | */ |
| 4457 | HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT); |
| 4458 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT); |
| 4459 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4460 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4461 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4462 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4463 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4464 | if (HA_ATOMIC_LOAD(&qc->err_code)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4465 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4466 | goto out; |
| 4467 | } |
| 4468 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4469 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4470 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4471 | b_cspace = b_contig_space(&qc->rx.buf); |
| 4472 | if (b_cspace < pkt->len) { |
| 4473 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4474 | if (b_cspace) { |
| 4475 | b_putchr(&qc->rx.buf, 0x00); |
| 4476 | b_cspace--; |
| 4477 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4478 | b_add(&qc->rx.buf, b_cspace); |
| 4479 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4480 | TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len); |
Frédéric Lécaille | 91ac6c3 | 2021-12-17 16:11:54 +0100 | [diff] [blame] | 4481 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4482 | goto err; |
| 4483 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4484 | } |
| 4485 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4486 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4487 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 4488 | goto err; |
| 4489 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4490 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4491 | TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4492 | if (pkt->aad_len) |
| 4493 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4494 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 4495 | /* Wake up the connection packet handler task from here only if all |
| 4496 | * the contexts have been initialized, especially the mux context |
| 4497 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 4498 | * will start it if these contexts for the connection are not already |
| 4499 | * initialized. |
| 4500 | */ |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 4501 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4502 | if (conn_ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4503 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4504 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4505 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4506 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4507 | if (qc) |
| 4508 | quic_conn_drop(qc); |
| 4509 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4510 | return pkt->len; |
| 4511 | |
| 4512 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4513 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 4514 | * This cannot be done by this thread. |
| 4515 | */ |
| 4516 | if (io_cb_wakeup) { |
| 4517 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
| 4518 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 4519 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4520 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 4521 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4522 | if (!pkt->len) |
| 4523 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 4524 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4525 | qc ? qc : NULL, pkt); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4526 | |
| 4527 | if (qc) |
| 4528 | quic_conn_drop(qc); |
| 4529 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4530 | return -1; |
| 4531 | } |
| 4532 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4533 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 4534 | * Return 1 if enough room to build this header, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4535 | */ |
| 4536 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 4537 | int type, size_t pn_len, struct quic_conn *conn) |
| 4538 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4539 | if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4540 | return 0; |
| 4541 | |
| 4542 | /* #0 byte flags */ |
| 4543 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 4544 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 4545 | /* Version */ |
| 4546 | quic_write_uint32(buf, end, conn->version); |
| 4547 | *(*buf)++ = conn->dcid.len; |
| 4548 | /* Destination connection ID */ |
| 4549 | if (conn->dcid.len) { |
| 4550 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4551 | *buf += conn->dcid.len; |
| 4552 | } |
| 4553 | /* Source connection ID */ |
| 4554 | *(*buf)++ = conn->scid.len; |
| 4555 | if (conn->scid.len) { |
| 4556 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 4557 | *buf += conn->scid.len; |
| 4558 | } |
| 4559 | |
| 4560 | return 1; |
| 4561 | } |
| 4562 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4563 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 4564 | * Return 1 if enough room to build this header, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4565 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4566 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 4567 | size_t pn_len, struct quic_conn *conn, |
| 4568 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4569 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4570 | if (end - *buf < 1 + conn->dcid.len) |
| 4571 | return 0; |
| 4572 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4573 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 4574 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 4575 | ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4576 | /* Destination connection ID */ |
| 4577 | if (conn->dcid.len) { |
| 4578 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4579 | *buf += conn->dcid.len; |
| 4580 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4581 | |
| 4582 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 4586 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 4587 | * with <aead> as AEAD cipher and <key> as secret key. |
| 4588 | * Returns 1 if succeeded or 0 if failed. |
| 4589 | */ |
| 4590 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 4591 | const EVP_CIPHER *aead, const unsigned char *key) |
| 4592 | { |
| 4593 | int i, ret, outlen; |
| 4594 | EVP_CIPHER_CTX *ctx; |
| 4595 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 4596 | * and at most 4 bytes for the packet number |
| 4597 | */ |
| 4598 | unsigned char mask[5] = {0}; |
| 4599 | |
| 4600 | ret = 0; |
| 4601 | ctx = EVP_CIPHER_CTX_new(); |
| 4602 | if (!ctx) |
| 4603 | return 0; |
| 4604 | |
| 4605 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 4606 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 4607 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 4608 | goto out; |
| 4609 | |
| 4610 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 4611 | for (i = 0; i < pnlen; i++) |
| 4612 | pn[i] ^= mask[i + 1]; |
| 4613 | |
| 4614 | ret = 1; |
| 4615 | |
| 4616 | out: |
| 4617 | EVP_CIPHER_CTX_free(ctx); |
| 4618 | |
| 4619 | return ret; |
| 4620 | } |
| 4621 | |
| 4622 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 4623 | * ACK ranges if needed to a value below <limit> in bytes. |
| 4624 | * Return 1 if succeeded, 0 if not. |
| 4625 | */ |
| 4626 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 4627 | { |
| 4628 | size_t room, ack_delay_sz; |
| 4629 | |
| 4630 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 4631 | /* A frame is made of 1 byte for the frame type. */ |
| 4632 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4633 | if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4634 | return 0; |
| 4635 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4636 | return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4637 | } |
| 4638 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4639 | /* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames |
| 4640 | * for <qel> encryption level to be encoded in a buffer with <room> as available room, |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4641 | * and <*len> the packet Length field initialized with the number of bytes already present |
| 4642 | * in this buffer which must be taken into an account for the Length packet field value. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4643 | * <headlen> is the number of bytes already present in this packet before building frames. |
| 4644 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4645 | * Update consequently <*len> to reflect the size of these frames built |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4646 | * by this function. Also attach these frames to <l> frame list. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4647 | * Return 1 if succeeded, 0 if not. |
| 4648 | */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4649 | static inline int qc_build_frms(struct list *l, |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4650 | size_t room, size_t *len, size_t headlen, |
| 4651 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4652 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4653 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4654 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4655 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4656 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4657 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 4658 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4659 | return 0; |
| 4660 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4661 | /* If we are not probing we must take into an account the congestion |
| 4662 | * control window. |
| 4663 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 4664 | if (!qel->pktns->tx.pto_probe) { |
| 4665 | size_t remain = quic_path_prep_data(qc->path); |
| 4666 | |
| 4667 | if (headlen > remain) |
| 4668 | return 0; |
| 4669 | |
| 4670 | room = QUIC_MIN(room, remain - headlen); |
| 4671 | } |
| 4672 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4673 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4674 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4675 | list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4676 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4677 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4678 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4679 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4680 | break; |
| 4681 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4682 | switch (cf->type) { |
| 4683 | case QUIC_FT_CRYPTO: |
| 4684 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4685 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4686 | /* Compute the length of this CRYPTO frame header */ |
| 4687 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 4688 | /* Compute the data length of this CRyPTO frame. */ |
| 4689 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 4690 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4691 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4692 | if (!dlen) |
| 4693 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4694 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4695 | /* CRYPTO frame length. */ |
| 4696 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 4697 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4698 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4699 | /* Add the CRYPTO data length and its encoded length to the packet |
| 4700 | * length and the length of this length. |
| 4701 | */ |
| 4702 | *len += flen; |
| 4703 | room -= flen; |
| 4704 | if (dlen == cf->crypto.len) { |
| 4705 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4706 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4707 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4708 | } |
| 4709 | else { |
| 4710 | struct quic_frame *new_cf; |
| 4711 | |
| 4712 | new_cf = pool_alloc(pool_head_quic_frame); |
| 4713 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4714 | TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4715 | return 0; |
| 4716 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4717 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4718 | new_cf->type = QUIC_FT_CRYPTO; |
| 4719 | new_cf->crypto.len = dlen; |
| 4720 | new_cf->crypto.offset = cf->crypto.offset; |
| 4721 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4722 | LIST_APPEND(l, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4723 | /* Consume <dlen> bytes of the current frame. */ |
| 4724 | cf->crypto.len -= dlen; |
| 4725 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4726 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4727 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4728 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4729 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4730 | /* Note that these frames are accepted in short packets only without |
| 4731 | * "Length" packet field. Here, <*len> is used only to compute the |
| 4732 | * sum of the lengths of the already built frames for this packet. |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 4733 | * |
| 4734 | * Compute the length of this STREAM frame "header" made a all the field |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4735 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 4736 | */ |
| 4737 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4738 | ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4739 | /* Compute the data length of this STREAM frame. */ |
| 4740 | avail_room = room - hlen - *len; |
| 4741 | if ((ssize_t)avail_room <= 0) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4742 | break; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4743 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 4744 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4745 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4746 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 4747 | dlen = max_available_room(avail_room, &dlen_sz); |
| 4748 | if (dlen > cf->stream.len) { |
| 4749 | dlen = cf->stream.len; |
| 4750 | } |
| 4751 | dlen_sz = quic_int_getsize(dlen); |
| 4752 | flen = hlen + dlen_sz + dlen; |
| 4753 | } |
| 4754 | else { |
| 4755 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 4756 | flen = hlen + dlen; |
| 4757 | } |
| 4758 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4759 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4760 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4761 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4762 | /* Add the STREAM data length and its encoded length to the packet |
| 4763 | * length and the length of this length. |
| 4764 | */ |
| 4765 | *len += flen; |
| 4766 | room -= flen; |
| 4767 | if (dlen == cf->stream.len) { |
| 4768 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4769 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4770 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4771 | } |
| 4772 | else { |
| 4773 | struct quic_frame *new_cf; |
| 4774 | |
| 4775 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 4776 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4777 | TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4778 | return 0; |
| 4779 | } |
| 4780 | |
| 4781 | new_cf->type = cf->type; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4782 | new_cf->stream.qcs = cf->stream.qcs; |
| 4783 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4784 | new_cf->stream.id = cf->stream.id; |
| 4785 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 4786 | new_cf->stream.offset = cf->stream.offset; |
| 4787 | new_cf->stream.len = dlen; |
| 4788 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 4789 | /* FIN bit reset */ |
| 4790 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 4791 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4792 | LIST_APPEND(l, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4793 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 4794 | /* Consume <dlen> bytes of the current frame. */ |
| 4795 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4796 | cf->stream.offset.key += dlen; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4797 | cf->stream.data += dlen; |
| 4798 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4799 | break; |
| 4800 | |
| 4801 | default: |
| 4802 | flen = qc_frm_len(cf); |
| 4803 | BUG_ON(!flen); |
| 4804 | if (flen > room) |
| 4805 | continue; |
| 4806 | |
| 4807 | *len += flen; |
| 4808 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4809 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4810 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4811 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4812 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4813 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4814 | } |
| 4815 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4816 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4817 | } |
| 4818 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4819 | /* This function builds a clear packet from <pkt> information (its type) |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4820 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 4821 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 4822 | * filling the buffer with as much frames as possible. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4823 | * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4824 | * reserved so that to ensure there is enough room to build this AEAD TAG after |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4825 | * having returned from this function. |
| 4826 | * This function also updates the value of <buf_pn> pointer to point to the packet |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4827 | * number field in this packet. <pn_len> will also have the packet number |
| 4828 | * length as value. |
| 4829 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4830 | * Return 1 if succeeded (enough room to buile this packet), O if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4831 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4832 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 4833 | size_t dglen, struct quic_tx_packet *pkt, |
| 4834 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 4835 | int ack, int padding, int cc, int probe, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4836 | struct quic_enc_level *qel, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4837 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4838 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4839 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4840 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 4841 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4842 | struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, }; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4843 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4844 | int64_t largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4845 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4846 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4847 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4848 | /* Length field value with CRYPTO frames if present. */ |
| 4849 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4850 | beg = pos; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4851 | /* When not probing and not acking, and no immediate close is required, |
| 4852 | * reduce the size of this buffer to respect |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4853 | * the congestion controller window. So, we do not limit the size of this |
| 4854 | * packet if we have an ACK frame to send because an ACK frame is not |
| 4855 | * ack-eliciting. This size will be limited if we have ack-eliciting |
| 4856 | * frames to send from qel->pktns->tx.frms. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4857 | */ |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 4858 | if (!probe && !ack && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4859 | size_t path_room; |
| 4860 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4861 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4862 | if (end - beg > path_room) |
| 4863 | end = beg + path_room; |
| 4864 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4865 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4866 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 4867 | * length field if any. |
| 4868 | */ |
| 4869 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 4870 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 4871 | goto no_room; |
| 4872 | |
| 4873 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | e1aa0d3 | 2021-08-03 16:03:09 +0200 | [diff] [blame] | 4874 | largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4875 | /* packet number length */ |
| 4876 | *pn_len = quic_packet_number_length(pn, largest_acked_pn); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4877 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4878 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4879 | !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) || |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4880 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4881 | !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc))) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4882 | goto no_room; |
| 4883 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4884 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 4885 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4886 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4887 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4888 | /* Build an ACK frame if required. */ |
| 4889 | ack_frm_len = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4890 | if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4891 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4892 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4893 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 4894 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 4895 | * that from here, we do not know if we will have to send a PING frame. |
| 4896 | * This will be decided after having computed the ack-eliciting frames |
| 4897 | * to be added to this packet. |
| 4898 | */ |
| 4899 | ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - 1 - *pn_len - pos); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4900 | if (!ack_frm_len) |
| 4901 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4902 | } |
| 4903 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4904 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4905 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4906 | len_frms = 0; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4907 | if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4908 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4909 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4910 | /* Initialize the length of the frames built below to <len>. |
| 4911 | * If any frame could be successfully built by qc_build_frms(), |
| 4912 | * we will have len_frms > len. |
| 4913 | */ |
| 4914 | len_frms = len; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4915 | if (!qc_build_frms(&frm_list, end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4916 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4917 | qc, NULL, NULL, &room); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4918 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4919 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4920 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4921 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 4922 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 4923 | * for the encryption tag. It must be taken into an account for the length |
| 4924 | * of this packet. |
| 4925 | */ |
| 4926 | if (len_frms) |
| 4927 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 4928 | else |
| 4929 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4930 | /* CONNECTION_CLOSE frame */ |
| 4931 | if (cc) { |
| 4932 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4933 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4934 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4935 | len += qc_frm_len(&cc_frm); |
| 4936 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4937 | add_ping_frm = 0; |
| 4938 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4939 | len_sz = quic_int_getsize(len); |
| 4940 | /* Add this packet size to <dglen> */ |
| 4941 | dglen += head_len + len_sz + len; |
| 4942 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 4943 | /* This is a maximum padding size */ |
| 4944 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 4945 | /* The length field value is of this packet is <len> + <padding_len> |
| 4946 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 4947 | * <len_sz>. So, let's deduce the difference between these to packet |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4948 | * sizes from <padding_len>. |
| 4949 | */ |
| 4950 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 4951 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4952 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4953 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4954 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4955 | /* If we cannot send a frame, we send a PING frame. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4956 | add_ping_frm = 1; |
| 4957 | len += 1; |
| 4958 | } |
| 4959 | /* If there is no frame at all to follow, add at least a PADDING frame. */ |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4960 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4961 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 4962 | } |
| 4963 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4964 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 4965 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4966 | |
| 4967 | /* Packet number field address. */ |
| 4968 | *buf_pn = pos; |
| 4969 | |
| 4970 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4971 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 4972 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4973 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4974 | if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4975 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4976 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4977 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4978 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4979 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4980 | |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4981 | list_for_each_entry(cf, &frm_list, list) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4982 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4983 | ssize_t room = end - pos; |
| 4984 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4985 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4986 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4987 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4988 | } |
| 4989 | } |
| 4990 | |
| 4991 | /* Build a PING frame if needed. */ |
| 4992 | if (add_ping_frm) { |
| 4993 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4994 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4995 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4996 | } |
| 4997 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4998 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4999 | if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5000 | goto no_room; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5001 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5002 | /* Build a PADDING frame if needed. */ |
| 5003 | if (padding_len) { |
| 5004 | frm.type = QUIC_FT_PADDING; |
| 5005 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5006 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5007 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5008 | } |
| 5009 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5010 | /* If this packet is ack-eliciting and we are probing let's |
| 5011 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5012 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5013 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 5014 | qel->pktns->tx.pto_probe) |
| 5015 | qel->pktns->tx.pto_probe--; |
| 5016 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5017 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5018 | LIST_SPLICE(&pkt->frms, &frm_list); |
| 5019 | TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5020 | |
| 5021 | return 1; |
| 5022 | |
| 5023 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5024 | /* Replace the pre-built frames which could not be add to this packet */ |
| 5025 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5026 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5027 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5028 | } |
| 5029 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5030 | static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5031 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5032 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5033 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5034 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 5035 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5036 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5037 | pkt->next = NULL; |
| 5038 | pkt->refcnt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5039 | } |
| 5040 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5041 | /* Free <pkt> TX packet which has not already attached to any tree. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5042 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 5043 | { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 5044 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5045 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5046 | if (!pkt) |
| 5047 | return; |
| 5048 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5049 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5050 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 5051 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5052 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5053 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5054 | } |
| 5055 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5056 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 5057 | * type for <qc> QUIC connection from <qel> encryption level. |
| 5058 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 5059 | * -1 if there was not enough room to build a packet. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5060 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5061 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 5062 | const unsigned char *buf_end, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5063 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5064 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 5065 | int pkt_type, int ack, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5066 | { |
| 5067 | /* The pointer to the packet number field. */ |
| 5068 | unsigned char *buf_pn; |
| 5069 | unsigned char *beg, *end, *payload; |
| 5070 | int64_t pn; |
| 5071 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5072 | struct quic_tls_ctx *tls_ctx; |
| 5073 | struct quic_tx_packet *pkt; |
| 5074 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5075 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5076 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5077 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 5078 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5079 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5080 | *err = -2; |
| 5081 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5082 | } |
| 5083 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5084 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5085 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5086 | pn_len = 0; |
| 5087 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5088 | |
| 5089 | pn = qel->pktns->tx.next_pn + 1; |
| 5090 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 5091 | ack, padding, cc, probe, qel, qc)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5092 | *err = -1; |
| 5093 | goto err; |
| 5094 | } |
| 5095 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5096 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5097 | payload = buf_pn + pn_len; |
| 5098 | payload_len = end - payload; |
| 5099 | aad_len = payload - beg; |
| 5100 | |
| 5101 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 5102 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5103 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5104 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5105 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5106 | |
| 5107 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5108 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5109 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 5110 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5111 | TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5112 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5113 | goto err; |
| 5114 | } |
| 5115 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5116 | /* Consume a packet number */ |
| 5117 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 5118 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 41a0760 | 2022-01-04 16:57:37 +0100 | [diff] [blame] | 5119 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes) |
| 5120 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5121 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 5122 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5123 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 5124 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5125 | /* Set the packet in fligth length for in flight packet only. */ |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5126 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5127 | pkt->in_flight_len = pkt->len; |
| 5128 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5129 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5130 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5131 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5132 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5133 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5134 | |
| 5135 | err: |
| 5136 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5137 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5138 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5139 | } |
| 5140 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5141 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5142 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 5143 | * event subscriber <es> is not allowed to change from a previous call as long |
| 5144 | * as at least one event is still subscribed. The <event_type> must only be a |
| 5145 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 5146 | */ |
| 5147 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5148 | { |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5149 | struct qcc *qcc = conn->qc->qcc; |
| 5150 | |
| 5151 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 5152 | BUG_ON(qcc->subs && qcc->subs != es); |
| 5153 | |
| 5154 | es->events |= event_type; |
| 5155 | qcc->subs = es; |
| 5156 | |
| 5157 | if (event_type & SUB_RETRY_RECV) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5158 | TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5159 | |
| 5160 | if (event_type & SUB_RETRY_SEND) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5161 | TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5162 | |
| 5163 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5164 | } |
| 5165 | |
| 5166 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5167 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5168 | * subscribe() call. It always returns zero. |
| 5169 | */ |
| 5170 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5171 | { |
| 5172 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 5173 | } |
| 5174 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5175 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 5176 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5177 | */ |
| 5178 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 5179 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5180 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5181 | |
| 5182 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 5183 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5184 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5185 | if (*xprt_ctx) |
| 5186 | goto out; |
| 5187 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5188 | HA_ATOMIC_STORE(xprt_ctx, conn->qc->xprt_ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5189 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5190 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5191 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5192 | |
| 5193 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5194 | } |
| 5195 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5196 | /* Start the QUIC transport layer */ |
| 5197 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 5198 | { |
| 5199 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5200 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5201 | |
| 5202 | qc = conn->qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 5203 | quic_mux_transport_params_update(qc->qcc); |
| 5204 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 5205 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
| 5206 | return 0; |
| 5207 | } |
| 5208 | |
| 5209 | /* mux-quic can now be considered ready. */ |
| 5210 | qc->mux_state = QC_MUX_READY; |
| 5211 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5212 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 5213 | return 1; |
| 5214 | } |
| 5215 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5216 | /* transport-layer operations for QUIC connections. */ |
| 5217 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 5218 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5219 | .subscribe = quic_conn_subscribe, |
| 5220 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5221 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5222 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5223 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 5224 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 5225 | .get_alpn = ssl_sock_get_alpn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5226 | .name = "QUIC", |
| 5227 | }; |
| 5228 | |
| 5229 | __attribute__((constructor)) |
| 5230 | static void __quic_conn_init(void) |
| 5231 | { |
| 5232 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 5233 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 5234 | } |
| 5235 | |
| 5236 | __attribute__((destructor)) |
| 5237 | static void __quic_conn_deinit(void) |
| 5238 | { |
| 5239 | BIO_meth_free(ha_quic_meth); |
| 5240 | } |
| 5241 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5242 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 5243 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5244 | * Return the number of bytes read if succeeded, -1 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5245 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5246 | struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5247 | { |
| 5248 | unsigned char *pos; |
| 5249 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5250 | struct quic_dghdlr *dghdlr = ctx; |
| 5251 | struct quic_dgram *dgram; |
| 5252 | int first_pkt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5253 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5254 | while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) { |
| 5255 | if (!dgram) |
| 5256 | goto err; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5257 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5258 | pos = dgram->buf; |
| 5259 | end = pos + dgram->len; |
| 5260 | do { |
| 5261 | int ret; |
| 5262 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5263 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5264 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 5265 | if (!pkt) |
| 5266 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5267 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5268 | quic_rx_packet_refinc(pkt); |
| 5269 | ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram); |
| 5270 | first_pkt = 0; |
| 5271 | pos += pkt->len; |
| 5272 | quic_rx_packet_refdec(pkt); |
| 5273 | if (ret == -1) |
| 5274 | /* If the packet length could not be found, we cannot continue. */ |
| 5275 | break; |
| 5276 | } while (pos < end); |
| 5277 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5278 | /* Increasing the received bytes counter by the UDP datagram length |
| 5279 | * if this datagram could be associated to a connection. |
| 5280 | */ |
| 5281 | if (dgram->qc) |
| 5282 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 5283 | |
| 5284 | /* Mark this datagram as consumed */ |
| 5285 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5286 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5287 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5288 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5289 | |
| 5290 | err: |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5291 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5292 | } |
| 5293 | |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5294 | /* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 5295 | * Returns 1 if succeeded, 0 if not. |
| 5296 | */ |
| 5297 | static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 5298 | unsigned char **dcid, size_t *dcid_len) |
| 5299 | { |
| 5300 | int long_header; |
| 5301 | size_t minlen, skip; |
| 5302 | |
| 5303 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) |
| 5304 | goto err; |
| 5305 | |
| 5306 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
| 5307 | minlen = long_header ? |
| 5308 | QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN; |
| 5309 | skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF; |
Frédéric Lécaille | bfa3236 | 2022-02-02 10:44:36 +0100 | [diff] [blame] | 5310 | if (end - buf <= minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5311 | goto err; |
| 5312 | |
| 5313 | buf += skip; |
| 5314 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 5315 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 5316 | goto err; |
| 5317 | |
| 5318 | *dcid = buf; |
| 5319 | |
| 5320 | return 1; |
| 5321 | |
| 5322 | err: |
| 5323 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT); |
| 5324 | return 0; |
| 5325 | } |
| 5326 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 5327 | /* Retrieve the DCID from the datagram found in <buf> and deliver it to the |
| 5328 | * correct datagram handler. |
| 5329 | * Return 1 if a correct datagram could be found, 0 if not. |
| 5330 | */ |
| 5331 | int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner, |
| 5332 | struct sockaddr_storage *saddr, |
| 5333 | struct quic_dgram *new_dgram, struct list *dgrams) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5334 | { |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5335 | struct quic_dgram *dgram; |
| 5336 | unsigned char *dcid; |
| 5337 | size_t dcid_len; |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5338 | int cid_tid; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5339 | struct listener *l = owner; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5340 | |
| 5341 | if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len)) |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5342 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5343 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 5344 | dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5345 | if (!dgram) |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5346 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5347 | |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5348 | cid_tid = quic_get_cid_tid(dcid); |
| 5349 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5350 | /* All the members must be initialized! */ |
| 5351 | dgram->owner = owner; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5352 | dgram->buf = buf; |
| 5353 | dgram->len = len; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5354 | dgram->dcid = dcid; |
| 5355 | dgram->dcid_len = dcid_len; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5356 | dgram->saddr = *saddr; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5357 | dgram->qc = NULL; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5358 | LIST_APPEND(dgrams, &dgram->list); |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5359 | MT_LIST_APPEND(&l->rx.dghdlrs[cid_tid]->dgrams, &dgram->mt_list); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5360 | |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5361 | tasklet_wakeup(l->rx.dghdlrs[cid_tid]->task); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5362 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5363 | return 1; |
| 5364 | |
| 5365 | err: |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5366 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5367 | } |
| 5368 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5369 | /* Function to automatically activate QUIC traces on stdout. |
| 5370 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 5371 | * Main use for now is in the docker image for QUIC interop testing. |
| 5372 | */ |
| 5373 | static void quic_init_stdout_traces(void) |
| 5374 | { |
| 5375 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 5376 | trace_quic.sink = sink_find("stdout"); |
| 5377 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5378 | trace_quic.state = TRACE_STATE_RUNNING; |
| 5379 | #endif |
| 5380 | } |
| 5381 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 5382 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5383 | /* |
| 5384 | * Local variables: |
| 5385 | * c-indent-level: 8 |
| 5386 | * c-basic-offset: 8 |
| 5387 | * End: |
| 5388 | */ |