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