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