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