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