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> |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 47 | #include <haproxy/cbuf.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 48 | #include <haproxy/quic_tls.h> |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 49 | #include <haproxy/sink.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 50 | #include <haproxy/ssl_sock.h> |
| 51 | #include <haproxy/stream_interface.h> |
| 52 | #include <haproxy/task.h> |
| 53 | #include <haproxy/trace.h> |
| 54 | #include <haproxy/xprt_quic.h> |
| 55 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 56 | /* list of supported QUIC versions by this implementation */ |
| 57 | static int quic_supported_version[] = { |
| 58 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 59 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 60 | |
| 61 | /* placeholder, do not add entry after this */ |
| 62 | 0x0 |
| 63 | }; |
| 64 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 65 | /* This is the values of some QUIC transport parameters when absent. |
| 66 | * Should be used to initialize any transport parameters (local or remote) |
| 67 | * before updating them with customized values. |
| 68 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 69 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 70 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 71 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 72 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 73 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /* trace source and events */ |
| 77 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 78 | const struct trace_source *src, |
| 79 | const struct ist where, const struct ist func, |
| 80 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 81 | |
| 82 | static const struct trace_event quic_trace_events[] = { |
| 83 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 84 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 85 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 86 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 87 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 89 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 90 | { .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] | 91 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 92 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 93 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 94 | { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" }, |
| 95 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 96 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 97 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 100 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 101 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 102 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 103 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 104 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 105 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 106 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 107 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 108 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 109 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 110 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 111 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 112 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 113 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 114 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 115 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 116 | { .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] | 117 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 118 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 119 | { /* end */ } |
| 120 | }; |
| 121 | |
| 122 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 123 | /* arg1 */ { /* already used by the connection */ }, |
| 124 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 125 | /* arg3 */ { }, |
| 126 | /* arg4 */ { } |
| 127 | }; |
| 128 | |
| 129 | static const struct name_desc quic_trace_decoding[] = { |
| 130 | #define QUIC_VERB_CLEAN 1 |
| 131 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 132 | { /* end */ } |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | struct trace_source trace_quic = { |
| 137 | .name = IST("quic"), |
| 138 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 139 | .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] | 140 | .default_cb = quic_trace, |
| 141 | .known_events = quic_trace_events, |
| 142 | .lockon_args = quic_trace_lockon_args, |
| 143 | .decoding = quic_trace_decoding, |
| 144 | .report_events = ~0, /* report everything by default */ |
| 145 | }; |
| 146 | |
| 147 | #define TRACE_SOURCE &trace_quic |
| 148 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 149 | |
| 150 | static BIO_METHOD *ha_quic_meth; |
| 151 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 152 | 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] | 153 | 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] | 154 | 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] | 155 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 156 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 157 | 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] | 158 | DECLARE_POOL(pool_head_quic_connection_id, |
| 159 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 160 | 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] | 161 | 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] | 162 | 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] | 163 | 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] | 164 | 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] | 165 | 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] | 166 | 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] | 167 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 168 | 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] | 169 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 170 | struct quic_conn *qc, size_t dglen, int pkt_type, |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 171 | int padding, int ack, int nb_pto_dgrams, int cc, int *err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 172 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 173 | /* Only for debug purpose */ |
| 174 | struct enc_debug_info { |
| 175 | unsigned char *payload; |
| 176 | size_t payload_len; |
| 177 | unsigned char *aad; |
| 178 | size_t aad_len; |
| 179 | uint64_t pn; |
| 180 | }; |
| 181 | |
| 182 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 183 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 184 | unsigned char *payload, size_t payload_len, |
| 185 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 186 | { |
| 187 | edi->payload = payload; |
| 188 | edi->payload_len = payload_len; |
| 189 | edi->aad = aad; |
| 190 | edi->aad_len = aad_len; |
| 191 | edi->pn = pn; |
| 192 | } |
| 193 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 194 | /* Trace callback for QUIC. |
| 195 | * These traces always expect that arg1, if non-null, is of type connection. |
| 196 | */ |
| 197 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 198 | const struct ist where, const struct ist func, |
| 199 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 200 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 201 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 202 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 203 | if (qc) { |
| 204 | const struct quic_tls_secrets *secs; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 205 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 206 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 207 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 208 | chunk_appendf(&trace_buf, "\n odcid"); |
| 209 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 210 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 211 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 212 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 213 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 217 | const enum ssl_encryption_level_t *level = a2; |
| 218 | const size_t *len = a3; |
| 219 | |
| 220 | if (level) { |
| 221 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 222 | |
| 223 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 224 | } |
| 225 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 226 | 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] | 227 | } |
| 228 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 229 | /* Initial read & write secrets. */ |
| 230 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 231 | const unsigned char *rx_sec = a2; |
| 232 | const unsigned char *tx_sec = a3; |
| 233 | |
| 234 | secs = &qc->els[level].tls_ctx.rx; |
| 235 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 236 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 237 | if (rx_sec) |
| 238 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 239 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 240 | } |
| 241 | secs = &qc->els[level].tls_ctx.tx; |
| 242 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 243 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 244 | if (tx_sec) |
| 245 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 246 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 247 | } |
| 248 | } |
| 249 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 250 | const enum ssl_encryption_level_t *level = a2; |
| 251 | const unsigned char *secret = a3; |
| 252 | const size_t *secret_len = a4; |
| 253 | |
| 254 | if (level) { |
| 255 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 256 | |
| 257 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 258 | if (secret && secret_len) |
| 259 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 260 | secs = &qc->els[lvl].tls_ctx.rx; |
| 261 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 262 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 267 | const enum ssl_encryption_level_t *level = a2; |
| 268 | const unsigned char *secret = a3; |
| 269 | const size_t *secret_len = a4; |
| 270 | |
| 271 | if (level) { |
| 272 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 273 | |
| 274 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 275 | if (secret && secret_len) |
| 276 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 277 | secs = &qc->els[lvl].tls_ctx.tx; |
| 278 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 279 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 280 | } |
| 281 | |
| 282 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 283 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 284 | 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] | 285 | const struct quic_tx_packet *pkt = a2; |
| 286 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 287 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 288 | |
| 289 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 290 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 291 | 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] | 292 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 293 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 294 | (unsigned long long)qc->path->cwnd, |
| 295 | (unsigned long long)qc->path->prep_in_flight, |
| 296 | (unsigned long long)qc->path->in_flight, |
| 297 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 298 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 299 | } |
| 300 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 301 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 302 | if (pkt->pn_node.key != (uint64_t)-1) |
| 303 | 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] | 304 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 305 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 306 | chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes); |
| 307 | } |
| 308 | |
| 309 | if (room) { |
| 310 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 311 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 312 | (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] | 313 | } |
| 314 | } |
| 315 | |
| 316 | if (mask & QUIC_EV_CONN_HDSHK) { |
| 317 | const enum quic_handshake_state *state = a2; |
| 318 | const int *err = a3; |
| 319 | |
| 320 | if (state) |
| 321 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 322 | if (err) |
| 323 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 324 | } |
| 325 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 326 | 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] | 327 | const struct quic_rx_packet *pkt = a2; |
| 328 | const unsigned long *pktlen = a3; |
| 329 | const SSL *ssl = a4; |
| 330 | |
| 331 | if (pkt) { |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 332 | chunk_appendf(&trace_buf, " pkt@%p el=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 333 | pkt, quic_packet_type_enc_level_char(pkt->type)); |
| 334 | if (pkt->pnl) |
| 335 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 336 | (unsigned long long)pkt->pn); |
| 337 | if (pkt->token_len) |
| 338 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 339 | (unsigned long long)pkt->token_len); |
| 340 | if (pkt->aad_len) |
| 341 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 342 | (unsigned long long)pkt->aad_len); |
| 343 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 344 | pkt->flags, (unsigned long long)pkt->len); |
| 345 | } |
| 346 | if (pktlen) |
| 347 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 348 | if (ssl) { |
| 349 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 350 | chunk_appendf(&trace_buf, " el=%c", |
| 351 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 356 | const struct quic_rx_packet *pkt = a2; |
| 357 | const struct quic_rx_crypto_frm *cf = a3; |
| 358 | const SSL *ssl = a4; |
| 359 | |
| 360 | if (pkt) |
| 361 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 362 | quic_packet_type_enc_level_char(pkt->type), |
| 363 | (unsigned long long)pkt->pn); |
| 364 | if (cf) |
| 365 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 366 | (unsigned long long)cf->offset_node.key, |
| 367 | (unsigned long long)cf->len); |
| 368 | if (ssl) { |
| 369 | 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] | 370 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 371 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 372 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 373 | |
| 374 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 375 | 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] | 376 | } |
| 377 | |
| 378 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 379 | const struct quic_frame *frm = a2; |
| 380 | |
| 381 | if (frm) |
| 382 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 383 | } |
| 384 | |
| 385 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 386 | const struct quic_enc_level *qel = a2; |
| 387 | |
| 388 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 389 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 390 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 391 | " 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] | 392 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 546186b | 2021-08-03 14:25:36 +0200 | [diff] [blame] | 393 | quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)), |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 394 | !!(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] | 395 | (unsigned long long)qc->path->cwnd, |
| 396 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 397 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 398 | (unsigned long long)pktns->tx.in_flight, |
| 399 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 403 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 404 | const struct enc_debug_info *edi = a2; |
| 405 | |
| 406 | if (edi) |
| 407 | chunk_appendf(&trace_buf, |
| 408 | " payload=@%p payload_len=%llu" |
| 409 | " aad=@%p aad_len=%llu pn=%llu", |
| 410 | edi->payload, (unsigned long long)edi->payload_len, |
| 411 | edi->aad, (unsigned long long)edi->aad_len, |
| 412 | (unsigned long long)edi->pn); |
| 413 | } |
| 414 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 415 | if (mask & QUIC_EV_CONN_RMHP) { |
| 416 | const struct quic_rx_packet *pkt = a2; |
| 417 | |
| 418 | if (pkt) { |
| 419 | const int *ret = a3; |
| 420 | |
| 421 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 422 | if (ret && *ret) |
| 423 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 424 | pkt->pnl, (unsigned long long)pkt->pn); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 429 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 430 | const unsigned long *val1 = a3; |
| 431 | const unsigned long *val2 = a4; |
| 432 | |
| 433 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 434 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 435 | if (val1) |
| 436 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 437 | if (val2) |
| 438 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 439 | } |
| 440 | |
| 441 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 442 | const unsigned int *rtt_sample = a2; |
| 443 | const unsigned int *ack_delay = a3; |
| 444 | const struct quic_loss *ql = a4; |
| 445 | |
| 446 | if (rtt_sample) |
| 447 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 448 | if (ack_delay) |
| 449 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 450 | if (ql) |
| 451 | chunk_appendf(&trace_buf, |
| 452 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 453 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 454 | } |
| 455 | if (mask & QUIC_EV_CONN_CC) { |
| 456 | const struct quic_cc_event *ev = a2; |
| 457 | const struct quic_cc *cc = a3; |
| 458 | |
| 459 | if (a2) |
| 460 | quic_cc_event_trace(&trace_buf, ev); |
| 461 | if (a3) |
| 462 | quic_cc_state_trace(&trace_buf, cc); |
| 463 | } |
| 464 | |
| 465 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 466 | const struct quic_pktns *pktns = a2; |
| 467 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 468 | |
| 469 | if (pktns) { |
| 470 | chunk_appendf(&trace_buf, " pktns=%s", |
| 471 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 472 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 473 | if (pktns->tx.loss_time) |
| 474 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 475 | 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] | 476 | } |
| 477 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 478 | struct quic_tx_packet *pkt; |
| 479 | |
| 480 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 481 | list_for_each_entry(pkt, lost_pkts, list) |
| 482 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | 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] | 487 | const struct quic_pktns *pktns = a2; |
| 488 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 489 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 490 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 491 | if (ifae_pkts) |
| 492 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 493 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 494 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 495 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 496 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 497 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 498 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 499 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 500 | if (pktns->tx.in_flight) |
| 501 | 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] | 502 | if (pktns->tx.loss_time) |
| 503 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 504 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 505 | } |
| 506 | if (mask & QUIC_EV_CONN_SPTO) { |
| 507 | if (pktns->tx.time_of_last_eliciting) |
| 508 | chunk_appendf(&trace_buf, " tole=%dms", |
| 509 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 510 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 511 | 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] | 512 | } |
| 513 | } |
| 514 | |
| 515 | if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) { |
| 516 | chunk_appendf(&trace_buf, |
| 517 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 522 | const struct quic_tx_packet *pkt = a2; |
| 523 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 524 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 525 | (unsigned long long)qc->path->cwnd, |
| 526 | (unsigned long long)qc->path->prep_in_flight, |
| 527 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 528 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 529 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 530 | (unsigned long)pkt->pn_node.key, |
| 531 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 532 | 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] | 533 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 534 | } |
| 535 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 536 | |
| 537 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 538 | const uint8_t *alert = a2; |
| 539 | const enum ssl_encryption_level_t *level = a3; |
| 540 | |
| 541 | if (alert) |
| 542 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 543 | if (level) |
| 544 | chunk_appendf(&trace_buf, " el=%c", |
| 545 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 546 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 547 | |
| 548 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 549 | const size_t *sz1 = a2; |
| 550 | const size_t *sz2 = a3; |
| 551 | const size_t *sz3 = a4; |
| 552 | |
| 553 | if (sz1) |
| 554 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 555 | if (sz2) |
| 556 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 557 | if (sz3) |
| 558 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 559 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 560 | |
| 561 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 562 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 563 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 564 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 565 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 566 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 567 | } |
| 568 | if (mask & QUIC_EV_CONN_LPKT) { |
| 569 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 570 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 571 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 572 | if (pkt) { |
| 573 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 574 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 575 | if (pkt->pn_node.key != (uint64_t)-1) |
| 576 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 577 | } |
| 578 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 579 | if (len) |
| 580 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | } |
| 584 | |
| 585 | /* 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] | 586 | 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] | 587 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 588 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 589 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 590 | if (objt_server(qc->conn->target)) |
| 591 | return 1; |
| 592 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 593 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 594 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 595 | if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) || |
| 596 | (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] | 597 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 598 | return 1; |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 604 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 605 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 606 | 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] | 607 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 608 | struct quic_pktns *pktns; |
| 609 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 610 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 611 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 612 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 613 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 614 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 615 | pktns = quic_loss_pktns(qc); |
| 616 | if (tick_isset(pktns->tx.loss_time)) { |
| 617 | qc->timer = pktns->tx.loss_time; |
| 618 | goto out; |
| 619 | } |
| 620 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 621 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 622 | * cancelled for a server which reached the anti-amplification limit. |
| 623 | */ |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 624 | if (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 625 | 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] | 626 | qc->timer = TICK_ETERNITY; |
| 627 | goto out; |
| 628 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 629 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 630 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 631 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 632 | /* Timer cancellation. */ |
| 633 | qc->timer = TICK_ETERNITY; |
| 634 | goto out; |
| 635 | } |
| 636 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 637 | handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE; |
| 638 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 639 | if (tick_isset(pto)) |
| 640 | qc->timer = pto; |
| 641 | out: |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 642 | if (qc->timer_task && qc->timer != TICK_ETERNITY) |
Amaury Denoyelle | 336f6fd | 2021-10-05 14:42:25 +0200 | [diff] [blame] | 643 | task_schedule(qc->timer_task, qc->timer); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 644 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 645 | } |
| 646 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 647 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 648 | * connection. |
| 649 | * Return 1 if succeeded, 0 if not. |
| 650 | */ |
| 651 | static int quic_tls_key_update(struct quic_conn *qc) |
| 652 | { |
| 653 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 654 | struct quic_tls_secrets *rx, *tx; |
| 655 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 656 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 657 | |
| 658 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 659 | rx = &tls_ctx->rx; |
| 660 | tx = &tls_ctx->tx; |
| 661 | nxt_rx = &qc->ku.nxt_rx; |
| 662 | nxt_tx = &qc->ku.nxt_tx; |
| 663 | |
| 664 | /* Prepare new RX secrets */ |
| 665 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 666 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 667 | 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] | 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 672 | nxt_rx->key, nxt_rx->keylen, |
| 673 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 674 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 675 | 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] | 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | /* Prepare new TX secrets */ |
| 680 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 681 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 682 | 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] | 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 687 | nxt_tx->key, nxt_tx->keylen, |
| 688 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 689 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 690 | 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] | 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | return 1; |
| 695 | } |
| 696 | |
| 697 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 698 | * Must be used after having updated them. |
| 699 | * Always succeeds. |
| 700 | */ |
| 701 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 702 | { |
| 703 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 704 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 705 | |
| 706 | /* Rotate the RX secrets */ |
| 707 | curr_secret = tls_ctx->rx.secret; |
| 708 | curr_iv = tls_ctx->rx.iv; |
| 709 | curr_key = tls_ctx->rx.key; |
| 710 | |
| 711 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 712 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 713 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 714 | |
| 715 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 716 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 717 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 718 | |
| 719 | qc->ku.prv_rx.secret = curr_secret; |
| 720 | qc->ku.prv_rx.iv = curr_iv; |
| 721 | qc->ku.prv_rx.key = curr_key; |
| 722 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 723 | |
| 724 | /* Update the TX secrets */ |
| 725 | curr_secret = tls_ctx->tx.secret; |
| 726 | curr_iv = tls_ctx->tx.iv; |
| 727 | curr_key = tls_ctx->tx.key; |
| 728 | |
| 729 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 730 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 731 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 732 | |
| 733 | qc->ku.nxt_tx.secret = curr_secret; |
| 734 | qc->ku.nxt_tx.iv = curr_iv; |
| 735 | qc->ku.nxt_tx.key = curr_key; |
| 736 | } |
| 737 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 738 | #ifndef OPENSSL_IS_BORINGSSL |
| 739 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 740 | const uint8_t *read_secret, |
| 741 | const uint8_t *write_secret, size_t secret_len) |
| 742 | { |
| 743 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 744 | struct quic_tls_ctx *tls_ctx = |
| 745 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 746 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 747 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 748 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 749 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 750 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 751 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 752 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 753 | goto out; |
| 754 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 755 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 756 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 757 | TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 758 | goto err; |
| 759 | } |
| 760 | |
| 761 | rx = &tls_ctx->rx; |
| 762 | tx = &tls_ctx->tx; |
| 763 | |
| 764 | rx->aead = tx->aead = tls_aead(cipher); |
| 765 | rx->md = tx->md = tls_md(cipher); |
| 766 | rx->hp = tx->hp = tls_hp(cipher); |
| 767 | |
| 768 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 769 | 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] | 770 | read_secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 771 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 772 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 773 | } |
| 774 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 775 | rx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 776 | |
| 777 | if (!write_secret) |
| 778 | goto tp; |
| 779 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 780 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 781 | 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] | 782 | write_secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 783 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 784 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 785 | } |
| 786 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 787 | tx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 788 | tp: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 789 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 790 | const unsigned char *buf; |
| 791 | size_t buflen; |
| 792 | |
| 793 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 794 | if (!buflen) |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 795 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 796 | |
| 797 | if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen)) |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 798 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 799 | } |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 800 | |
| 801 | if (level == ssl_encryption_application) { |
| 802 | struct quic_conn *qc = conn->qc; |
| 803 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 804 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 805 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 806 | |
| 807 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 808 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 809 | TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 810 | goto err; |
| 811 | } |
| 812 | |
| 813 | memcpy(rx->secret, read_secret, secret_len); |
| 814 | rx->secretlen = secret_len; |
| 815 | memcpy(tx->secret, write_secret, secret_len); |
| 816 | tx->secretlen = secret_len; |
| 817 | /* Initialize all the secret keys lengths */ |
| 818 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 819 | /* Prepare the next key update */ |
| 820 | if (!quic_tls_key_update(qc)) |
| 821 | goto err; |
| 822 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 823 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 824 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn->qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 825 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 826 | |
| 827 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 828 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 829 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 830 | } |
| 831 | #else |
| 832 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 833 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 834 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 835 | */ |
| 836 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 837 | const SSL_CIPHER *cipher, |
| 838 | const uint8_t *secret, size_t secret_len) |
| 839 | { |
| 840 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 841 | struct quic_tls_ctx *tls_ctx = |
| 842 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 843 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 844 | TRACE_ENTER(QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 845 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 846 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 847 | goto out; |
| 848 | } |
| 849 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 850 | tls_ctx->rx.aead = tls_aead(cipher); |
| 851 | tls_ctx->rx.md = tls_md(cipher); |
| 852 | tls_ctx->rx.hp = tls_hp(cipher); |
| 853 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 854 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 855 | goto err; |
| 856 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 857 | 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] | 858 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 859 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 860 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 861 | secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 862 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 863 | goto err; |
| 864 | } |
| 865 | |
| 866 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 867 | const unsigned char *buf; |
| 868 | size_t buflen; |
| 869 | |
| 870 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 871 | if (!buflen) |
| 872 | goto err; |
| 873 | |
| 874 | if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen)) |
| 875 | goto err; |
| 876 | } |
| 877 | |
| 878 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 879 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 880 | TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn->qc, &level, secret, &secret_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 881 | |
| 882 | return 1; |
| 883 | |
| 884 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 885 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 890 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 891 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 892 | */ |
| 893 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 894 | const SSL_CIPHER *cipher, |
| 895 | const uint8_t *secret, size_t secret_len) |
| 896 | { |
| 897 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 898 | struct quic_tls_ctx *tls_ctx = |
| 899 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 900 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 901 | TRACE_ENTER(QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 902 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 903 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 904 | goto out; |
| 905 | } |
| 906 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 907 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 908 | goto err; |
| 909 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 910 | tls_ctx->tx.aead = tls_aead(cipher); |
| 911 | tls_ctx->tx.md = tls_md(cipher); |
| 912 | tls_ctx->tx.hp = tls_hp(cipher); |
| 913 | |
| 914 | 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] | 915 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 916 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 917 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 918 | secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 919 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 920 | goto err; |
| 921 | } |
| 922 | |
| 923 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 924 | TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn->qc, &level, secret, &secret_len); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 925 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 926 | return 1; |
| 927 | |
| 928 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 929 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 930 | return 0; |
| 931 | } |
| 932 | #endif |
| 933 | |
| 934 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 935 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 936 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 937 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 938 | * store all the data. |
| 939 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 940 | */ |
| 941 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 942 | const unsigned char *data, size_t len) |
| 943 | { |
| 944 | struct quic_crypto_buf **qcb; |
| 945 | /* The remaining byte to store in CRYPTO buffers. */ |
| 946 | size_t cf_offset, cf_len, *nb_buf; |
| 947 | unsigned char *pos; |
| 948 | |
| 949 | nb_buf = &qel->tx.crypto.nb_buf; |
| 950 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 951 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 952 | cf_len = len; |
| 953 | |
| 954 | while (len) { |
| 955 | size_t to_copy, room; |
| 956 | |
| 957 | pos = (*qcb)->data + (*qcb)->sz; |
| 958 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 959 | to_copy = len > room ? room : len; |
| 960 | if (to_copy) { |
| 961 | memcpy(pos, data, to_copy); |
| 962 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 963 | qel->tx.crypto.sz += to_copy; |
| 964 | (*qcb)->sz += to_copy; |
| 965 | pos += to_copy; |
| 966 | len -= to_copy; |
| 967 | data += to_copy; |
| 968 | } |
| 969 | else { |
| 970 | struct quic_crypto_buf **tmp; |
| 971 | |
| 972 | tmp = realloc(qel->tx.crypto.bufs, |
| 973 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 974 | if (tmp) { |
| 975 | qel->tx.crypto.bufs = tmp; |
| 976 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 977 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 978 | if (!*qcb) |
| 979 | return 0; |
| 980 | |
| 981 | (*qcb)->sz = 0; |
| 982 | ++*nb_buf; |
| 983 | } |
| 984 | else { |
| 985 | break; |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 991 | * have been buffered. |
| 992 | */ |
| 993 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 994 | struct quic_frame *frm; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 995 | |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 996 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 997 | if (!frm) |
| 998 | return 0; |
| 999 | |
| 1000 | frm->type = QUIC_FT_CRYPTO; |
| 1001 | frm->crypto.offset = cf_offset; |
| 1002 | frm->crypto.len = cf_len; |
Frédéric Lécaille | f252adb | 2021-08-03 17:01:25 +0200 | [diff] [blame] | 1003 | frm->crypto.qel = qel; |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 1004 | MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | return len == 0; |
| 1008 | } |
| 1009 | |
| 1010 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1011 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1012 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1013 | { |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1014 | 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] | 1015 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1016 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1017 | } |
| 1018 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1019 | /* Set the application for <qc> QUIC connection. |
| 1020 | * Return 1 if succeeded, 0 if not. |
| 1021 | */ |
| 1022 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1023 | { |
| 1024 | const struct qcc_app_ops *app_ops; |
| 1025 | |
| 1026 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) { |
| 1027 | app_ops = qc->qcc->app_ops = &h3_ops; |
| 1028 | } |
| 1029 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) { |
| 1030 | app_ops = qc->qcc->app_ops = &hq_interop_ops; |
| 1031 | } |
| 1032 | else |
| 1033 | return 0; |
| 1034 | |
| 1035 | if (app_ops->init && !app_ops->init(qc->qcc)) |
| 1036 | return 0; |
| 1037 | |
| 1038 | if (app_ops->finalize) |
| 1039 | app_ops->finalize(qc->qcc->ctx); |
| 1040 | |
| 1041 | return 1; |
| 1042 | } |
| 1043 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1044 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1045 | * wants to provide the QUIC layer with CRYPTO data. |
| 1046 | * Returns 1 if succeeded, 0 if not. |
| 1047 | */ |
| 1048 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1049 | const uint8_t *data, size_t len) |
| 1050 | { |
| 1051 | struct connection *conn; |
| 1052 | enum quic_tls_enc_level tel; |
| 1053 | struct quic_enc_level *qel; |
| 1054 | |
| 1055 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1056 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1057 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1058 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1059 | goto out; |
| 1060 | } |
| 1061 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1062 | tel = ssl_to_quic_enc_level(level); |
| 1063 | qel = &conn->qc->els[tel]; |
| 1064 | |
| 1065 | if (tel == -1) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1066 | TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1067 | goto err; |
| 1068 | } |
| 1069 | |
| 1070 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1071 | TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1072 | goto err; |
| 1073 | } |
| 1074 | |
| 1075 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1076 | conn->qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1077 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1078 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1079 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1080 | return 1; |
| 1081 | |
| 1082 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1083 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | int ha_quic_flush_flight(SSL *ssl) |
| 1088 | { |
| 1089 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1090 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1091 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn->qc); |
| 1092 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1093 | |
| 1094 | return 1; |
| 1095 | } |
| 1096 | |
| 1097 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1098 | { |
| 1099 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1100 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1101 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn->qc, &alert, &level); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1102 | quic_set_tls_alert(conn->qc, alert); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1103 | HA_ATOMIC_STORE(&conn->qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1104 | return 1; |
| 1105 | } |
| 1106 | |
| 1107 | /* QUIC TLS methods */ |
| 1108 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1109 | #ifdef OPENSSL_IS_BORINGSSL |
| 1110 | .set_read_secret = ha_set_rsec, |
| 1111 | .set_write_secret = ha_set_wsec, |
| 1112 | #else |
| 1113 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1114 | #endif |
| 1115 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1116 | .flush_flight = ha_quic_flush_flight, |
| 1117 | .send_alert = ha_quic_send_alert, |
| 1118 | }; |
| 1119 | |
| 1120 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1121 | * Returns an error count. |
| 1122 | */ |
| 1123 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1124 | { |
| 1125 | struct proxy *curproxy = bind_conf->frontend; |
| 1126 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1127 | int cfgerr = 0; |
| 1128 | |
| 1129 | #if 0 |
| 1130 | /* XXX Did not manage to use this. */ |
| 1131 | const char *ciphers = |
| 1132 | "TLS_AES_128_GCM_SHA256:" |
| 1133 | "TLS_AES_256_GCM_SHA384:" |
| 1134 | "TLS_CHACHA20_POLY1305_SHA256:" |
| 1135 | "TLS_AES_128_CCM_SHA256"; |
| 1136 | #endif |
Frédéric Lécaille | 4b1fddc | 2021-07-01 17:09:05 +0200 | [diff] [blame] | 1137 | 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] | 1138 | long options = |
| 1139 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1140 | SSL_OP_SINGLE_ECDH_USE | |
| 1141 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1142 | SSL_CTX *ctx; |
| 1143 | |
| 1144 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1145 | bind_conf->initial_ctx = ctx; |
| 1146 | |
| 1147 | SSL_CTX_set_options(ctx, options); |
| 1148 | #if 0 |
| 1149 | if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) { |
| 1150 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' " |
| 1151 | "for bind '%s' at [%s:%d].\n", |
| 1152 | curproxy->id, ciphers, |
| 1153 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1154 | cfgerr++; |
| 1155 | } |
| 1156 | #endif |
| 1157 | |
| 1158 | if (SSL_CTX_set1_curves_list(ctx, groups) != 1) { |
| 1159 | ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' " |
| 1160 | "for bind '%s' at [%s:%d].\n", |
| 1161 | curproxy->id, groups, |
| 1162 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1163 | cfgerr++; |
| 1164 | } |
| 1165 | |
| 1166 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1167 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1168 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1169 | SSL_CTX_set_default_verify_paths(ctx); |
| 1170 | |
| 1171 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1172 | #ifdef OPENSSL_IS_BORINGSSL |
| 1173 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1174 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1175 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1176 | if (bind_conf->ssl_conf.early_data) { |
| 1177 | 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] | 1178 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1179 | } |
| 1180 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1181 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1182 | #else |
| 1183 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1184 | #endif |
| 1185 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1186 | #endif |
| 1187 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1188 | |
| 1189 | return cfgerr; |
| 1190 | } |
| 1191 | |
| 1192 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1193 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1194 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1195 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1196 | */ |
| 1197 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1198 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1199 | { |
| 1200 | uint64_t expected_pn = largest_pn + 1; |
| 1201 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1202 | uint64_t pn_hwin = pn_win / 2; |
| 1203 | uint64_t pn_mask = pn_win - 1; |
| 1204 | uint64_t candidate_pn; |
| 1205 | |
| 1206 | |
| 1207 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1208 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1209 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1210 | candidate_pn + pn_hwin <= expected_pn) |
| 1211 | return candidate_pn + pn_win; |
| 1212 | |
| 1213 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1214 | return candidate_pn - pn_win; |
| 1215 | |
| 1216 | return candidate_pn; |
| 1217 | } |
| 1218 | |
| 1219 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1220 | * cryptographic context. |
| 1221 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1222 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1223 | * <end> points to one byte past the end of this packet. |
| 1224 | * Returns 1 if succeeded, 0 if not. |
| 1225 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1226 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1227 | 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] | 1228 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1229 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1230 | { |
| 1231 | int ret, outlen, i, pnlen; |
| 1232 | uint64_t packet_number; |
| 1233 | uint32_t truncated_pn = 0; |
| 1234 | unsigned char mask[5] = {0}; |
| 1235 | unsigned char *sample; |
| 1236 | EVP_CIPHER_CTX *cctx; |
| 1237 | unsigned char *hp_key; |
| 1238 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1239 | /* Check there is enough data in this packet. */ |
| 1240 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1241 | 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] | 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | cctx = EVP_CIPHER_CTX_new(); |
| 1246 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1247 | 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] | 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | ret = 0; |
| 1252 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1253 | |
| 1254 | hp_key = tls_ctx->rx.hp_key; |
| 1255 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1256 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1257 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1258 | 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] | 1259 | goto out; |
| 1260 | } |
| 1261 | |
| 1262 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1263 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1264 | for (i = 0; i < pnlen; i++) { |
| 1265 | pn[i] ^= mask[i + 1]; |
| 1266 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1267 | } |
| 1268 | |
| 1269 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1270 | /* Store remaining information for this unprotected header */ |
| 1271 | pkt->pn = packet_number; |
| 1272 | pkt->pnl = pnlen; |
| 1273 | |
| 1274 | ret = 1; |
| 1275 | |
| 1276 | out: |
| 1277 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1278 | |
| 1279 | return ret; |
| 1280 | } |
| 1281 | |
| 1282 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1283 | * address, with <payload_len> as payload length, <aad> as address of |
| 1284 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1285 | * context. |
| 1286 | * Returns 1 if succeeded, 0 if not. |
| 1287 | */ |
| 1288 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1289 | unsigned char *aad, size_t aad_len, uint64_t pn, |
| 1290 | struct quic_tls_ctx *tls_ctx, struct connection *conn) |
| 1291 | { |
| 1292 | unsigned char iv[12]; |
| 1293 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1294 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1295 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1296 | |
| 1297 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1298 | TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn->qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1299 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1303 | tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1304 | TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn->qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1305 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1309 | |
| 1310 | err: |
| 1311 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1312 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn->qc, &edi); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1313 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1317 | * Returns 1 if succeeded, 0 if not. |
| 1318 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1319 | 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] | 1320 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1321 | int ret, kp_changed; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1322 | unsigned char iv[12]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1323 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1324 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1325 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1326 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1327 | |
| 1328 | kp_changed = 0; |
| 1329 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1330 | /* The two tested bits are not at the same position, |
| 1331 | * this is why they are first both inversed. |
| 1332 | */ |
| 1333 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1334 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1335 | /* The lowest packet number of a previous key phase |
| 1336 | * cannot be null if it really stores previous key phase |
| 1337 | * secrets. |
| 1338 | */ |
| 1339 | if (!pkt->qc->ku.prv_rx.pn) |
| 1340 | return 0; |
| 1341 | |
| 1342 | rx_iv = pkt->qc->ku.prv_rx.iv; |
| 1343 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1344 | } |
| 1345 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1346 | /* Next key phase */ |
| 1347 | kp_changed = 1; |
| 1348 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
| 1349 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1350 | } |
| 1351 | } |
| 1352 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1353 | |
| 1354 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1355 | return 0; |
| 1356 | |
| 1357 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1358 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1359 | tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1360 | if (!ret) |
| 1361 | return 0; |
| 1362 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1363 | /* Update the keys only if the packet decryption succeeded. */ |
| 1364 | if (kp_changed) { |
| 1365 | quic_tls_rotate_keys(pkt->qc); |
| 1366 | /* Toggle the Key Phase bit */ |
| 1367 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1368 | /* Store the lowest packet number received for the current key phase */ |
| 1369 | tls_ctx->rx.pn = pkt->pn; |
| 1370 | /* Prepare the next key update */ |
| 1371 | if (!quic_tls_key_update(pkt->qc)) |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1375 | /* Update the packet length (required to parse the frames). */ |
| 1376 | pkt->len = pkt->aad_len + ret; |
| 1377 | |
| 1378 | return 1; |
| 1379 | } |
| 1380 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1381 | /* Remove from <qcs> stream the acknowledged frames. |
| 1382 | * Never fails. |
| 1383 | */ |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1384 | static int qcs_try_to_consume(struct qcs *qcs) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1385 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1386 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1387 | struct eb64_node *frm_node; |
| 1388 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1389 | ret = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1390 | frm_node = eb64_first(&qcs->tx.acked_frms); |
| 1391 | while (frm_node) { |
| 1392 | struct quic_stream *strm; |
| 1393 | |
| 1394 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
| 1395 | if (strm->offset.key != qcs->tx.ack_offset) |
| 1396 | break; |
| 1397 | |
| 1398 | b_del(strm->buf, strm->len); |
| 1399 | qcs->tx.ack_offset += strm->len; |
| 1400 | frm_node = eb64_next(frm_node); |
| 1401 | eb64_delete(&strm->offset); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1402 | ret = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1403 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1404 | |
| 1405 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1406 | } |
| 1407 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1408 | /* 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] | 1409 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1410 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1411 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1412 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1413 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1414 | 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] | 1415 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1416 | switch (frm->type) { |
| 1417 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1418 | { |
| 1419 | struct qcs *qcs = frm->stream.qcs; |
| 1420 | struct quic_stream *strm = &frm->stream; |
| 1421 | |
| 1422 | if (qcs->tx.ack_offset == strm->offset.key) { |
| 1423 | b_del(strm->buf, strm->len); |
| 1424 | qcs->tx.ack_offset += strm->len; |
| 1425 | LIST_DELETE(&frm->list); |
| 1426 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1427 | stream_acked = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1428 | } |
| 1429 | else { |
| 1430 | eb64_insert(&qcs->tx.acked_frms, &strm->offset); |
| 1431 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1432 | stream_acked |= qcs_try_to_consume(qcs); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1433 | } |
| 1434 | break; |
| 1435 | default: |
| 1436 | LIST_DELETE(&frm->list); |
| 1437 | pool_free(pool_head_quic_frame, frm); |
| 1438 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1439 | |
| 1440 | if (stream_acked) { |
| 1441 | struct qcc *qcc = qc->qcc; |
| 1442 | |
| 1443 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1444 | tasklet_wakeup(qcc->subs->tasklet); |
| 1445 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1446 | if (!qcc->subs->events) |
| 1447 | qcc->subs = NULL; |
| 1448 | } |
| 1449 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1453 | * deallocating them, and their TX frames. |
| 1454 | * Returns the last node reached to be used for the next range. |
| 1455 | * May be NULL if <largest> node could not be found. |
| 1456 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1457 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1458 | struct eb_root *pkts, |
| 1459 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1460 | struct list *newly_acked_pkts, |
| 1461 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1462 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1463 | { |
| 1464 | struct eb64_node *node; |
| 1465 | struct quic_tx_packet *pkt; |
| 1466 | |
| 1467 | if (largest_node) |
| 1468 | node = largest_node; |
| 1469 | else { |
| 1470 | node = eb64_lookup(pkts, largest); |
| 1471 | while (!node && largest > smallest) { |
| 1472 | node = eb64_lookup(pkts, --largest); |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1477 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1478 | |
| 1479 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1480 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1481 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1482 | 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] | 1483 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1484 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1485 | node = eb64_prev(node); |
| 1486 | eb64_delete(&pkt->pn_node); |
| 1487 | } |
| 1488 | |
| 1489 | return node; |
| 1490 | } |
| 1491 | |
| 1492 | /* Treat <frm> frame whose packet it is attached to has just been detected as non |
| 1493 | * acknowledged. |
| 1494 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1495 | static inline void qc_treat_nacked_tx_frm(struct quic_conn *qc, |
| 1496 | struct quic_frame *frm, |
| 1497 | struct quic_pktns *pktns) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1498 | { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1499 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1500 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 1501 | MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | |
| 1505 | /* Free the TX packets of <pkts> list */ |
| 1506 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1507 | { |
| 1508 | struct quic_tx_packet *pkt, *tmp; |
| 1509 | |
| 1510 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1511 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1512 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1513 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | /* Send a packet loss event nofification to the congestion controller |
| 1518 | * attached to <qc> connection with <lost_bytes> the number of lost bytes, |
| 1519 | * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet |
| 1520 | * at <now_us> current time. |
| 1521 | * Always succeeds. |
| 1522 | */ |
| 1523 | static inline void qc_cc_loss_event(struct quic_conn *qc, |
| 1524 | unsigned int lost_bytes, |
| 1525 | unsigned int newest_time_sent, |
| 1526 | unsigned int period, |
| 1527 | unsigned int now_us) |
| 1528 | { |
| 1529 | struct quic_cc_event ev = { |
| 1530 | .type = QUIC_CC_EVT_LOSS, |
| 1531 | .loss.now_ms = now_ms, |
| 1532 | .loss.max_ack_delay = qc->max_ack_delay, |
| 1533 | .loss.lost_bytes = lost_bytes, |
| 1534 | .loss.newest_time_sent = newest_time_sent, |
| 1535 | .loss.period = period, |
| 1536 | }; |
| 1537 | |
| 1538 | quic_cc_event(&qc->path->cc, &ev); |
| 1539 | } |
| 1540 | |
| 1541 | /* Send a packet ack event nofication for each newly acked packet of |
| 1542 | * <newly_acked_pkts> list and free them. |
| 1543 | * Always succeeds. |
| 1544 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1545 | 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] | 1546 | struct list *newly_acked_pkts) |
| 1547 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1548 | struct quic_tx_packet *pkt, *tmp; |
| 1549 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1550 | |
| 1551 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1552 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1553 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1554 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1555 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1556 | ev.ack.acked = pkt->in_flight_len; |
| 1557 | ev.ack.time_sent = pkt->time_sent; |
| 1558 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1559 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1560 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1561 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | } |
| 1565 | |
| 1566 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1567 | * their TX frames. |
| 1568 | * Send a packet loss event to the congestion controller if |
| 1569 | * in flight packet have been lost. |
| 1570 | * Also frees the packet in <pkts> list. |
| 1571 | * Never fails. |
| 1572 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1573 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1574 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1575 | struct list *pkts, |
| 1576 | uint64_t now_us) |
| 1577 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1578 | struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost; |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1579 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1580 | uint64_t lost_bytes; |
| 1581 | |
| 1582 | lost_bytes = 0; |
| 1583 | oldest_lost = newest_lost = NULL; |
| 1584 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 1585 | lost_bytes += pkt->in_flight_len; |
| 1586 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1587 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1588 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1589 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1590 | /* Treat the frames of this lost packet. */ |
| 1591 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1592 | qc_treat_nacked_tx_frm(qc, frm, pktns); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1593 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1594 | if (!oldest_lost) { |
| 1595 | oldest_lost = newest_lost = pkt; |
| 1596 | } |
| 1597 | else { |
| 1598 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1599 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1600 | newest_lost = pkt; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | if (lost_bytes) { |
| 1605 | /* Sent a packet loss event to the congestion controller. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1606 | 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] | 1607 | 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] | 1608 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1609 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1610 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1615 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1616 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1617 | * space if any not deemed lost. |
| 1618 | * Should be called after having received an ACK frame with newly acknowledged |
| 1619 | * packets or when the the loss detection timer has expired. |
| 1620 | * Always succeeds. |
| 1621 | */ |
| 1622 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1623 | struct quic_conn *qc, |
| 1624 | struct list *lost_pkts) |
| 1625 | { |
| 1626 | struct eb_root *pkts; |
| 1627 | struct eb64_node *node; |
| 1628 | struct quic_loss *ql; |
| 1629 | unsigned int loss_delay; |
| 1630 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1631 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1632 | pkts = &pktns->tx.pkts; |
| 1633 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1634 | if (eb_is_empty(pkts)) |
| 1635 | goto out; |
| 1636 | |
| 1637 | ql = &qc->path->loss; |
| 1638 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
| 1639 | loss_delay += loss_delay >> 3; |
| 1640 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1641 | |
| 1642 | node = eb64_first(pkts); |
| 1643 | while (node) { |
| 1644 | struct quic_tx_packet *pkt; |
| 1645 | int64_t largest_acked_pn; |
| 1646 | unsigned int loss_time_limit, time_sent; |
| 1647 | |
| 1648 | 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] | 1649 | 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] | 1650 | node = eb64_next(node); |
| 1651 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1652 | break; |
| 1653 | |
| 1654 | time_sent = pkt->time_sent; |
| 1655 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1656 | if (tick_is_le(time_sent, now_ms) || |
| 1657 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1658 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1659 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1660 | } |
| 1661 | else { |
Frédéric Lécaille | dc90c07 | 2021-12-27 18:15:27 +0100 | [diff] [blame] | 1662 | if (tick_isset(pktns->tx.loss_time)) |
| 1663 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1664 | else |
| 1665 | pktns->tx.loss_time = loss_time_limit; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1670 | 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] | 1671 | } |
| 1672 | |
| 1673 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1674 | * 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] | 1675 | * 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] | 1676 | * acked ack-eliciting packet. |
| 1677 | * Return 1, if succeeded, 0 if not. |
| 1678 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1679 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1680 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1681 | struct quic_enc_level *qel, |
| 1682 | unsigned int *rtt_sample, |
| 1683 | const unsigned char **pos, const unsigned char *end) |
| 1684 | { |
| 1685 | struct quic_ack *ack = &frm->ack; |
| 1686 | uint64_t smallest, largest; |
| 1687 | struct eb_root *pkts; |
| 1688 | struct eb64_node *largest_node; |
| 1689 | unsigned int time_sent, pkt_flags; |
| 1690 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1691 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1692 | |
| 1693 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1694 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1695 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1696 | goto err; |
| 1697 | } |
| 1698 | |
| 1699 | if (ack->first_ack_range > ack->largest_ack) { |
| 1700 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1701 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1702 | goto err; |
| 1703 | } |
| 1704 | |
| 1705 | largest = ack->largest_ack; |
| 1706 | smallest = largest - ack->first_ack_range; |
| 1707 | pkts = &qel->pktns->tx.pkts; |
| 1708 | pkt_flags = 0; |
| 1709 | largest_node = NULL; |
| 1710 | time_sent = 0; |
| 1711 | |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1712 | 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] | 1713 | largest_node = eb64_lookup(pkts, largest); |
| 1714 | if (!largest_node) { |
| 1715 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1716 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1717 | } |
| 1718 | else { |
| 1719 | time_sent = eb64_entry(&largest_node->node, |
| 1720 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1721 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1725 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1726 | do { |
| 1727 | uint64_t gap, ack_range; |
| 1728 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1729 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1730 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1731 | if (!ack->ack_range_num--) |
| 1732 | break; |
| 1733 | |
| 1734 | if (!quic_dec_int(&gap, pos, end)) |
| 1735 | goto err; |
| 1736 | |
| 1737 | if (smallest < gap + 2) { |
| 1738 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1739 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1740 | goto err; |
| 1741 | } |
| 1742 | |
| 1743 | largest = smallest - gap - 2; |
| 1744 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1745 | goto err; |
| 1746 | |
| 1747 | if (largest < ack_range) { |
| 1748 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1749 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1750 | goto err; |
| 1751 | } |
| 1752 | |
| 1753 | /* Do not use this node anymore. */ |
| 1754 | largest_node = NULL; |
| 1755 | /* Next range */ |
| 1756 | smallest = largest - ack_range; |
| 1757 | |
| 1758 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1759 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1760 | } while (1); |
| 1761 | |
| 1762 | /* 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] | 1763 | 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] | 1764 | |
| 1765 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1766 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1767 | 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] | 1768 | } |
| 1769 | |
| 1770 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1771 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1772 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1773 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1774 | 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] | 1775 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1776 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 1777 | if (quic_peer_validated_addr(qc)) |
| 1778 | qc->path->loss.pto_count = 0; |
| 1779 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | |
| 1783 | return 1; |
| 1784 | |
| 1785 | err: |
| 1786 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1787 | 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] | 1788 | return 0; |
| 1789 | } |
| 1790 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 1791 | /* This function gives the detail of the SSL error. It is used only |
| 1792 | * if the debug mode and the verbose mode are activated. It dump all |
| 1793 | * the SSL error until the stack was empty. |
| 1794 | */ |
| 1795 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 1796 | { |
| 1797 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 1798 | while (1) { |
| 1799 | unsigned long ret; |
| 1800 | |
| 1801 | ret = ERR_get_error(); |
| 1802 | if (!ret) |
| 1803 | return; |
| 1804 | |
| 1805 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 1806 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1811 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 1812 | const char **str, int *len); |
| 1813 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1814 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 1815 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1816 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1817 | * Return 1 if succeeded, 0 if not. |
| 1818 | */ |
| 1819 | 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] | 1820 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1821 | const unsigned char *data, size_t len, |
| 1822 | struct quic_rx_packet *pkt, |
| 1823 | struct quic_rx_crypto_frm *cf) |
| 1824 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1825 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 1826 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1827 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1828 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 1829 | qc = ctx->qc; |
| 1830 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1831 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 1832 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1833 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 1834 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1835 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1836 | goto err; |
| 1837 | } |
| 1838 | |
| 1839 | el->rx.crypto.offset += len; |
| 1840 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 1841 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1842 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1843 | state = HA_ATOMIC_LOAD(&qc->state); |
| 1844 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1845 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 1846 | if (ssl_err != 1) { |
| 1847 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1848 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1849 | TRACE_PROTO("SSL handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1850 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1851 | goto out; |
| 1852 | } |
| 1853 | |
| 1854 | TRACE_DEVEL("SSL handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1855 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 1856 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 1857 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1858 | goto err; |
| 1859 | } |
| 1860 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1861 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1862 | if (objt_listener(ctx->conn->target)) |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1863 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1864 | else |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1865 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1866 | } else { |
| 1867 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 1868 | if (ssl_err != 1) { |
| 1869 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1870 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1871 | TRACE_DEVEL("SSL post handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1872 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1873 | goto out; |
| 1874 | } |
| 1875 | |
| 1876 | TRACE_DEVEL("SSL post handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1877 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1878 | goto err; |
| 1879 | } |
| 1880 | |
| 1881 | TRACE_PROTO("SSL post handshake succeeded", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1882 | QUIC_EV_CONN_HDSHK, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1883 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 1884 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1885 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1886 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1887 | return 1; |
| 1888 | |
| 1889 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1890 | 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] | 1891 | return 0; |
| 1892 | } |
| 1893 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1894 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 1895 | * <pkt> RX packet. |
| 1896 | * Return it if succeeded, NULL if not. |
| 1897 | */ |
| 1898 | static inline |
| 1899 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 1900 | struct quic_rx_packet *pkt) |
| 1901 | { |
| 1902 | struct quic_rx_strm_frm *frm; |
| 1903 | |
| 1904 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 1905 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1906 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1907 | frm->len = stream_frm->len; |
| 1908 | frm->data = stream_frm->data; |
| 1909 | frm->pkt = pkt; |
| 1910 | } |
| 1911 | |
| 1912 | return frm; |
| 1913 | } |
| 1914 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1915 | /* 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] | 1916 | * 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] | 1917 | */ |
| 1918 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 1919 | { |
| 1920 | size_t ret; |
| 1921 | |
| 1922 | ret = 0; |
| 1923 | while (strm_frm->len) { |
| 1924 | size_t try; |
| 1925 | |
| 1926 | try = b_contig_space(buf); |
| 1927 | if (!try) |
| 1928 | break; |
| 1929 | |
| 1930 | if (try > strm_frm->len) |
| 1931 | try = strm_frm->len; |
| 1932 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1933 | strm_frm->len -= try; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1934 | strm_frm->offset.key += try; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1935 | b_add(buf, try); |
| 1936 | ret += try; |
| 1937 | } |
| 1938 | |
| 1939 | return ret; |
| 1940 | } |
| 1941 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 1942 | /* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer. |
| 1943 | * Also update <strm_frm> frame to reflect the data which have been consumed. |
| 1944 | */ |
| 1945 | static size_t qc_rx_strm_frm_cpy(struct buffer *buf, |
| 1946 | struct quic_rx_strm_frm *strm_frm) |
| 1947 | { |
| 1948 | size_t ret; |
| 1949 | |
| 1950 | ret = 0; |
| 1951 | while (strm_frm->len) { |
| 1952 | size_t try; |
| 1953 | |
| 1954 | try = b_contig_space(buf); |
| 1955 | if (!try) |
| 1956 | break; |
| 1957 | |
| 1958 | if (try > strm_frm->len) |
| 1959 | try = strm_frm->len; |
| 1960 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1961 | strm_frm->len -= try; |
| 1962 | strm_frm->offset_node.key += try; |
| 1963 | b_add(buf, try); |
| 1964 | ret += try; |
| 1965 | } |
| 1966 | |
| 1967 | return ret; |
| 1968 | } |
| 1969 | |
| 1970 | /* Process as much as possible RX STREAM frames received for <qcs> */ |
| 1971 | static size_t qc_treat_rx_strm_frms(struct qcs *qcs) |
| 1972 | { |
| 1973 | int total; |
| 1974 | struct eb64_node *frm_node; |
| 1975 | |
| 1976 | total = 0; |
| 1977 | frm_node = eb64_first(&qcs->rx.frms); |
| 1978 | while (frm_node) { |
| 1979 | int ret; |
| 1980 | struct quic_rx_strm_frm *frm; |
| 1981 | |
| 1982 | frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node); |
| 1983 | if (frm->offset_node.key != qcs->rx.offset) |
| 1984 | break; |
| 1985 | |
| 1986 | ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm); |
| 1987 | qcs->rx.offset += ret; |
| 1988 | total += ret; |
| 1989 | if (frm->len) { |
| 1990 | /* If there is remaining data in this frame |
| 1991 | * this is because the destination buffer is full. |
| 1992 | */ |
| 1993 | break; |
| 1994 | } |
| 1995 | |
| 1996 | frm_node = eb64_next(frm_node); |
| 1997 | quic_rx_packet_refdec(frm->pkt); |
| 1998 | eb64_delete(&frm->offset_node); |
| 1999 | pool_free(pool_head_quic_rx_strm_frm, frm); |
| 2000 | } |
| 2001 | |
| 2002 | return total; |
| 2003 | } |
| 2004 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2005 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2006 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2007 | * If not, the STREAM frame is stored to be treated again later. |
| 2008 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2009 | * Return 1 if succeeded, 0 if not. |
| 2010 | */ |
| 2011 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2012 | struct quic_stream *strm_frm, |
| 2013 | struct quic_conn *qc) |
| 2014 | { |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2015 | int total; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2016 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2017 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2018 | struct quic_rx_strm_frm *frm; |
| 2019 | |
| 2020 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2021 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2022 | 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] | 2023 | return 0; |
| 2024 | } |
| 2025 | |
| 2026 | 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] | 2027 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2028 | size_t diff; |
| 2029 | |
| 2030 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2031 | TRACE_PROTO("Already received STREAM data", |
| 2032 | QUIC_EV_CONN_PSTRM, qc); |
| 2033 | goto out; |
| 2034 | } |
| 2035 | |
| 2036 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2037 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2038 | strm_frm->offset.key = strm->rx.offset; |
| 2039 | strm_frm->len -= diff; |
| 2040 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2041 | } |
| 2042 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2043 | total = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2044 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2045 | int ret; |
| 2046 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2047 | if (!qc_get_buf(strm, &strm->rx.buf)) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2048 | goto store_frm; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2049 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2050 | |
| 2051 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2052 | total += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2053 | strm->rx.offset += ret; |
| 2054 | } |
| 2055 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2056 | total += qc_treat_rx_strm_frms(strm); |
| 2057 | 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] | 2058 | TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2059 | return 0; |
| 2060 | } |
| 2061 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2062 | if (!strm_frm->len) |
| 2063 | goto out; |
| 2064 | |
| 2065 | store_frm: |
| 2066 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2067 | if (!frm) { |
| 2068 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2069 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2070 | return 0; |
| 2071 | } |
| 2072 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2073 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2074 | quic_rx_packet_refinc(pkt); |
| 2075 | |
| 2076 | out: |
| 2077 | return 1; |
| 2078 | } |
| 2079 | |
| 2080 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2081 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2082 | * If not, the STREAM frame is stored to be treated again later. |
| 2083 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2084 | * Return 1 if succeeded, 0 if not. |
| 2085 | */ |
| 2086 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2087 | struct quic_stream *strm_frm, |
| 2088 | struct quic_conn *qc) |
| 2089 | { |
| 2090 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2091 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2092 | struct quic_rx_strm_frm *frm; |
| 2093 | size_t strm_frm_len; |
| 2094 | |
| 2095 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2096 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2097 | 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] | 2098 | return 0; |
| 2099 | } |
| 2100 | |
| 2101 | 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] | 2102 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2103 | size_t diff; |
| 2104 | |
| 2105 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2106 | TRACE_PROTO("Already received STREAM data", |
| 2107 | QUIC_EV_CONN_PSTRM, qc); |
| 2108 | goto out; |
| 2109 | } |
| 2110 | |
| 2111 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2112 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2113 | strm_frm->offset.key = strm->rx.offset; |
| 2114 | strm_frm->len -= diff; |
| 2115 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2119 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2120 | int ret; |
| 2121 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 2122 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2123 | goto store_frm; |
| 2124 | |
| 2125 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 2126 | * of bytes copied. |
| 2127 | */ |
| 2128 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 2129 | /* Inform the application of the arrival of this new stream */ |
| 2130 | 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] | 2131 | 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] | 2132 | return 0; |
| 2133 | } |
| 2134 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2135 | if (ret) |
| 2136 | qcs_notify_recv(strm); |
| 2137 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2138 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2139 | } |
| 2140 | /* Take this frame into an account for the stream flow control */ |
| 2141 | strm->rx.offset += strm_frm_len; |
| 2142 | /* 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] | 2143 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2144 | */ |
| 2145 | if (!strm_frm->len) |
| 2146 | goto out; |
| 2147 | |
| 2148 | store_frm: |
| 2149 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2150 | if (!frm) { |
| 2151 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2152 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2153 | return 0; |
| 2154 | } |
| 2155 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2156 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2157 | quic_rx_packet_refinc(pkt); |
| 2158 | |
| 2159 | out: |
| 2160 | return 1; |
| 2161 | } |
| 2162 | |
| 2163 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2164 | struct quic_stream *strm_frm, |
| 2165 | struct quic_conn *qc) |
| 2166 | { |
| 2167 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2168 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2169 | else |
| 2170 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2171 | } |
| 2172 | |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame^] | 2173 | /* Prepare a fast retransmission from <qel> encryption level |
| 2174 | * which must be Initial encryption level. |
| 2175 | */ |
| 2176 | static void qc_prep_fast_retrans(struct quic_enc_level *qel, |
| 2177 | struct quic_conn *qc) |
| 2178 | { |
| 2179 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
| 2180 | struct eb64_node *node = eb64_first(pkts); |
| 2181 | struct quic_tx_packet *pkt; |
| 2182 | struct quic_frame *frm, *frmbak; |
| 2183 | |
| 2184 | start: |
| 2185 | pkts = &qel->pktns->tx.pkts; |
| 2186 | node = eb64_first(pkts); |
| 2187 | if (!node) |
| 2188 | return; |
| 2189 | |
| 2190 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2191 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 2192 | qc_treat_nacked_tx_frm(qc, frm, qel->pktns); |
| 2193 | if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] && |
| 2194 | qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.in_flight) { |
| 2195 | qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.pto_probe = 1; |
| 2196 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2197 | goto start; |
| 2198 | } |
| 2199 | } |
| 2200 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2201 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2202 | * as I/O handler context and <qel> as encryption level. |
| 2203 | * Returns 1 if succeeded, 0 if failed. |
| 2204 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2205 | 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] | 2206 | struct quic_enc_level *qel) |
| 2207 | { |
| 2208 | struct quic_frame frm; |
| 2209 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2210 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame^] | 2211 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2212 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2213 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2214 | /* Skip the AAD */ |
| 2215 | pos = pkt->data + pkt->aad_len; |
| 2216 | end = pkt->data + pkt->len; |
| 2217 | |
| 2218 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2219 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2220 | goto err; |
| 2221 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2222 | 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] | 2223 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2224 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2225 | break; |
| 2226 | case QUIC_FT_PING: |
| 2227 | break; |
| 2228 | case QUIC_FT_ACK: |
| 2229 | { |
| 2230 | unsigned int rtt_sample; |
| 2231 | |
| 2232 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2233 | 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] | 2234 | goto err; |
| 2235 | |
| 2236 | if (rtt_sample) { |
| 2237 | unsigned int ack_delay; |
| 2238 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2239 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2240 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ? |
| 2241 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2242 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2243 | 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] | 2244 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2245 | break; |
| 2246 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2247 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2248 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2249 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2250 | { |
| 2251 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2252 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2253 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2254 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2255 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2256 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2257 | |
| 2258 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2259 | cfdebug.len = frm.crypto.len; |
| 2260 | /* Nothing to do */ |
| 2261 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2262 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame^] | 2263 | if (objt_listener(ctx->conn->target) && |
| 2264 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2265 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2266 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2267 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2268 | else { |
| 2269 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2270 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2271 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2272 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2273 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2274 | cfdebug.len = frm.crypto.len; |
| 2275 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2276 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2277 | frm.crypto.len -= diff; |
| 2278 | frm.crypto.data += diff; |
| 2279 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2280 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2281 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2282 | |
| 2283 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2284 | /* 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] | 2285 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2286 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2287 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2288 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2289 | if (!qc_provide_cdata(qel, ctx, |
| 2290 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2291 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2292 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2293 | |
| 2294 | break; |
| 2295 | } |
| 2296 | |
| 2297 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2298 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2299 | if (!cf) { |
| 2300 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2301 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2302 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2303 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2304 | |
| 2305 | cf->offset_node.key = frm.crypto.offset; |
| 2306 | cf->len = frm.crypto.len; |
| 2307 | cf->data = frm.crypto.data; |
| 2308 | cf->pkt = pkt; |
| 2309 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2310 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2311 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2312 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2313 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2314 | { |
| 2315 | struct quic_stream *stream = &frm.stream; |
| 2316 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2317 | if (objt_listener(ctx->conn->target)) { |
| 2318 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2319 | goto err; |
| 2320 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2321 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2322 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2323 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2324 | goto err; |
| 2325 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2326 | break; |
| 2327 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2328 | case QUIC_FT_MAX_DATA: |
| 2329 | case QUIC_FT_MAX_STREAM_DATA: |
| 2330 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2331 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2332 | case QUIC_FT_DATA_BLOCKED: |
| 2333 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2334 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2335 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2336 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2337 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2338 | break; |
| 2339 | case QUIC_FT_CONNECTION_CLOSE: |
| 2340 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Amaury Denoyelle | 5154e7a | 2021-12-08 14:51:04 +0100 | [diff] [blame] | 2341 | /* warn the mux to close the connection */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2342 | qc->qcc->flags |= QC_CF_CC_RECV; |
| 2343 | tasklet_wakeup(qc->qcc->wait_event.tasklet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2344 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2345 | case QUIC_FT_HANDSHAKE_DONE: |
| 2346 | if (objt_listener(ctx->conn->target)) |
| 2347 | goto err; |
| 2348 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2349 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2350 | break; |
| 2351 | default: |
| 2352 | goto err; |
| 2353 | } |
| 2354 | } |
| 2355 | |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame^] | 2356 | if (fast_retrans) { |
| 2357 | qc_prep_fast_retrans(qel, qc); |
| 2358 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.in_flight) |
| 2359 | qc_prep_fast_retrans(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE], qc); |
| 2360 | } |
| 2361 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2362 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2363 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2364 | * be discarded. |
| 2365 | */ |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2366 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2367 | int state = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2368 | |
| 2369 | if (state >= QUIC_HS_ST_SERVER_INITIAL) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2370 | 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] | 2371 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2372 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2373 | qc_set_timer(ctx->qc); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2374 | if (state < QUIC_HS_ST_SERVER_HANDSHAKE) |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2375 | 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] | 2376 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2379 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2380 | return 1; |
| 2381 | |
| 2382 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2383 | 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] | 2384 | return 0; |
| 2385 | } |
| 2386 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2387 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2388 | * 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] | 2389 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2390 | * This function must be called only after having built a correct datagram. |
| 2391 | * Always succeeds. |
| 2392 | */ |
| 2393 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2394 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2395 | { |
| 2396 | write_u16(cb_wr(cbuf), dglen); |
| 2397 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2398 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2399 | } |
| 2400 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2401 | /* 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] | 2402 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2403 | * several packets in the same datagram. A header made of two fields is added |
| 2404 | * to each datagram: the datagram length followed by the address of the first |
| 2405 | * packet in this datagram. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2406 | * Returns 1 if succeeded, or 0 if something wrong happened. |
| 2407 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2408 | static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2409 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2410 | enum quic_tls_enc_level tel, next_tel; |
| 2411 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2412 | struct cbuf *cbuf; |
| 2413 | unsigned char *end_buf, *end, *pos, *spos; |
| 2414 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2415 | /* length of datagrams */ |
| 2416 | uint16_t dglen; |
| 2417 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2418 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2419 | /* Each datagram is prepended with its length followed by the |
| 2420 | * address of the first packet in the datagram. |
| 2421 | */ |
| 2422 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2423 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2424 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2425 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 2426 | if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state), 0)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2427 | TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2428 | goto err; |
| 2429 | } |
| 2430 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2431 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2432 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2433 | total = 0; |
| 2434 | padding = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2435 | qel = &qc->els[tel]; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2436 | cbuf = qr->cbuf; |
| 2437 | spos = pos = cb_wr(cbuf); |
| 2438 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2439 | * to ensure there is enough room to mark the end of prepared |
| 2440 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2441 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2442 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2443 | first_pkt = prv_pkt = NULL; |
| 2444 | 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] | 2445 | int err, probe, ack, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2446 | enum quic_pkt_type pkt_type; |
| 2447 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2448 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2449 | probe = ack = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 2450 | cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 2451 | if (!cc) { |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2452 | if (!prv_pkt) |
| 2453 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2454 | 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] | 2455 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2456 | /* 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] | 2457 | * 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] | 2458 | * 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] | 2459 | * and if there is no more CRYPTO data available or in flight |
| 2460 | * congestion control limit is reached for prepared data |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2461 | */ |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2462 | 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] | 2463 | (!cc && !ack && !probe && |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2464 | (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) || |
| 2465 | qc->path->prep_in_flight >= qc->path->cwnd))) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2466 | 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] | 2467 | /* Set the current datagram as prepared into <cbuf> if |
| 2468 | * the was already a correct packet which was previously written. |
| 2469 | */ |
| 2470 | if (prv_pkt) |
| 2471 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2472 | break; |
| 2473 | } |
| 2474 | |
| 2475 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2476 | if (!prv_pkt) { |
| 2477 | /* Leave room for the datagram header */ |
| 2478 | pos += dg_headlen; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2479 | if (!quic_peer_validated_addr(qc) && objt_listener(qc->conn->target)) { |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 2480 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes) |
| 2481 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 2482 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2483 | } |
| 2484 | else { |
| 2485 | end = pos + qc->path->mtu; |
| 2486 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2487 | } |
| 2488 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2489 | 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] | 2490 | pkt_type, ack, probe, cc, &err); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2491 | /* 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] | 2492 | if (err < 0) { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2493 | if (ack) |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2494 | 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] | 2495 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2496 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2497 | case -2: |
| 2498 | goto err; |
| 2499 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2500 | /* If there was already a correct packet present, set the |
| 2501 | * current datagram as prepared into <cbuf>. |
| 2502 | */ |
| 2503 | if (prv_pkt) { |
| 2504 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2505 | goto stop_build; |
| 2506 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2507 | goto out; |
| 2508 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2509 | break; |
| 2510 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2511 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2512 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2513 | if (!cur_pkt) |
| 2514 | goto err; |
| 2515 | |
| 2516 | total += cur_pkt->len; |
| 2517 | /* keep trace of the first packet in the datagram */ |
| 2518 | if (!first_pkt) |
| 2519 | first_pkt = cur_pkt; |
| 2520 | /* Attach the current one to the previous one */ |
| 2521 | if (prv_pkt) |
| 2522 | prv_pkt->next = cur_pkt; |
| 2523 | /* Let's say we have to build a new dgram */ |
| 2524 | prv_pkt = NULL; |
| 2525 | dglen += cur_pkt->len; |
| 2526 | /* Client: discard the Initial encryption keys as soon as |
| 2527 | * a handshake packet could be built. |
| 2528 | */ |
| 2529 | if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL && |
| 2530 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2531 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2532 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 2533 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2534 | qc_set_timer(qc); |
| 2535 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE); |
| 2536 | } |
| 2537 | /* If the data for the current encryption level have all been sent, |
| 2538 | * select the next level. |
| 2539 | */ |
| 2540 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
| 2541 | (MT_LIST_ISEMPTY(&qel->pktns->tx.frms))) { |
| 2542 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2543 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2544 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 2545 | tel = next_tel; |
| 2546 | qel = &qc->els[tel]; |
| 2547 | if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
| 2548 | /* If there is data for the next level, do not |
| 2549 | * consume a datagram. |
| 2550 | */ |
| 2551 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2552 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2553 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2554 | /* If we have to build a new datagram, set the current datagram as |
| 2555 | * prepared into <cbuf>. |
| 2556 | */ |
| 2557 | if (!prv_pkt) { |
| 2558 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2559 | first_pkt = NULL; |
| 2560 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2561 | padding = 0; |
| 2562 | } |
| 2563 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
| 2564 | (objt_server(qc->conn->target) || |
| 2565 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2566 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | stop_build: |
| 2571 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2572 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
| 2573 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2574 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2575 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2576 | if (cb_contig_space(cbuf) >= sizeof(uint16_t)) { |
| 2577 | if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) { |
| 2578 | /* Mark the end of contiguous data for the reader */ |
| 2579 | write_u16(cb_wr(cbuf), 0); |
| 2580 | cb_add(cbuf, sizeof(uint16_t)); |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | if (rd && rd <= cbuf->wr) { |
| 2585 | cb_wr_reset(cbuf); |
| 2586 | if (pos == spos) { |
| 2587 | /* Reuse the same buffer if nothing was built. */ |
| 2588 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2594 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2595 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2596 | |
| 2597 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2598 | 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] | 2599 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | /* 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] | 2603 | * 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] | 2604 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2605 | 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] | 2606 | { |
| 2607 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2608 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2609 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2610 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2611 | cbuf = qr->cbuf; |
| 2612 | while (cb_contig_data(cbuf)) { |
| 2613 | unsigned char *pos; |
| 2614 | struct buffer tmpbuf = { }; |
| 2615 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 2616 | uint16_t dglen; |
| 2617 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2618 | unsigned int time_sent; |
| 2619 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2620 | pos = cb_rd(cbuf); |
| 2621 | dglen = read_u16(pos); |
| 2622 | /* End of prepared datagrams. |
| 2623 | * Reset the reader index only if in front of the writer index. |
| 2624 | */ |
| 2625 | if (!dglen) { |
| 2626 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 2627 | |
| 2628 | if (wr && wr < cbuf->rd) { |
| 2629 | cb_rd_reset(cbuf); |
| 2630 | continue; |
| 2631 | } |
| 2632 | break; |
| 2633 | } |
| 2634 | |
| 2635 | pos += sizeof dglen; |
| 2636 | first_pkt = read_ptr(pos); |
| 2637 | pos += sizeof first_pkt; |
| 2638 | tmpbuf.area = (char *)pos; |
| 2639 | tmpbuf.size = tmpbuf.data = dglen; |
| 2640 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2641 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2642 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2643 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2644 | if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx, |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2645 | &tmpbuf, tmpbuf.data, 0) <= 0) { |
| 2646 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2647 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2648 | break; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2649 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2650 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2651 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2652 | qc->tx.bytes += tmpbuf.data; |
| 2653 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2654 | |
| 2655 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 2656 | pkt->time_sent = time_sent; |
| 2657 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 2658 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 2659 | qc->path->ifae_pkts++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2660 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2661 | qc->path->in_flight += pkt->in_flight_len; |
| 2662 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 2663 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2664 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2665 | 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] | 2666 | next_pkt = pkt->next; |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 2667 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2668 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2669 | } |
| 2670 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2671 | |
| 2672 | return 1; |
| 2673 | } |
| 2674 | |
| 2675 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 2676 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 2677 | * a HANDSHAKE_DONE frame. |
| 2678 | * Return 1 if succeeded, 0 if not. |
| 2679 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2680 | 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] | 2681 | { |
| 2682 | int i; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2683 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2684 | struct quic_frame *frm; |
| 2685 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2686 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2687 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2688 | if (!objt_server(qc->conn->target)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2689 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 2690 | if (!frm) |
| 2691 | return 0; |
| 2692 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2693 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2694 | MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2697 | 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] | 2698 | struct quic_connection_id *cid; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2699 | struct listener *l = __objt_listener(qc->conn->target); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2700 | |
| 2701 | frm = pool_alloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2702 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2703 | goto err; |
| 2704 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2705 | cid = new_quic_cid(&qc->cids, qc, i); |
| 2706 | if (!cid) |
| 2707 | goto err; |
| 2708 | |
| 2709 | /* insert the allocated CID in the receiver tree */ |
| 2710 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2711 | ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len); |
| 2712 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2713 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2714 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2715 | MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | return 1; |
| 2719 | |
| 2720 | err: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2721 | return 0; |
| 2722 | } |
| 2723 | |
| 2724 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2725 | void free_quic_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2726 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2727 | struct eb64_node *n; |
| 2728 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2729 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2730 | n = eb64_first(&arngs->root); |
| 2731 | while (n) { |
| 2732 | struct eb64_node *next; |
| 2733 | |
| 2734 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 2735 | next = eb64_next(n); |
| 2736 | eb64_delete(n); |
| 2737 | free(ar); |
| 2738 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2739 | } |
| 2740 | } |
| 2741 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2742 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 2743 | * descending order. |
| 2744 | */ |
| 2745 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 2746 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2747 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2748 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | |
| 2752 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 2753 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2754 | * 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] | 2755 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2756 | 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] | 2757 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2758 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2759 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2760 | last = eb64_last(&arngs->root); |
| 2761 | while (last && arngs->enc_sz > limit) { |
| 2762 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2763 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2764 | prev = eb64_prev(last); |
| 2765 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2766 | return 0; |
| 2767 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2768 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 2769 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 2770 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 2771 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 2772 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 2773 | --arngs->sz; |
| 2774 | eb64_delete(last); |
| 2775 | pool_free(pool_head_quic_arng, last); |
| 2776 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | return 1; |
| 2780 | } |
| 2781 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2782 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 2783 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 2784 | { |
| 2785 | struct eb64_node *node, *next; |
| 2786 | struct quic_arng_node *ar, *ar_next; |
| 2787 | |
| 2788 | node = eb64_last(&arngs->root); |
| 2789 | if (!node) |
| 2790 | return; |
| 2791 | |
| 2792 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2793 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 2794 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 2795 | |
| 2796 | while ((next = eb64_prev(node))) { |
| 2797 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 2798 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 2799 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 2800 | node = next; |
| 2801 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2802 | } |
| 2803 | } |
| 2804 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2805 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 2806 | * 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] | 2807 | */ |
| 2808 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2809 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 2810 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2811 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2812 | struct quic_arng_node *new_ar; |
| 2813 | |
| 2814 | new_ar = pool_alloc(pool_head_quic_arng); |
| 2815 | if (new_ar) { |
| 2816 | new_ar->first.key = ar->first; |
| 2817 | new_ar->last = ar->last; |
| 2818 | eb64_insert(&arngs->root, &new_ar->first); |
| 2819 | arngs->sz++; |
| 2820 | } |
| 2821 | |
| 2822 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | /* 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] | 2826 | * 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] | 2827 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2828 | * |
| 2829 | * Descending order |
| 2830 | * -------------> |
| 2831 | * range1 range2 |
| 2832 | * ..........|--------|..............|--------| |
| 2833 | * ^ ^ ^ ^ |
| 2834 | * | | | | |
| 2835 | * last1 first1 last2 first2 |
| 2836 | * ..........+--------+--------------+--------+...... |
| 2837 | * diff1 gap12 diff2 |
| 2838 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2839 | * To encode the previous list of ranges we must encode integers as follows in |
| 2840 | * descending order: |
| 2841 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2842 | * with diff1 = last1 - first1 |
| 2843 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2844 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2845 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2846 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2847 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 2848 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2849 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2850 | struct eb64_node *le; |
| 2851 | struct quic_arng_node *new_node; |
| 2852 | struct eb64_node *new; |
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 | new = NULL; |
| 2855 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2856 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2857 | if (!new_node) |
| 2858 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2859 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2860 | goto out; |
| 2861 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2862 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2863 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 2864 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2865 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2866 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2867 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 2868 | |
| 2869 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2870 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2871 | else { |
| 2872 | struct quic_arng_node *le_ar = |
| 2873 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2874 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2875 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 2876 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2877 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2878 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2879 | if (le_ar->last + 1 >= ar->first) { |
| 2880 | le_ar->last = ar->last; |
| 2881 | new = le; |
| 2882 | new_node = le_ar; |
| 2883 | } |
| 2884 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2885 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2886 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2887 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 2888 | |
| 2889 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2890 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2891 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2892 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2893 | /* Verify that the new inserted node does not overlap the nodes |
| 2894 | * which follow it. |
| 2895 | */ |
| 2896 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2897 | struct eb64_node *next; |
| 2898 | struct quic_arng_node *next_node; |
| 2899 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2900 | while ((next = eb64_next(new))) { |
| 2901 | next_node = |
| 2902 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 2903 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2904 | break; |
| 2905 | |
| 2906 | if (next_node->last > new_node->last) |
| 2907 | new_node->last = next_node->last; |
| 2908 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 2909 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2910 | /* Decrement the size of these ranges. */ |
| 2911 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2912 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2913 | } |
| 2914 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 2915 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2916 | quic_arngs_set_enc_sz(arngs); |
| 2917 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2918 | return 1; |
| 2919 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2920 | /* Remove the header protection of packets at <el> encryption level. |
| 2921 | * Always succeeds. |
| 2922 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2923 | 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] | 2924 | { |
| 2925 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2926 | struct quic_rx_packet *pqpkt; |
| 2927 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2928 | struct quic_enc_level *app_qel; |
| 2929 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2930 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 2931 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2932 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2933 | if (el == app_qel && objt_listener(qc->conn->target) && |
| 2934 | HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2935 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2936 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2937 | goto out; |
| 2938 | } |
| 2939 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2940 | 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] | 2941 | 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] | 2942 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2943 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 2944 | 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] | 2945 | /* XXX TO DO XXX */ |
| 2946 | } |
| 2947 | else { |
| 2948 | /* The AAD includes the packet number field */ |
| 2949 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 2950 | /* Store the packet into the tree of packets to decrypt. */ |
| 2951 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 2952 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 2953 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 2954 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 2955 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2956 | 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] | 2957 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2958 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 2959 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2963 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | /* Process all the CRYPTO frame at <el> encryption level. |
| 2967 | * Return 1 if succeeded, 0 if not. |
| 2968 | */ |
| 2969 | 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] | 2970 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2971 | { |
| 2972 | struct eb64_node *node; |
| 2973 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2974 | node = eb64_first(&el->rx.crypto.frms); |
| 2975 | while (node) { |
| 2976 | struct quic_rx_crypto_frm *cf; |
| 2977 | |
| 2978 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 2979 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 2980 | break; |
| 2981 | |
| 2982 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 2983 | goto err; |
| 2984 | |
| 2985 | node = eb64_next(node); |
| 2986 | quic_rx_packet_refdec(cf->pkt); |
| 2987 | eb64_delete(&cf->offset_node); |
| 2988 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 2989 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2990 | return 1; |
| 2991 | |
| 2992 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2993 | 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] | 2994 | return 0; |
| 2995 | } |
| 2996 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 2997 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2998 | * 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] | 2999 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3000 | * Return 1 if succeeded, 0 if not. |
| 3001 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3002 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3003 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3004 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3005 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3006 | int64_t largest_pn = -1; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3007 | struct quic_conn *qc = ctx->conn->qc; |
| 3008 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3009 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3010 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3011 | qel = cur_el; |
| 3012 | next_tel: |
| 3013 | if (!qel) |
| 3014 | goto out; |
| 3015 | |
| 3016 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3017 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3018 | while (node) { |
| 3019 | struct quic_rx_packet *pkt; |
| 3020 | |
| 3021 | 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] | 3022 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3023 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3024 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3025 | /* Drop the packet */ |
| 3026 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3027 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3028 | } |
| 3029 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3030 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3031 | /* Drop the packet */ |
| 3032 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3033 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3034 | } |
| 3035 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3036 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3037 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3038 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING && |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3039 | (!(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] | 3040 | 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] | 3041 | if (pkt->pn > largest_pn) |
| 3042 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3043 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3044 | 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] | 3045 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3046 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3047 | } |
| 3048 | } |
| 3049 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3050 | eb64_delete(&pkt->pn_node); |
| 3051 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3052 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3053 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3054 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3055 | /* Update the largest packet number. */ |
| 3056 | if (largest_pn != -1) |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3057 | HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn); |
| 3058 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3059 | goto err; |
| 3060 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3061 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3062 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3063 | qel = next_el; |
| 3064 | goto next_tel; |
| 3065 | } |
| 3066 | |
| 3067 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3068 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3069 | return 1; |
| 3070 | |
| 3071 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3072 | 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] | 3073 | return 0; |
| 3074 | } |
| 3075 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3076 | /* QUIC connection packet handler task. */ |
| 3077 | 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] | 3078 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3079 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3080 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3081 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3082 | enum quic_tls_enc_level tel, next_tel; |
| 3083 | struct quic_enc_level *qel, *next_qel; |
| 3084 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3085 | struct qring *qr; // Tx ring |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3086 | int prev_st, st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3087 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3088 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3089 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3090 | qr = NULL; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3091 | st = HA_ATOMIC_LOAD(&qc->state); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3092 | TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3093 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3094 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3095 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3096 | 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] | 3097 | start: |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3098 | 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] | 3099 | goto err; |
| 3100 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3101 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3102 | 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] | 3103 | |
| 3104 | next_level: |
| 3105 | tls_ctx = &qel->tls_ctx; |
| 3106 | |
| 3107 | /* If the header protection key for this level has been derived, |
| 3108 | * remove the packet header protections. |
| 3109 | */ |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3110 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3111 | (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3112 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3113 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3114 | prev_st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3115 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3116 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3117 | 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] | 3118 | goto err; |
| 3119 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3120 | if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) && |
| 3121 | (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3122 | qel = next_qel; |
| 3123 | next_qel = NULL; |
| 3124 | goto next_level; |
| 3125 | } |
| 3126 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3127 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 754f99e | 2021-08-19 15:35:59 +0200 | [diff] [blame] | 3128 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3129 | (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) { |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3130 | /* Discard the Handshake keys. */ |
| 3131 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3132 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3133 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3134 | qc_set_timer(qc); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3135 | if (!quic_build_post_handshake_frames(qc)) |
| 3136 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3137 | |
| 3138 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3139 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3140 | goto start; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3141 | } |
| 3142 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3143 | if (!qr) |
| 3144 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3145 | ret = qc_prep_pkts(qc, qr); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3146 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3147 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3148 | else if (ret == 0) |
| 3149 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3150 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3151 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3152 | goto err; |
| 3153 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3154 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3155 | /* Check if there is something to do for the next level. |
| 3156 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3157 | if (next_qel && next_qel != qel && |
| 3158 | (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] | 3159 | (!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] | 3160 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3161 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3162 | goto next_level; |
| 3163 | } |
| 3164 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3165 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3166 | TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3167 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3168 | |
| 3169 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3170 | if (qr) |
| 3171 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3172 | 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] | 3173 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3174 | } |
| 3175 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3176 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3177 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 3178 | { |
| 3179 | int i; |
| 3180 | |
| 3181 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 3182 | if (qel->tx.crypto.bufs[i]) { |
| 3183 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 3184 | qel->tx.crypto.bufs[i] = NULL; |
| 3185 | } |
| 3186 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3187 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3191 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3192 | * Returns 1 if succeeded, 0 if not. |
| 3193 | */ |
| 3194 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 3195 | enum quic_tls_enc_level level) |
| 3196 | { |
| 3197 | struct quic_enc_level *qel; |
| 3198 | |
| 3199 | qel = &qc->els[level]; |
| 3200 | qel->level = quic_to_ssl_enc_level(level); |
| 3201 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 3202 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 3203 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 3204 | qel->tls_ctx.rx.flags = 0; |
| 3205 | qel->tls_ctx.tx.flags = 0; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 3206 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3207 | |
| 3208 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3209 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3210 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 3211 | qel->rx.crypto.offset = 0; |
| 3212 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3213 | |
| 3214 | /* Allocate only one buffer. */ |
| 3215 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 3216 | if (!qel->tx.crypto.bufs) |
| 3217 | goto err; |
| 3218 | |
| 3219 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 3220 | if (!qel->tx.crypto.bufs[0]) |
| 3221 | goto err; |
| 3222 | |
| 3223 | qel->tx.crypto.bufs[0]->sz = 0; |
| 3224 | qel->tx.crypto.nb_buf = 1; |
| 3225 | |
| 3226 | qel->tx.crypto.sz = 0; |
| 3227 | qel->tx.crypto.offset = 0; |
| 3228 | |
| 3229 | return 1; |
| 3230 | |
| 3231 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3232 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3233 | return 0; |
| 3234 | } |
| 3235 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3236 | /* Increment the <qc> refcount. |
| 3237 | * |
| 3238 | * This operation must be conducted when manipulating the quic_conn outside of |
| 3239 | * the connection pinned thread. These threads can only retrieve the connection |
| 3240 | * in the CID tree, so this function must be conducted under the CID lock. |
| 3241 | */ |
| 3242 | static inline void quic_conn_take(struct quic_conn *qc) |
| 3243 | { |
| 3244 | HA_ATOMIC_INC(&qc->refcount); |
| 3245 | } |
| 3246 | |
| 3247 | /* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the |
| 3248 | * substraction, the quic_conn is freed. |
| 3249 | */ |
| 3250 | static void quic_conn_drop(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3251 | { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3252 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3253 | int i; |
| 3254 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3255 | if (!qc) |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3256 | return; |
| 3257 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3258 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
| 3259 | if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1)) { |
| 3260 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
| 3261 | return; |
| 3262 | } |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3263 | |
| 3264 | /* remove the connection from receiver cids trees */ |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3265 | ebmb_delete(&qc->odcid_node); |
| 3266 | ebmb_delete(&qc->scid_node); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3267 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3268 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3269 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3270 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
| 3271 | if (conn_ctx) { |
| 3272 | tasklet_free(conn_ctx->wait_event.tasklet); |
| 3273 | conn_ctx->wait_event.tasklet = NULL; |
| 3274 | |
| 3275 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
| 3276 | } |
| 3277 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3278 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3279 | quic_conn_enc_level_uninit(&qc->els[i]); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3280 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3281 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 3282 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3283 | } |
| 3284 | |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3285 | void quic_close(struct connection *conn, void *xprt_ctx) |
| 3286 | { |
| 3287 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
| 3288 | struct quic_conn *qc = conn_ctx->conn->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3289 | |
| 3290 | /* This task must be deleted by the connection-pinned thread. */ |
| 3291 | if (qc->timer_task) { |
| 3292 | task_destroy(qc->timer_task); |
| 3293 | qc->timer_task = NULL; |
| 3294 | } |
| 3295 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3296 | quic_conn_drop(qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3297 | } |
| 3298 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3299 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3300 | 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] | 3301 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3302 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3303 | struct quic_conn *qc; |
| 3304 | struct quic_pktns *pktns; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3305 | int st; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3306 | |
| 3307 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3308 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3309 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3310 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3311 | task->expire = TICK_ETERNITY; |
| 3312 | pktns = quic_loss_pktns(qc); |
| 3313 | if (tick_isset(pktns->tx.loss_time)) { |
| 3314 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 3315 | |
| 3316 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 3317 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3318 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 3319 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3320 | goto out; |
| 3321 | } |
| 3322 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3323 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3324 | if (qc->path->in_flight) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3325 | pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3326 | pktns->tx.pto_probe = 1; |
| 3327 | } |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3328 | else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3329 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3330 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3331 | |
| 3332 | if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3333 | hel->pktns->tx.pto_probe = 1; |
| 3334 | if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3335 | iel->pktns->tx.pto_probe = 1; |
| 3336 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3337 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3338 | qc->path->loss.pto_count++; |
| 3339 | |
| 3340 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3341 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3342 | |
| 3343 | return task; |
| 3344 | } |
| 3345 | |
| 3346 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 3347 | * connections used to identify the first Initial packets of client connecting |
| 3348 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 3349 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 3350 | * <scid> is the source connection ID with <scid_len> as length. |
| 3351 | * Returns 1 if succeeded, 0 if not. |
| 3352 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3353 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3354 | 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] | 3355 | 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] | 3356 | { |
| 3357 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3358 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3359 | /* Initial CID. */ |
| 3360 | struct quic_connection_id *icid; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3361 | char *buf_area; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3362 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3363 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3364 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3365 | qc = pool_zalloc(pool_head_quic_conn); |
| 3366 | if (!qc) { |
| 3367 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 3368 | goto err; |
| 3369 | } |
| 3370 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3371 | HA_ATOMIC_STORE(&qc->refcount, 0); |
| 3372 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3373 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 3374 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3375 | 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] | 3376 | goto err; |
| 3377 | } |
| 3378 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3379 | qc->cids = EB_ROOT; |
| 3380 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3381 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3382 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3383 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3384 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL); |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3385 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3386 | qc->odcid.len = dcid_len; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3387 | qc->odcid.addrlen = dcid_addr_len; |
| 3388 | 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] | 3389 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 3390 | /* 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] | 3391 | if (scid_len) |
| 3392 | memcpy(qc->dcid.data, scid, scid_len); |
| 3393 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 3394 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3395 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3396 | } |
| 3397 | /* QUIC Client (outgoing connection to servers) */ |
| 3398 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3399 | 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] | 3400 | if (dcid_len) |
| 3401 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 3402 | qc->dcid.len = dcid_len; |
| 3403 | } |
| 3404 | |
| 3405 | /* Initialize the output buffer */ |
| 3406 | qc->obuf.pos = qc->obuf.data; |
| 3407 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3408 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3409 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3410 | 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] | 3411 | goto err; |
| 3412 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3413 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3414 | /* insert the allocated CID in the receiver tree */ |
| 3415 | if (server) { |
| 3416 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3417 | ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len); |
| 3418 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3419 | } |
| 3420 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3421 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 3422 | qc->scid = icid->cid; |
| 3423 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3424 | /* Packet number spaces initialization. */ |
| 3425 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 3426 | quic_pktns_init(&qc->pktns[i]); |
| 3427 | /* QUIC encryption level context initialization. */ |
| 3428 | 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] | 3429 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3430 | 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] | 3431 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3432 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3433 | /* Initialize the packet number space. */ |
| 3434 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 3435 | } |
| 3436 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 3437 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 3438 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 3439 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 3440 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3441 | /* TX part. */ |
| 3442 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3443 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 3444 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 3445 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3446 | /* RX part. */ |
| 3447 | qc->rx.bytes = 0; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3448 | qc->rx.nb_ack_eliciting = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3449 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 3450 | HA_RWLOCK_INIT(&qc->rx.buf_rwlock); |
| 3451 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 3452 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3453 | 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] | 3454 | goto err; |
| 3455 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3456 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3457 | /* XXX TO DO: Only one path at this time. */ |
| 3458 | qc->path = &qc->paths[0]; |
| 3459 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 3460 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3461 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3462 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3463 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3464 | |
| 3465 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3466 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3467 | quic_conn_drop(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3468 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | /* Initialize the timer task of <qc> QUIC connection. |
| 3472 | * Returns 1 if succeeded, 0 if not. |
| 3473 | */ |
| 3474 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 3475 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 3476 | /* Attach this task to the same thread ID used for the connection */ |
| 3477 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3478 | if (!qc->timer_task) |
| 3479 | return 0; |
| 3480 | |
| 3481 | qc->timer = TICK_ETERNITY; |
| 3482 | qc->timer_task->process = process_timer; |
| 3483 | qc->timer_task->context = qc->conn->xprt_ctx; |
| 3484 | |
| 3485 | return 1; |
| 3486 | } |
| 3487 | |
| 3488 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 3489 | * past one byte of this buffer. |
| 3490 | */ |
| 3491 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 3492 | struct quic_rx_packet *pkt) |
| 3493 | { |
| 3494 | unsigned char dcid_len, scid_len; |
| 3495 | |
| 3496 | /* Version */ |
| 3497 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 3498 | return 0; |
| 3499 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3500 | /* Destination Connection ID Length */ |
| 3501 | dcid_len = *(*buf)++; |
| 3502 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 3503 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 3504 | /* XXX MUST BE DROPPED */ |
| 3505 | return 0; |
| 3506 | |
| 3507 | if (dcid_len) { |
| 3508 | /* Check that the length of this received DCID matches the CID lengths |
| 3509 | * of our implementation for non Initials packets only. |
| 3510 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3511 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 3512 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3513 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3514 | return 0; |
| 3515 | |
| 3516 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 3517 | } |
| 3518 | |
| 3519 | pkt->dcid.len = dcid_len; |
| 3520 | *buf += dcid_len; |
| 3521 | |
| 3522 | /* Source Connection ID Length */ |
| 3523 | scid_len = *(*buf)++; |
| 3524 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 3525 | /* XXX MUST BE DROPPED */ |
| 3526 | return 0; |
| 3527 | |
| 3528 | if (scid_len) |
| 3529 | memcpy(pkt->scid.data, *buf, scid_len); |
| 3530 | pkt->scid.len = scid_len; |
| 3531 | *buf += scid_len; |
| 3532 | |
| 3533 | return 1; |
| 3534 | } |
| 3535 | |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3536 | /* If the header protection of <pkt> packet attached to <qc> connection with <ctx> |
| 3537 | * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated |
| 3538 | * encryption level matching with the packet type. <*qel> may be null if not found. |
| 3539 | * Note that <ctx> may be null (for Initial packets). |
| 3540 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3541 | static int qc_pkt_may_rm_hp(struct quic_conn *qc, struct quic_rx_packet *pkt, |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3542 | struct quic_enc_level **qel) |
| 3543 | { |
| 3544 | enum quic_tls_enc_level tel; |
| 3545 | |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3546 | tel = quic_packet_type_enc_level(pkt->type); |
| 3547 | if (tel == QUIC_TLS_ENC_LEVEL_NONE) { |
| 3548 | *qel = NULL; |
| 3549 | return 0; |
| 3550 | } |
| 3551 | |
| 3552 | *qel = &qc->els[tel]; |
| 3553 | if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3554 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3555 | return 0; |
| 3556 | } |
| 3557 | |
| 3558 | if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) && |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3559 | (tel != QUIC_TLS_ENC_LEVEL_APP || |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3560 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)) |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3561 | return 1; |
| 3562 | |
| 3563 | return 0; |
| 3564 | } |
| 3565 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3566 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 3567 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 3568 | { |
| 3569 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 3570 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3571 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3572 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 3573 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | /* 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] | 3577 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 3578 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 3579 | * the packet first byte. |
| 3580 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 3581 | * Returns 1 if succeeded, 0 if not. |
| 3582 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3583 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 3584 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3585 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3586 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3587 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3588 | { |
| 3589 | unsigned char *pn = NULL; /* Packet number field */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3590 | struct quic_enc_level *qel; |
| 3591 | /* Only for traces. */ |
| 3592 | struct quic_rx_packet *qpkt_trace; |
| 3593 | |
| 3594 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3595 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3596 | /* The packet number is here. This is also the start minus |
| 3597 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 3598 | * protection. |
| 3599 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3600 | pn = buf; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3601 | if (qc_pkt_may_rm_hp(qc, pkt, &qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3602 | /* Note that the following function enables us to unprotect the packet |
| 3603 | * number and its length subsequently used to decrypt the entire |
| 3604 | * packets. |
| 3605 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3606 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 3607 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 3608 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3609 | goto err; |
| 3610 | } |
| 3611 | |
| 3612 | /* The AAD includes the packet number field found at <pn>. */ |
| 3613 | pkt->aad_len = pn - beg + pkt->pnl; |
| 3614 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3615 | } |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3616 | else if (qel) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3617 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 3618 | /* If the packet number space has been discarded, this packet |
| 3619 | * will be not parsed. |
| 3620 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3621 | 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] | 3622 | goto out; |
| 3623 | } |
| 3624 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3625 | 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] | 3626 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3627 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 3628 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3629 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3630 | else { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3631 | 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] | 3632 | goto err; |
| 3633 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3634 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3635 | *el = qel; |
| 3636 | /* No reference counter incrementation here!!! */ |
| 3637 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 3638 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 3639 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 3640 | b_add(&qc->rx.buf, pkt->len); |
| 3641 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3642 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3643 | return 1; |
| 3644 | |
| 3645 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3646 | 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] | 3647 | return 0; |
| 3648 | } |
| 3649 | |
| 3650 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 3651 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 3652 | */ |
| 3653 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 3654 | unsigned char byte0, int *long_header) |
| 3655 | { |
| 3656 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 3657 | pkt->type = |
| 3658 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 3659 | *long_header = 1; |
| 3660 | } |
| 3661 | else { |
| 3662 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 3663 | *long_header = 0; |
| 3664 | } |
| 3665 | } |
| 3666 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3667 | /* |
| 3668 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 3669 | */ |
| 3670 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 3671 | { |
| 3672 | int j = 0, version; |
| 3673 | |
| 3674 | do { |
| 3675 | version = quic_supported_version[j]; |
| 3676 | if (version == pkt->version) |
| 3677 | return 1; |
| 3678 | |
| 3679 | version = quic_supported_version[++j]; |
| 3680 | } while(version); |
| 3681 | |
| 3682 | return 0; |
| 3683 | } |
| 3684 | |
Frédéric Lécaille | c5c69a0 | 2021-10-20 17:24:42 +0200 | [diff] [blame] | 3685 | __attribute__((unused)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3686 | static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end, |
| 3687 | struct quic_rx_packet *pkt, |
| 3688 | struct quic_dgram_ctx *dgram_ctx, |
| 3689 | struct sockaddr_storage *saddr) |
| 3690 | { |
| 3691 | unsigned char *beg; |
| 3692 | uint64_t len; |
| 3693 | struct quic_conn *qc; |
| 3694 | struct eb_root *cids; |
| 3695 | struct ebmb_node *node; |
| 3696 | struct connection *srv_conn; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3697 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3698 | int long_header; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3699 | size_t b_cspace; |
| 3700 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3701 | |
| 3702 | qc = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 3703 | qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3704 | TRACE_ENTER(QUIC_EV_CONN_SPKT); |
| 3705 | if (end <= *buf) |
| 3706 | goto err; |
| 3707 | |
| 3708 | /* Fixed bit */ |
| 3709 | if (!(**buf & QUIC_PACKET_FIXED_BIT)) |
| 3710 | /* XXX TO BE DISCARDED */ |
| 3711 | goto err; |
| 3712 | |
| 3713 | srv_conn = dgram_ctx->owner; |
| 3714 | beg = *buf; |
| 3715 | /* Header form */ |
| 3716 | qc_parse_hd_form(pkt, *(*buf)++, &long_header); |
| 3717 | if (long_header) { |
| 3718 | size_t cid_lookup_len; |
| 3719 | |
| 3720 | if (!quic_packet_read_long_header(buf, end, pkt)) |
| 3721 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3722 | |
| 3723 | /* unsupported QUIC version */ |
| 3724 | if (!qc_pkt_is_supported_version(pkt)) { |
| 3725 | TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT); |
| 3726 | goto err; |
| 3727 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3728 | |
| 3729 | /* For Initial packets, and for servers (QUIC clients connections), |
| 3730 | * there is no Initial connection IDs storage. |
| 3731 | */ |
| 3732 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3733 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
| 3734 | cid_lookup_len = pkt->dcid.len; |
| 3735 | } |
| 3736 | else { |
| 3737 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3738 | cid_lookup_len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len); |
| 3742 | if (!node) |
| 3743 | goto err; |
| 3744 | |
| 3745 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
| 3746 | |
| 3747 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3748 | qc->dcid.len = pkt->scid.len; |
| 3749 | if (pkt->scid.len) |
| 3750 | memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len); |
| 3751 | } |
| 3752 | |
| 3753 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3754 | uint64_t token_len; |
| 3755 | |
| 3756 | if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len) |
| 3757 | goto err; |
| 3758 | |
| 3759 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 3760 | * A server which sends an Initial packet must not set the token. |
| 3761 | * So, a client which receives an Initial packet with a token |
| 3762 | * MUST discard the packet or generate a connection error with |
| 3763 | * PROTOCOL_VIOLATION as type. |
| 3764 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 3765 | */ |
| 3766 | pkt->token_len = token_len; |
| 3767 | } |
| 3768 | } |
| 3769 | else { |
| 3770 | /* XXX TO DO: Short header XXX */ |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3771 | if (end - *buf < QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3772 | goto err; |
| 3773 | |
| 3774 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3775 | node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3776 | if (!node) |
| 3777 | goto err; |
| 3778 | |
| 3779 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3780 | *buf += QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3781 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3782 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3783 | /* Only packets packets with long headers and not RETRY or VERSION as type |
| 3784 | * have a length field. |
| 3785 | */ |
| 3786 | if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) { |
| 3787 | if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len) |
| 3788 | goto err; |
| 3789 | |
| 3790 | pkt->len = len; |
| 3791 | } |
| 3792 | else if (!long_header) { |
| 3793 | /* A short packet is the last one of an UDP datagram. */ |
| 3794 | pkt->len = end - *buf; |
| 3795 | } |
| 3796 | |
| 3797 | conn_ctx = qc->conn->xprt_ctx; |
| 3798 | |
| 3799 | /* Increase the total length of this packet by the header length. */ |
| 3800 | pkt->len += *buf - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3801 | |
| 3802 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 3803 | * they must have the same DCID. |
| 3804 | * |
| 3805 | * This check must be done after the final update to pkt.len to |
| 3806 | * properly drop the packet on failure. |
| 3807 | */ |
| 3808 | if (!dgram_ctx->dcid.len) { |
| 3809 | memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len); |
| 3810 | } |
| 3811 | else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3812 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3813 | goto err; |
| 3814 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3815 | dgram_ctx->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3816 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3817 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3818 | b_cspace = b_contig_space(&qc->rx.buf); |
| 3819 | if (b_cspace < pkt->len) { |
| 3820 | /* Let us consume the remaining contiguous space. */ |
| 3821 | b_add(&qc->rx.buf, b_cspace); |
| 3822 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 3823 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3824 | TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3825 | goto err; |
| 3826 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3827 | } |
| 3828 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3829 | if (!qc_try_rm_hp(qc, pkt, *buf, beg, end, &qel)) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3830 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3831 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3832 | goto err; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3833 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3834 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3835 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3836 | if (pkt->aad_len) |
| 3837 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3838 | /* Wake the tasklet of the QUIC connection packet handler. */ |
| 3839 | if (conn_ctx) |
| 3840 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3841 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3842 | TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3843 | |
| 3844 | return pkt->len; |
| 3845 | |
| 3846 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3847 | TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3848 | return -1; |
| 3849 | } |
| 3850 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3851 | /* |
| 3852 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 3853 | * address <addr>. |
| 3854 | * Implementation of RFC9000 6. Version Negotiation |
| 3855 | * |
| 3856 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 3857 | * |
| 3858 | * Returns 0 on success else non-zero |
| 3859 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 3860 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 3861 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3862 | { |
| 3863 | char buf[256]; |
| 3864 | int i = 0, j, version; |
| 3865 | const socklen_t addrlen = get_addr_len(addr); |
| 3866 | |
| 3867 | /* |
| 3868 | * header form |
| 3869 | * long header, fixed bit to 0 for Version Negotiation |
| 3870 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3871 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 3872 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3873 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3874 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3875 | /* null version for Version Negotiation */ |
| 3876 | buf[i++] = '\x00'; |
| 3877 | buf[i++] = '\x00'; |
| 3878 | buf[i++] = '\x00'; |
| 3879 | buf[i++] = '\x00'; |
| 3880 | |
| 3881 | /* source connection id */ |
| 3882 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3883 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3884 | i += pkt->scid.len; |
| 3885 | |
| 3886 | /* destination connection id */ |
| 3887 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3888 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3889 | i += pkt->dcid.len; |
| 3890 | |
| 3891 | /* supported version */ |
| 3892 | j = 0; |
| 3893 | do { |
| 3894 | version = htonl(quic_supported_version[j]); |
| 3895 | memcpy(&buf[i], &version, sizeof(version)); |
| 3896 | i += sizeof(version); |
| 3897 | |
| 3898 | version = quic_supported_version[++j]; |
| 3899 | } while (version); |
| 3900 | |
| 3901 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 3902 | return 1; |
| 3903 | |
| 3904 | return 0; |
| 3905 | } |
| 3906 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3907 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 3908 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 3909 | * concatenated to the <pkt> DCID field. |
| 3910 | * |
| 3911 | * Returns the instance or NULL if not found. |
| 3912 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 3913 | 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] | 3914 | struct listener *l, |
| 3915 | struct sockaddr_storage *saddr) |
| 3916 | { |
| 3917 | struct quic_conn *qc = NULL; |
| 3918 | struct ebmb_node *node; |
| 3919 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 3920 | /* set if the quic_conn is found in the second DCID tree */ |
| 3921 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3922 | |
| 3923 | HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3924 | |
| 3925 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 3926 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 3927 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 3928 | /* DCIDs of first packets coming from multiple clients may have |
| 3929 | * the same values. Let's distinguish them by concatenating the |
| 3930 | * socket addresses. |
| 3931 | */ |
| 3932 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 3933 | node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data, |
| 3934 | pkt->dcid.len + pkt->dcid.addrlen); |
| 3935 | if (node) { |
| 3936 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 3937 | goto end; |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 3942 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 3943 | * used. |
| 3944 | */ |
| 3945 | node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len); |
| 3946 | if (!node) |
| 3947 | goto end; |
| 3948 | |
| 3949 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 3950 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 3951 | found_in_dcid = 1; |
| 3952 | |
| 3953 | end: |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3954 | if (qc) |
| 3955 | quic_conn_take(qc); |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 3956 | HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3957 | |
| 3958 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 3959 | * If already done, this is a noop. |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 3960 | * |
| 3961 | * node.leaf_p is first checked to avoid unnecessary locking. |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3962 | */ |
Amaury Denoyelle | c6fab98 | 2021-12-23 16:27:56 +0100 | [diff] [blame] | 3963 | if (qc && found_in_dcid && qc->odcid_node.node.leaf_p) { |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 3964 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3965 | ebmb_delete(&qc->odcid_node); |
| 3966 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3967 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3968 | |
| 3969 | return qc; |
| 3970 | } |
| 3971 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3972 | static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3973 | struct quic_rx_packet *pkt, |
| 3974 | struct quic_dgram_ctx *dgram_ctx, |
| 3975 | struct sockaddr_storage *saddr) |
| 3976 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3977 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3978 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3979 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3980 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3981 | int long_header = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3982 | size_t b_cspace; |
| 3983 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3984 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3985 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3986 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 3987 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 3988 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 3989 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 3990 | /* This ist only to please to traces and distinguish the |
| 3991 | * packet with parsed packet number from others. |
| 3992 | */ |
| 3993 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3994 | if (end <= buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3995 | goto err; |
| 3996 | |
| 3997 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3998 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3999 | /* XXX TO BE DISCARDED */ |
| 4000 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4001 | goto err; |
| 4002 | } |
| 4003 | |
| 4004 | l = dgram_ctx->owner; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4005 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4006 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4007 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4008 | uint64_t len; |
| 4009 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4010 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4011 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4012 | goto err; |
| 4013 | } |
| 4014 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4015 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 4016 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 4017 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4018 | goto err; |
| 4019 | } |
| 4020 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4021 | /* RFC9000 6. Version Negotiation */ |
| 4022 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4023 | /* unsupported version, send Negotiation packet */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4024 | if (send_version_negotiation(l->rx.fd, saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4025 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 4026 | goto err; |
| 4027 | } |
| 4028 | |
| 4029 | 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] | 4030 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4031 | } |
| 4032 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4033 | /* For Initial packets, and for servers (QUIC clients connections), |
| 4034 | * there is no Initial connection IDs storage. |
| 4035 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4036 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4037 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4038 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4039 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 4040 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4041 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4042 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4043 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4044 | |
| 4045 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 4046 | * A server which sends an Initial packet must not set the token. |
| 4047 | * So, a client which receives an Initial packet with a token |
| 4048 | * MUST discard the packet or generate a connection error with |
| 4049 | * PROTOCOL_VIOLATION as type. |
| 4050 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 4051 | */ |
| 4052 | pkt->token_len = token_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4053 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4054 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4055 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4056 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4057 | goto err; |
| 4058 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4059 | } |
| 4060 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4061 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 4062 | end - buf < len) { |
| 4063 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4064 | goto err; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4065 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4066 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4067 | payload = buf; |
| 4068 | pkt->len = len + payload - beg; |
| 4069 | |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4070 | qc = retrieve_qc_conn_from_cid(pkt, l, saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4071 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4072 | int ipv4; |
| 4073 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4074 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4075 | const unsigned char *salt = initial_salt_v1; |
| 4076 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4077 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4078 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 4079 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4080 | goto err; |
| 4081 | } |
| 4082 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4083 | pkt->saddr = *saddr; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4084 | ipv4 = saddr->ss_family == AF_INET; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4085 | qc = qc_new_conn(pkt->version, ipv4, |
| 4086 | pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4087 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4088 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4089 | goto err; |
| 4090 | |
| 4091 | odcid = &qc->rx.params.original_destination_connection_id; |
| 4092 | /* Copy the transport parameters. */ |
| 4093 | qc->rx.params = l->bind_conf->quic_params; |
| 4094 | /* Copy original_destination_connection_id transport parameter. */ |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4095 | memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len); |
| 4096 | odcid->len = pkt->dcid.len; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4097 | /* Copy the initial source connection ID. */ |
| 4098 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4099 | qc->enc_params_len = |
| 4100 | quic_transport_params_encode(qc->enc_params, |
| 4101 | qc->enc_params + sizeof qc->enc_params, |
| 4102 | &qc->rx.params, 1); |
| 4103 | if (!qc->enc_params_len) |
| 4104 | goto err; |
| 4105 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4106 | /* NOTE: the socket address has been concatenated to the destination ID |
| 4107 | * chosen by the client for Initial packets. |
| 4108 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4109 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4110 | salt = initial_salt_draft_29; |
| 4111 | salt_len = sizeof initial_salt_draft_29; |
| 4112 | } |
| 4113 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4114 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4115 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4116 | goto err; |
| 4117 | } |
| 4118 | |
Frédéric Lécaille | b0006ee | 2021-11-03 19:01:31 +0100 | [diff] [blame] | 4119 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4120 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4121 | n = ebmb_insert(&l->rx.odcids, &qc->odcid_node, |
| 4122 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4123 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4124 | /* If the insertion failed, it means that another |
| 4125 | * thread has already allocated a QUIC connection for |
| 4126 | * the same CID. Liberate our allocated connection. |
| 4127 | */ |
| 4128 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4129 | qc_to_purge = qc; |
| 4130 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4131 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 4132 | pkt->qc = qc; |
| 4133 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4134 | |
| 4135 | quic_conn_take(qc); |
| 4136 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 4137 | |
| 4138 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4139 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 4140 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4141 | MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list); |
| 4142 | /* Try to accept a new connection. */ |
| 4143 | listener_accept(l); |
| 4144 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4145 | else { |
| 4146 | quic_conn_drop(qc_to_purge); |
| 4147 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4148 | } |
| 4149 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4150 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4151 | } |
| 4152 | } |
| 4153 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4154 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4155 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4156 | goto err; |
| 4157 | } |
| 4158 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4159 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4160 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4161 | buf += QUIC_HAP_CID_LEN; |
| 4162 | |
| 4163 | /* A short packet is the last one of a UDP datagram. */ |
| 4164 | payload = buf; |
| 4165 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4166 | |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4167 | qc = retrieve_qc_conn_from_cid(pkt, l, saddr); |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4168 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4169 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4170 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
| 4171 | goto err; |
| 4172 | } |
| 4173 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4174 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4175 | } |
| 4176 | |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4177 | |
| 4178 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4179 | * they must have the same DCID. |
| 4180 | * |
| 4181 | * This check must be done after the final update to pkt.len to |
| 4182 | * properly drop the packet on failure. |
| 4183 | */ |
| 4184 | if (!dgram_ctx->dcid.len) { |
| 4185 | memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len); |
| 4186 | } |
| 4187 | else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4188 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4189 | goto err; |
| 4190 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4191 | dgram_ctx->qc = qc; |
| 4192 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4193 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4194 | if (HA_ATOMIC_LOAD(&qc->err_code)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4195 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4196 | goto out; |
| 4197 | } |
| 4198 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4199 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4200 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4201 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4202 | b_cspace = b_contig_space(&qc->rx.buf); |
| 4203 | if (b_cspace < pkt->len) { |
| 4204 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4205 | if (b_cspace) { |
| 4206 | b_putchr(&qc->rx.buf, 0x00); |
| 4207 | b_cspace--; |
| 4208 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4209 | b_add(&qc->rx.buf, b_cspace); |
| 4210 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 4211 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4212 | 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] | 4213 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4214 | goto err; |
| 4215 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4216 | } |
| 4217 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4218 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4219 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4220 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 4221 | goto err; |
| 4222 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4223 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4224 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4225 | 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] | 4226 | if (pkt->aad_len) |
| 4227 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4228 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 4229 | /* Wake up the connection packet handler task from here only if all |
| 4230 | * the contexts have been initialized, especially the mux context |
| 4231 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 4232 | * will start it if these contexts for the connection are not already |
| 4233 | * initialized. |
| 4234 | */ |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 4235 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
| 4236 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4237 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4238 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4239 | 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] | 4240 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4241 | if (qc) |
| 4242 | quic_conn_drop(qc); |
| 4243 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4244 | return pkt->len; |
| 4245 | |
| 4246 | err: |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4247 | /* If length not found, consume the entire packet */ |
| 4248 | if (!pkt->len) |
| 4249 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 4250 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4251 | qc ? qc : NULL, pkt); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4252 | |
| 4253 | if (qc) |
| 4254 | quic_conn_drop(qc); |
| 4255 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4256 | return -1; |
| 4257 | } |
| 4258 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4259 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 4260 | * 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] | 4261 | */ |
| 4262 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 4263 | int type, size_t pn_len, struct quic_conn *conn) |
| 4264 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4265 | 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] | 4266 | return 0; |
| 4267 | |
| 4268 | /* #0 byte flags */ |
| 4269 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 4270 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 4271 | /* Version */ |
| 4272 | quic_write_uint32(buf, end, conn->version); |
| 4273 | *(*buf)++ = conn->dcid.len; |
| 4274 | /* Destination connection ID */ |
| 4275 | if (conn->dcid.len) { |
| 4276 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4277 | *buf += conn->dcid.len; |
| 4278 | } |
| 4279 | /* Source connection ID */ |
| 4280 | *(*buf)++ = conn->scid.len; |
| 4281 | if (conn->scid.len) { |
| 4282 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 4283 | *buf += conn->scid.len; |
| 4284 | } |
| 4285 | |
| 4286 | return 1; |
| 4287 | } |
| 4288 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4289 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 4290 | * 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] | 4291 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4292 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 4293 | size_t pn_len, struct quic_conn *conn, |
| 4294 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4295 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4296 | if (end - *buf < 1 + conn->dcid.len) |
| 4297 | return 0; |
| 4298 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4299 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 4300 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 4301 | ((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] | 4302 | /* Destination connection ID */ |
| 4303 | if (conn->dcid.len) { |
| 4304 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4305 | *buf += conn->dcid.len; |
| 4306 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4307 | |
| 4308 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4309 | } |
| 4310 | |
| 4311 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 4312 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 4313 | * with <aead> as AEAD cipher and <key> as secret key. |
| 4314 | * Returns 1 if succeeded or 0 if failed. |
| 4315 | */ |
| 4316 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 4317 | const EVP_CIPHER *aead, const unsigned char *key) |
| 4318 | { |
| 4319 | int i, ret, outlen; |
| 4320 | EVP_CIPHER_CTX *ctx; |
| 4321 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 4322 | * and at most 4 bytes for the packet number |
| 4323 | */ |
| 4324 | unsigned char mask[5] = {0}; |
| 4325 | |
| 4326 | ret = 0; |
| 4327 | ctx = EVP_CIPHER_CTX_new(); |
| 4328 | if (!ctx) |
| 4329 | return 0; |
| 4330 | |
| 4331 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 4332 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 4333 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 4334 | goto out; |
| 4335 | |
| 4336 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 4337 | for (i = 0; i < pnlen; i++) |
| 4338 | pn[i] ^= mask[i + 1]; |
| 4339 | |
| 4340 | ret = 1; |
| 4341 | |
| 4342 | out: |
| 4343 | EVP_CIPHER_CTX_free(ctx); |
| 4344 | |
| 4345 | return ret; |
| 4346 | } |
| 4347 | |
| 4348 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 4349 | * ACK ranges if needed to a value below <limit> in bytes. |
| 4350 | * Return 1 if succeeded, 0 if not. |
| 4351 | */ |
| 4352 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 4353 | { |
| 4354 | size_t room, ack_delay_sz; |
| 4355 | |
| 4356 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 4357 | /* A frame is made of 1 byte for the frame type. */ |
| 4358 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4359 | 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] | 4360 | return 0; |
| 4361 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4362 | 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] | 4363 | } |
| 4364 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4365 | /* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames |
| 4366 | * 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] | 4367 | * and <*len> the packet Length field initialized with the number of bytes already present |
| 4368 | * 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] | 4369 | * <headlen> is the number of bytes already present in this packet before building frames. |
| 4370 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4371 | * Update consequently <*len> to reflect the size of these frames built |
| 4372 | * by this function. Also attach these frames to <pkt> QUIC packet. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4373 | * Return 1 if succeeded, 0 if not. |
| 4374 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4375 | static inline int qc_build_frms(struct quic_tx_packet *pkt, |
| 4376 | size_t room, size_t *len, size_t headlen, |
| 4377 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4378 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4379 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4380 | int ret; |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4381 | struct quic_frame *cf; |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 4382 | struct mt_list *tmp1, tmp2; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4383 | size_t remain = quic_path_prep_data(qc->path); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4384 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4385 | ret = 0; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4386 | if (*len > room || headlen > remain) |
| 4387 | return 0; |
| 4388 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4389 | /* If we are not probing we must take into an account the congestion |
| 4390 | * control window. |
| 4391 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 4392 | if (!qel->pktns->tx.pto_probe) |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4393 | room = QUIC_MIN(room, quic_path_prep_data(qc->path) - headlen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4394 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4395 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 4396 | mt_list_for_each_entry_safe(cf, &qel->pktns->tx.frms, mt_list, tmp1, tmp2) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4397 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4398 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4399 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4400 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4401 | break; |
| 4402 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4403 | switch (cf->type) { |
| 4404 | case QUIC_FT_CRYPTO: |
| 4405 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4406 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4407 | /* Compute the length of this CRYPTO frame header */ |
| 4408 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 4409 | /* Compute the data length of this CRyPTO frame. */ |
| 4410 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 4411 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4412 | 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] | 4413 | if (!dlen) |
| 4414 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4415 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4416 | /* CRYPTO frame length. */ |
| 4417 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 4418 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4419 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4420 | /* Add the CRYPTO data length and its encoded length to the packet |
| 4421 | * length and the length of this length. |
| 4422 | */ |
| 4423 | *len += flen; |
| 4424 | room -= flen; |
| 4425 | if (dlen == cf->crypto.len) { |
| 4426 | /* <cf> CRYPTO data have been consumed. */ |
| 4427 | MT_LIST_DELETE_SAFE(tmp1); |
| 4428 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4429 | } |
| 4430 | else { |
| 4431 | struct quic_frame *new_cf; |
| 4432 | |
| 4433 | new_cf = pool_alloc(pool_head_quic_frame); |
| 4434 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4435 | 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] | 4436 | return 0; |
| 4437 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4438 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4439 | new_cf->type = QUIC_FT_CRYPTO; |
| 4440 | new_cf->crypto.len = dlen; |
| 4441 | new_cf->crypto.offset = cf->crypto.offset; |
| 4442 | new_cf->crypto.qel = qel; |
| 4443 | LIST_APPEND(&pkt->frms, &new_cf->list); |
| 4444 | /* Consume <dlen> bytes of the current frame. */ |
| 4445 | cf->crypto.len -= dlen; |
| 4446 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4447 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4448 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4449 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4450 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4451 | /* Note that these frames are accepted in short packets only without |
| 4452 | * "Length" packet field. Here, <*len> is used only to compute the |
| 4453 | * 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] | 4454 | * |
| 4455 | * 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] | 4456 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 4457 | */ |
| 4458 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4459 | ((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] | 4460 | /* Compute the data length of this STREAM frame. */ |
| 4461 | avail_room = room - hlen - *len; |
| 4462 | if ((ssize_t)avail_room <= 0) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4463 | break; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4464 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 4465 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4466 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4467 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 4468 | dlen = max_available_room(avail_room, &dlen_sz); |
| 4469 | if (dlen > cf->stream.len) { |
| 4470 | dlen = cf->stream.len; |
| 4471 | } |
| 4472 | dlen_sz = quic_int_getsize(dlen); |
| 4473 | flen = hlen + dlen_sz + dlen; |
| 4474 | } |
| 4475 | else { |
| 4476 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 4477 | flen = hlen + dlen; |
| 4478 | } |
| 4479 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4480 | 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] | 4481 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4482 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4483 | /* Add the STREAM data length and its encoded length to the packet |
| 4484 | * length and the length of this length. |
| 4485 | */ |
| 4486 | *len += flen; |
| 4487 | room -= flen; |
| 4488 | if (dlen == cf->stream.len) { |
| 4489 | /* <cf> STREAM data have been consumed. */ |
| 4490 | MT_LIST_DELETE_SAFE(tmp1); |
| 4491 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4492 | } |
| 4493 | else { |
| 4494 | struct quic_frame *new_cf; |
| 4495 | |
| 4496 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 4497 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4498 | 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] | 4499 | return 0; |
| 4500 | } |
| 4501 | |
| 4502 | new_cf->type = cf->type; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4503 | new_cf->stream.qcs = cf->stream.qcs; |
| 4504 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4505 | new_cf->stream.id = cf->stream.id; |
| 4506 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 4507 | new_cf->stream.offset = cf->stream.offset; |
| 4508 | new_cf->stream.len = dlen; |
| 4509 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 4510 | /* FIN bit reset */ |
| 4511 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 4512 | new_cf->stream.data = cf->stream.data; |
| 4513 | LIST_APPEND(&pkt->frms, &new_cf->list); |
| 4514 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 4515 | /* Consume <dlen> bytes of the current frame. */ |
| 4516 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4517 | cf->stream.offset.key += dlen; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4518 | cf->stream.data += dlen; |
| 4519 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4520 | break; |
| 4521 | |
| 4522 | default: |
| 4523 | flen = qc_frm_len(cf); |
| 4524 | BUG_ON(!flen); |
| 4525 | if (flen > room) |
| 4526 | continue; |
| 4527 | |
| 4528 | *len += flen; |
| 4529 | room -= flen; |
| 4530 | MT_LIST_DELETE_SAFE(tmp1); |
| 4531 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4532 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4533 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4534 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4535 | } |
| 4536 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4537 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4538 | } |
| 4539 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4540 | /* 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] | 4541 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 4542 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 4543 | * filling the buffer with as much frames as possible. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4544 | * 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] | 4545 | * 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] | 4546 | * having returned from this function. |
| 4547 | * 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] | 4548 | * number field in this packet. <pn_len> will also have the packet number |
| 4549 | * length as value. |
| 4550 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4551 | * 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] | 4552 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4553 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 4554 | size_t dglen, struct quic_tx_packet *pkt, |
| 4555 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
| 4556 | int ack, int padding, int cc, int nb_pto_dgrams, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4557 | struct quic_enc_level *qel, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4558 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4559 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4560 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4561 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 4562 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4563 | 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] | 4564 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4565 | int64_t largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4566 | int add_ping_frm; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4567 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4568 | /* Length field value with CRYPTO frames if present. */ |
| 4569 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4570 | beg = pos; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4571 | /* When not probing and not acking, and no immediate close is required, |
| 4572 | * reduce the size of this buffer to respect |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4573 | * the congestion controller window. So, we do not limit the size of this |
| 4574 | * packet if we have an ACK frame to send because an ACK frame is not |
| 4575 | * ack-eliciting. This size will be limited if we have ack-eliciting |
| 4576 | * frames to send from qel->pktns->tx.frms. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4577 | */ |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4578 | if (!nb_pto_dgrams && !ack && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4579 | size_t path_room; |
| 4580 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4581 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4582 | if (end - beg > path_room) |
| 4583 | end = beg + path_room; |
| 4584 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4585 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4586 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 4587 | * length field if any. |
| 4588 | */ |
| 4589 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 4590 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 4591 | goto no_room; |
| 4592 | |
| 4593 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | e1aa0d3 | 2021-08-03 16:03:09 +0200 | [diff] [blame] | 4594 | 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] | 4595 | /* packet number length */ |
| 4596 | *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] | 4597 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4598 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4599 | !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] | 4600 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4601 | !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] | 4602 | goto no_room; |
| 4603 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4604 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 4605 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4606 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4607 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4608 | /* Build an ACK frame if required. */ |
| 4609 | ack_frm_len = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4610 | 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] | 4611 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4612 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4613 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 4614 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 4615 | * that from here, we do not know if we will have to send a PING frame. |
| 4616 | * This will be decided after having computed the ack-eliciting frames |
| 4617 | * to be added to this packet. |
| 4618 | */ |
| 4619 | 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] | 4620 | if (!ack_frm_len) |
| 4621 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4622 | } |
| 4623 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4624 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4625 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4626 | len_frms = 0; |
| 4627 | if (!cc && !MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4628 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4629 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4630 | /* Initialize the length of the frames built below to <len>. |
| 4631 | * If any frame could be successfully built by qc_build_frms(), |
| 4632 | * we will have len_frms > len. |
| 4633 | */ |
| 4634 | len_frms = len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4635 | if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4636 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4637 | qc, NULL, NULL, &room); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4638 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4639 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4640 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4641 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 4642 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 4643 | * for the encryption tag. It must be taken into an account for the length |
| 4644 | * of this packet. |
| 4645 | */ |
| 4646 | if (len_frms) |
| 4647 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 4648 | else |
| 4649 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4650 | /* CONNECTION_CLOSE frame */ |
| 4651 | if (cc) { |
| 4652 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4653 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4654 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4655 | len += qc_frm_len(&cc_frm); |
| 4656 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4657 | add_ping_frm = 0; |
| 4658 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4659 | len_sz = quic_int_getsize(len); |
| 4660 | /* Add this packet size to <dglen> */ |
| 4661 | dglen += head_len + len_sz + len; |
| 4662 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 4663 | /* This is a maximum padding size */ |
| 4664 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 4665 | /* The length field value is of this packet is <len> + <padding_len> |
| 4666 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 4667 | * <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] | 4668 | * sizes from <padding_len>. |
| 4669 | */ |
| 4670 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 4671 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4672 | } |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4673 | else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4674 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4675 | /* 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] | 4676 | add_ping_frm = 1; |
| 4677 | len += 1; |
| 4678 | } |
| 4679 | /* 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] | 4680 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4681 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 4682 | } |
| 4683 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4684 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 4685 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4686 | |
| 4687 | /* Packet number field address. */ |
| 4688 | *buf_pn = pos; |
| 4689 | |
| 4690 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4691 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 4692 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4693 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4694 | 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] | 4695 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4696 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4697 | /* Ack-eliciting frames */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4698 | if (!LIST_ISEMPTY(&pkt->frms)) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4699 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4700 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4701 | TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4702 | list_for_each_entry(cf, &pkt->frms, list) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4703 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4704 | ssize_t room = end - pos; |
| 4705 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4706 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4707 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4708 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4709 | } |
| 4710 | } |
| 4711 | |
| 4712 | /* Build a PING frame if needed. */ |
| 4713 | if (add_ping_frm) { |
| 4714 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4715 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4716 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4717 | } |
| 4718 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4719 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4720 | 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] | 4721 | goto no_room; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4722 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4723 | /* Build a PADDING frame if needed. */ |
| 4724 | if (padding_len) { |
| 4725 | frm.type = QUIC_FT_PADDING; |
| 4726 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4727 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4728 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4729 | } |
| 4730 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 4731 | /* If this packet is ack-eliciting and we are probing let's |
| 4732 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4733 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 4734 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 4735 | qel->pktns->tx.pto_probe) |
| 4736 | qel->pktns->tx.pto_probe--; |
| 4737 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4738 | pkt->len = pos - beg; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4739 | |
| 4740 | return 1; |
| 4741 | |
| 4742 | no_room: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4743 | 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] | 4744 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4745 | } |
| 4746 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4747 | 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] | 4748 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4749 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4750 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4751 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 4752 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4753 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4754 | pkt->next = NULL; |
| 4755 | pkt->refcnt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4756 | } |
| 4757 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4758 | /* 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] | 4759 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 4760 | { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4761 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4762 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4763 | if (!pkt) |
| 4764 | return; |
| 4765 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4766 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4767 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4768 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4769 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4770 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4771 | } |
| 4772 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4773 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 4774 | * type for <qc> QUIC connection from <qel> encryption level. |
| 4775 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 4776 | * -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] | 4777 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4778 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 4779 | const unsigned char *buf_end, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4780 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4781 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4782 | int pkt_type, int ack, int nb_pto_dgrams, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4783 | { |
| 4784 | /* The pointer to the packet number field. */ |
| 4785 | unsigned char *buf_pn; |
| 4786 | unsigned char *beg, *end, *payload; |
| 4787 | int64_t pn; |
| 4788 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4789 | struct quic_tls_ctx *tls_ctx; |
| 4790 | struct quic_tx_packet *pkt; |
| 4791 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4792 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4793 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4794 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 4795 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4796 | 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] | 4797 | *err = -2; |
| 4798 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4799 | } |
| 4800 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4801 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4802 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4803 | pn_len = 0; |
| 4804 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4805 | |
| 4806 | pn = qel->pktns->tx.next_pn + 1; |
| 4807 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
| 4808 | ack, padding, cc, nb_pto_dgrams, qel, qc)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4809 | *err = -1; |
| 4810 | goto err; |
| 4811 | } |
| 4812 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4813 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4814 | payload = buf_pn + pn_len; |
| 4815 | payload_len = end - payload; |
| 4816 | aad_len = payload - beg; |
| 4817 | |
| 4818 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4819 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) { |
| 4820 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4821 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4822 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4823 | |
| 4824 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4825 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4826 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 4827 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4828 | 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] | 4829 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4830 | goto err; |
| 4831 | } |
| 4832 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4833 | /* Consume a packet number */ |
| 4834 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 4835 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4836 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 4837 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4838 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 4839 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4840 | /* 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] | 4841 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4842 | pkt->in_flight_len = pkt->len; |
| 4843 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4844 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4845 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4846 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4847 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4848 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4849 | |
| 4850 | err: |
| 4851 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4852 | 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] | 4853 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4854 | } |
| 4855 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4856 | /* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>. |
| 4857 | * Return the number of bytes which have been copied. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4858 | */ |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4859 | static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx, |
| 4860 | struct buffer *buf, size_t count, int flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4861 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4862 | size_t try, done = 0; |
| 4863 | |
| 4864 | if (!conn_ctrl_ready(conn)) |
| 4865 | return 0; |
| 4866 | |
| 4867 | if (!fd_recv_ready(conn->handle.fd)) |
| 4868 | return 0; |
| 4869 | |
| 4870 | conn->flags &= ~CO_FL_WAIT_ROOM; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4871 | |
| 4872 | /* read the largest possible block. For this, we perform only one call |
| 4873 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4874 | * in which case we accept to do it once again. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4875 | */ |
| 4876 | while (count > 0) { |
| 4877 | try = b_contig_space(buf); |
| 4878 | if (!try) |
| 4879 | break; |
| 4880 | |
| 4881 | if (try > count) |
| 4882 | try = count; |
| 4883 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4884 | b_add(buf, try); |
| 4885 | done += try; |
| 4886 | count -= try; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 4890 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4891 | |
| 4892 | leave: |
| 4893 | return done; |
| 4894 | |
| 4895 | read0: |
| 4896 | conn_sock_read0(conn); |
| 4897 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4898 | |
| 4899 | /* Now a final check for a possible asynchronous low-level error |
| 4900 | * report. This can happen when a connection receives a reset |
| 4901 | * after a shutdown, both POLL_HUP and POLL_ERR are queued, and |
| 4902 | * we might have come from there by just checking POLL_HUP instead |
| 4903 | * of recv()'s return value 0, so we have no way to tell there was |
| 4904 | * an error without checking. |
| 4905 | */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 4906 | if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4907 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 4908 | goto leave; |
| 4909 | } |
| 4910 | |
| 4911 | |
| 4912 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 4913 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 4914 | * other pending data for example, but this flag is ignored at the moment. |
| 4915 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 4916 | * a second call may be performed. The connection's flags are updated with |
| 4917 | * whatever special event is detected (error, empty). The caller is responsible |
| 4918 | * for taking care of those events and avoiding the call if inappropriate. The |
| 4919 | * function does not call the connection's polling update function, so the caller |
| 4920 | * is responsible for this. It's up to the caller to update the buffer's contents |
| 4921 | * based on the return value. |
| 4922 | */ |
| 4923 | static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags) |
| 4924 | { |
| 4925 | ssize_t ret; |
| 4926 | size_t try, done; |
| 4927 | int send_flag; |
| 4928 | |
| 4929 | if (!conn_ctrl_ready(conn)) |
| 4930 | return 0; |
| 4931 | |
| 4932 | if (!fd_send_ready(conn->handle.fd)) |
| 4933 | return 0; |
| 4934 | |
| 4935 | done = 0; |
| 4936 | /* send the largest possible block. For this we perform only one call |
| 4937 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 4938 | * in which case we accept to do it once again. |
| 4939 | */ |
| 4940 | while (count) { |
| 4941 | try = b_contig_data(buf, done); |
| 4942 | if (try > count) |
| 4943 | try = count; |
| 4944 | |
| 4945 | send_flag = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 4946 | if (try < count || flags & CO_SFL_MSG_MORE) |
| 4947 | send_flag |= MSG_MORE; |
| 4948 | |
| 4949 | ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag, |
| 4950 | (struct sockaddr *)conn->dst, get_addr_len(conn->dst)); |
| 4951 | if (ret > 0) { |
| 4952 | count -= ret; |
| 4953 | done += ret; |
| 4954 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4955 | /* A send succeeded, so we can consider ourself connected */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4956 | conn->flags |= CO_FL_WAIT_L4L6; |
| 4957 | /* if the system buffer is full, don't insist */ |
| 4958 | if (ret < try) |
| 4959 | break; |
| 4960 | } |
| 4961 | else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) { |
| 4962 | /* nothing written, we need to poll for write first */ |
| 4963 | fd_cant_send(conn->handle.fd); |
| 4964 | break; |
| 4965 | } |
| 4966 | else if (errno != EINTR) { |
| 4967 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 4968 | break; |
| 4969 | } |
| 4970 | } |
| 4971 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 4972 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4973 | |
| 4974 | if (done > 0) { |
| 4975 | /* we count the total bytes sent, and the send rate for 32-byte |
| 4976 | * blocks. The reason for the latter is that freq_ctr are |
| 4977 | * limited to 4GB and that it's not enough per second. |
| 4978 | */ |
| 4979 | _HA_ATOMIC_ADD(&global.out_bytes, done); |
| 4980 | update_freq_ctr(&global.out_32bps, (done + 16) / 32); |
| 4981 | } |
| 4982 | return done; |
| 4983 | } |
| 4984 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 4985 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 4986 | * event subscriber <es> is not allowed to change from a previous call as long |
| 4987 | * as at least one event is still subscribed. The <event_type> must only be a |
| 4988 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 4989 | */ |
| 4990 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 4991 | { |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 4992 | struct qcc *qcc = conn->qc->qcc; |
| 4993 | |
| 4994 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 4995 | BUG_ON(qcc->subs && qcc->subs != es); |
| 4996 | |
| 4997 | es->events |= event_type; |
| 4998 | qcc->subs = es; |
| 4999 | |
| 5000 | if (event_type & SUB_RETRY_RECV) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5001 | 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] | 5002 | |
| 5003 | if (event_type & SUB_RETRY_SEND) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5004 | 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] | 5005 | |
| 5006 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5007 | } |
| 5008 | |
| 5009 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5010 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5011 | * subscribe() call. It always returns zero. |
| 5012 | */ |
| 5013 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5014 | { |
| 5015 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 5016 | } |
| 5017 | |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 5018 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 5019 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 5020 | * parameters of this session. |
| 5021 | * This is the responsibility of the caller to check the validity of all the |
| 5022 | * pointers passed as parameter to this function. |
| 5023 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 5024 | * CO_ER_SSL_NO_MEM. |
| 5025 | */ |
| 5026 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 5027 | unsigned char *params, size_t params_len) |
| 5028 | { |
| 5029 | int retry; |
| 5030 | |
| 5031 | retry = 1; |
| 5032 | retry: |
| 5033 | *ssl = SSL_new(ssl_ctx); |
| 5034 | if (!*ssl) { |
| 5035 | if (!retry--) |
| 5036 | goto err; |
| 5037 | |
| 5038 | pool_gc(NULL); |
| 5039 | goto retry; |
| 5040 | } |
| 5041 | |
| 5042 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 5043 | !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) || |
| 5044 | !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) { |
| 5045 | goto err; |
| 5046 | |
| 5047 | SSL_free(*ssl); |
| 5048 | *ssl = NULL; |
| 5049 | if (!retry--) |
| 5050 | goto err; |
| 5051 | |
| 5052 | pool_gc(NULL); |
| 5053 | goto retry; |
| 5054 | } |
| 5055 | |
| 5056 | return 0; |
| 5057 | |
| 5058 | err: |
| 5059 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 5060 | return -1; |
| 5061 | } |
| 5062 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5063 | /* Initialize a QUIC connection (quic_conn struct) to be attached to <conn> |
| 5064 | * connection with <xprt_ctx> as address of the xprt context. |
| 5065 | * Returns 1 if succeeded, 0 if not. |
| 5066 | */ |
| 5067 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 5068 | { |
Amaury Denoyelle | 9979d0d | 2021-12-23 16:32:24 +0100 | [diff] [blame] | 5069 | struct ssl_sock_ctx *ctx = NULL; |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5070 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5071 | |
| 5072 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 5073 | |
| 5074 | if (*xprt_ctx) |
| 5075 | goto out; |
| 5076 | |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5077 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5078 | if (!ctx) { |
| 5079 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 5080 | goto err; |
| 5081 | } |
| 5082 | |
| 5083 | ctx->wait_event.tasklet = tasklet_new(); |
| 5084 | if (!ctx->wait_event.tasklet) { |
| 5085 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 5086 | goto err; |
| 5087 | } |
| 5088 | |
| 5089 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 5090 | ctx->wait_event.tasklet->context = ctx; |
| 5091 | ctx->wait_event.events = 0; |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5092 | HA_ATOMIC_STORE(&ctx->conn, conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5093 | ctx->subs = NULL; |
| 5094 | ctx->xprt_ctx = NULL; |
| 5095 | |
| 5096 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 5097 | if (objt_server(conn->target)) { |
| 5098 | /* Server */ |
| 5099 | struct server *srv = __objt_server(conn->target); |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 5100 | unsigned char dcid[QUIC_HAP_CID_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5101 | int ssl_err, ipv4; |
| 5102 | |
| 5103 | ssl_err = SSL_ERROR_NONE; |
| 5104 | if (RAND_bytes(dcid, sizeof dcid) != 1) |
| 5105 | goto err; |
| 5106 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5107 | ipv4 = conn->dst->ss_family == AF_INET; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5108 | qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5109 | dcid, sizeof dcid, 0, NULL, 0, 0, srv); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5110 | if (qc == NULL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5111 | goto err; |
| 5112 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 5113 | ctx->qc = qc; |
| 5114 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5115 | /* Insert our SCID, the connection ID for the QUIC client. */ |
| 5116 | ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len); |
| 5117 | |
| 5118 | conn->qc = qc; |
| 5119 | qc->conn = conn; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 5120 | if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1, |
| 5121 | dcid, sizeof dcid, 0)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5122 | goto err; |
| 5123 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5124 | qc->rx.params = srv->quic_params; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5125 | /* Copy the initial source connection ID. */ |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5126 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 5127 | qc->enc_params_len = |
| 5128 | quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params, |
| 5129 | &qc->rx.params, 0); |
| 5130 | if (!qc->enc_params_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5131 | goto err; |
| 5132 | |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 5133 | if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl, |
| 5134 | qc->enc_params, qc->enc_params_len) == -1) |
| 5135 | goto err; |
| 5136 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5137 | SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5138 | SSL_set_connect_state(ctx->ssl); |
| 5139 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 5140 | if (ssl_err != 1) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 5141 | int st; |
| 5142 | |
| 5143 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5144 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 5145 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5146 | TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5147 | } |
| 5148 | else { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5149 | TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5150 | goto err; |
| 5151 | } |
| 5152 | } |
| 5153 | } |
| 5154 | else if (objt_listener(conn->target)) { |
| 5155 | /* Listener */ |
| 5156 | struct bind_conf *bc = __objt_listener(conn->target)->bind_conf; |
| 5157 | |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5158 | qc = ctx->conn->qc; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 5159 | ctx->qc = qc; |
| 5160 | |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 5161 | qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid); |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 5162 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 5163 | qc->enc_params, qc->enc_params_len) == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5164 | goto err; |
| 5165 | |
Frédéric Lécaille | ad3c07a | 2021-12-14 19:23:43 +0100 | [diff] [blame] | 5166 | /* Enabling 0-RTT */ |
| 5167 | if (bc->ssl_conf.early_data) |
| 5168 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 5169 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5170 | SSL_set_accept_state(ctx->ssl); |
| 5171 | } |
| 5172 | |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5173 | HA_ATOMIC_STORE(xprt_ctx, ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5174 | |
| 5175 | /* Leave init state and start handshake */ |
| 5176 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5177 | |
| 5178 | out: |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5179 | HA_ATOMIC_STORE(&qc->xprt_ctx, ctx); |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5180 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5181 | |
| 5182 | return 0; |
| 5183 | |
| 5184 | err: |
Willy Tarreau | 7deb28c | 2021-05-10 07:40:27 +0200 | [diff] [blame] | 5185 | if (ctx && ctx->wait_event.tasklet) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5186 | tasklet_free(ctx->wait_event.tasklet); |
| 5187 | pool_free(pool_head_quic_conn_ctx, ctx); |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 5188 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5189 | return -1; |
| 5190 | } |
| 5191 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5192 | /* Start the QUIC transport layer */ |
| 5193 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 5194 | { |
| 5195 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5196 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5197 | |
| 5198 | qc = conn->qc; |
| 5199 | if (!quic_conn_init_timer(qc)) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5200 | TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5201 | return 0; |
| 5202 | } |
| 5203 | |
| 5204 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 5205 | return 1; |
| 5206 | } |
| 5207 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5208 | /* transport-layer operations for QUIC connections. */ |
| 5209 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 5210 | .close = quic_close, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5211 | .snd_buf = quic_conn_from_buf, |
| 5212 | .rcv_buf = quic_conn_to_buf, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5213 | .subscribe = quic_conn_subscribe, |
| 5214 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5215 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5216 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5217 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 5218 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 5219 | .get_alpn = ssl_sock_get_alpn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5220 | .name = "QUIC", |
| 5221 | }; |
| 5222 | |
| 5223 | __attribute__((constructor)) |
| 5224 | static void __quic_conn_init(void) |
| 5225 | { |
| 5226 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 5227 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 5228 | } |
| 5229 | |
| 5230 | __attribute__((destructor)) |
| 5231 | static void __quic_conn_deinit(void) |
| 5232 | { |
| 5233 | BIO_meth_free(ha_quic_meth); |
| 5234 | } |
| 5235 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5236 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 5237 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5238 | * 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] | 5239 | */ |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5240 | static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5241 | struct sockaddr_storage *saddr, qpkt_read_func *func) |
| 5242 | { |
| 5243 | unsigned char *pos; |
| 5244 | const unsigned char *end; |
| 5245 | struct quic_dgram_ctx dgram_ctx = { |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5246 | .qc = NULL, |
| 5247 | .dcid.len = 0, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5248 | .owner = owner, |
| 5249 | }; |
| 5250 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5251 | pos = (unsigned char *)b_head(buf); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5252 | end = pos + len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5253 | do { |
| 5254 | int ret; |
| 5255 | struct quic_rx_packet *pkt; |
| 5256 | |
Willy Tarreau | e449893 | 2021-03-22 21:13:05 +0100 | [diff] [blame] | 5257 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5258 | if (!pkt) |
| 5259 | goto err; |
| 5260 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5261 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5262 | ret = func(pos, end, pkt, &dgram_ctx, saddr); |
| 5263 | pos += pkt->len; |
Frédéric Lécaille | 310d1bd | 2021-09-22 15:10:49 +0200 | [diff] [blame] | 5264 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5265 | if (ret == -1) |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 5266 | /* If the packet length could not be found, we cannot continue. */ |
| 5267 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5268 | } while (pos < end); |
| 5269 | |
| 5270 | /* Increasing the received bytes counter by the UDP datagram length |
| 5271 | * if this datagram could be associated to a connection. |
| 5272 | */ |
| 5273 | if (dgram_ctx.qc) |
| 5274 | dgram_ctx.qc->rx.bytes += len; |
| 5275 | |
| 5276 | return pos - (unsigned char *)buf; |
| 5277 | |
| 5278 | err: |
| 5279 | return -1; |
| 5280 | } |
| 5281 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5282 | ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5283 | struct sockaddr_storage *saddr) |
| 5284 | { |
| 5285 | return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv); |
| 5286 | } |
| 5287 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5288 | /* Function to automatically activate QUIC traces on stdout. |
| 5289 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 5290 | * Main use for now is in the docker image for QUIC interop testing. |
| 5291 | */ |
| 5292 | static void quic_init_stdout_traces(void) |
| 5293 | { |
| 5294 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 5295 | trace_quic.sink = sink_find("stdout"); |
| 5296 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5297 | trace_quic.state = TRACE_STATE_RUNNING; |
| 5298 | #endif |
| 5299 | } |
| 5300 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 5301 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5302 | /* |
| 5303 | * Local variables: |
| 5304 | * c-indent-level: 8 |
| 5305 | * c-basic-offset: 8 |
| 5306 | * End: |
| 5307 | */ |