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