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