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 " |
| 292 | "if=%llu pp=%u pdg=%d", |
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, |
| 298 | pktns->tx.pto_probe, qc->tx.nb_pto_dgrams); |
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) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 389 | const struct quic_pktns *pktns = qc->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 | 546186b | 2021-08-03 14:25:36 +0200 | [diff] [blame] | 391 | " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u pdg=%llu", |
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 | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 398 | (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe, |
| 399 | (unsigned long long)qc->tx.nb_pto_dgrams); |
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 | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 499 | if (mask & QUIC_EV_CONN_STIMER) { |
| 500 | if (pktns->tx.loss_time) |
| 501 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 502 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 503 | } |
| 504 | if (mask & QUIC_EV_CONN_SPTO) { |
| 505 | if (pktns->tx.time_of_last_eliciting) |
| 506 | chunk_appendf(&trace_buf, " tole=%dms", |
| 507 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 508 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 509 | 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] | 510 | } |
| 511 | } |
| 512 | |
| 513 | if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) { |
| 514 | chunk_appendf(&trace_buf, |
| 515 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 520 | const struct quic_tx_packet *pkt = a2; |
| 521 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 522 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 523 | (unsigned long long)qc->path->cwnd, |
| 524 | (unsigned long long)qc->path->prep_in_flight, |
| 525 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 526 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 527 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 528 | (unsigned long)pkt->pn_node.key, |
| 529 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 530 | 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] | 531 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 532 | } |
| 533 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 534 | |
| 535 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 536 | const uint8_t *alert = a2; |
| 537 | const enum ssl_encryption_level_t *level = a3; |
| 538 | |
| 539 | if (alert) |
| 540 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 541 | if (level) |
| 542 | chunk_appendf(&trace_buf, " el=%c", |
| 543 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 544 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 545 | |
| 546 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 547 | const size_t *sz1 = a2; |
| 548 | const size_t *sz2 = a3; |
| 549 | const size_t *sz3 = a4; |
| 550 | |
| 551 | if (sz1) |
| 552 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 553 | if (sz2) |
| 554 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 555 | if (sz3) |
| 556 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 557 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 558 | |
| 559 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 560 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 561 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 562 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 563 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 564 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 565 | } |
| 566 | if (mask & QUIC_EV_CONN_LPKT) { |
| 567 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 568 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 569 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 570 | if (pkt) { |
| 571 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 572 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 573 | if (pkt->pn_node.key != (uint64_t)-1) |
| 574 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 575 | } |
| 576 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 577 | if (len) |
| 578 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | } |
| 582 | |
| 583 | /* 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] | 584 | 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] | 585 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 586 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 587 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 588 | if (objt_server(qc->conn->target)) |
| 589 | return 1; |
| 590 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 591 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 592 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 593 | if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) || |
| 594 | (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] | 595 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 596 | return 1; |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 602 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 603 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 604 | 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] | 605 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 606 | struct quic_pktns *pktns; |
| 607 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 608 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 609 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 610 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 611 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 612 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 613 | pktns = quic_loss_pktns(qc); |
| 614 | if (tick_isset(pktns->tx.loss_time)) { |
| 615 | qc->timer = pktns->tx.loss_time; |
| 616 | goto out; |
| 617 | } |
| 618 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 619 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 620 | * cancelled for a server which reached the anti-amplification limit. |
| 621 | */ |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 622 | if (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 623 | 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] | 624 | qc->timer = TICK_ETERNITY; |
| 625 | goto out; |
| 626 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 627 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 628 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 629 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 630 | /* Timer cancellation. */ |
| 631 | qc->timer = TICK_ETERNITY; |
| 632 | goto out; |
| 633 | } |
| 634 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 635 | handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE; |
| 636 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 637 | if (tick_isset(pto)) |
| 638 | qc->timer = pto; |
| 639 | out: |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 640 | if (qc->timer_task && qc->timer != TICK_ETERNITY) |
Amaury Denoyelle | 336f6fd | 2021-10-05 14:42:25 +0200 | [diff] [blame] | 641 | task_schedule(qc->timer_task, qc->timer); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 642 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 643 | } |
| 644 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 645 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 646 | * connection. |
| 647 | * Return 1 if succeeded, 0 if not. |
| 648 | */ |
| 649 | static int quic_tls_key_update(struct quic_conn *qc) |
| 650 | { |
| 651 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 652 | struct quic_tls_secrets *rx, *tx; |
| 653 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 654 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 655 | |
| 656 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 657 | rx = &tls_ctx->rx; |
| 658 | tx = &tls_ctx->tx; |
| 659 | nxt_rx = &qc->ku.nxt_rx; |
| 660 | nxt_tx = &qc->ku.nxt_tx; |
| 661 | |
| 662 | /* Prepare new RX secrets */ |
| 663 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 664 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 665 | 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] | 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 670 | nxt_rx->key, nxt_rx->keylen, |
| 671 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 672 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 673 | 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] | 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /* Prepare new TX secrets */ |
| 678 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 679 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 680 | 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] | 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 685 | nxt_tx->key, nxt_tx->keylen, |
| 686 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 687 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 688 | 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] | 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | return 1; |
| 693 | } |
| 694 | |
| 695 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 696 | * Must be used after having updated them. |
| 697 | * Always succeeds. |
| 698 | */ |
| 699 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 700 | { |
| 701 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 702 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 703 | |
| 704 | /* Rotate the RX secrets */ |
| 705 | curr_secret = tls_ctx->rx.secret; |
| 706 | curr_iv = tls_ctx->rx.iv; |
| 707 | curr_key = tls_ctx->rx.key; |
| 708 | |
| 709 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 710 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 711 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 712 | |
| 713 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 714 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 715 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 716 | |
| 717 | qc->ku.prv_rx.secret = curr_secret; |
| 718 | qc->ku.prv_rx.iv = curr_iv; |
| 719 | qc->ku.prv_rx.key = curr_key; |
| 720 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 721 | |
| 722 | /* Update the TX secrets */ |
| 723 | curr_secret = tls_ctx->tx.secret; |
| 724 | curr_iv = tls_ctx->tx.iv; |
| 725 | curr_key = tls_ctx->tx.key; |
| 726 | |
| 727 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 728 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 729 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 730 | |
| 731 | qc->ku.nxt_tx.secret = curr_secret; |
| 732 | qc->ku.nxt_tx.iv = curr_iv; |
| 733 | qc->ku.nxt_tx.key = curr_key; |
| 734 | } |
| 735 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 736 | #ifndef OPENSSL_IS_BORINGSSL |
| 737 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 738 | const uint8_t *read_secret, |
| 739 | const uint8_t *write_secret, size_t secret_len) |
| 740 | { |
| 741 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 742 | struct quic_tls_ctx *tls_ctx = |
| 743 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 744 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 745 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 746 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 747 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 748 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 749 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 750 | 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] | 751 | goto out; |
| 752 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 753 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 754 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 755 | 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] | 756 | goto err; |
| 757 | } |
| 758 | |
| 759 | rx = &tls_ctx->rx; |
| 760 | tx = &tls_ctx->tx; |
| 761 | |
| 762 | rx->aead = tx->aead = tls_aead(cipher); |
| 763 | rx->md = tx->md = tls_md(cipher); |
| 764 | rx->hp = tx->hp = tls_hp(cipher); |
| 765 | |
| 766 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 767 | 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] | 768 | read_secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 769 | 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] | 770 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 771 | } |
| 772 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 773 | rx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 774 | |
| 775 | if (!write_secret) |
| 776 | goto tp; |
| 777 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 778 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 779 | 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] | 780 | write_secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 781 | 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] | 782 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 785 | tx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 786 | tp: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 787 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 788 | const unsigned char *buf; |
| 789 | size_t buflen; |
| 790 | |
| 791 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 792 | if (!buflen) |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 793 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 794 | |
| 795 | 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] | 796 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 797 | } |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 798 | |
| 799 | if (level == ssl_encryption_application) { |
| 800 | struct quic_conn *qc = conn->qc; |
| 801 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 802 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 803 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 804 | |
| 805 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 806 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 807 | 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] | 808 | goto err; |
| 809 | } |
| 810 | |
| 811 | memcpy(rx->secret, read_secret, secret_len); |
| 812 | rx->secretlen = secret_len; |
| 813 | memcpy(tx->secret, write_secret, secret_len); |
| 814 | tx->secretlen = secret_len; |
| 815 | /* Initialize all the secret keys lengths */ |
| 816 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 817 | /* Prepare the next key update */ |
| 818 | if (!quic_tls_key_update(qc)) |
| 819 | goto err; |
| 820 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 821 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 822 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn->qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 823 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 824 | |
| 825 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 826 | 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] | 827 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 828 | } |
| 829 | #else |
| 830 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 831 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 832 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 833 | */ |
| 834 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 835 | const SSL_CIPHER *cipher, |
| 836 | const uint8_t *secret, size_t secret_len) |
| 837 | { |
| 838 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 839 | struct quic_tls_ctx *tls_ctx = |
| 840 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 841 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 842 | TRACE_ENTER(QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 843 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 844 | 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] | 845 | goto out; |
| 846 | } |
| 847 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 848 | tls_ctx->rx.aead = tls_aead(cipher); |
| 849 | tls_ctx->rx.md = tls_md(cipher); |
| 850 | tls_ctx->rx.hp = tls_hp(cipher); |
| 851 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 852 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 853 | goto err; |
| 854 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 855 | 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] | 856 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 857 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 858 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 859 | secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 860 | 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] | 861 | goto err; |
| 862 | } |
| 863 | |
| 864 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 865 | const unsigned char *buf; |
| 866 | size_t buflen; |
| 867 | |
| 868 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 869 | if (!buflen) |
| 870 | goto err; |
| 871 | |
| 872 | if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen)) |
| 873 | goto err; |
| 874 | } |
| 875 | |
| 876 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 877 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 878 | 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] | 879 | |
| 880 | return 1; |
| 881 | |
| 882 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 883 | 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] | 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 888 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 889 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 890 | */ |
| 891 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 892 | const SSL_CIPHER *cipher, |
| 893 | const uint8_t *secret, size_t secret_len) |
| 894 | { |
| 895 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 896 | struct quic_tls_ctx *tls_ctx = |
| 897 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 898 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 899 | TRACE_ENTER(QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 900 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 901 | 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] | 902 | goto out; |
| 903 | } |
| 904 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 905 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 906 | goto err; |
| 907 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 908 | tls_ctx->tx.aead = tls_aead(cipher); |
| 909 | tls_ctx->tx.md = tls_md(cipher); |
| 910 | tls_ctx->tx.hp = tls_hp(cipher); |
| 911 | |
| 912 | 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] | 913 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 914 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 915 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 916 | secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 917 | 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] | 918 | goto err; |
| 919 | } |
| 920 | |
| 921 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 922 | 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] | 923 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 924 | return 1; |
| 925 | |
| 926 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 927 | 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] | 928 | return 0; |
| 929 | } |
| 930 | #endif |
| 931 | |
| 932 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 933 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 934 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 935 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 936 | * store all the data. |
| 937 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 938 | */ |
| 939 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 940 | const unsigned char *data, size_t len) |
| 941 | { |
| 942 | struct quic_crypto_buf **qcb; |
| 943 | /* The remaining byte to store in CRYPTO buffers. */ |
| 944 | size_t cf_offset, cf_len, *nb_buf; |
| 945 | unsigned char *pos; |
| 946 | |
| 947 | nb_buf = &qel->tx.crypto.nb_buf; |
| 948 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 949 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 950 | cf_len = len; |
| 951 | |
| 952 | while (len) { |
| 953 | size_t to_copy, room; |
| 954 | |
| 955 | pos = (*qcb)->data + (*qcb)->sz; |
| 956 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 957 | to_copy = len > room ? room : len; |
| 958 | if (to_copy) { |
| 959 | memcpy(pos, data, to_copy); |
| 960 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 961 | qel->tx.crypto.sz += to_copy; |
| 962 | (*qcb)->sz += to_copy; |
| 963 | pos += to_copy; |
| 964 | len -= to_copy; |
| 965 | data += to_copy; |
| 966 | } |
| 967 | else { |
| 968 | struct quic_crypto_buf **tmp; |
| 969 | |
| 970 | tmp = realloc(qel->tx.crypto.bufs, |
| 971 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 972 | if (tmp) { |
| 973 | qel->tx.crypto.bufs = tmp; |
| 974 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 975 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 976 | if (!*qcb) |
| 977 | return 0; |
| 978 | |
| 979 | (*qcb)->sz = 0; |
| 980 | ++*nb_buf; |
| 981 | } |
| 982 | else { |
| 983 | break; |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 989 | * have been buffered. |
| 990 | */ |
| 991 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 992 | struct quic_frame *frm; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 993 | |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 994 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 995 | if (!frm) |
| 996 | return 0; |
| 997 | |
| 998 | frm->type = QUIC_FT_CRYPTO; |
| 999 | frm->crypto.offset = cf_offset; |
| 1000 | frm->crypto.len = cf_len; |
Frédéric Lécaille | f252adb | 2021-08-03 17:01:25 +0200 | [diff] [blame] | 1001 | frm->crypto.qel = qel; |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 1002 | 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] | 1003 | } |
| 1004 | |
| 1005 | return len == 0; |
| 1006 | } |
| 1007 | |
| 1008 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1009 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1010 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1011 | { |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1012 | 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] | 1013 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1014 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1015 | } |
| 1016 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1017 | /* Set the application for <qc> QUIC connection. |
| 1018 | * Return 1 if succeeded, 0 if not. |
| 1019 | */ |
| 1020 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1021 | { |
| 1022 | const struct qcc_app_ops *app_ops; |
| 1023 | |
| 1024 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) { |
| 1025 | app_ops = qc->qcc->app_ops = &h3_ops; |
| 1026 | } |
| 1027 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) { |
| 1028 | app_ops = qc->qcc->app_ops = &hq_interop_ops; |
| 1029 | } |
| 1030 | else |
| 1031 | return 0; |
| 1032 | |
| 1033 | if (app_ops->init && !app_ops->init(qc->qcc)) |
| 1034 | return 0; |
| 1035 | |
| 1036 | if (app_ops->finalize) |
| 1037 | app_ops->finalize(qc->qcc->ctx); |
| 1038 | |
| 1039 | return 1; |
| 1040 | } |
| 1041 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1042 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1043 | * wants to provide the QUIC layer with CRYPTO data. |
| 1044 | * Returns 1 if succeeded, 0 if not. |
| 1045 | */ |
| 1046 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1047 | const uint8_t *data, size_t len) |
| 1048 | { |
| 1049 | struct connection *conn; |
| 1050 | enum quic_tls_enc_level tel; |
| 1051 | struct quic_enc_level *qel; |
| 1052 | |
| 1053 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1054 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1055 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1056 | 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] | 1057 | goto out; |
| 1058 | } |
| 1059 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1060 | tel = ssl_to_quic_enc_level(level); |
| 1061 | qel = &conn->qc->els[tel]; |
| 1062 | |
| 1063 | if (tel == -1) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1064 | 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] | 1065 | goto err; |
| 1066 | } |
| 1067 | |
| 1068 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1069 | 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] | 1070 | goto err; |
| 1071 | } |
| 1072 | |
| 1073 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1074 | conn->qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1075 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1076 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1077 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1078 | return 1; |
| 1079 | |
| 1080 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1081 | 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] | 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | int ha_quic_flush_flight(SSL *ssl) |
| 1086 | { |
| 1087 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1088 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1089 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn->qc); |
| 1090 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1091 | |
| 1092 | return 1; |
| 1093 | } |
| 1094 | |
| 1095 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1096 | { |
| 1097 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1098 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1099 | 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] | 1100 | quic_set_tls_alert(conn->qc, alert); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1101 | 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] | 1102 | return 1; |
| 1103 | } |
| 1104 | |
| 1105 | /* QUIC TLS methods */ |
| 1106 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1107 | #ifdef OPENSSL_IS_BORINGSSL |
| 1108 | .set_read_secret = ha_set_rsec, |
| 1109 | .set_write_secret = ha_set_wsec, |
| 1110 | #else |
| 1111 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1112 | #endif |
| 1113 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1114 | .flush_flight = ha_quic_flush_flight, |
| 1115 | .send_alert = ha_quic_send_alert, |
| 1116 | }; |
| 1117 | |
| 1118 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1119 | * Returns an error count. |
| 1120 | */ |
| 1121 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1122 | { |
| 1123 | struct proxy *curproxy = bind_conf->frontend; |
| 1124 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1125 | int cfgerr = 0; |
| 1126 | |
| 1127 | #if 0 |
| 1128 | /* XXX Did not manage to use this. */ |
| 1129 | const char *ciphers = |
| 1130 | "TLS_AES_128_GCM_SHA256:" |
| 1131 | "TLS_AES_256_GCM_SHA384:" |
| 1132 | "TLS_CHACHA20_POLY1305_SHA256:" |
| 1133 | "TLS_AES_128_CCM_SHA256"; |
| 1134 | #endif |
Frédéric Lécaille | 4b1fddc | 2021-07-01 17:09:05 +0200 | [diff] [blame] | 1135 | 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] | 1136 | long options = |
| 1137 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1138 | SSL_OP_SINGLE_ECDH_USE | |
| 1139 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1140 | SSL_CTX *ctx; |
| 1141 | |
| 1142 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1143 | bind_conf->initial_ctx = ctx; |
| 1144 | |
| 1145 | SSL_CTX_set_options(ctx, options); |
| 1146 | #if 0 |
| 1147 | if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) { |
| 1148 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' " |
| 1149 | "for bind '%s' at [%s:%d].\n", |
| 1150 | curproxy->id, ciphers, |
| 1151 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1152 | cfgerr++; |
| 1153 | } |
| 1154 | #endif |
| 1155 | |
| 1156 | if (SSL_CTX_set1_curves_list(ctx, groups) != 1) { |
| 1157 | ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' " |
| 1158 | "for bind '%s' at [%s:%d].\n", |
| 1159 | curproxy->id, groups, |
| 1160 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1161 | cfgerr++; |
| 1162 | } |
| 1163 | |
| 1164 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1165 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1166 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1167 | SSL_CTX_set_default_verify_paths(ctx); |
| 1168 | |
| 1169 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1170 | #ifdef OPENSSL_IS_BORINGSSL |
| 1171 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1172 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1173 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1174 | if (bind_conf->ssl_conf.early_data) { |
| 1175 | 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] | 1176 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1177 | } |
| 1178 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1179 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1180 | #else |
| 1181 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1182 | #endif |
| 1183 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1184 | #endif |
| 1185 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1186 | |
| 1187 | return cfgerr; |
| 1188 | } |
| 1189 | |
| 1190 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1191 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1192 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1193 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1194 | */ |
| 1195 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1196 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1197 | { |
| 1198 | uint64_t expected_pn = largest_pn + 1; |
| 1199 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1200 | uint64_t pn_hwin = pn_win / 2; |
| 1201 | uint64_t pn_mask = pn_win - 1; |
| 1202 | uint64_t candidate_pn; |
| 1203 | |
| 1204 | |
| 1205 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1206 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1207 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1208 | candidate_pn + pn_hwin <= expected_pn) |
| 1209 | return candidate_pn + pn_win; |
| 1210 | |
| 1211 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1212 | return candidate_pn - pn_win; |
| 1213 | |
| 1214 | return candidate_pn; |
| 1215 | } |
| 1216 | |
| 1217 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1218 | * cryptographic context. |
| 1219 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1220 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1221 | * <end> points to one byte past the end of this packet. |
| 1222 | * Returns 1 if succeeded, 0 if not. |
| 1223 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1224 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1225 | 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] | 1226 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1227 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1228 | { |
| 1229 | int ret, outlen, i, pnlen; |
| 1230 | uint64_t packet_number; |
| 1231 | uint32_t truncated_pn = 0; |
| 1232 | unsigned char mask[5] = {0}; |
| 1233 | unsigned char *sample; |
| 1234 | EVP_CIPHER_CTX *cctx; |
| 1235 | unsigned char *hp_key; |
| 1236 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1237 | /* Check there is enough data in this packet. */ |
| 1238 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1239 | 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] | 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | cctx = EVP_CIPHER_CTX_new(); |
| 1244 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1245 | 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] | 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | ret = 0; |
| 1250 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1251 | |
| 1252 | hp_key = tls_ctx->rx.hp_key; |
| 1253 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1254 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1255 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1256 | 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] | 1257 | goto out; |
| 1258 | } |
| 1259 | |
| 1260 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1261 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1262 | for (i = 0; i < pnlen; i++) { |
| 1263 | pn[i] ^= mask[i + 1]; |
| 1264 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1265 | } |
| 1266 | |
| 1267 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1268 | /* Store remaining information for this unprotected header */ |
| 1269 | pkt->pn = packet_number; |
| 1270 | pkt->pnl = pnlen; |
| 1271 | |
| 1272 | ret = 1; |
| 1273 | |
| 1274 | out: |
| 1275 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1276 | |
| 1277 | return ret; |
| 1278 | } |
| 1279 | |
| 1280 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1281 | * address, with <payload_len> as payload length, <aad> as address of |
| 1282 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1283 | * context. |
| 1284 | * Returns 1 if succeeded, 0 if not. |
| 1285 | */ |
| 1286 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1287 | unsigned char *aad, size_t aad_len, uint64_t pn, |
| 1288 | struct quic_tls_ctx *tls_ctx, struct connection *conn) |
| 1289 | { |
| 1290 | unsigned char iv[12]; |
| 1291 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1292 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1293 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1294 | |
| 1295 | 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] | 1296 | 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] | 1297 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1301 | tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1302 | 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] | 1303 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1307 | |
| 1308 | err: |
| 1309 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1310 | 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] | 1311 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1315 | * Returns 1 if succeeded, 0 if not. |
| 1316 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1317 | 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] | 1318 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1319 | int ret, kp_changed; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1320 | unsigned char iv[12]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1321 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1322 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1323 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1324 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1325 | |
| 1326 | kp_changed = 0; |
| 1327 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1328 | /* The two tested bits are not at the same position, |
| 1329 | * this is why they are first both inversed. |
| 1330 | */ |
| 1331 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1332 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1333 | /* The lowest packet number of a previous key phase |
| 1334 | * cannot be null if it really stores previous key phase |
| 1335 | * secrets. |
| 1336 | */ |
| 1337 | if (!pkt->qc->ku.prv_rx.pn) |
| 1338 | return 0; |
| 1339 | |
| 1340 | rx_iv = pkt->qc->ku.prv_rx.iv; |
| 1341 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1342 | } |
| 1343 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1344 | /* Next key phase */ |
| 1345 | kp_changed = 1; |
| 1346 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
| 1347 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1348 | } |
| 1349 | } |
| 1350 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1351 | |
| 1352 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1353 | return 0; |
| 1354 | |
| 1355 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1356 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1357 | tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1358 | if (!ret) |
| 1359 | return 0; |
| 1360 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1361 | /* Update the keys only if the packet decryption succeeded. */ |
| 1362 | if (kp_changed) { |
| 1363 | quic_tls_rotate_keys(pkt->qc); |
| 1364 | /* Toggle the Key Phase bit */ |
| 1365 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1366 | /* Store the lowest packet number received for the current key phase */ |
| 1367 | tls_ctx->rx.pn = pkt->pn; |
| 1368 | /* Prepare the next key update */ |
| 1369 | if (!quic_tls_key_update(pkt->qc)) |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1373 | /* Update the packet length (required to parse the frames). */ |
| 1374 | pkt->len = pkt->aad_len + ret; |
| 1375 | |
| 1376 | return 1; |
| 1377 | } |
| 1378 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1379 | /* Remove from <qcs> stream the acknowledged frames. |
| 1380 | * Never fails. |
| 1381 | */ |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1382 | static int qcs_try_to_consume(struct qcs *qcs) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1383 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1384 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1385 | struct eb64_node *frm_node; |
| 1386 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1387 | ret = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1388 | frm_node = eb64_first(&qcs->tx.acked_frms); |
| 1389 | while (frm_node) { |
| 1390 | struct quic_stream *strm; |
| 1391 | |
| 1392 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
| 1393 | if (strm->offset.key != qcs->tx.ack_offset) |
| 1394 | break; |
| 1395 | |
| 1396 | b_del(strm->buf, strm->len); |
| 1397 | qcs->tx.ack_offset += strm->len; |
| 1398 | frm_node = eb64_next(frm_node); |
| 1399 | eb64_delete(&strm->offset); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1400 | ret = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1401 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1402 | |
| 1403 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1404 | } |
| 1405 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1406 | /* 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] | 1407 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1408 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1409 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1410 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1411 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1412 | 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] | 1413 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1414 | switch (frm->type) { |
| 1415 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1416 | { |
| 1417 | struct qcs *qcs = frm->stream.qcs; |
| 1418 | struct quic_stream *strm = &frm->stream; |
| 1419 | |
| 1420 | if (qcs->tx.ack_offset == strm->offset.key) { |
| 1421 | b_del(strm->buf, strm->len); |
| 1422 | qcs->tx.ack_offset += strm->len; |
| 1423 | LIST_DELETE(&frm->list); |
| 1424 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1425 | stream_acked = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1426 | } |
| 1427 | else { |
| 1428 | eb64_insert(&qcs->tx.acked_frms, &strm->offset); |
| 1429 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1430 | stream_acked |= qcs_try_to_consume(qcs); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1431 | } |
| 1432 | break; |
| 1433 | default: |
| 1434 | LIST_DELETE(&frm->list); |
| 1435 | pool_free(pool_head_quic_frame, frm); |
| 1436 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1437 | |
| 1438 | if (stream_acked) { |
| 1439 | struct qcc *qcc = qc->qcc; |
| 1440 | |
| 1441 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1442 | tasklet_wakeup(qcc->subs->tasklet); |
| 1443 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1444 | if (!qcc->subs->events) |
| 1445 | qcc->subs = NULL; |
| 1446 | } |
| 1447 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1451 | * deallocating them, and their TX frames. |
| 1452 | * Returns the last node reached to be used for the next range. |
| 1453 | * May be NULL if <largest> node could not be found. |
| 1454 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1455 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1456 | struct eb_root *pkts, |
| 1457 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1458 | struct list *newly_acked_pkts, |
| 1459 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1460 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1461 | { |
| 1462 | struct eb64_node *node; |
| 1463 | struct quic_tx_packet *pkt; |
| 1464 | |
| 1465 | if (largest_node) |
| 1466 | node = largest_node; |
| 1467 | else { |
| 1468 | node = eb64_lookup(pkts, largest); |
| 1469 | while (!node && largest > smallest) { |
| 1470 | node = eb64_lookup(pkts, --largest); |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1475 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1476 | |
| 1477 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1478 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1479 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1480 | 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] | 1481 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1482 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1483 | node = eb64_prev(node); |
| 1484 | eb64_delete(&pkt->pn_node); |
| 1485 | } |
| 1486 | |
| 1487 | return node; |
| 1488 | } |
| 1489 | |
| 1490 | /* Treat <frm> frame whose packet it is attached to has just been detected as non |
| 1491 | * acknowledged. |
| 1492 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1493 | static inline void qc_treat_nacked_tx_frm(struct quic_conn *qc, |
| 1494 | struct quic_frame *frm, |
| 1495 | struct quic_pktns *pktns) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1496 | { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1497 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1498 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 1499 | MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | /* Free the TX packets of <pkts> list */ |
| 1504 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1505 | { |
| 1506 | struct quic_tx_packet *pkt, *tmp; |
| 1507 | |
| 1508 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1509 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1510 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1511 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | /* Send a packet loss event nofification to the congestion controller |
| 1516 | * attached to <qc> connection with <lost_bytes> the number of lost bytes, |
| 1517 | * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet |
| 1518 | * at <now_us> current time. |
| 1519 | * Always succeeds. |
| 1520 | */ |
| 1521 | static inline void qc_cc_loss_event(struct quic_conn *qc, |
| 1522 | unsigned int lost_bytes, |
| 1523 | unsigned int newest_time_sent, |
| 1524 | unsigned int period, |
| 1525 | unsigned int now_us) |
| 1526 | { |
| 1527 | struct quic_cc_event ev = { |
| 1528 | .type = QUIC_CC_EVT_LOSS, |
| 1529 | .loss.now_ms = now_ms, |
| 1530 | .loss.max_ack_delay = qc->max_ack_delay, |
| 1531 | .loss.lost_bytes = lost_bytes, |
| 1532 | .loss.newest_time_sent = newest_time_sent, |
| 1533 | .loss.period = period, |
| 1534 | }; |
| 1535 | |
| 1536 | quic_cc_event(&qc->path->cc, &ev); |
| 1537 | } |
| 1538 | |
| 1539 | /* Send a packet ack event nofication for each newly acked packet of |
| 1540 | * <newly_acked_pkts> list and free them. |
| 1541 | * Always succeeds. |
| 1542 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1543 | 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] | 1544 | struct list *newly_acked_pkts) |
| 1545 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1546 | struct quic_tx_packet *pkt, *tmp; |
| 1547 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1548 | |
| 1549 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1550 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1551 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1552 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1553 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1554 | ev.ack.acked = pkt->in_flight_len; |
| 1555 | ev.ack.time_sent = pkt->time_sent; |
| 1556 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1557 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1558 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1559 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | } |
| 1563 | |
| 1564 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1565 | * their TX frames. |
| 1566 | * Send a packet loss event to the congestion controller if |
| 1567 | * in flight packet have been lost. |
| 1568 | * Also frees the packet in <pkts> list. |
| 1569 | * Never fails. |
| 1570 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1571 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1572 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1573 | struct list *pkts, |
| 1574 | uint64_t now_us) |
| 1575 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1576 | 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] | 1577 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1578 | uint64_t lost_bytes; |
| 1579 | |
| 1580 | lost_bytes = 0; |
| 1581 | oldest_lost = newest_lost = NULL; |
| 1582 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 1583 | lost_bytes += pkt->in_flight_len; |
| 1584 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1585 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1586 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1587 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1588 | /* Treat the frames of this lost packet. */ |
| 1589 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1590 | qc_treat_nacked_tx_frm(qc, frm, pktns); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1591 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1592 | if (!oldest_lost) { |
| 1593 | oldest_lost = newest_lost = pkt; |
| 1594 | } |
| 1595 | else { |
| 1596 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1597 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1598 | newest_lost = pkt; |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | if (lost_bytes) { |
| 1603 | /* Sent a packet loss event to the congestion controller. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1604 | 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] | 1605 | 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] | 1606 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1607 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1608 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1613 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1614 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1615 | * space if any not deemed lost. |
| 1616 | * Should be called after having received an ACK frame with newly acknowledged |
| 1617 | * packets or when the the loss detection timer has expired. |
| 1618 | * Always succeeds. |
| 1619 | */ |
| 1620 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1621 | struct quic_conn *qc, |
| 1622 | struct list *lost_pkts) |
| 1623 | { |
| 1624 | struct eb_root *pkts; |
| 1625 | struct eb64_node *node; |
| 1626 | struct quic_loss *ql; |
| 1627 | unsigned int loss_delay; |
| 1628 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1629 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1630 | pkts = &pktns->tx.pkts; |
| 1631 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1632 | if (eb_is_empty(pkts)) |
| 1633 | goto out; |
| 1634 | |
| 1635 | ql = &qc->path->loss; |
| 1636 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
| 1637 | loss_delay += loss_delay >> 3; |
| 1638 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1639 | |
| 1640 | node = eb64_first(pkts); |
| 1641 | while (node) { |
| 1642 | struct quic_tx_packet *pkt; |
| 1643 | int64_t largest_acked_pn; |
| 1644 | unsigned int loss_time_limit, time_sent; |
| 1645 | |
| 1646 | 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] | 1647 | 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] | 1648 | node = eb64_next(node); |
| 1649 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1650 | break; |
| 1651 | |
| 1652 | time_sent = pkt->time_sent; |
| 1653 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1654 | if (tick_is_le(time_sent, now_ms) || |
| 1655 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1656 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1657 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1658 | } |
| 1659 | else { |
| 1660 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1665 | 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] | 1666 | } |
| 1667 | |
| 1668 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1669 | * 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] | 1670 | * 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] | 1671 | * acked ack-eliciting packet. |
| 1672 | * Return 1, if succeeded, 0 if not. |
| 1673 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1674 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1675 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1676 | struct quic_enc_level *qel, |
| 1677 | unsigned int *rtt_sample, |
| 1678 | const unsigned char **pos, const unsigned char *end) |
| 1679 | { |
| 1680 | struct quic_ack *ack = &frm->ack; |
| 1681 | uint64_t smallest, largest; |
| 1682 | struct eb_root *pkts; |
| 1683 | struct eb64_node *largest_node; |
| 1684 | unsigned int time_sent, pkt_flags; |
| 1685 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1686 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1687 | |
| 1688 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1689 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1690 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1691 | goto err; |
| 1692 | } |
| 1693 | |
| 1694 | if (ack->first_ack_range > ack->largest_ack) { |
| 1695 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1696 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1697 | goto err; |
| 1698 | } |
| 1699 | |
| 1700 | largest = ack->largest_ack; |
| 1701 | smallest = largest - ack->first_ack_range; |
| 1702 | pkts = &qel->pktns->tx.pkts; |
| 1703 | pkt_flags = 0; |
| 1704 | largest_node = NULL; |
| 1705 | time_sent = 0; |
| 1706 | |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1707 | 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] | 1708 | largest_node = eb64_lookup(pkts, largest); |
| 1709 | if (!largest_node) { |
| 1710 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1711 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1712 | } |
| 1713 | else { |
| 1714 | time_sent = eb64_entry(&largest_node->node, |
| 1715 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1716 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1720 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1721 | do { |
| 1722 | uint64_t gap, ack_range; |
| 1723 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1724 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1725 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1726 | if (!ack->ack_range_num--) |
| 1727 | break; |
| 1728 | |
| 1729 | if (!quic_dec_int(&gap, pos, end)) |
| 1730 | goto err; |
| 1731 | |
| 1732 | if (smallest < gap + 2) { |
| 1733 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1734 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1735 | goto err; |
| 1736 | } |
| 1737 | |
| 1738 | largest = smallest - gap - 2; |
| 1739 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1740 | goto err; |
| 1741 | |
| 1742 | if (largest < ack_range) { |
| 1743 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1744 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1745 | goto err; |
| 1746 | } |
| 1747 | |
| 1748 | /* Do not use this node anymore. */ |
| 1749 | largest_node = NULL; |
| 1750 | /* Next range */ |
| 1751 | smallest = largest - ack_range; |
| 1752 | |
| 1753 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1754 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1755 | } while (1); |
| 1756 | |
| 1757 | /* 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] | 1758 | 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] | 1759 | |
| 1760 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1761 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1762 | 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] | 1763 | } |
| 1764 | |
| 1765 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1766 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1767 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1768 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1769 | 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] | 1770 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1771 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 1772 | if (quic_peer_validated_addr(qc)) |
| 1773 | qc->path->loss.pto_count = 0; |
| 1774 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | |
| 1778 | return 1; |
| 1779 | |
| 1780 | err: |
| 1781 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1782 | 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] | 1783 | return 0; |
| 1784 | } |
| 1785 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 1786 | /* This function gives the detail of the SSL error. It is used only |
| 1787 | * if the debug mode and the verbose mode are activated. It dump all |
| 1788 | * the SSL error until the stack was empty. |
| 1789 | */ |
| 1790 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 1791 | { |
| 1792 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 1793 | while (1) { |
| 1794 | unsigned long ret; |
| 1795 | |
| 1796 | ret = ERR_get_error(); |
| 1797 | if (!ret) |
| 1798 | return; |
| 1799 | |
| 1800 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 1801 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 1802 | } |
| 1803 | } |
| 1804 | } |
| 1805 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1806 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 1807 | const char **str, int *len); |
| 1808 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1809 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 1810 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1811 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1812 | * Return 1 if succeeded, 0 if not. |
| 1813 | */ |
| 1814 | 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] | 1815 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1816 | const unsigned char *data, size_t len, |
| 1817 | struct quic_rx_packet *pkt, |
| 1818 | struct quic_rx_crypto_frm *cf) |
| 1819 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1820 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 1821 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1822 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1823 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 1824 | qc = ctx->qc; |
| 1825 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1826 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 1827 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1828 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 1829 | TRACE_PROTO("SSL_provide_quic_data() error", |
| 1830 | QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl); |
| 1831 | goto err; |
| 1832 | } |
| 1833 | |
| 1834 | el->rx.crypto.offset += len; |
| 1835 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 1836 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1837 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1838 | state = HA_ATOMIC_LOAD(&qc->state); |
| 1839 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1840 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 1841 | if (ssl_err != 1) { |
| 1842 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1843 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1844 | TRACE_PROTO("SSL handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1845 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1846 | goto out; |
| 1847 | } |
| 1848 | |
| 1849 | TRACE_DEVEL("SSL handshake error", |
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 | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 1851 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 1852 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1853 | goto err; |
| 1854 | } |
| 1855 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1856 | 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] | 1857 | if (objt_listener(ctx->conn->target)) |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1858 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1859 | else |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1860 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1861 | } else { |
| 1862 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 1863 | if (ssl_err != 1) { |
| 1864 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1865 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1866 | TRACE_DEVEL("SSL post handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1867 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1868 | goto out; |
| 1869 | } |
| 1870 | |
| 1871 | TRACE_DEVEL("SSL post handshake error", |
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 err; |
| 1874 | } |
| 1875 | |
| 1876 | TRACE_PROTO("SSL post handshake succeeded", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1877 | QUIC_EV_CONN_HDSHK, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1878 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 1879 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1880 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1881 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1882 | return 1; |
| 1883 | |
| 1884 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1885 | 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] | 1886 | return 0; |
| 1887 | } |
| 1888 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1889 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 1890 | * <pkt> RX packet. |
| 1891 | * Return it if succeeded, NULL if not. |
| 1892 | */ |
| 1893 | static inline |
| 1894 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 1895 | struct quic_rx_packet *pkt) |
| 1896 | { |
| 1897 | struct quic_rx_strm_frm *frm; |
| 1898 | |
| 1899 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 1900 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1901 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1902 | frm->len = stream_frm->len; |
| 1903 | frm->data = stream_frm->data; |
| 1904 | frm->pkt = pkt; |
| 1905 | } |
| 1906 | |
| 1907 | return frm; |
| 1908 | } |
| 1909 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1910 | /* 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] | 1911 | * 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] | 1912 | */ |
| 1913 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 1914 | { |
| 1915 | size_t ret; |
| 1916 | |
| 1917 | ret = 0; |
| 1918 | while (strm_frm->len) { |
| 1919 | size_t try; |
| 1920 | |
| 1921 | try = b_contig_space(buf); |
| 1922 | if (!try) |
| 1923 | break; |
| 1924 | |
| 1925 | if (try > strm_frm->len) |
| 1926 | try = strm_frm->len; |
| 1927 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1928 | strm_frm->len -= try; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1929 | strm_frm->offset.key += try; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1930 | b_add(buf, try); |
| 1931 | ret += try; |
| 1932 | } |
| 1933 | |
| 1934 | return ret; |
| 1935 | } |
| 1936 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 1937 | /* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer. |
| 1938 | * Also update <strm_frm> frame to reflect the data which have been consumed. |
| 1939 | */ |
| 1940 | static size_t qc_rx_strm_frm_cpy(struct buffer *buf, |
| 1941 | struct quic_rx_strm_frm *strm_frm) |
| 1942 | { |
| 1943 | size_t ret; |
| 1944 | |
| 1945 | ret = 0; |
| 1946 | while (strm_frm->len) { |
| 1947 | size_t try; |
| 1948 | |
| 1949 | try = b_contig_space(buf); |
| 1950 | if (!try) |
| 1951 | break; |
| 1952 | |
| 1953 | if (try > strm_frm->len) |
| 1954 | try = strm_frm->len; |
| 1955 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1956 | strm_frm->len -= try; |
| 1957 | strm_frm->offset_node.key += try; |
| 1958 | b_add(buf, try); |
| 1959 | ret += try; |
| 1960 | } |
| 1961 | |
| 1962 | return ret; |
| 1963 | } |
| 1964 | |
| 1965 | /* Process as much as possible RX STREAM frames received for <qcs> */ |
| 1966 | static size_t qc_treat_rx_strm_frms(struct qcs *qcs) |
| 1967 | { |
| 1968 | int total; |
| 1969 | struct eb64_node *frm_node; |
| 1970 | |
| 1971 | total = 0; |
| 1972 | frm_node = eb64_first(&qcs->rx.frms); |
| 1973 | while (frm_node) { |
| 1974 | int ret; |
| 1975 | struct quic_rx_strm_frm *frm; |
| 1976 | |
| 1977 | frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node); |
| 1978 | if (frm->offset_node.key != qcs->rx.offset) |
| 1979 | break; |
| 1980 | |
| 1981 | ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm); |
| 1982 | qcs->rx.offset += ret; |
| 1983 | total += ret; |
| 1984 | if (frm->len) { |
| 1985 | /* If there is remaining data in this frame |
| 1986 | * this is because the destination buffer is full. |
| 1987 | */ |
| 1988 | break; |
| 1989 | } |
| 1990 | |
| 1991 | frm_node = eb64_next(frm_node); |
| 1992 | quic_rx_packet_refdec(frm->pkt); |
| 1993 | eb64_delete(&frm->offset_node); |
| 1994 | pool_free(pool_head_quic_rx_strm_frm, frm); |
| 1995 | } |
| 1996 | |
| 1997 | return total; |
| 1998 | } |
| 1999 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2000 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2001 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2002 | * If not, the STREAM frame is stored to be treated again later. |
| 2003 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2004 | * Return 1 if succeeded, 0 if not. |
| 2005 | */ |
| 2006 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2007 | struct quic_stream *strm_frm, |
| 2008 | struct quic_conn *qc) |
| 2009 | { |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2010 | int total; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2011 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2012 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2013 | struct quic_rx_strm_frm *frm; |
| 2014 | |
| 2015 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2016 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2017 | 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] | 2018 | return 0; |
| 2019 | } |
| 2020 | |
| 2021 | 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] | 2022 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2023 | size_t diff; |
| 2024 | |
| 2025 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2026 | TRACE_PROTO("Already received STREAM data", |
| 2027 | QUIC_EV_CONN_PSTRM, qc); |
| 2028 | goto out; |
| 2029 | } |
| 2030 | |
| 2031 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2032 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2033 | strm_frm->offset.key = strm->rx.offset; |
| 2034 | strm_frm->len -= diff; |
| 2035 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2036 | } |
| 2037 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2038 | total = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2039 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2040 | int ret; |
| 2041 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2042 | if (!qc_get_buf(strm, &strm->rx.buf)) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2043 | goto store_frm; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2044 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2045 | |
| 2046 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2047 | total += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2048 | strm->rx.offset += ret; |
| 2049 | } |
| 2050 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2051 | total += qc_treat_rx_strm_frms(strm); |
| 2052 | if (total && qc->qcc->app_ops->decode_qcs(strm, strm_frm->fin, qc->qcc->ctx) < 0) { |
| 2053 | TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM); |
| 2054 | return 0; |
| 2055 | } |
| 2056 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2057 | if (!strm_frm->len) |
| 2058 | goto out; |
| 2059 | |
| 2060 | store_frm: |
| 2061 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2062 | if (!frm) { |
| 2063 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2064 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2065 | return 0; |
| 2066 | } |
| 2067 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2068 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2069 | quic_rx_packet_refinc(pkt); |
| 2070 | |
| 2071 | out: |
| 2072 | return 1; |
| 2073 | } |
| 2074 | |
| 2075 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2076 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2077 | * If not, the STREAM frame is stored to be treated again later. |
| 2078 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2079 | * Return 1 if succeeded, 0 if not. |
| 2080 | */ |
| 2081 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2082 | struct quic_stream *strm_frm, |
| 2083 | struct quic_conn *qc) |
| 2084 | { |
| 2085 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2086 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2087 | struct quic_rx_strm_frm *frm; |
| 2088 | size_t strm_frm_len; |
| 2089 | |
| 2090 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2091 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2092 | 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] | 2093 | return 0; |
| 2094 | } |
| 2095 | |
| 2096 | 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] | 2097 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2098 | size_t diff; |
| 2099 | |
| 2100 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2101 | TRACE_PROTO("Already received STREAM data", |
| 2102 | QUIC_EV_CONN_PSTRM, qc); |
| 2103 | goto out; |
| 2104 | } |
| 2105 | |
| 2106 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2107 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2108 | strm_frm->offset.key = strm->rx.offset; |
| 2109 | strm_frm->len -= diff; |
| 2110 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2114 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2115 | int ret; |
| 2116 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 2117 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2118 | goto store_frm; |
| 2119 | |
| 2120 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 2121 | * of bytes copied. |
| 2122 | */ |
| 2123 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 2124 | /* Inform the application of the arrival of this new stream */ |
| 2125 | 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] | 2126 | 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] | 2127 | return 0; |
| 2128 | } |
| 2129 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2130 | if (ret) |
| 2131 | qcs_notify_recv(strm); |
| 2132 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2133 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2134 | } |
| 2135 | /* Take this frame into an account for the stream flow control */ |
| 2136 | strm->rx.offset += strm_frm_len; |
| 2137 | /* 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] | 2138 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2139 | */ |
| 2140 | if (!strm_frm->len) |
| 2141 | goto out; |
| 2142 | |
| 2143 | store_frm: |
| 2144 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2145 | if (!frm) { |
| 2146 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2147 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2148 | return 0; |
| 2149 | } |
| 2150 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2151 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2152 | quic_rx_packet_refinc(pkt); |
| 2153 | |
| 2154 | out: |
| 2155 | return 1; |
| 2156 | } |
| 2157 | |
| 2158 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2159 | struct quic_stream *strm_frm, |
| 2160 | struct quic_conn *qc) |
| 2161 | { |
| 2162 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2163 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2164 | else |
| 2165 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2166 | } |
| 2167 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2168 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2169 | * as I/O handler context and <qel> as encryption level. |
| 2170 | * Returns 1 if succeeded, 0 if failed. |
| 2171 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2172 | 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] | 2173 | struct quic_enc_level *qel) |
| 2174 | { |
| 2175 | struct quic_frame frm; |
| 2176 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2177 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2178 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2179 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2180 | /* Skip the AAD */ |
| 2181 | pos = pkt->data + pkt->aad_len; |
| 2182 | end = pkt->data + pkt->len; |
| 2183 | |
| 2184 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2185 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2186 | goto err; |
| 2187 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2188 | 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] | 2189 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2190 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2191 | break; |
| 2192 | case QUIC_FT_PING: |
| 2193 | break; |
| 2194 | case QUIC_FT_ACK: |
| 2195 | { |
| 2196 | unsigned int rtt_sample; |
| 2197 | |
| 2198 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2199 | 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] | 2200 | goto err; |
| 2201 | |
| 2202 | if (rtt_sample) { |
| 2203 | unsigned int ack_delay; |
| 2204 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2205 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
| 2206 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)); |
| 2207 | 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] | 2208 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2209 | break; |
| 2210 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2211 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2212 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2213 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2214 | { |
| 2215 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2216 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2217 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2218 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2219 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2220 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2221 | |
| 2222 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2223 | cfdebug.len = frm.crypto.len; |
| 2224 | /* Nothing to do */ |
| 2225 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2226 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2227 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2228 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2229 | else { |
| 2230 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2231 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2232 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2233 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2234 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2235 | cfdebug.len = frm.crypto.len; |
| 2236 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2237 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2238 | frm.crypto.len -= diff; |
| 2239 | frm.crypto.data += diff; |
| 2240 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2241 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2242 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2243 | |
| 2244 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2245 | /* 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] | 2246 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2247 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2248 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2249 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2250 | if (!qc_provide_cdata(qel, ctx, |
| 2251 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2252 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2253 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2254 | |
| 2255 | break; |
| 2256 | } |
| 2257 | |
| 2258 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2259 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2260 | if (!cf) { |
| 2261 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2262 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2263 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2264 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2265 | |
| 2266 | cf->offset_node.key = frm.crypto.offset; |
| 2267 | cf->len = frm.crypto.len; |
| 2268 | cf->data = frm.crypto.data; |
| 2269 | cf->pkt = pkt; |
| 2270 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2271 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2272 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2273 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2274 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2275 | { |
| 2276 | struct quic_stream *stream = &frm.stream; |
| 2277 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2278 | if (objt_listener(ctx->conn->target)) { |
| 2279 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2280 | goto err; |
| 2281 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2282 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2283 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2284 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2285 | goto err; |
| 2286 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2287 | break; |
| 2288 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2289 | case QUIC_FT_MAX_DATA: |
| 2290 | case QUIC_FT_MAX_STREAM_DATA: |
| 2291 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2292 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2293 | case QUIC_FT_DATA_BLOCKED: |
| 2294 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2295 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2296 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2297 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2298 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2299 | break; |
| 2300 | case QUIC_FT_CONNECTION_CLOSE: |
| 2301 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Amaury Denoyelle | 5154e7a | 2021-12-08 14:51:04 +0100 | [diff] [blame] | 2302 | /* warn the mux to close the connection */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2303 | qc->qcc->flags |= QC_CF_CC_RECV; |
| 2304 | tasklet_wakeup(qc->qcc->wait_event.tasklet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2305 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2306 | case QUIC_FT_HANDSHAKE_DONE: |
| 2307 | if (objt_listener(ctx->conn->target)) |
| 2308 | goto err; |
| 2309 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2310 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2311 | break; |
| 2312 | default: |
| 2313 | goto err; |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2318 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2319 | * be discarded. |
| 2320 | */ |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2321 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2322 | int state = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2323 | |
| 2324 | if (state >= QUIC_HS_ST_SERVER_INITIAL) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2325 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2326 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2327 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2328 | qc_set_timer(ctx->qc); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2329 | if (state < QUIC_HS_ST_SERVER_HANDSHAKE) |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2330 | 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] | 2331 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2332 | } |
| 2333 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2334 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2335 | return 1; |
| 2336 | |
| 2337 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2338 | 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] | 2339 | return 0; |
| 2340 | } |
| 2341 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2342 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2343 | * 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] | 2344 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2345 | * This function must be called only after having built a correct datagram. |
| 2346 | * Always succeeds. |
| 2347 | */ |
| 2348 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2349 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2350 | { |
| 2351 | write_u16(cb_wr(cbuf), dglen); |
| 2352 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2353 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2354 | } |
| 2355 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2356 | /* 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] | 2357 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2358 | * several packets in the same datagram. A header made of two fields is added |
| 2359 | * to each datagram: the datagram length followed by the address of the first |
| 2360 | * packet in this datagram. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2361 | * Returns 1 if succeeded, or 0 if something wrong happened. |
| 2362 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2363 | 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] | 2364 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2365 | enum quic_tls_enc_level tel, next_tel; |
| 2366 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2367 | struct cbuf *cbuf; |
| 2368 | unsigned char *end_buf, *end, *pos, *spos; |
| 2369 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2370 | /* length of datagrams */ |
| 2371 | uint16_t dglen; |
| 2372 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2373 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2374 | /* Each datagram is prepended with its length followed by the |
| 2375 | * address of the first packet in the datagram. |
| 2376 | */ |
| 2377 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2378 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2379 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2380 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 2381 | 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] | 2382 | 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] | 2383 | goto err; |
| 2384 | } |
| 2385 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2386 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2387 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2388 | total = 0; |
| 2389 | padding = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2390 | qel = &qc->els[tel]; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2391 | cbuf = qr->cbuf; |
| 2392 | spos = pos = cb_wr(cbuf); |
| 2393 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2394 | * to ensure there is enough room to mark the end of prepared |
| 2395 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2396 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2397 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2398 | first_pkt = prv_pkt = NULL; |
| 2399 | while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2400 | int err, nb_ptos, ack, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2401 | enum quic_pkt_type pkt_type; |
| 2402 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2403 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 2404 | nb_ptos = ack = 0; |
| 2405 | cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 2406 | if (!cc) { |
| 2407 | if (!prv_pkt) { |
| 2408 | /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */ |
| 2409 | do { |
| 2410 | nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams); |
| 2411 | } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1)); |
| 2412 | } |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2413 | 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] | 2414 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2415 | /* 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] | 2416 | * 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] | 2417 | * 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] | 2418 | * and if there is no more CRYPTO data available or in flight |
| 2419 | * congestion control limit is reached for prepared data |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2420 | */ |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2421 | if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) || |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2422 | (!cc && !ack && !nb_ptos && |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2423 | (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) || |
| 2424 | qc->path->prep_in_flight >= qc->path->cwnd))) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2425 | 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] | 2426 | /* Set the current datagram as prepared into <cbuf> if |
| 2427 | * the was already a correct packet which was previously written. |
| 2428 | */ |
| 2429 | if (prv_pkt) |
| 2430 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2431 | break; |
| 2432 | } |
| 2433 | |
| 2434 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2435 | if (!prv_pkt) { |
| 2436 | /* Leave room for the datagram header */ |
| 2437 | pos += dg_headlen; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2438 | 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] | 2439 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes) |
| 2440 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 2441 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2442 | } |
| 2443 | else { |
| 2444 | end = pos + qc->path->mtu; |
| 2445 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2446 | } |
| 2447 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2448 | cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding, |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2449 | pkt_type, ack, nb_ptos, cc, &err); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2450 | /* 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] | 2451 | if (err < 0) { |
| 2452 | if (!prv_pkt && nb_ptos) |
| 2453 | HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2454 | if (ack) |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2455 | 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] | 2456 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2457 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2458 | case -2: |
| 2459 | goto err; |
| 2460 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2461 | /* If there was already a correct packet present, set the |
| 2462 | * current datagram as prepared into <cbuf>. |
| 2463 | */ |
| 2464 | if (prv_pkt) { |
| 2465 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2466 | goto stop_build; |
| 2467 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2468 | goto out; |
| 2469 | default: |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2470 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2471 | if (!cur_pkt) |
| 2472 | goto err; |
| 2473 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2474 | total += cur_pkt->len; |
| 2475 | /* keep trace of the first packet in the datagram */ |
| 2476 | if (!first_pkt) |
| 2477 | first_pkt = cur_pkt; |
| 2478 | /* Attach the current one to the previous one */ |
| 2479 | if (prv_pkt) |
| 2480 | prv_pkt->next = cur_pkt; |
| 2481 | /* Let's say we have to build a new dgram */ |
| 2482 | prv_pkt = NULL; |
| 2483 | dglen += cur_pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2484 | /* Discard the Initial encryption keys as soon as |
| 2485 | * a handshake packet could be built. |
| 2486 | */ |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2487 | if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2488 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2489 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2490 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2491 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2492 | qc_set_timer(qc); |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2493 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2494 | } |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2495 | /* If the data for the current encryption level have all been sent, |
| 2496 | * select the next level. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2497 | */ |
Frédéric Lécaille | d067088 | 2021-08-19 07:53:27 +0200 | [diff] [blame] | 2498 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 2499 | (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) || |
| 2500 | (next_tel != QUIC_TLS_ENC_LEVEL_NONE && qc->els[next_tel].pktns->tx.in_flight))) { |
Frédéric Lécaille | 4bade77 | 2021-08-23 08:54:28 +0200 | [diff] [blame] | 2501 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2502 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2503 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2504 | tel = next_tel; |
| 2505 | qel = &qc->els[tel]; |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 2506 | if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2507 | /* If there is data for the next level, do not |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2508 | * consume a datagram. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2509 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2510 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2511 | } |
| 2512 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | /* If we have to build a new datagram, set the current datagram as |
| 2516 | * prepared into <cbuf>. |
| 2517 | */ |
| 2518 | if (!prv_pkt) { |
| 2519 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2520 | first_pkt = NULL; |
| 2521 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2522 | padding = 0; |
| 2523 | } |
| 2524 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
| 2525 | (objt_server(qc->conn->target) || |
| 2526 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2527 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | stop_build: |
| 2532 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2533 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
| 2534 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2535 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2536 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2537 | if (cb_contig_space(cbuf) >= sizeof(uint16_t)) { |
| 2538 | if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) { |
| 2539 | /* Mark the end of contiguous data for the reader */ |
| 2540 | write_u16(cb_wr(cbuf), 0); |
| 2541 | cb_add(cbuf, sizeof(uint16_t)); |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | if (rd && rd <= cbuf->wr) { |
| 2546 | cb_wr_reset(cbuf); |
| 2547 | if (pos == spos) { |
| 2548 | /* Reuse the same buffer if nothing was built. */ |
| 2549 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2550 | } |
| 2551 | } |
| 2552 | } |
| 2553 | |
| 2554 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2555 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2556 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2557 | |
| 2558 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2559 | 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] | 2560 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | /* 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] | 2564 | * 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] | 2565 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2566 | 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] | 2567 | { |
| 2568 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2569 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2570 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2571 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2572 | cbuf = qr->cbuf; |
| 2573 | while (cb_contig_data(cbuf)) { |
| 2574 | unsigned char *pos; |
| 2575 | struct buffer tmpbuf = { }; |
| 2576 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 2577 | uint16_t dglen; |
| 2578 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2579 | unsigned int time_sent; |
| 2580 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2581 | pos = cb_rd(cbuf); |
| 2582 | dglen = read_u16(pos); |
| 2583 | /* End of prepared datagrams. |
| 2584 | * Reset the reader index only if in front of the writer index. |
| 2585 | */ |
| 2586 | if (!dglen) { |
| 2587 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 2588 | |
| 2589 | if (wr && wr < cbuf->rd) { |
| 2590 | cb_rd_reset(cbuf); |
| 2591 | continue; |
| 2592 | } |
| 2593 | break; |
| 2594 | } |
| 2595 | |
| 2596 | pos += sizeof dglen; |
| 2597 | first_pkt = read_ptr(pos); |
| 2598 | pos += sizeof first_pkt; |
| 2599 | tmpbuf.area = (char *)pos; |
| 2600 | tmpbuf.size = tmpbuf.data = dglen; |
| 2601 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2602 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2603 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2604 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2605 | 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] | 2606 | &tmpbuf, tmpbuf.data, 0) <= 0) { |
| 2607 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2608 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2609 | break; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2610 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2611 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2612 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2613 | qc->tx.bytes += tmpbuf.data; |
| 2614 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2615 | |
| 2616 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 2617 | pkt->time_sent = time_sent; |
| 2618 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 2619 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 2620 | qc->path->ifae_pkts++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2621 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2622 | qc->path->in_flight += pkt->in_flight_len; |
| 2623 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 2624 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2625 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2626 | 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] | 2627 | next_pkt = pkt->next; |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 2628 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2629 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2630 | } |
| 2631 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2632 | |
| 2633 | return 1; |
| 2634 | } |
| 2635 | |
| 2636 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 2637 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 2638 | * a HANDSHAKE_DONE frame. |
| 2639 | * Return 1 if succeeded, 0 if not. |
| 2640 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2641 | 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] | 2642 | { |
| 2643 | int i; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2644 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2645 | struct quic_frame *frm; |
| 2646 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2647 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2648 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2649 | if (!objt_server(qc->conn->target)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2650 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 2651 | if (!frm) |
| 2652 | return 0; |
| 2653 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2654 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2655 | 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] | 2656 | } |
| 2657 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2658 | 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] | 2659 | struct quic_connection_id *cid; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2660 | struct listener *l = __objt_listener(qc->conn->target); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2661 | |
| 2662 | frm = pool_alloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2663 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2664 | goto err; |
| 2665 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2666 | cid = new_quic_cid(&qc->cids, qc, i); |
| 2667 | if (!cid) |
| 2668 | goto err; |
| 2669 | |
| 2670 | /* insert the allocated CID in the receiver tree */ |
| 2671 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2672 | ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len); |
| 2673 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2674 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2675 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2676 | 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] | 2677 | } |
| 2678 | |
| 2679 | return 1; |
| 2680 | |
| 2681 | err: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2682 | return 0; |
| 2683 | } |
| 2684 | |
| 2685 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2686 | void free_quic_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2687 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2688 | struct eb64_node *n; |
| 2689 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2690 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2691 | n = eb64_first(&arngs->root); |
| 2692 | while (n) { |
| 2693 | struct eb64_node *next; |
| 2694 | |
| 2695 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 2696 | next = eb64_next(n); |
| 2697 | eb64_delete(n); |
| 2698 | free(ar); |
| 2699 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2700 | } |
| 2701 | } |
| 2702 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2703 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 2704 | * descending order. |
| 2705 | */ |
| 2706 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 2707 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2708 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2709 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | |
| 2713 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 2714 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2715 | * 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] | 2716 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2717 | 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] | 2718 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2719 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2720 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2721 | last = eb64_last(&arngs->root); |
| 2722 | while (last && arngs->enc_sz > limit) { |
| 2723 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2724 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2725 | prev = eb64_prev(last); |
| 2726 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2727 | return 0; |
| 2728 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2729 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 2730 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 2731 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 2732 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 2733 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 2734 | --arngs->sz; |
| 2735 | eb64_delete(last); |
| 2736 | pool_free(pool_head_quic_arng, last); |
| 2737 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | return 1; |
| 2741 | } |
| 2742 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2743 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 2744 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 2745 | { |
| 2746 | struct eb64_node *node, *next; |
| 2747 | struct quic_arng_node *ar, *ar_next; |
| 2748 | |
| 2749 | node = eb64_last(&arngs->root); |
| 2750 | if (!node) |
| 2751 | return; |
| 2752 | |
| 2753 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2754 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 2755 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 2756 | |
| 2757 | while ((next = eb64_prev(node))) { |
| 2758 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 2759 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 2760 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 2761 | node = next; |
| 2762 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2763 | } |
| 2764 | } |
| 2765 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2766 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 2767 | * 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] | 2768 | */ |
| 2769 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2770 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 2771 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2772 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2773 | struct quic_arng_node *new_ar; |
| 2774 | |
| 2775 | new_ar = pool_alloc(pool_head_quic_arng); |
| 2776 | if (new_ar) { |
| 2777 | new_ar->first.key = ar->first; |
| 2778 | new_ar->last = ar->last; |
| 2779 | eb64_insert(&arngs->root, &new_ar->first); |
| 2780 | arngs->sz++; |
| 2781 | } |
| 2782 | |
| 2783 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | /* 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] | 2787 | * 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] | 2788 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2789 | * |
| 2790 | * Descending order |
| 2791 | * -------------> |
| 2792 | * range1 range2 |
| 2793 | * ..........|--------|..............|--------| |
| 2794 | * ^ ^ ^ ^ |
| 2795 | * | | | | |
| 2796 | * last1 first1 last2 first2 |
| 2797 | * ..........+--------+--------------+--------+...... |
| 2798 | * diff1 gap12 diff2 |
| 2799 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2800 | * To encode the previous list of ranges we must encode integers as follows in |
| 2801 | * descending order: |
| 2802 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2803 | * with diff1 = last1 - first1 |
| 2804 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2805 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2806 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2807 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2808 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 2809 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2810 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2811 | struct eb64_node *le; |
| 2812 | struct quic_arng_node *new_node; |
| 2813 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2814 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2815 | new = NULL; |
| 2816 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2817 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2818 | if (!new_node) |
| 2819 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2820 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2821 | goto out; |
| 2822 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2823 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2824 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 2825 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2826 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2827 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2828 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 2829 | |
| 2830 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2831 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2832 | else { |
| 2833 | struct quic_arng_node *le_ar = |
| 2834 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2835 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2836 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 2837 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2838 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2839 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2840 | if (le_ar->last + 1 >= ar->first) { |
| 2841 | le_ar->last = ar->last; |
| 2842 | new = le; |
| 2843 | new_node = le_ar; |
| 2844 | } |
| 2845 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2846 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2847 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2848 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 2849 | |
| 2850 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2851 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2852 | } |
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 | /* Verify that the new inserted node does not overlap the nodes |
| 2855 | * which follow it. |
| 2856 | */ |
| 2857 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2858 | struct eb64_node *next; |
| 2859 | struct quic_arng_node *next_node; |
| 2860 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2861 | while ((next = eb64_next(new))) { |
| 2862 | next_node = |
| 2863 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 2864 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2865 | break; |
| 2866 | |
| 2867 | if (next_node->last > new_node->last) |
| 2868 | new_node->last = next_node->last; |
| 2869 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 2870 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2871 | /* Decrement the size of these ranges. */ |
| 2872 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2873 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2874 | } |
| 2875 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 2876 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2877 | quic_arngs_set_enc_sz(arngs); |
| 2878 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2879 | return 1; |
| 2880 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2881 | /* Remove the header protection of packets at <el> encryption level. |
| 2882 | * Always succeeds. |
| 2883 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2884 | 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] | 2885 | { |
| 2886 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2887 | struct quic_rx_packet *pqpkt; |
| 2888 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2889 | struct quic_enc_level *app_qel; |
| 2890 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2891 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 2892 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2893 | /* 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] | 2894 | if (el == app_qel && objt_listener(qc->conn->target) && |
| 2895 | HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2896 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2897 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2898 | goto out; |
| 2899 | } |
| 2900 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2901 | 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] | 2902 | 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] | 2903 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2904 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 2905 | 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] | 2906 | /* XXX TO DO XXX */ |
| 2907 | } |
| 2908 | else { |
| 2909 | /* The AAD includes the packet number field */ |
| 2910 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 2911 | /* Store the packet into the tree of packets to decrypt. */ |
| 2912 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 2913 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 2914 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 2915 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 2916 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2917 | 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] | 2918 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2919 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 2920 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2924 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | /* Process all the CRYPTO frame at <el> encryption level. |
| 2928 | * Return 1 if succeeded, 0 if not. |
| 2929 | */ |
| 2930 | 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] | 2931 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2932 | { |
| 2933 | struct eb64_node *node; |
| 2934 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2935 | node = eb64_first(&el->rx.crypto.frms); |
| 2936 | while (node) { |
| 2937 | struct quic_rx_crypto_frm *cf; |
| 2938 | |
| 2939 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 2940 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 2941 | break; |
| 2942 | |
| 2943 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 2944 | goto err; |
| 2945 | |
| 2946 | node = eb64_next(node); |
| 2947 | quic_rx_packet_refdec(cf->pkt); |
| 2948 | eb64_delete(&cf->offset_node); |
| 2949 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 2950 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2951 | return 1; |
| 2952 | |
| 2953 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2954 | 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] | 2955 | return 0; |
| 2956 | } |
| 2957 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 2958 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2959 | * 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] | 2960 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2961 | * Return 1 if succeeded, 0 if not. |
| 2962 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2963 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 2964 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2965 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2966 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 2967 | int64_t largest_pn = -1; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2968 | struct quic_conn *qc = ctx->conn->qc; |
| 2969 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2970 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2971 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2972 | qel = cur_el; |
| 2973 | next_tel: |
| 2974 | if (!qel) |
| 2975 | goto out; |
| 2976 | |
| 2977 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 2978 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2979 | while (node) { |
| 2980 | struct quic_rx_packet *pkt; |
| 2981 | |
| 2982 | 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] | 2983 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2984 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 2985 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2986 | /* Drop the packet */ |
| 2987 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2988 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2989 | } |
| 2990 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2991 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2992 | /* Drop the packet */ |
| 2993 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2994 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2995 | } |
| 2996 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2997 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 2998 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 2999 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING && |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3000 | (!(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] | 3001 | 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] | 3002 | if (pkt->pn > largest_pn) |
| 3003 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3004 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3005 | 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] | 3006 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3007 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3008 | } |
| 3009 | } |
| 3010 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3011 | eb64_delete(&pkt->pn_node); |
| 3012 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3013 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3014 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3015 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3016 | /* Update the largest packet number. */ |
| 3017 | if (largest_pn != -1) |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3018 | HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn); |
| 3019 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3020 | goto err; |
| 3021 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3022 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3023 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3024 | qel = next_el; |
| 3025 | goto next_tel; |
| 3026 | } |
| 3027 | |
| 3028 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3029 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3030 | return 1; |
| 3031 | |
| 3032 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3033 | 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] | 3034 | return 0; |
| 3035 | } |
| 3036 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3037 | /* QUIC connection packet handler task. */ |
| 3038 | 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] | 3039 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3040 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3041 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3042 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3043 | enum quic_tls_enc_level tel, next_tel; |
| 3044 | struct quic_enc_level *qel, *next_qel; |
| 3045 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3046 | struct qring *qr; // Tx ring |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3047 | int prev_st, st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3048 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3049 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3050 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3051 | qr = NULL; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3052 | st = HA_ATOMIC_LOAD(&qc->state); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3053 | TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3054 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3055 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3056 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3057 | 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] | 3058 | start: |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3059 | 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] | 3060 | goto err; |
| 3061 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3062 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3063 | 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] | 3064 | |
| 3065 | next_level: |
| 3066 | tls_ctx = &qel->tls_ctx; |
| 3067 | |
| 3068 | /* If the header protection key for this level has been derived, |
| 3069 | * remove the packet header protections. |
| 3070 | */ |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3071 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3072 | (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3073 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3074 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3075 | prev_st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3076 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3077 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3078 | 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] | 3079 | goto err; |
| 3080 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3081 | if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) && |
| 3082 | (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3083 | qel = next_qel; |
| 3084 | next_qel = NULL; |
| 3085 | goto next_level; |
| 3086 | } |
| 3087 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3088 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 754f99e | 2021-08-19 15:35:59 +0200 | [diff] [blame] | 3089 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3090 | (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] | 3091 | /* Discard the Handshake keys. */ |
| 3092 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3093 | 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] | 3094 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3095 | qc_set_timer(qc); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3096 | if (!quic_build_post_handshake_frames(qc)) |
| 3097 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3098 | |
| 3099 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3100 | 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] | 3101 | goto start; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3102 | } |
| 3103 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3104 | if (!qr) |
| 3105 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3106 | ret = qc_prep_pkts(qc, qr); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3107 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3108 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3109 | else if (ret == 0) |
| 3110 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3111 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3112 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3113 | goto err; |
| 3114 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3115 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3116 | /* Check if there is something to do for the next level. |
| 3117 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3118 | if (next_qel && next_qel != qel && |
| 3119 | (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] | 3120 | (!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] | 3121 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3122 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3123 | goto next_level; |
| 3124 | } |
| 3125 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3126 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3127 | TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3128 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3129 | |
| 3130 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3131 | if (qr) |
| 3132 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3133 | 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] | 3134 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3135 | } |
| 3136 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3137 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3138 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 3139 | { |
| 3140 | int i; |
| 3141 | |
| 3142 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 3143 | if (qel->tx.crypto.bufs[i]) { |
| 3144 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 3145 | qel->tx.crypto.bufs[i] = NULL; |
| 3146 | } |
| 3147 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3148 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3152 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3153 | * Returns 1 if succeeded, 0 if not. |
| 3154 | */ |
| 3155 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 3156 | enum quic_tls_enc_level level) |
| 3157 | { |
| 3158 | struct quic_enc_level *qel; |
| 3159 | |
| 3160 | qel = &qc->els[level]; |
| 3161 | qel->level = quic_to_ssl_enc_level(level); |
| 3162 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 3163 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 3164 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 3165 | qel->tls_ctx.rx.flags = 0; |
| 3166 | qel->tls_ctx.tx.flags = 0; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 3167 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3168 | |
| 3169 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3170 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3171 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 3172 | qel->rx.crypto.offset = 0; |
| 3173 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3174 | |
| 3175 | /* Allocate only one buffer. */ |
| 3176 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 3177 | if (!qel->tx.crypto.bufs) |
| 3178 | goto err; |
| 3179 | |
| 3180 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 3181 | if (!qel->tx.crypto.bufs[0]) |
| 3182 | goto err; |
| 3183 | |
| 3184 | qel->tx.crypto.bufs[0]->sz = 0; |
| 3185 | qel->tx.crypto.nb_buf = 1; |
| 3186 | |
| 3187 | qel->tx.crypto.sz = 0; |
| 3188 | qel->tx.crypto.offset = 0; |
| 3189 | |
| 3190 | return 1; |
| 3191 | |
| 3192 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3193 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3194 | return 0; |
| 3195 | } |
| 3196 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 3197 | /* Increment the <qc> refcount. |
| 3198 | * |
| 3199 | * This operation must be conducted when manipulating the quic_conn outside of |
| 3200 | * the connection pinned thread. These threads can only retrieve the connection |
| 3201 | * in the CID tree, so this function must be conducted under the CID lock. |
| 3202 | */ |
| 3203 | static inline void quic_conn_take(struct quic_conn *qc) |
| 3204 | { |
| 3205 | HA_ATOMIC_INC(&qc->refcount); |
| 3206 | } |
| 3207 | |
| 3208 | /* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the |
| 3209 | * substraction, the quic_conn is freed. |
| 3210 | */ |
| 3211 | static void quic_conn_drop(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3212 | { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 3213 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3214 | int i; |
| 3215 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3216 | if (!qc) |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3217 | return; |
| 3218 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 3219 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
| 3220 | if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1)) { |
| 3221 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
| 3222 | return; |
| 3223 | } |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3224 | |
| 3225 | /* remove the connection from receiver cids trees */ |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3226 | ebmb_delete(&qc->odcid_node); |
| 3227 | ebmb_delete(&qc->scid_node); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 3228 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3229 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3230 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 3231 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
| 3232 | if (conn_ctx) { |
| 3233 | tasklet_free(conn_ctx->wait_event.tasklet); |
| 3234 | conn_ctx->wait_event.tasklet = NULL; |
| 3235 | |
| 3236 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
| 3237 | } |
| 3238 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3239 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3240 | quic_conn_enc_level_uninit(&qc->els[i]); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3241 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3242 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 3243 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3244 | } |
| 3245 | |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3246 | void quic_close(struct connection *conn, void *xprt_ctx) |
| 3247 | { |
| 3248 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
| 3249 | struct quic_conn *qc = conn_ctx->conn->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3250 | |
| 3251 | /* This task must be deleted by the connection-pinned thread. */ |
| 3252 | if (qc->timer_task) { |
| 3253 | task_destroy(qc->timer_task); |
| 3254 | qc->timer_task = NULL; |
| 3255 | } |
| 3256 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 3257 | quic_conn_drop(qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3258 | } |
| 3259 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3260 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3261 | 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] | 3262 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3263 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3264 | struct quic_conn *qc; |
| 3265 | struct quic_pktns *pktns; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3266 | int st; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3267 | |
| 3268 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3269 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3270 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3271 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3272 | task->expire = TICK_ETERNITY; |
| 3273 | pktns = quic_loss_pktns(qc); |
| 3274 | if (tick_isset(pktns->tx.loss_time)) { |
| 3275 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 3276 | |
| 3277 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 3278 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3279 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 3280 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3281 | goto out; |
| 3282 | } |
| 3283 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3284 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3285 | if (qc->path->in_flight) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3286 | 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] | 3287 | pktns->tx.pto_probe = 1; |
| 3288 | } |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3289 | 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] | 3290 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3291 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3292 | |
| 3293 | if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3294 | hel->pktns->tx.pto_probe = 1; |
| 3295 | if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3296 | iel->pktns->tx.pto_probe = 1; |
| 3297 | } |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 3298 | HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3299 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3300 | qc->path->loss.pto_count++; |
| 3301 | |
| 3302 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3303 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3304 | |
| 3305 | return task; |
| 3306 | } |
| 3307 | |
| 3308 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 3309 | * connections used to identify the first Initial packets of client connecting |
| 3310 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 3311 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 3312 | * <scid> is the source connection ID with <scid_len> as length. |
| 3313 | * Returns 1 if succeeded, 0 if not. |
| 3314 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3315 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3316 | 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] | 3317 | 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] | 3318 | { |
| 3319 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3320 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3321 | /* Initial CID. */ |
| 3322 | struct quic_connection_id *icid; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3323 | char *buf_area; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3324 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3325 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3326 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3327 | qc = pool_zalloc(pool_head_quic_conn); |
| 3328 | if (!qc) { |
| 3329 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 3330 | goto err; |
| 3331 | } |
| 3332 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 3333 | HA_ATOMIC_STORE(&qc->refcount, 0); |
| 3334 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3335 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 3336 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3337 | 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] | 3338 | goto err; |
| 3339 | } |
| 3340 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3341 | qc->cids = EB_ROOT; |
| 3342 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3343 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3344 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3345 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3346 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL); |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3347 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3348 | qc->odcid.len = dcid_len; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3349 | qc->odcid.addrlen = dcid_addr_len; |
| 3350 | 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] | 3351 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 3352 | /* 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] | 3353 | if (scid_len) |
| 3354 | memcpy(qc->dcid.data, scid, scid_len); |
| 3355 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 3356 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3357 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3358 | } |
| 3359 | /* QUIC Client (outgoing connection to servers) */ |
| 3360 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3361 | 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] | 3362 | if (dcid_len) |
| 3363 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 3364 | qc->dcid.len = dcid_len; |
| 3365 | } |
| 3366 | |
| 3367 | /* Initialize the output buffer */ |
| 3368 | qc->obuf.pos = qc->obuf.data; |
| 3369 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3370 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3371 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3372 | 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] | 3373 | goto err; |
| 3374 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3375 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3376 | /* insert the allocated CID in the receiver tree */ |
| 3377 | if (server) { |
| 3378 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3379 | ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len); |
| 3380 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3381 | } |
| 3382 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3383 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 3384 | qc->scid = icid->cid; |
| 3385 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3386 | /* Packet number spaces initialization. */ |
| 3387 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 3388 | quic_pktns_init(&qc->pktns[i]); |
| 3389 | /* QUIC encryption level context initialization. */ |
| 3390 | 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] | 3391 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3392 | 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] | 3393 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3394 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3395 | /* Initialize the packet number space. */ |
| 3396 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 3397 | } |
| 3398 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 3399 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 3400 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 3401 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 3402 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3403 | /* TX part. */ |
| 3404 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3405 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 3406 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 3407 | qc->tx.bytes = 0; |
| 3408 | qc->tx.nb_pto_dgrams = 0; |
| 3409 | /* RX part. */ |
| 3410 | qc->rx.bytes = 0; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3411 | qc->rx.nb_ack_eliciting = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3412 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 3413 | HA_RWLOCK_INIT(&qc->rx.buf_rwlock); |
| 3414 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 3415 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3416 | 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] | 3417 | goto err; |
| 3418 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3419 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3420 | /* XXX TO DO: Only one path at this time. */ |
| 3421 | qc->path = &qc->paths[0]; |
| 3422 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 3423 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3424 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3425 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3426 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3427 | |
| 3428 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3429 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 3430 | quic_conn_drop(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3431 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | /* Initialize the timer task of <qc> QUIC connection. |
| 3435 | * Returns 1 if succeeded, 0 if not. |
| 3436 | */ |
| 3437 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 3438 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 3439 | /* Attach this task to the same thread ID used for the connection */ |
| 3440 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3441 | if (!qc->timer_task) |
| 3442 | return 0; |
| 3443 | |
| 3444 | qc->timer = TICK_ETERNITY; |
| 3445 | qc->timer_task->process = process_timer; |
| 3446 | qc->timer_task->context = qc->conn->xprt_ctx; |
| 3447 | |
| 3448 | return 1; |
| 3449 | } |
| 3450 | |
| 3451 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 3452 | * past one byte of this buffer. |
| 3453 | */ |
| 3454 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 3455 | struct quic_rx_packet *pkt) |
| 3456 | { |
| 3457 | unsigned char dcid_len, scid_len; |
| 3458 | |
| 3459 | /* Version */ |
| 3460 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 3461 | return 0; |
| 3462 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3463 | /* Destination Connection ID Length */ |
| 3464 | dcid_len = *(*buf)++; |
| 3465 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 3466 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 3467 | /* XXX MUST BE DROPPED */ |
| 3468 | return 0; |
| 3469 | |
| 3470 | if (dcid_len) { |
| 3471 | /* Check that the length of this received DCID matches the CID lengths |
| 3472 | * of our implementation for non Initials packets only. |
| 3473 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3474 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 3475 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3476 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3477 | return 0; |
| 3478 | |
| 3479 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 3480 | } |
| 3481 | |
| 3482 | pkt->dcid.len = dcid_len; |
| 3483 | *buf += dcid_len; |
| 3484 | |
| 3485 | /* Source Connection ID Length */ |
| 3486 | scid_len = *(*buf)++; |
| 3487 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 3488 | /* XXX MUST BE DROPPED */ |
| 3489 | return 0; |
| 3490 | |
| 3491 | if (scid_len) |
| 3492 | memcpy(pkt->scid.data, *buf, scid_len); |
| 3493 | pkt->scid.len = scid_len; |
| 3494 | *buf += scid_len; |
| 3495 | |
| 3496 | return 1; |
| 3497 | } |
| 3498 | |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3499 | /* If the header protection of <pkt> packet attached to <qc> connection with <ctx> |
| 3500 | * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated |
| 3501 | * encryption level matching with the packet type. <*qel> may be null if not found. |
| 3502 | * Note that <ctx> may be null (for Initial packets). |
| 3503 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3504 | 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] | 3505 | struct quic_enc_level **qel) |
| 3506 | { |
| 3507 | enum quic_tls_enc_level tel; |
| 3508 | |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3509 | tel = quic_packet_type_enc_level(pkt->type); |
| 3510 | if (tel == QUIC_TLS_ENC_LEVEL_NONE) { |
| 3511 | *qel = NULL; |
| 3512 | return 0; |
| 3513 | } |
| 3514 | |
| 3515 | *qel = &qc->els[tel]; |
| 3516 | if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3517 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3518 | return 0; |
| 3519 | } |
| 3520 | |
| 3521 | 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] | 3522 | (tel != QUIC_TLS_ENC_LEVEL_APP || |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3523 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)) |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3524 | return 1; |
| 3525 | |
| 3526 | return 0; |
| 3527 | } |
| 3528 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3529 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 3530 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 3531 | { |
| 3532 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 3533 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3534 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3535 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 3536 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | /* 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] | 3540 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 3541 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 3542 | * the packet first byte. |
| 3543 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 3544 | * Returns 1 if succeeded, 0 if not. |
| 3545 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3546 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 3547 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3548 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3549 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3550 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3551 | { |
| 3552 | unsigned char *pn = NULL; /* Packet number field */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3553 | struct quic_enc_level *qel; |
| 3554 | /* Only for traces. */ |
| 3555 | struct quic_rx_packet *qpkt_trace; |
| 3556 | |
| 3557 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3558 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3559 | /* The packet number is here. This is also the start minus |
| 3560 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 3561 | * protection. |
| 3562 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3563 | pn = buf; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3564 | if (qc_pkt_may_rm_hp(qc, pkt, &qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3565 | /* Note that the following function enables us to unprotect the packet |
| 3566 | * number and its length subsequently used to decrypt the entire |
| 3567 | * packets. |
| 3568 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3569 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 3570 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 3571 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3572 | goto err; |
| 3573 | } |
| 3574 | |
| 3575 | /* The AAD includes the packet number field found at <pn>. */ |
| 3576 | pkt->aad_len = pn - beg + pkt->pnl; |
| 3577 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3578 | } |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3579 | else if (qel) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3580 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 3581 | /* If the packet number space has been discarded, this packet |
| 3582 | * will be not parsed. |
| 3583 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3584 | 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] | 3585 | goto out; |
| 3586 | } |
| 3587 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3588 | 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] | 3589 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3590 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 3591 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3592 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3593 | else { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3594 | 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] | 3595 | goto err; |
| 3596 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3597 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3598 | *el = qel; |
| 3599 | /* No reference counter incrementation here!!! */ |
| 3600 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 3601 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 3602 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 3603 | b_add(&qc->rx.buf, pkt->len); |
| 3604 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3605 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3606 | return 1; |
| 3607 | |
| 3608 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3609 | 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] | 3610 | return 0; |
| 3611 | } |
| 3612 | |
| 3613 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 3614 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 3615 | */ |
| 3616 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 3617 | unsigned char byte0, int *long_header) |
| 3618 | { |
| 3619 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 3620 | pkt->type = |
| 3621 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 3622 | *long_header = 1; |
| 3623 | } |
| 3624 | else { |
| 3625 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 3626 | *long_header = 0; |
| 3627 | } |
| 3628 | } |
| 3629 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3630 | /* |
| 3631 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 3632 | */ |
| 3633 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 3634 | { |
| 3635 | int j = 0, version; |
| 3636 | |
| 3637 | do { |
| 3638 | version = quic_supported_version[j]; |
| 3639 | if (version == pkt->version) |
| 3640 | return 1; |
| 3641 | |
| 3642 | version = quic_supported_version[++j]; |
| 3643 | } while(version); |
| 3644 | |
| 3645 | return 0; |
| 3646 | } |
| 3647 | |
Frédéric Lécaille | c5c69a0 | 2021-10-20 17:24:42 +0200 | [diff] [blame] | 3648 | __attribute__((unused)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3649 | static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end, |
| 3650 | struct quic_rx_packet *pkt, |
| 3651 | struct quic_dgram_ctx *dgram_ctx, |
| 3652 | struct sockaddr_storage *saddr) |
| 3653 | { |
| 3654 | unsigned char *beg; |
| 3655 | uint64_t len; |
| 3656 | struct quic_conn *qc; |
| 3657 | struct eb_root *cids; |
| 3658 | struct ebmb_node *node; |
| 3659 | struct connection *srv_conn; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3660 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3661 | int long_header; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3662 | size_t b_cspace; |
| 3663 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3664 | |
| 3665 | qc = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 3666 | qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3667 | TRACE_ENTER(QUIC_EV_CONN_SPKT); |
| 3668 | if (end <= *buf) |
| 3669 | goto err; |
| 3670 | |
| 3671 | /* Fixed bit */ |
| 3672 | if (!(**buf & QUIC_PACKET_FIXED_BIT)) |
| 3673 | /* XXX TO BE DISCARDED */ |
| 3674 | goto err; |
| 3675 | |
| 3676 | srv_conn = dgram_ctx->owner; |
| 3677 | beg = *buf; |
| 3678 | /* Header form */ |
| 3679 | qc_parse_hd_form(pkt, *(*buf)++, &long_header); |
| 3680 | if (long_header) { |
| 3681 | size_t cid_lookup_len; |
| 3682 | |
| 3683 | if (!quic_packet_read_long_header(buf, end, pkt)) |
| 3684 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3685 | |
| 3686 | /* unsupported QUIC version */ |
| 3687 | if (!qc_pkt_is_supported_version(pkt)) { |
| 3688 | TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT); |
| 3689 | goto err; |
| 3690 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3691 | |
| 3692 | /* For Initial packets, and for servers (QUIC clients connections), |
| 3693 | * there is no Initial connection IDs storage. |
| 3694 | */ |
| 3695 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3696 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
| 3697 | cid_lookup_len = pkt->dcid.len; |
| 3698 | } |
| 3699 | else { |
| 3700 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3701 | cid_lookup_len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len); |
| 3705 | if (!node) |
| 3706 | goto err; |
| 3707 | |
| 3708 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
| 3709 | |
| 3710 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3711 | qc->dcid.len = pkt->scid.len; |
| 3712 | if (pkt->scid.len) |
| 3713 | memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len); |
| 3714 | } |
| 3715 | |
| 3716 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3717 | uint64_t token_len; |
| 3718 | |
| 3719 | if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len) |
| 3720 | goto err; |
| 3721 | |
| 3722 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 3723 | * A server which sends an Initial packet must not set the token. |
| 3724 | * So, a client which receives an Initial packet with a token |
| 3725 | * MUST discard the packet or generate a connection error with |
| 3726 | * PROTOCOL_VIOLATION as type. |
| 3727 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 3728 | */ |
| 3729 | pkt->token_len = token_len; |
| 3730 | } |
| 3731 | } |
| 3732 | else { |
| 3733 | /* XXX TO DO: Short header XXX */ |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3734 | if (end - *buf < QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3735 | goto err; |
| 3736 | |
| 3737 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3738 | node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3739 | if (!node) |
| 3740 | goto err; |
| 3741 | |
| 3742 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3743 | *buf += QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3744 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3745 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3746 | /* Only packets packets with long headers and not RETRY or VERSION as type |
| 3747 | * have a length field. |
| 3748 | */ |
| 3749 | if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) { |
| 3750 | if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len) |
| 3751 | goto err; |
| 3752 | |
| 3753 | pkt->len = len; |
| 3754 | } |
| 3755 | else if (!long_header) { |
| 3756 | /* A short packet is the last one of an UDP datagram. */ |
| 3757 | pkt->len = end - *buf; |
| 3758 | } |
| 3759 | |
| 3760 | conn_ctx = qc->conn->xprt_ctx; |
| 3761 | |
| 3762 | /* Increase the total length of this packet by the header length. */ |
| 3763 | pkt->len += *buf - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3764 | |
| 3765 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 3766 | * they must have the same DCID. |
| 3767 | * |
| 3768 | * This check must be done after the final update to pkt.len to |
| 3769 | * properly drop the packet on failure. |
| 3770 | */ |
| 3771 | if (!dgram_ctx->dcid.len) { |
| 3772 | memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len); |
| 3773 | } |
| 3774 | 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] | 3775 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3776 | goto err; |
| 3777 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3778 | dgram_ctx->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3779 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3780 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3781 | b_cspace = b_contig_space(&qc->rx.buf); |
| 3782 | if (b_cspace < pkt->len) { |
| 3783 | /* Let us consume the remaining contiguous space. */ |
| 3784 | b_add(&qc->rx.buf, b_cspace); |
| 3785 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 3786 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3787 | 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] | 3788 | goto err; |
| 3789 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3790 | } |
| 3791 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3792 | 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] | 3793 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3794 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3795 | goto err; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3796 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3797 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3798 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3799 | if (pkt->aad_len) |
| 3800 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3801 | /* Wake the tasklet of the QUIC connection packet handler. */ |
| 3802 | if (conn_ctx) |
| 3803 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3804 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3805 | TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3806 | |
| 3807 | return pkt->len; |
| 3808 | |
| 3809 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3810 | 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] | 3811 | return -1; |
| 3812 | } |
| 3813 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3814 | /* |
| 3815 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 3816 | * address <addr>. |
| 3817 | * Implementation of RFC9000 6. Version Negotiation |
| 3818 | * |
| 3819 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 3820 | * |
| 3821 | * Returns 0 on success else non-zero |
| 3822 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 3823 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 3824 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3825 | { |
| 3826 | char buf[256]; |
| 3827 | int i = 0, j, version; |
| 3828 | const socklen_t addrlen = get_addr_len(addr); |
| 3829 | |
| 3830 | /* |
| 3831 | * header form |
| 3832 | * long header, fixed bit to 0 for Version Negotiation |
| 3833 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3834 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 3835 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3836 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3837 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3838 | /* null version for Version Negotiation */ |
| 3839 | buf[i++] = '\x00'; |
| 3840 | buf[i++] = '\x00'; |
| 3841 | buf[i++] = '\x00'; |
| 3842 | buf[i++] = '\x00'; |
| 3843 | |
| 3844 | /* source connection id */ |
| 3845 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3846 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3847 | i += pkt->scid.len; |
| 3848 | |
| 3849 | /* destination connection id */ |
| 3850 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3851 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3852 | i += pkt->dcid.len; |
| 3853 | |
| 3854 | /* supported version */ |
| 3855 | j = 0; |
| 3856 | do { |
| 3857 | version = htonl(quic_supported_version[j]); |
| 3858 | memcpy(&buf[i], &version, sizeof(version)); |
| 3859 | i += sizeof(version); |
| 3860 | |
| 3861 | version = quic_supported_version[++j]; |
| 3862 | } while (version); |
| 3863 | |
| 3864 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 3865 | return 1; |
| 3866 | |
| 3867 | return 0; |
| 3868 | } |
| 3869 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3870 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 3871 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 3872 | * concatenated to the <pkt> DCID field. |
| 3873 | * |
| 3874 | * Returns the instance or NULL if not found. |
| 3875 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 3876 | 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] | 3877 | struct listener *l, |
| 3878 | struct sockaddr_storage *saddr) |
| 3879 | { |
| 3880 | struct quic_conn *qc = NULL; |
| 3881 | struct ebmb_node *node; |
| 3882 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 3883 | /* set if the quic_conn is found in the second DCID tree */ |
| 3884 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3885 | |
| 3886 | HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3887 | |
| 3888 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 3889 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 3890 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 3891 | /* DCIDs of first packets coming from multiple clients may have |
| 3892 | * the same values. Let's distinguish them by concatenating the |
| 3893 | * socket addresses. |
| 3894 | */ |
| 3895 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 3896 | node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data, |
| 3897 | pkt->dcid.len + pkt->dcid.addrlen); |
| 3898 | if (node) { |
| 3899 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 3900 | goto end; |
| 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 3905 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 3906 | * used. |
| 3907 | */ |
| 3908 | node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len); |
| 3909 | if (!node) |
| 3910 | goto end; |
| 3911 | |
| 3912 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 3913 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 3914 | found_in_dcid = 1; |
| 3915 | |
| 3916 | end: |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 3917 | if (qc) |
| 3918 | quic_conn_take(qc); |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 3919 | HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3920 | |
| 3921 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 3922 | * If already done, this is a noop. |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 3923 | * |
| 3924 | * node.leaf_p is first checked to avoid unnecessary locking. |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3925 | */ |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 3926 | if (found_in_dcid && qc->odcid_node.node.leaf_p) { |
| 3927 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3928 | ebmb_delete(&qc->odcid_node); |
| 3929 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3930 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3931 | |
| 3932 | return qc; |
| 3933 | } |
| 3934 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3935 | 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] | 3936 | struct quic_rx_packet *pkt, |
| 3937 | struct quic_dgram_ctx *dgram_ctx, |
| 3938 | struct sockaddr_storage *saddr) |
| 3939 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3940 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 3941 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3942 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3943 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3944 | int long_header = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3945 | size_t b_cspace; |
| 3946 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3947 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3948 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3949 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 3950 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 3951 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 3952 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 3953 | /* This ist only to please to traces and distinguish the |
| 3954 | * packet with parsed packet number from others. |
| 3955 | */ |
| 3956 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3957 | if (end <= buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3958 | goto err; |
| 3959 | |
| 3960 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3961 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3962 | /* XXX TO BE DISCARDED */ |
| 3963 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3964 | goto err; |
| 3965 | } |
| 3966 | |
| 3967 | l = dgram_ctx->owner; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3968 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3969 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3970 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 3971 | uint64_t len; |
| 3972 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3973 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3974 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3975 | goto err; |
| 3976 | } |
| 3977 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 3978 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 3979 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 3980 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3981 | goto err; |
| 3982 | } |
| 3983 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3984 | /* RFC9000 6. Version Negotiation */ |
| 3985 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3986 | /* unsupported version, send Negotiation packet */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 3987 | if (send_version_negotiation(l->rx.fd, saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3988 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 3989 | goto err; |
| 3990 | } |
| 3991 | |
| 3992 | 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] | 3993 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3994 | } |
| 3995 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3996 | /* For Initial packets, and for servers (QUIC clients connections), |
| 3997 | * there is no Initial connection IDs storage. |
| 3998 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 3999 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4000 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4001 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4002 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 4003 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4004 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4005 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4006 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4007 | |
| 4008 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 4009 | * A server which sends an Initial packet must not set the token. |
| 4010 | * So, a client which receives an Initial packet with a token |
| 4011 | * MUST discard the packet or generate a connection error with |
| 4012 | * PROTOCOL_VIOLATION as type. |
| 4013 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 4014 | */ |
| 4015 | pkt->token_len = token_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4016 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4017 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4018 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4019 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4020 | goto err; |
| 4021 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4022 | } |
| 4023 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4024 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 4025 | end - buf < len) { |
| 4026 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4027 | goto err; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4028 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4029 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4030 | payload = buf; |
| 4031 | pkt->len = len + payload - beg; |
| 4032 | |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4033 | qc = retrieve_qc_conn_from_cid(pkt, l, saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4034 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4035 | int ipv4; |
| 4036 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4037 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4038 | const unsigned char *salt = initial_salt_v1; |
| 4039 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4040 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4041 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 4042 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4043 | goto err; |
| 4044 | } |
| 4045 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4046 | pkt->saddr = *saddr; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4047 | ipv4 = saddr->ss_family == AF_INET; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4048 | qc = qc_new_conn(pkt->version, ipv4, |
| 4049 | pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4050 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4051 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4052 | goto err; |
| 4053 | |
| 4054 | odcid = &qc->rx.params.original_destination_connection_id; |
| 4055 | /* Copy the transport parameters. */ |
| 4056 | qc->rx.params = l->bind_conf->quic_params; |
| 4057 | /* Copy original_destination_connection_id transport parameter. */ |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4058 | memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len); |
| 4059 | odcid->len = pkt->dcid.len; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4060 | /* Copy the initial source connection ID. */ |
| 4061 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4062 | qc->enc_params_len = |
| 4063 | quic_transport_params_encode(qc->enc_params, |
| 4064 | qc->enc_params + sizeof qc->enc_params, |
| 4065 | &qc->rx.params, 1); |
| 4066 | if (!qc->enc_params_len) |
| 4067 | goto err; |
| 4068 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4069 | /* NOTE: the socket address has been concatenated to the destination ID |
| 4070 | * chosen by the client for Initial packets. |
| 4071 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4072 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4073 | salt = initial_salt_draft_29; |
| 4074 | salt_len = sizeof initial_salt_draft_29; |
| 4075 | } |
| 4076 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4077 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4078 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4079 | goto err; |
| 4080 | } |
| 4081 | |
Frédéric Lécaille | b0006ee | 2021-11-03 19:01:31 +0100 | [diff] [blame] | 4082 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4083 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4084 | n = ebmb_insert(&l->rx.odcids, &qc->odcid_node, |
| 4085 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4086 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4087 | /* If the insertion failed, it means that another |
| 4088 | * thread has already allocated a QUIC connection for |
| 4089 | * the same CID. Liberate our allocated connection. |
| 4090 | */ |
| 4091 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 4092 | qc_to_purge = qc; |
| 4093 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4094 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 4095 | pkt->qc = qc; |
| 4096 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 4097 | |
| 4098 | quic_conn_take(qc); |
| 4099 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 4100 | |
| 4101 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4102 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 4103 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4104 | MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list); |
| 4105 | /* Try to accept a new connection. */ |
| 4106 | listener_accept(l); |
| 4107 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 4108 | else { |
| 4109 | quic_conn_drop(qc_to_purge); |
| 4110 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4111 | } |
| 4112 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4113 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4114 | } |
| 4115 | } |
| 4116 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4117 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4118 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4119 | goto err; |
| 4120 | } |
| 4121 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4122 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4123 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4124 | buf += QUIC_HAP_CID_LEN; |
| 4125 | |
| 4126 | /* A short packet is the last one of a UDP datagram. */ |
| 4127 | payload = buf; |
| 4128 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4129 | |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4130 | qc = retrieve_qc_conn_from_cid(pkt, l, saddr); |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4131 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4132 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4133 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
| 4134 | goto err; |
| 4135 | } |
| 4136 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4137 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4138 | } |
| 4139 | |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4140 | |
| 4141 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4142 | * they must have the same DCID. |
| 4143 | * |
| 4144 | * This check must be done after the final update to pkt.len to |
| 4145 | * properly drop the packet on failure. |
| 4146 | */ |
| 4147 | if (!dgram_ctx->dcid.len) { |
| 4148 | memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len); |
| 4149 | } |
| 4150 | 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] | 4151 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4152 | goto err; |
| 4153 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4154 | dgram_ctx->qc = qc; |
| 4155 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4156 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4157 | if (HA_ATOMIC_LOAD(&qc->err_code)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4158 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4159 | goto out; |
| 4160 | } |
| 4161 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4162 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4163 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4164 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4165 | b_cspace = b_contig_space(&qc->rx.buf); |
| 4166 | if (b_cspace < pkt->len) { |
| 4167 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4168 | if (b_cspace) { |
| 4169 | b_putchr(&qc->rx.buf, 0x00); |
| 4170 | b_cspace--; |
| 4171 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4172 | b_add(&qc->rx.buf, b_cspace); |
| 4173 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 4174 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4175 | 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] | 4176 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4177 | goto err; |
| 4178 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4179 | } |
| 4180 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4181 | 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] | 4182 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4183 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 4184 | goto err; |
| 4185 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4186 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4187 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4188 | 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] | 4189 | if (pkt->aad_len) |
| 4190 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4191 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 4192 | /* Wake up the connection packet handler task from here only if all |
| 4193 | * the contexts have been initialized, especially the mux context |
| 4194 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 4195 | * will start it if these contexts for the connection are not already |
| 4196 | * initialized. |
| 4197 | */ |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 4198 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
| 4199 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4200 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4201 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4202 | 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] | 4203 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 4204 | if (qc) |
| 4205 | quic_conn_drop(qc); |
| 4206 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4207 | return pkt->len; |
| 4208 | |
| 4209 | err: |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4210 | /* If length not found, consume the entire packet */ |
| 4211 | if (!pkt->len) |
| 4212 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 4213 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4214 | qc ? qc : NULL, pkt); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame^] | 4215 | |
| 4216 | if (qc) |
| 4217 | quic_conn_drop(qc); |
| 4218 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4219 | return -1; |
| 4220 | } |
| 4221 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4222 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 4223 | * 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] | 4224 | */ |
| 4225 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 4226 | int type, size_t pn_len, struct quic_conn *conn) |
| 4227 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4228 | 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] | 4229 | return 0; |
| 4230 | |
| 4231 | /* #0 byte flags */ |
| 4232 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 4233 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 4234 | /* Version */ |
| 4235 | quic_write_uint32(buf, end, conn->version); |
| 4236 | *(*buf)++ = conn->dcid.len; |
| 4237 | /* Destination connection ID */ |
| 4238 | if (conn->dcid.len) { |
| 4239 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4240 | *buf += conn->dcid.len; |
| 4241 | } |
| 4242 | /* Source connection ID */ |
| 4243 | *(*buf)++ = conn->scid.len; |
| 4244 | if (conn->scid.len) { |
| 4245 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 4246 | *buf += conn->scid.len; |
| 4247 | } |
| 4248 | |
| 4249 | return 1; |
| 4250 | } |
| 4251 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4252 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 4253 | * 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] | 4254 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4255 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 4256 | size_t pn_len, struct quic_conn *conn, |
| 4257 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4258 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4259 | if (end - *buf < 1 + conn->dcid.len) |
| 4260 | return 0; |
| 4261 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4262 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 4263 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 4264 | ((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] | 4265 | /* Destination connection ID */ |
| 4266 | if (conn->dcid.len) { |
| 4267 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4268 | *buf += conn->dcid.len; |
| 4269 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4270 | |
| 4271 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 4275 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 4276 | * with <aead> as AEAD cipher and <key> as secret key. |
| 4277 | * Returns 1 if succeeded or 0 if failed. |
| 4278 | */ |
| 4279 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 4280 | const EVP_CIPHER *aead, const unsigned char *key) |
| 4281 | { |
| 4282 | int i, ret, outlen; |
| 4283 | EVP_CIPHER_CTX *ctx; |
| 4284 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 4285 | * and at most 4 bytes for the packet number |
| 4286 | */ |
| 4287 | unsigned char mask[5] = {0}; |
| 4288 | |
| 4289 | ret = 0; |
| 4290 | ctx = EVP_CIPHER_CTX_new(); |
| 4291 | if (!ctx) |
| 4292 | return 0; |
| 4293 | |
| 4294 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 4295 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 4296 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 4297 | goto out; |
| 4298 | |
| 4299 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 4300 | for (i = 0; i < pnlen; i++) |
| 4301 | pn[i] ^= mask[i + 1]; |
| 4302 | |
| 4303 | ret = 1; |
| 4304 | |
| 4305 | out: |
| 4306 | EVP_CIPHER_CTX_free(ctx); |
| 4307 | |
| 4308 | return ret; |
| 4309 | } |
| 4310 | |
| 4311 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 4312 | * ACK ranges if needed to a value below <limit> in bytes. |
| 4313 | * Return 1 if succeeded, 0 if not. |
| 4314 | */ |
| 4315 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 4316 | { |
| 4317 | size_t room, ack_delay_sz; |
| 4318 | |
| 4319 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 4320 | /* A frame is made of 1 byte for the frame type. */ |
| 4321 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4322 | 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] | 4323 | return 0; |
| 4324 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4325 | 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] | 4326 | } |
| 4327 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4328 | /* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames |
| 4329 | * 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] | 4330 | * and <*len> the packet Length field initialized with the number of bytes already present |
| 4331 | * 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] | 4332 | * <headlen> is the number of bytes already present in this packet before building frames. |
| 4333 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4334 | * Update consequently <*len> to reflect the size of these frames built |
| 4335 | * 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] | 4336 | * Return 1 if succeeded, 0 if not. |
| 4337 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4338 | static inline int qc_build_frms(struct quic_tx_packet *pkt, |
| 4339 | size_t room, size_t *len, size_t headlen, |
| 4340 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4341 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4342 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4343 | int ret; |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4344 | struct quic_frame *cf; |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 4345 | struct mt_list *tmp1, tmp2; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4346 | size_t remain = quic_path_prep_data(qc->path); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4347 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4348 | ret = 0; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4349 | if (*len > room || headlen > remain) |
| 4350 | return 0; |
| 4351 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4352 | /* If we are not probing we must take into an account the congestion |
| 4353 | * control window. |
| 4354 | */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4355 | if (!qc->tx.nb_pto_dgrams) |
| 4356 | 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] | 4357 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4358 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 4359 | 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] | 4360 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4361 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4362 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4363 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4364 | break; |
| 4365 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4366 | switch (cf->type) { |
| 4367 | case QUIC_FT_CRYPTO: |
| 4368 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4369 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4370 | /* Compute the length of this CRYPTO frame header */ |
| 4371 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 4372 | /* Compute the data length of this CRyPTO frame. */ |
| 4373 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 4374 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4375 | 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] | 4376 | if (!dlen) |
| 4377 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4378 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4379 | /* CRYPTO frame length. */ |
| 4380 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 4381 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4382 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4383 | /* Add the CRYPTO data length and its encoded length to the packet |
| 4384 | * length and the length of this length. |
| 4385 | */ |
| 4386 | *len += flen; |
| 4387 | room -= flen; |
| 4388 | if (dlen == cf->crypto.len) { |
| 4389 | /* <cf> CRYPTO data have been consumed. */ |
| 4390 | MT_LIST_DELETE_SAFE(tmp1); |
| 4391 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4392 | } |
| 4393 | else { |
| 4394 | struct quic_frame *new_cf; |
| 4395 | |
| 4396 | new_cf = pool_alloc(pool_head_quic_frame); |
| 4397 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4398 | 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] | 4399 | return 0; |
| 4400 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4401 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4402 | new_cf->type = QUIC_FT_CRYPTO; |
| 4403 | new_cf->crypto.len = dlen; |
| 4404 | new_cf->crypto.offset = cf->crypto.offset; |
| 4405 | new_cf->crypto.qel = qel; |
| 4406 | LIST_APPEND(&pkt->frms, &new_cf->list); |
| 4407 | /* Consume <dlen> bytes of the current frame. */ |
| 4408 | cf->crypto.len -= dlen; |
| 4409 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4410 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4411 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4412 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4413 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4414 | /* Note that these frames are accepted in short packets only without |
| 4415 | * "Length" packet field. Here, <*len> is used only to compute the |
| 4416 | * 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] | 4417 | * |
| 4418 | * 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] | 4419 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 4420 | */ |
| 4421 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4422 | ((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] | 4423 | /* Compute the data length of this STREAM frame. */ |
| 4424 | avail_room = room - hlen - *len; |
| 4425 | if ((ssize_t)avail_room <= 0) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4426 | break; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4427 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 4428 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4429 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4430 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 4431 | dlen = max_available_room(avail_room, &dlen_sz); |
| 4432 | if (dlen > cf->stream.len) { |
| 4433 | dlen = cf->stream.len; |
| 4434 | } |
| 4435 | dlen_sz = quic_int_getsize(dlen); |
| 4436 | flen = hlen + dlen_sz + dlen; |
| 4437 | } |
| 4438 | else { |
| 4439 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 4440 | flen = hlen + dlen; |
| 4441 | } |
| 4442 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4443 | 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] | 4444 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4445 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4446 | /* Add the STREAM data length and its encoded length to the packet |
| 4447 | * length and the length of this length. |
| 4448 | */ |
| 4449 | *len += flen; |
| 4450 | room -= flen; |
| 4451 | if (dlen == cf->stream.len) { |
| 4452 | /* <cf> STREAM data have been consumed. */ |
| 4453 | MT_LIST_DELETE_SAFE(tmp1); |
| 4454 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4455 | } |
| 4456 | else { |
| 4457 | struct quic_frame *new_cf; |
| 4458 | |
| 4459 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 4460 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4461 | 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] | 4462 | return 0; |
| 4463 | } |
| 4464 | |
| 4465 | new_cf->type = cf->type; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4466 | new_cf->stream.qcs = cf->stream.qcs; |
| 4467 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4468 | new_cf->stream.id = cf->stream.id; |
| 4469 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 4470 | new_cf->stream.offset = cf->stream.offset; |
| 4471 | new_cf->stream.len = dlen; |
| 4472 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 4473 | /* FIN bit reset */ |
| 4474 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 4475 | new_cf->stream.data = cf->stream.data; |
| 4476 | LIST_APPEND(&pkt->frms, &new_cf->list); |
| 4477 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 4478 | /* Consume <dlen> bytes of the current frame. */ |
| 4479 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4480 | cf->stream.offset.key += dlen; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4481 | cf->stream.data += dlen; |
| 4482 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4483 | break; |
| 4484 | |
| 4485 | default: |
| 4486 | flen = qc_frm_len(cf); |
| 4487 | BUG_ON(!flen); |
| 4488 | if (flen > room) |
| 4489 | continue; |
| 4490 | |
| 4491 | *len += flen; |
| 4492 | room -= flen; |
| 4493 | MT_LIST_DELETE_SAFE(tmp1); |
| 4494 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4495 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4496 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4497 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4498 | } |
| 4499 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4500 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4501 | } |
| 4502 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4503 | /* 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] | 4504 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 4505 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 4506 | * filling the buffer with as much frames as possible. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4507 | * 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] | 4508 | * 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] | 4509 | * having returned from this function. |
| 4510 | * 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] | 4511 | * number field in this packet. <pn_len> will also have the packet number |
| 4512 | * length as value. |
| 4513 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4514 | * 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] | 4515 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4516 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 4517 | size_t dglen, struct quic_tx_packet *pkt, |
| 4518 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
| 4519 | int ack, int padding, int cc, int nb_pto_dgrams, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4520 | struct quic_enc_level *qel, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4521 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4522 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4523 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4524 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 4525 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4526 | 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] | 4527 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4528 | int64_t largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4529 | int add_ping_frm; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4530 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4531 | /* Length field value with CRYPTO frames if present. */ |
| 4532 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4533 | beg = pos; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4534 | /* When not probing and not acking, and no immediate close is required, |
| 4535 | * reduce the size of this buffer to respect |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4536 | * the congestion controller window. So, we do not limit the size of this |
| 4537 | * packet if we have an ACK frame to send because an ACK frame is not |
| 4538 | * ack-eliciting. This size will be limited if we have ack-eliciting |
| 4539 | * frames to send from qel->pktns->tx.frms. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4540 | */ |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4541 | if (!nb_pto_dgrams && !ack && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4542 | size_t path_room; |
| 4543 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4544 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4545 | if (end - beg > path_room) |
| 4546 | end = beg + path_room; |
| 4547 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4548 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4549 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 4550 | * length field if any. |
| 4551 | */ |
| 4552 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 4553 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 4554 | goto no_room; |
| 4555 | |
| 4556 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | e1aa0d3 | 2021-08-03 16:03:09 +0200 | [diff] [blame] | 4557 | 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] | 4558 | /* packet number length */ |
| 4559 | *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] | 4560 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4561 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4562 | !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] | 4563 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4564 | !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] | 4565 | goto no_room; |
| 4566 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4567 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 4568 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4569 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4570 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4571 | /* Build an ACK frame if required. */ |
| 4572 | ack_frm_len = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4573 | 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] | 4574 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4575 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4576 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 4577 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 4578 | * that from here, we do not know if we will have to send a PING frame. |
| 4579 | * This will be decided after having computed the ack-eliciting frames |
| 4580 | * to be added to this packet. |
| 4581 | */ |
| 4582 | 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] | 4583 | if (!ack_frm_len) |
| 4584 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4585 | } |
| 4586 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4587 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4588 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4589 | len_frms = 0; |
| 4590 | if (!cc && !MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4591 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4592 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4593 | /* Initialize the length of the frames built below to <len>. |
| 4594 | * If any frame could be successfully built by qc_build_frms(), |
| 4595 | * we will have len_frms > len. |
| 4596 | */ |
| 4597 | len_frms = len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4598 | 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] | 4599 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4600 | qc, NULL, NULL, &room); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4601 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4602 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4603 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4604 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 4605 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 4606 | * for the encryption tag. It must be taken into an account for the length |
| 4607 | * of this packet. |
| 4608 | */ |
| 4609 | if (len_frms) |
| 4610 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 4611 | else |
| 4612 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4613 | /* CONNECTION_CLOSE frame */ |
| 4614 | if (cc) { |
| 4615 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4616 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4617 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4618 | len += qc_frm_len(&cc_frm); |
| 4619 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4620 | add_ping_frm = 0; |
| 4621 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4622 | len_sz = quic_int_getsize(len); |
| 4623 | /* Add this packet size to <dglen> */ |
| 4624 | dglen += head_len + len_sz + len; |
| 4625 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 4626 | /* This is a maximum padding size */ |
| 4627 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 4628 | /* The length field value is of this packet is <len> + <padding_len> |
| 4629 | * the size of which may be greater than the initial computed size |
| 4630 | * <len_sz>. So, let's deduce the difference betwen these to packet |
| 4631 | * sizes from <padding_len>. |
| 4632 | */ |
| 4633 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 4634 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4635 | } |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4636 | else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4637 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4638 | /* 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] | 4639 | add_ping_frm = 1; |
| 4640 | len += 1; |
| 4641 | } |
| 4642 | /* 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] | 4643 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4644 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 4645 | } |
| 4646 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4647 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 4648 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4649 | |
| 4650 | /* Packet number field address. */ |
| 4651 | *buf_pn = pos; |
| 4652 | |
| 4653 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4654 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 4655 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4656 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4657 | 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] | 4658 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4659 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4660 | /* Ack-eliciting frames */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4661 | if (!LIST_ISEMPTY(&pkt->frms)) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4662 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4663 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4664 | 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] | 4665 | list_for_each_entry(cf, &pkt->frms, list) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4666 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4667 | ssize_t room = end - pos; |
| 4668 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4669 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4670 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4671 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4672 | } |
| 4673 | } |
| 4674 | |
| 4675 | /* Build a PING frame if needed. */ |
| 4676 | if (add_ping_frm) { |
| 4677 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4678 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4679 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4680 | } |
| 4681 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4682 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4683 | 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] | 4684 | goto no_room; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4685 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4686 | /* Build a PADDING frame if needed. */ |
| 4687 | if (padding_len) { |
| 4688 | frm.type = QUIC_FT_PADDING; |
| 4689 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4690 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4691 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4692 | } |
| 4693 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4694 | /* Always reset this variable as this function has no idea |
| 4695 | * if it was set. It is handle by the loss detection timer. |
| 4696 | */ |
| 4697 | qel->pktns->tx.pto_probe = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4698 | pkt->len = pos - beg; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4699 | |
| 4700 | return 1; |
| 4701 | |
| 4702 | no_room: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4703 | 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] | 4704 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4705 | } |
| 4706 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4707 | 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] | 4708 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4709 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4710 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4711 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 4712 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4713 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4714 | pkt->next = NULL; |
| 4715 | pkt->refcnt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4716 | } |
| 4717 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4718 | /* 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] | 4719 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 4720 | { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4721 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4722 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4723 | if (!pkt) |
| 4724 | return; |
| 4725 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4726 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4727 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4728 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4729 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4730 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4731 | } |
| 4732 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4733 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 4734 | * type for <qc> QUIC connection from <qel> encryption level. |
| 4735 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 4736 | * -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] | 4737 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4738 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 4739 | const unsigned char *buf_end, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4740 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4741 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4742 | 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] | 4743 | { |
| 4744 | /* The pointer to the packet number field. */ |
| 4745 | unsigned char *buf_pn; |
| 4746 | unsigned char *beg, *end, *payload; |
| 4747 | int64_t pn; |
| 4748 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4749 | struct quic_tls_ctx *tls_ctx; |
| 4750 | struct quic_tx_packet *pkt; |
| 4751 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4752 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4753 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4754 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 4755 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4756 | 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] | 4757 | *err = -2; |
| 4758 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4759 | } |
| 4760 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4761 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4762 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4763 | pn_len = 0; |
| 4764 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4765 | |
| 4766 | pn = qel->pktns->tx.next_pn + 1; |
| 4767 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
| 4768 | ack, padding, cc, nb_pto_dgrams, qel, qc)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4769 | *err = -1; |
| 4770 | goto err; |
| 4771 | } |
| 4772 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4773 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4774 | payload = buf_pn + pn_len; |
| 4775 | payload_len = end - payload; |
| 4776 | aad_len = payload - beg; |
| 4777 | |
| 4778 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4779 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) { |
| 4780 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4781 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4782 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4783 | |
| 4784 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4785 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4786 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 4787 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4788 | 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] | 4789 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4790 | goto err; |
| 4791 | } |
| 4792 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4793 | /* Consume a packet number */ |
| 4794 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 4795 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4796 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 4797 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4798 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 4799 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4800 | /* 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] | 4801 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4802 | pkt->in_flight_len = pkt->len; |
| 4803 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4804 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4805 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4806 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4807 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4808 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4809 | |
| 4810 | err: |
| 4811 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4812 | 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] | 4813 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4814 | } |
| 4815 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4816 | /* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>. |
| 4817 | * Return the number of bytes which have been copied. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4818 | */ |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4819 | static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx, |
| 4820 | struct buffer *buf, size_t count, int flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4821 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4822 | size_t try, done = 0; |
| 4823 | |
| 4824 | if (!conn_ctrl_ready(conn)) |
| 4825 | return 0; |
| 4826 | |
| 4827 | if (!fd_recv_ready(conn->handle.fd)) |
| 4828 | return 0; |
| 4829 | |
| 4830 | conn->flags &= ~CO_FL_WAIT_ROOM; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4831 | |
| 4832 | /* read the largest possible block. For this, we perform only one call |
| 4833 | * 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] | 4834 | * 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] | 4835 | */ |
| 4836 | while (count > 0) { |
| 4837 | try = b_contig_space(buf); |
| 4838 | if (!try) |
| 4839 | break; |
| 4840 | |
| 4841 | if (try > count) |
| 4842 | try = count; |
| 4843 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4844 | b_add(buf, try); |
| 4845 | done += try; |
| 4846 | count -= try; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 4850 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4851 | |
| 4852 | leave: |
| 4853 | return done; |
| 4854 | |
| 4855 | read0: |
| 4856 | conn_sock_read0(conn); |
| 4857 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4858 | |
| 4859 | /* Now a final check for a possible asynchronous low-level error |
| 4860 | * report. This can happen when a connection receives a reset |
| 4861 | * after a shutdown, both POLL_HUP and POLL_ERR are queued, and |
| 4862 | * we might have come from there by just checking POLL_HUP instead |
| 4863 | * of recv()'s return value 0, so we have no way to tell there was |
| 4864 | * an error without checking. |
| 4865 | */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 4866 | 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] | 4867 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 4868 | goto leave; |
| 4869 | } |
| 4870 | |
| 4871 | |
| 4872 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 4873 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 4874 | * other pending data for example, but this flag is ignored at the moment. |
| 4875 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 4876 | * a second call may be performed. The connection's flags are updated with |
| 4877 | * whatever special event is detected (error, empty). The caller is responsible |
| 4878 | * for taking care of those events and avoiding the call if inappropriate. The |
| 4879 | * function does not call the connection's polling update function, so the caller |
| 4880 | * is responsible for this. It's up to the caller to update the buffer's contents |
| 4881 | * based on the return value. |
| 4882 | */ |
| 4883 | static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags) |
| 4884 | { |
| 4885 | ssize_t ret; |
| 4886 | size_t try, done; |
| 4887 | int send_flag; |
| 4888 | |
| 4889 | if (!conn_ctrl_ready(conn)) |
| 4890 | return 0; |
| 4891 | |
| 4892 | if (!fd_send_ready(conn->handle.fd)) |
| 4893 | return 0; |
| 4894 | |
| 4895 | done = 0; |
| 4896 | /* send the largest possible block. For this we perform only one call |
| 4897 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 4898 | * in which case we accept to do it once again. |
| 4899 | */ |
| 4900 | while (count) { |
| 4901 | try = b_contig_data(buf, done); |
| 4902 | if (try > count) |
| 4903 | try = count; |
| 4904 | |
| 4905 | send_flag = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 4906 | if (try < count || flags & CO_SFL_MSG_MORE) |
| 4907 | send_flag |= MSG_MORE; |
| 4908 | |
| 4909 | ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag, |
| 4910 | (struct sockaddr *)conn->dst, get_addr_len(conn->dst)); |
| 4911 | if (ret > 0) { |
| 4912 | count -= ret; |
| 4913 | done += ret; |
| 4914 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4915 | /* A send succeeded, so we can consider ourself connected */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4916 | conn->flags |= CO_FL_WAIT_L4L6; |
| 4917 | /* if the system buffer is full, don't insist */ |
| 4918 | if (ret < try) |
| 4919 | break; |
| 4920 | } |
| 4921 | else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) { |
| 4922 | /* nothing written, we need to poll for write first */ |
| 4923 | fd_cant_send(conn->handle.fd); |
| 4924 | break; |
| 4925 | } |
| 4926 | else if (errno != EINTR) { |
| 4927 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 4928 | break; |
| 4929 | } |
| 4930 | } |
| 4931 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 4932 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4933 | |
| 4934 | if (done > 0) { |
| 4935 | /* we count the total bytes sent, and the send rate for 32-byte |
| 4936 | * blocks. The reason for the latter is that freq_ctr are |
| 4937 | * limited to 4GB and that it's not enough per second. |
| 4938 | */ |
| 4939 | _HA_ATOMIC_ADD(&global.out_bytes, done); |
| 4940 | update_freq_ctr(&global.out_32bps, (done + 16) / 32); |
| 4941 | } |
| 4942 | return done; |
| 4943 | } |
| 4944 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 4945 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 4946 | * event subscriber <es> is not allowed to change from a previous call as long |
| 4947 | * as at least one event is still subscribed. The <event_type> must only be a |
| 4948 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 4949 | */ |
| 4950 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 4951 | { |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 4952 | struct qcc *qcc = conn->qc->qcc; |
| 4953 | |
| 4954 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 4955 | BUG_ON(qcc->subs && qcc->subs != es); |
| 4956 | |
| 4957 | es->events |= event_type; |
| 4958 | qcc->subs = es; |
| 4959 | |
| 4960 | if (event_type & SUB_RETRY_RECV) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4961 | 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] | 4962 | |
| 4963 | if (event_type & SUB_RETRY_SEND) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4964 | 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] | 4965 | |
| 4966 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 4967 | } |
| 4968 | |
| 4969 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 4970 | * The <es> pointer is not allowed to differ from the one passed to the |
| 4971 | * subscribe() call. It always returns zero. |
| 4972 | */ |
| 4973 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 4974 | { |
| 4975 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 4976 | } |
| 4977 | |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 4978 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 4979 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 4980 | * parameters of this session. |
| 4981 | * This is the responsibility of the caller to check the validity of all the |
| 4982 | * pointers passed as parameter to this function. |
| 4983 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 4984 | * CO_ER_SSL_NO_MEM. |
| 4985 | */ |
| 4986 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 4987 | unsigned char *params, size_t params_len) |
| 4988 | { |
| 4989 | int retry; |
| 4990 | |
| 4991 | retry = 1; |
| 4992 | retry: |
| 4993 | *ssl = SSL_new(ssl_ctx); |
| 4994 | if (!*ssl) { |
| 4995 | if (!retry--) |
| 4996 | goto err; |
| 4997 | |
| 4998 | pool_gc(NULL); |
| 4999 | goto retry; |
| 5000 | } |
| 5001 | |
| 5002 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 5003 | !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) || |
| 5004 | !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) { |
| 5005 | goto err; |
| 5006 | |
| 5007 | SSL_free(*ssl); |
| 5008 | *ssl = NULL; |
| 5009 | if (!retry--) |
| 5010 | goto err; |
| 5011 | |
| 5012 | pool_gc(NULL); |
| 5013 | goto retry; |
| 5014 | } |
| 5015 | |
| 5016 | return 0; |
| 5017 | |
| 5018 | err: |
| 5019 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 5020 | return -1; |
| 5021 | } |
| 5022 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5023 | /* Initialize a QUIC connection (quic_conn struct) to be attached to <conn> |
| 5024 | * connection with <xprt_ctx> as address of the xprt context. |
| 5025 | * Returns 1 if succeeded, 0 if not. |
| 5026 | */ |
| 5027 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 5028 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5029 | struct ssl_sock_ctx *ctx; |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5030 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5031 | |
| 5032 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 5033 | |
| 5034 | if (*xprt_ctx) |
| 5035 | goto out; |
| 5036 | |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5037 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5038 | if (!ctx) { |
| 5039 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 5040 | goto err; |
| 5041 | } |
| 5042 | |
| 5043 | ctx->wait_event.tasklet = tasklet_new(); |
| 5044 | if (!ctx->wait_event.tasklet) { |
| 5045 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 5046 | goto err; |
| 5047 | } |
| 5048 | |
| 5049 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 5050 | ctx->wait_event.tasklet->context = ctx; |
| 5051 | ctx->wait_event.events = 0; |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5052 | HA_ATOMIC_STORE(&ctx->conn, conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5053 | ctx->subs = NULL; |
| 5054 | ctx->xprt_ctx = NULL; |
| 5055 | |
| 5056 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 5057 | if (objt_server(conn->target)) { |
| 5058 | /* Server */ |
| 5059 | struct server *srv = __objt_server(conn->target); |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 5060 | unsigned char dcid[QUIC_HAP_CID_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5061 | int ssl_err, ipv4; |
| 5062 | |
| 5063 | ssl_err = SSL_ERROR_NONE; |
| 5064 | if (RAND_bytes(dcid, sizeof dcid) != 1) |
| 5065 | goto err; |
| 5066 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5067 | ipv4 = conn->dst->ss_family == AF_INET; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5068 | qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5069 | dcid, sizeof dcid, 0, NULL, 0, 0, srv); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5070 | if (qc == NULL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5071 | goto err; |
| 5072 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 5073 | ctx->qc = qc; |
| 5074 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5075 | /* Insert our SCID, the connection ID for the QUIC client. */ |
| 5076 | ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len); |
| 5077 | |
| 5078 | conn->qc = qc; |
| 5079 | qc->conn = conn; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 5080 | if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1, |
| 5081 | dcid, sizeof dcid, 0)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5082 | goto err; |
| 5083 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5084 | qc->rx.params = srv->quic_params; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5085 | /* Copy the initial source connection ID. */ |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5086 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 5087 | qc->enc_params_len = |
| 5088 | quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params, |
| 5089 | &qc->rx.params, 0); |
| 5090 | if (!qc->enc_params_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5091 | goto err; |
| 5092 | |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 5093 | if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl, |
| 5094 | qc->enc_params, qc->enc_params_len) == -1) |
| 5095 | goto err; |
| 5096 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5097 | 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] | 5098 | SSL_set_connect_state(ctx->ssl); |
| 5099 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 5100 | if (ssl_err != 1) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 5101 | int st; |
| 5102 | |
| 5103 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5104 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 5105 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 5106 | TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5107 | } |
| 5108 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 5109 | TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5110 | goto err; |
| 5111 | } |
| 5112 | } |
| 5113 | } |
| 5114 | else if (objt_listener(conn->target)) { |
| 5115 | /* Listener */ |
| 5116 | struct bind_conf *bc = __objt_listener(conn->target)->bind_conf; |
| 5117 | |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5118 | qc = ctx->conn->qc; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 5119 | ctx->qc = qc; |
| 5120 | |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 5121 | 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] | 5122 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 5123 | qc->enc_params, qc->enc_params_len) == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5124 | goto err; |
| 5125 | |
Frédéric Lécaille | ad3c07a | 2021-12-14 19:23:43 +0100 | [diff] [blame] | 5126 | /* Enabling 0-RTT */ |
| 5127 | if (bc->ssl_conf.early_data) |
| 5128 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 5129 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5130 | SSL_set_accept_state(ctx->ssl); |
| 5131 | } |
| 5132 | |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5133 | HA_ATOMIC_STORE(xprt_ctx, ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5134 | |
| 5135 | /* Leave init state and start handshake */ |
| 5136 | 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] | 5137 | |
| 5138 | out: |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5139 | HA_ATOMIC_STORE(&qc->xprt_ctx, ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5140 | TRACE_LEAVE(QUIC_EV_CONN_NEW, conn); |
| 5141 | |
| 5142 | return 0; |
| 5143 | |
| 5144 | err: |
Willy Tarreau | 7deb28c | 2021-05-10 07:40:27 +0200 | [diff] [blame] | 5145 | if (ctx && ctx->wait_event.tasklet) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5146 | tasklet_free(ctx->wait_event.tasklet); |
| 5147 | pool_free(pool_head_quic_conn_ctx, ctx); |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 5148 | 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] | 5149 | return -1; |
| 5150 | } |
| 5151 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5152 | /* Start the QUIC transport layer */ |
| 5153 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 5154 | { |
| 5155 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5156 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5157 | |
| 5158 | qc = conn->qc; |
| 5159 | if (!quic_conn_init_timer(qc)) { |
| 5160 | TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn); |
| 5161 | return 0; |
| 5162 | } |
| 5163 | |
| 5164 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 5165 | return 1; |
| 5166 | } |
| 5167 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5168 | /* transport-layer operations for QUIC connections. */ |
| 5169 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 5170 | .close = quic_close, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5171 | .snd_buf = quic_conn_from_buf, |
| 5172 | .rcv_buf = quic_conn_to_buf, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5173 | .subscribe = quic_conn_subscribe, |
| 5174 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5175 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5176 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5177 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 5178 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 5179 | .get_alpn = ssl_sock_get_alpn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5180 | .name = "QUIC", |
| 5181 | }; |
| 5182 | |
| 5183 | __attribute__((constructor)) |
| 5184 | static void __quic_conn_init(void) |
| 5185 | { |
| 5186 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 5187 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 5188 | } |
| 5189 | |
| 5190 | __attribute__((destructor)) |
| 5191 | static void __quic_conn_deinit(void) |
| 5192 | { |
| 5193 | BIO_meth_free(ha_quic_meth); |
| 5194 | } |
| 5195 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5196 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 5197 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5198 | * 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] | 5199 | */ |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5200 | 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] | 5201 | struct sockaddr_storage *saddr, qpkt_read_func *func) |
| 5202 | { |
| 5203 | unsigned char *pos; |
| 5204 | const unsigned char *end; |
| 5205 | struct quic_dgram_ctx dgram_ctx = { |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5206 | .qc = NULL, |
| 5207 | .dcid.len = 0, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5208 | .owner = owner, |
| 5209 | }; |
| 5210 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5211 | pos = (unsigned char *)b_head(buf); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5212 | end = pos + len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5213 | do { |
| 5214 | int ret; |
| 5215 | struct quic_rx_packet *pkt; |
| 5216 | |
Willy Tarreau | e449893 | 2021-03-22 21:13:05 +0100 | [diff] [blame] | 5217 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5218 | if (!pkt) |
| 5219 | goto err; |
| 5220 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5221 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5222 | ret = func(pos, end, pkt, &dgram_ctx, saddr); |
| 5223 | pos += pkt->len; |
Frédéric Lécaille | 310d1bd | 2021-09-22 15:10:49 +0200 | [diff] [blame] | 5224 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5225 | if (ret == -1) |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 5226 | /* If the packet length could not be found, we cannot continue. */ |
| 5227 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5228 | } while (pos < end); |
| 5229 | |
| 5230 | /* Increasing the received bytes counter by the UDP datagram length |
| 5231 | * if this datagram could be associated to a connection. |
| 5232 | */ |
| 5233 | if (dgram_ctx.qc) |
| 5234 | dgram_ctx.qc->rx.bytes += len; |
| 5235 | |
| 5236 | return pos - (unsigned char *)buf; |
| 5237 | |
| 5238 | err: |
| 5239 | return -1; |
| 5240 | } |
| 5241 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5242 | 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] | 5243 | struct sockaddr_storage *saddr) |
| 5244 | { |
| 5245 | return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv); |
| 5246 | } |
| 5247 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5248 | /* Function to automatically activate QUIC traces on stdout. |
| 5249 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 5250 | * Main use for now is in the docker image for QUIC interop testing. |
| 5251 | */ |
| 5252 | static void quic_init_stdout_traces(void) |
| 5253 | { |
| 5254 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 5255 | trace_quic.sink = sink_find("stdout"); |
| 5256 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5257 | trace_quic.state = TRACE_STATE_RUNNING; |
| 5258 | #endif |
| 5259 | } |
| 5260 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 5261 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5262 | /* |
| 5263 | * Local variables: |
| 5264 | * c-indent-level: 8 |
| 5265 | * c-basic-offset: 8 |
| 5266 | * End: |
| 5267 | */ |