Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QUIC protocol implementation. Lower layer with internal features implemented |
| 3 | * here such as QUIC encryption, idle timeout, acknowledgement and |
| 4 | * retransmission. |
| 5 | * |
| 6 | * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <haproxy/quic_conn.h> |
| 16 | |
| 17 | #define _GNU_SOURCE |
| 18 | #include <errno.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | #include <netinet/tcp.h> |
| 27 | |
| 28 | #include <import/ebmbtree.h> |
| 29 | |
| 30 | #include <haproxy/buf-t.h> |
| 31 | #include <haproxy/compat.h> |
| 32 | #include <haproxy/api.h> |
| 33 | #include <haproxy/debug.h> |
| 34 | #include <haproxy/tools.h> |
| 35 | #include <haproxy/ticks.h> |
| 36 | |
| 37 | #include <haproxy/connection.h> |
| 38 | #include <haproxy/fd.h> |
| 39 | #include <haproxy/freq_ctr.h> |
| 40 | #include <haproxy/global.h> |
| 41 | #include <haproxy/h3.h> |
| 42 | #include <haproxy/hq_interop.h> |
| 43 | #include <haproxy/log.h> |
| 44 | #include <haproxy/mux_quic.h> |
| 45 | #include <haproxy/ncbuf.h> |
| 46 | #include <haproxy/pipe.h> |
| 47 | #include <haproxy/proxy.h> |
| 48 | #include <haproxy/quic_cc.h> |
| 49 | #include <haproxy/quic_frame.h> |
| 50 | #include <haproxy/quic_enc.h> |
| 51 | #include <haproxy/quic_loss.h> |
| 52 | #include <haproxy/quic_sock.h> |
| 53 | #include <haproxy/quic_stats.h> |
| 54 | #include <haproxy/quic_stream.h> |
| 55 | #include <haproxy/quic_tp.h> |
| 56 | #include <haproxy/cbuf.h> |
| 57 | #include <haproxy/proto_quic.h> |
| 58 | #include <haproxy/quic_tls.h> |
| 59 | #include <haproxy/ssl_sock.h> |
| 60 | #include <haproxy/task.h> |
| 61 | #include <haproxy/trace.h> |
| 62 | |
| 63 | /* list of supported QUIC versions by this implementation */ |
| 64 | const struct quic_version quic_versions[] = { |
| 65 | { |
| 66 | .num = QUIC_PROTOCOL_VERSION_DRAFT_29, |
| 67 | .initial_salt = initial_salt_draft_29, |
| 68 | .initial_salt_len = sizeof initial_salt_draft_29, |
| 69 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
| 70 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
| 71 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
| 72 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
| 73 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
| 74 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
| 75 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
| 76 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
| 77 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT, |
| 78 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT, |
| 79 | }, |
| 80 | { |
| 81 | .num = QUIC_PROTOCOL_VERSION_1, |
| 82 | .initial_salt = initial_salt_v1, |
| 83 | .initial_salt_len = sizeof initial_salt_v1, |
| 84 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
| 85 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
| 86 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
| 87 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
| 88 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
| 89 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
| 90 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
| 91 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
| 92 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1, |
| 93 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1, |
| 94 | }, |
| 95 | { |
Frédéric Lécaille | 21c4c9b | 2023-01-13 16:37:02 +0100 | [diff] [blame] | 96 | .num = QUIC_PROTOCOL_VERSION_2, |
| 97 | .initial_salt = initial_salt_v2, |
| 98 | .initial_salt_len = sizeof initial_salt_v2, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 99 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2, |
| 100 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1, |
| 101 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2, |
| 102 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1, |
| 103 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2, |
| 104 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1, |
| 105 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2, |
| 106 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V2) - 1, |
Frédéric Lécaille | 21c4c9b | 2023-01-13 16:37:02 +0100 | [diff] [blame] | 107 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2, |
| 108 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 109 | }, |
| 110 | }; |
| 111 | |
| 112 | /* The total number of supported versions */ |
| 113 | const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions; |
| 114 | /* Listener only preferred version */ |
| 115 | const struct quic_version *preferred_version; |
| 116 | |
| 117 | /* trace source and events */ |
| 118 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 119 | const struct trace_source *src, |
| 120 | const struct ist where, const struct ist func, |
| 121 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 122 | |
| 123 | static const struct trace_event quic_trace_events[] = { |
| 124 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 125 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 126 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 127 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 128 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 129 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 130 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
| 131 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
| 132 | { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" }, |
| 133 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 134 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 135 | { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" }, |
| 136 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 137 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 138 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 139 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 140 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 141 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 142 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 143 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 144 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 145 | { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" }, |
| 146 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 147 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 148 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 149 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 150 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
| 151 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 152 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 153 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 154 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 155 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 156 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
| 157 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
| 158 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 159 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
| 160 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 161 | { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." }, |
| 162 | { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."}, |
| 163 | { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"}, |
| 164 | { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"}, |
| 165 | { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"}, |
| 166 | { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"}, |
| 167 | { .mask = QUIC_EV_CONN_SUB, .name = "xprt_sub", .desc = "RX/TX subcription or unsubscription to QUIC xprt"}, |
Amaury Denoyelle | 5b41486 | 2022-10-24 17:40:37 +0200 | [diff] [blame] | 168 | { .mask = QUIC_EV_CONN_RCV, .name = "conn_recv", .desc = "RX on connection" }, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 169 | { /* end */ } |
| 170 | }; |
| 171 | |
| 172 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 173 | /* arg1 */ { /* already used by the connection */ }, |
| 174 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 175 | /* arg3 */ { }, |
| 176 | /* arg4 */ { } |
| 177 | }; |
| 178 | |
| 179 | static const struct name_desc quic_trace_decoding[] = { |
| 180 | #define QUIC_VERB_CLEAN 1 |
| 181 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 182 | { /* end */ } |
| 183 | }; |
| 184 | |
| 185 | |
| 186 | struct trace_source trace_quic = { |
| 187 | .name = IST("quic"), |
| 188 | .desc = "QUIC xprt", |
| 189 | .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */ |
| 190 | .default_cb = quic_trace, |
| 191 | .known_events = quic_trace_events, |
| 192 | .lockon_args = quic_trace_lockon_args, |
| 193 | .decoding = quic_trace_decoding, |
| 194 | .report_events = ~0, /* report everything by default */ |
| 195 | }; |
| 196 | |
| 197 | #define TRACE_SOURCE &trace_quic |
| 198 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 199 | |
| 200 | static BIO_METHOD *ha_quic_meth; |
| 201 | |
| 202 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ); |
| 203 | DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ); |
| 204 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
| 205 | "quic_conn_ctx", sizeof(struct ssl_sock_ctx)); |
| 206 | DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn)); |
| 207 | DECLARE_POOL(pool_head_quic_connection_id, |
| 208 | "quic_connnection_id", sizeof(struct quic_connection_id)); |
| 209 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
| 210 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet)); |
| 211 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet)); |
| 212 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm)); |
| 213 | DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf", sizeof(struct quic_crypto_buf)); |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 214 | DECLARE_STATIC_POOL(pool_head_quic_cstream, "quic_cstream", sizeof(struct quic_cstream)); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 215 | DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame)); |
| 216 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node)); |
| 217 | |
| 218 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
| 219 | struct quic_enc_level *qel, struct quic_tls_ctx *ctx, |
| 220 | struct list *frms, struct quic_conn *qc, |
| 221 | const struct quic_version *ver, size_t dglen, int pkt_type, |
| 222 | int force_ack, int padding, int probe, int cc, int *err); |
| 223 | struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state); |
| 224 | static void qc_idle_timer_do_rearm(struct quic_conn *qc); |
| 225 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read); |
| 226 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc); |
| 227 | static int quic_conn_init_timer(struct quic_conn *qc); |
| 228 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc); |
| 229 | |
| 230 | /* Only for debug purpose */ |
| 231 | struct enc_debug_info { |
| 232 | unsigned char *payload; |
| 233 | size_t payload_len; |
| 234 | unsigned char *aad; |
| 235 | size_t aad_len; |
| 236 | uint64_t pn; |
| 237 | }; |
| 238 | |
| 239 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 240 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 241 | unsigned char *payload, size_t payload_len, |
| 242 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 243 | { |
| 244 | edi->payload = payload; |
| 245 | edi->payload_len = payload_len; |
| 246 | edi->aad = aad; |
| 247 | edi->aad_len = aad_len; |
| 248 | edi->pn = pn; |
| 249 | } |
| 250 | |
| 251 | /* Trace callback for QUIC. |
| 252 | * These traces always expect that arg1, if non-null, is of type connection. |
| 253 | */ |
| 254 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 255 | const struct ist where, const struct ist func, |
| 256 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 257 | { |
| 258 | const struct quic_conn *qc = a1; |
| 259 | |
| 260 | if (qc) { |
| 261 | const struct quic_tls_ctx *tls_ctx; |
| 262 | |
| 263 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
| 264 | if (mask & QUIC_EV_CONN_INIT) { |
| 265 | chunk_appendf(&trace_buf, "\n odcid"); |
| 266 | quic_cid_dump(&trace_buf, &qc->odcid); |
| 267 | chunk_appendf(&trace_buf, "\n dcid"); |
| 268 | quic_cid_dump(&trace_buf, &qc->dcid); |
| 269 | chunk_appendf(&trace_buf, "\n scid"); |
| 270 | quic_cid_dump(&trace_buf, &qc->scid); |
| 271 | } |
| 272 | |
| 273 | if (mask & QUIC_EV_TRANSP_PARAMS) { |
| 274 | const struct quic_transport_params *p = a2; |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame^] | 275 | |
| 276 | if (p) |
| 277 | quic_transport_params_dump(&trace_buf, qc, p); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 281 | const enum ssl_encryption_level_t *level = a2; |
| 282 | const size_t *len = a3; |
| 283 | |
| 284 | if (level) { |
| 285 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 286 | |
| 287 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 288 | } |
| 289 | if (len) |
| 290 | chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len); |
| 291 | } |
| 292 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 293 | /* Initial read & write secrets. */ |
| 294 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 295 | const unsigned char *rx_sec = a2; |
| 296 | const unsigned char *tx_sec = a3; |
| 297 | |
| 298 | tls_ctx = &qc->els[level].tls_ctx; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 299 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 300 | if (rx_sec) |
| 301 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 302 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
| 303 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 304 | if (tx_sec) |
| 305 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 306 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 307 | } |
| 308 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 309 | const enum ssl_encryption_level_t *level = a2; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 310 | |
| 311 | if (level) { |
| 312 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 313 | |
| 314 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 315 | if (quic_tls_has_rx_sec(&qc->els[lvl])) { |
| 316 | tls_ctx = &qc->els[lvl].tls_ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 317 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 318 | } |
| 319 | else |
| 320 | chunk_appendf(&trace_buf, " (none)"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| 324 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 325 | const enum ssl_encryption_level_t *level = a2; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 326 | |
| 327 | if (level) { |
| 328 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 329 | |
| 330 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 331 | if (quic_tls_has_tx_sec(&qc->els[lvl])) { |
| 332 | tls_ctx = &qc->els[lvl].tls_ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 333 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 334 | } |
| 335 | else |
| 336 | chunk_appendf(&trace_buf, " (none)"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | } |
| 340 | |
| 341 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 342 | const struct list *l = a2; |
| 343 | |
| 344 | if (l) { |
| 345 | const struct quic_frame *frm; |
| 346 | list_for_each_entry(frm, l, list) { |
| 347 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 348 | chunk_frm_appendf(&trace_buf, frm); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) { |
| 354 | const struct quic_tx_packet *pkt = a2; |
| 355 | const struct quic_enc_level *qel = a3; |
| 356 | const ssize_t *room = a4; |
| 357 | |
| 358 | if (qel) { |
| 359 | const struct quic_pktns *pktns = qel->pktns; |
| 360 | chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu " |
| 361 | "if=%llu pp=%u", |
| 362 | quic_enc_level_char_from_qel(qel, qc), |
| 363 | (unsigned long long)qc->path->cwnd, |
| 364 | (unsigned long long)qc->path->prep_in_flight, |
| 365 | (unsigned long long)qc->path->in_flight, |
| 366 | (unsigned long long)pktns->tx.in_flight, |
| 367 | pktns->tx.pto_probe); |
| 368 | } |
| 369 | if (pkt) { |
| 370 | const struct quic_frame *frm; |
| 371 | if (pkt->pn_node.key != (uint64_t)-1) |
| 372 | chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key); |
| 373 | list_for_each_entry(frm, &pkt->frms, list) { |
| 374 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 375 | chunk_frm_appendf(&trace_buf, frm); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if (room) { |
| 380 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 381 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 382 | (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | if (mask & QUIC_EV_CONN_IO_CB) { |
| 387 | const enum quic_handshake_state *state = a2; |
| 388 | const int *err = a3; |
| 389 | |
| 390 | if (state) |
| 391 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 392 | if (err) |
| 393 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 394 | } |
| 395 | |
| 396 | if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) { |
| 397 | const struct quic_rx_packet *pkt = a2; |
| 398 | const unsigned long *pktlen = a3; |
| 399 | const SSL *ssl = a4; |
| 400 | |
| 401 | if (pkt) { |
| 402 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 403 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 404 | chunk_appendf(&trace_buf, " kp=%d", |
| 405 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 406 | chunk_appendf(&trace_buf, " el=%c", |
| 407 | quic_packet_type_enc_level_char(pkt->type)); |
| 408 | if (pkt->pnl) |
| 409 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 410 | (unsigned long long)pkt->pn); |
| 411 | if (pkt->token_len) |
| 412 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 413 | (unsigned long long)pkt->token_len); |
| 414 | if (pkt->aad_len) |
| 415 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 416 | (unsigned long long)pkt->aad_len); |
| 417 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 418 | pkt->flags, (unsigned long long)pkt->len); |
| 419 | } |
| 420 | if (pktlen) |
| 421 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 422 | if (ssl) { |
| 423 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 424 | chunk_appendf(&trace_buf, " el=%c", |
| 425 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 430 | const struct quic_rx_packet *pkt = a2; |
| 431 | const struct quic_rx_crypto_frm *cf = a3; |
| 432 | const SSL *ssl = a4; |
| 433 | |
| 434 | if (pkt) |
| 435 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 436 | quic_packet_type_enc_level_char(pkt->type), |
| 437 | (unsigned long long)pkt->pn); |
| 438 | if (cf) |
| 439 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 440 | (unsigned long long)cf->offset_node.key, |
| 441 | (unsigned long long)cf->len); |
| 442 | if (ssl) { |
| 443 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 444 | chunk_appendf(&trace_buf, " rel=%c", |
| 445 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 446 | } |
| 447 | |
| 448 | if (qc->err.code) |
| 449 | chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code); |
| 450 | } |
| 451 | |
| 452 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 453 | const struct quic_frame *frm = a2; |
| 454 | |
| 455 | if (frm) |
| 456 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 457 | } |
| 458 | |
| 459 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 460 | const struct quic_enc_level *qel = a2; |
| 461 | |
| 462 | if (qel) { |
| 463 | const struct quic_pktns *pktns = qel->pktns; |
| 464 | chunk_appendf(&trace_buf, |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 465 | " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u off=%llu", |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 466 | quic_enc_level_char_from_qel(qel, qc), |
| 467 | quic_hdshk_state_str(qc->state), |
| 468 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
| 469 | (unsigned long long)qc->path->cwnd, |
| 470 | (unsigned long long)qc->path->prep_in_flight, |
| 471 | (unsigned long long)qc->path->in_flight, |
| 472 | (unsigned long long)pktns->tx.in_flight, |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 473 | pktns->tx.pto_probe, |
| 474 | qel->cstream ? (unsigned long long)qel->cstream->rx.offset : 0); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
| 478 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 479 | const struct enc_debug_info *edi = a2; |
| 480 | |
| 481 | if (edi) |
| 482 | chunk_appendf(&trace_buf, |
| 483 | " payload=@%p payload_len=%llu" |
| 484 | " aad=@%p aad_len=%llu pn=%llu", |
| 485 | edi->payload, (unsigned long long)edi->payload_len, |
| 486 | edi->aad, (unsigned long long)edi->aad_len, |
| 487 | (unsigned long long)edi->pn); |
| 488 | } |
| 489 | |
| 490 | if (mask & QUIC_EV_CONN_RMHP) { |
| 491 | const struct quic_rx_packet *pkt = a2; |
| 492 | |
| 493 | if (pkt) { |
| 494 | const int *ret = a3; |
| 495 | |
| 496 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 497 | if (ret && *ret) |
| 498 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 499 | pkt->pnl, (unsigned long long)pkt->pn); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
| 504 | const struct quic_frame *frm = a2; |
| 505 | const unsigned long *val1 = a3; |
| 506 | const unsigned long *val2 = a4; |
| 507 | |
| 508 | if (frm) { |
| 509 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 510 | chunk_frm_appendf(&trace_buf, frm); |
| 511 | } |
| 512 | if (val1) |
| 513 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 514 | if (val2) |
| 515 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 516 | } |
| 517 | |
| 518 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 519 | const struct quic_stream *s = a2; |
| 520 | const struct qc_stream_desc *stream = a3; |
| 521 | |
| 522 | if (s) |
| 523 | chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len); |
| 524 | if (stream) |
| 525 | chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset); |
| 526 | } |
| 527 | |
| 528 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 529 | const unsigned int *rtt_sample = a2; |
| 530 | const unsigned int *ack_delay = a3; |
| 531 | const struct quic_loss *ql = a4; |
| 532 | |
| 533 | if (rtt_sample) |
| 534 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 535 | if (ack_delay) |
| 536 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 537 | if (ql) |
| 538 | chunk_appendf(&trace_buf, |
| 539 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 540 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 541 | } |
| 542 | if (mask & QUIC_EV_CONN_CC) { |
| 543 | const struct quic_cc_event *ev = a2; |
| 544 | const struct quic_cc *cc = a3; |
| 545 | |
| 546 | if (a2) |
| 547 | quic_cc_event_trace(&trace_buf, ev); |
| 548 | if (a3) |
| 549 | quic_cc_state_trace(&trace_buf, cc); |
| 550 | } |
| 551 | |
| 552 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 553 | const struct quic_pktns *pktns = a2; |
| 554 | const struct list *lost_pkts = a3; |
| 555 | |
| 556 | if (pktns) { |
| 557 | chunk_appendf(&trace_buf, " pktns=%s", |
| 558 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 559 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 560 | if (pktns->tx.loss_time) |
| 561 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 562 | TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time))); |
| 563 | } |
| 564 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 565 | struct quic_tx_packet *pkt; |
| 566 | |
| 567 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 568 | list_for_each_entry(pkt, lost_pkts, list) |
| 569 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) { |
| 574 | const struct quic_pktns *pktns = a2; |
| 575 | const int *duration = a3; |
| 576 | const uint64_t *ifae_pkts = a4; |
| 577 | |
| 578 | if (ifae_pkts) |
| 579 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 580 | (unsigned long long)*ifae_pkts); |
| 581 | if (pktns) { |
| 582 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
| 583 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 584 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 585 | pktns->tx.pto_probe); |
| 586 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 587 | if (pktns->tx.in_flight) |
| 588 | chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight); |
| 589 | if (pktns->tx.loss_time) |
| 590 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 591 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 592 | } |
| 593 | if (mask & QUIC_EV_CONN_SPTO) { |
| 594 | if (pktns->tx.time_of_last_eliciting) |
| 595 | chunk_appendf(&trace_buf, " tole=%dms", |
| 596 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 597 | if (duration) |
| 598 | chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration)); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) { |
| 603 | chunk_appendf(&trace_buf, |
| 604 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 609 | const struct quic_tx_packet *pkt = a2; |
| 610 | |
| 611 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 612 | (unsigned long long)qc->path->cwnd, |
| 613 | (unsigned long long)qc->path->prep_in_flight, |
| 614 | (unsigned long long)qc->path->in_flight); |
| 615 | if (pkt) { |
| 616 | const struct quic_frame *frm; |
| 617 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
| 618 | (unsigned long)pkt->pn_node.key, |
| 619 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 620 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 621 | (unsigned long long)pkt->in_flight_len); |
| 622 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 623 | (unsigned long long)qc->rx.bytes, |
| 624 | (unsigned long long)qc->tx.bytes); |
| 625 | list_for_each_entry(frm, &pkt->frms, list) { |
| 626 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 627 | chunk_frm_appendf(&trace_buf, frm); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 633 | const uint8_t *alert = a2; |
| 634 | const enum ssl_encryption_level_t *level = a3; |
| 635 | |
| 636 | if (alert) |
| 637 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 638 | if (level) |
| 639 | chunk_appendf(&trace_buf, " el=%c", |
| 640 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 641 | } |
| 642 | |
| 643 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 644 | const size_t *sz1 = a2; |
| 645 | const size_t *sz2 = a3; |
| 646 | const size_t *sz3 = a4; |
| 647 | |
| 648 | if (sz1) |
| 649 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 650 | if (sz2) |
| 651 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 652 | if (sz3) |
| 653 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 654 | } |
| 655 | |
| 656 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 657 | const struct quic_frame *frm = a2; |
| 658 | |
| 659 | if (frm) { |
| 660 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 661 | chunk_frm_appendf(&trace_buf, frm); |
| 662 | } |
| 663 | } |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 664 | |
| 665 | if (mask & QUIC_EV_CONN_ELEVELSEL) { |
| 666 | const enum quic_handshake_state *state = a2; |
| 667 | const enum quic_tls_enc_level *level = a3; |
| 668 | const enum quic_tls_enc_level *next_level = a4; |
| 669 | |
| 670 | if (state) |
| 671 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(qc->state)); |
| 672 | if (level) |
| 673 | chunk_appendf(&trace_buf, " level=%c", quic_enc_level_char(*level)); |
| 674 | if (next_level) |
| 675 | chunk_appendf(&trace_buf, " next_level=%c", quic_enc_level_char(*next_level)); |
| 676 | |
| 677 | } |
Amaury Denoyelle | 5b41486 | 2022-10-24 17:40:37 +0200 | [diff] [blame] | 678 | |
| 679 | if (mask & QUIC_EV_CONN_RCV) { |
| 680 | const struct quic_dgram *dgram = a2; |
| 681 | |
| 682 | if (dgram) |
| 683 | chunk_appendf(&trace_buf, " dgram.len=%zu", dgram->len); |
| 684 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 685 | } |
| 686 | if (mask & QUIC_EV_CONN_LPKT) { |
| 687 | const struct quic_rx_packet *pkt = a2; |
| 688 | const uint64_t *len = a3; |
| 689 | const struct quic_version *ver = a4; |
| 690 | |
| 691 | if (pkt) { |
| 692 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 693 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 694 | if (pkt->pn_node.key != (uint64_t)-1) |
| 695 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 696 | } |
| 697 | |
| 698 | if (len) |
| 699 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
| 700 | |
| 701 | if (ver) |
| 702 | chunk_appendf(&trace_buf, " ver=0x%08x", ver->num); |
| 703 | } |
| 704 | |
| 705 | if (mask & QUIC_EV_STATELESS_RST) { |
| 706 | const struct quic_cid *cid = a2; |
| 707 | |
| 708 | if (cid) |
| 709 | quic_cid_dump(&trace_buf, cid); |
| 710 | } |
| 711 | |
| 712 | } |
| 713 | |
| 714 | /* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */ |
| 715 | static inline int quic_peer_validated_addr(struct quic_conn *qc) |
| 716 | { |
| 717 | struct quic_pktns *hdshk_pktns, *app_pktns; |
| 718 | |
| 719 | if (!qc_is_listener(qc)) |
| 720 | return 1; |
| 721 | |
| 722 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 723 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 724 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 725 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 726 | qc->state >= QUIC_HS_ST_COMPLETE) |
| 727 | return 1; |
| 728 | |
| 729 | return 0; |
| 730 | } |
| 731 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame^] | 732 | /* To be called to kill a connection as soon as possible (without sending any packet). */ |
| 733 | void qc_kill_conn(struct quic_conn *qc) |
| 734 | { |
| 735 | qc->flags |= QUIC_FL_CONN_TO_KILL; |
| 736 | task_wakeup(qc->idle_timer_task, TASK_WOKEN_OTHER); |
| 737 | } |
| 738 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 739 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 740 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 741 | */ |
| 742 | static inline void qc_set_timer(struct quic_conn *qc) |
| 743 | { |
| 744 | struct quic_pktns *pktns; |
| 745 | unsigned int pto; |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 746 | int handshake_confirmed; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 747 | |
| 748 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
| 749 | NULL, NULL, &qc->path->ifae_pkts); |
| 750 | |
| 751 | pktns = quic_loss_pktns(qc); |
| 752 | if (tick_isset(pktns->tx.loss_time)) { |
| 753 | qc->timer = pktns->tx.loss_time; |
| 754 | goto out; |
| 755 | } |
| 756 | |
| 757 | /* anti-amplification: the timer must be |
| 758 | * cancelled for a server which reached the anti-amplification limit. |
| 759 | */ |
| 760 | if (!quic_peer_validated_addr(qc) && |
| 761 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
| 762 | TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc); |
| 763 | qc->timer = TICK_ETERNITY; |
| 764 | goto out; |
| 765 | } |
| 766 | |
| 767 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
| 768 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
| 769 | /* Timer cancellation. */ |
| 770 | qc->timer = TICK_ETERNITY; |
| 771 | goto out; |
| 772 | } |
| 773 | |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 774 | handshake_confirmed = qc->state >= QUIC_HS_ST_CONFIRMED; |
| 775 | pktns = quic_pto_pktns(qc, handshake_confirmed, &pto); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 776 | if (tick_isset(pto)) |
| 777 | qc->timer = pto; |
| 778 | out: |
| 779 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
| 780 | if (tick_is_expired(qc->timer, now_ms)) { |
| 781 | TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc); |
| 782 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 783 | } |
| 784 | else { |
| 785 | TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc); |
| 786 | task_schedule(qc->timer_task, qc->timer); |
| 787 | } |
| 788 | } |
| 789 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
| 790 | } |
| 791 | |
| 792 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 793 | * connection. |
| 794 | * Return 1 if succeeded, 0 if not. |
| 795 | */ |
| 796 | static int quic_tls_key_update(struct quic_conn *qc) |
| 797 | { |
| 798 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 799 | struct quic_tls_secrets *rx, *tx; |
| 800 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 801 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 802 | const struct quic_version *ver = |
| 803 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
| 804 | int ret = 0; |
| 805 | |
| 806 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
| 807 | |
| 808 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 809 | rx = &tls_ctx->rx; |
| 810 | tx = &tls_ctx->tx; |
| 811 | nxt_rx = &qc->ku.nxt_rx; |
| 812 | nxt_tx = &qc->ku.nxt_tx; |
| 813 | |
| 814 | /* Prepare new RX secrets */ |
| 815 | if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen, |
| 816 | rx->secret, rx->secretlen)) { |
| 817 | TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
| 818 | goto leave; |
| 819 | } |
| 820 | |
| 821 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver, |
| 822 | nxt_rx->key, nxt_rx->keylen, |
| 823 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 824 | nxt_rx->secret, nxt_rx->secretlen)) { |
| 825 | TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 826 | goto leave; |
| 827 | } |
| 828 | |
| 829 | /* Prepare new TX secrets */ |
| 830 | if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen, |
| 831 | tx->secret, tx->secretlen)) { |
| 832 | TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
| 833 | goto leave; |
| 834 | } |
| 835 | |
| 836 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver, |
| 837 | nxt_tx->key, nxt_tx->keylen, |
| 838 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 839 | nxt_tx->secret, nxt_tx->secretlen)) { |
| 840 | TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 841 | goto leave; |
| 842 | } |
| 843 | |
| 844 | if (nxt_rx->ctx) { |
| 845 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 846 | nxt_rx->ctx = NULL; |
| 847 | } |
| 848 | |
| 849 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
| 850 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 851 | goto leave; |
| 852 | } |
| 853 | |
| 854 | if (nxt_tx->ctx) { |
| 855 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 856 | nxt_tx->ctx = NULL; |
| 857 | } |
| 858 | |
| 859 | if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
| 860 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 861 | goto leave; |
| 862 | } |
| 863 | |
| 864 | ret = 1; |
| 865 | leave: |
| 866 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc); |
| 867 | return ret; |
| 868 | } |
| 869 | |
| 870 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 871 | * Must be used after having updated them. |
| 872 | * Always succeeds. |
| 873 | */ |
| 874 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 875 | { |
| 876 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 877 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 878 | EVP_CIPHER_CTX *curr_ctx; |
| 879 | |
| 880 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 881 | |
| 882 | /* Rotate the RX secrets */ |
| 883 | curr_ctx = tls_ctx->rx.ctx; |
| 884 | curr_secret = tls_ctx->rx.secret; |
| 885 | curr_iv = tls_ctx->rx.iv; |
| 886 | curr_key = tls_ctx->rx.key; |
| 887 | |
| 888 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
| 889 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 890 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 891 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 892 | |
| 893 | qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx; |
| 894 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 895 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 896 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 897 | |
| 898 | qc->ku.prv_rx.ctx = curr_ctx; |
| 899 | qc->ku.prv_rx.secret = curr_secret; |
| 900 | qc->ku.prv_rx.iv = curr_iv; |
| 901 | qc->ku.prv_rx.key = curr_key; |
| 902 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 903 | |
| 904 | /* Update the TX secrets */ |
| 905 | curr_ctx = tls_ctx->tx.ctx; |
| 906 | curr_secret = tls_ctx->tx.secret; |
| 907 | curr_iv = tls_ctx->tx.iv; |
| 908 | curr_key = tls_ctx->tx.key; |
| 909 | |
| 910 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
| 911 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 912 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 913 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 914 | |
| 915 | qc->ku.nxt_tx.ctx = curr_ctx; |
| 916 | qc->ku.nxt_tx.secret = curr_secret; |
| 917 | qc->ku.nxt_tx.iv = curr_iv; |
| 918 | qc->ku.nxt_tx.key = curr_key; |
| 919 | |
| 920 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 921 | } |
| 922 | |
| 923 | /* returns 0 on error, 1 on success */ |
| 924 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 925 | const uint8_t *read_secret, |
| 926 | const uint8_t *write_secret, size_t secret_len) |
| 927 | { |
| 928 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 929 | struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 930 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 931 | struct quic_tls_secrets *rx = NULL, *tx = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 932 | const struct quic_version *ver = |
| 933 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
| 934 | int ret = 0; |
| 935 | |
| 936 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
| 937 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame^] | 938 | |
| 939 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 940 | TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc); |
| 941 | goto out; |
| 942 | } |
| 943 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 944 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 945 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 946 | goto out; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 947 | } |
| 948 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 949 | if (!read_secret) |
| 950 | goto write; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 951 | |
| 952 | rx = &tls_ctx->rx; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 953 | if (!quic_tls_secrets_keys_alloc(rx)) { |
| 954 | TRACE_ERROR("RX keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 955 | goto leave; |
| 956 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 957 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 958 | rx->aead = tls_aead(cipher); |
| 959 | rx->md = tls_md(cipher); |
| 960 | rx->hp = tls_hp(cipher); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 961 | |
| 962 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen, |
| 963 | rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key, |
| 964 | read_secret, secret_len)) { |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 965 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 966 | goto leave; |
| 967 | } |
| 968 | |
| 969 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 970 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 971 | goto leave; |
| 972 | } |
| 973 | |
| 974 | if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) { |
| 975 | TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 976 | goto leave; |
| 977 | } |
| 978 | |
| 979 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 980 | * listener. Note that a listener derives only RX secrets for this |
| 981 | * level. |
| 982 | */ |
| 983 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) { |
| 984 | TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc); |
| 985 | quic_accept_push_qc(qc); |
| 986 | } |
| 987 | |
| 988 | write: |
| 989 | |
| 990 | if (!write_secret) |
| 991 | goto out; |
| 992 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 993 | tx = &tls_ctx->tx; |
| 994 | if (!quic_tls_secrets_keys_alloc(tx)) { |
| 995 | TRACE_ERROR("TX keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 996 | goto leave; |
| 997 | } |
| 998 | |
| 999 | tx->aead = tls_aead(cipher); |
| 1000 | tx->md = tls_md(cipher); |
| 1001 | tx->hp = tls_hp(cipher); |
| 1002 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1003 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen, |
| 1004 | tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key, |
| 1005 | write_secret, secret_len)) { |
| 1006 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 1007 | goto leave; |
| 1008 | } |
| 1009 | |
| 1010 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 1011 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 1012 | goto leave; |
| 1013 | } |
| 1014 | |
| 1015 | if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) { |
| 1016 | TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 1017 | goto leave; |
| 1018 | } |
| 1019 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 1020 | if (level == ssl_encryption_handshake && qc_is_listener(qc)) { |
| 1021 | qc->enc_params_len = |
| 1022 | quic_transport_params_encode(qc->enc_params, |
| 1023 | qc->enc_params + sizeof qc->enc_params, |
| 1024 | &qc->rx.params, ver, 1); |
| 1025 | if (!qc->enc_params_len) { |
| 1026 | TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_RWSEC); |
| 1027 | goto leave; |
| 1028 | } |
| 1029 | |
| 1030 | if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) { |
| 1031 | TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_RWSEC); |
| 1032 | goto leave; |
| 1033 | } |
| 1034 | } |
| 1035 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1036 | if (level == ssl_encryption_application) { |
| 1037 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 1038 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 1039 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 1040 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1041 | if (rx) { |
| 1042 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
| 1043 | TRACE_ERROR("Could not allocate RX Application secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 1044 | goto leave; |
| 1045 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1046 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1047 | memcpy(rx->secret, read_secret, secret_len); |
| 1048 | rx->secretlen = secret_len; |
| 1049 | } |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1050 | |
| 1051 | if (tx) { |
| 1052 | if (!(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
| 1053 | TRACE_ERROR("Could not allocate TX Application secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 1054 | goto leave; |
| 1055 | } |
| 1056 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1057 | memcpy(tx->secret, write_secret, secret_len); |
| 1058 | tx->secretlen = secret_len; |
| 1059 | } |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1060 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1061 | /* Initialize all the secret keys lengths */ |
| 1062 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | out: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1066 | ret = 1; |
| 1067 | leave: |
| 1068 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
| 1072 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 1073 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 1074 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 1075 | * It fails (returns 0) only if it could not managed to allocate enough CRYPTO |
| 1076 | * buffers to store all the data. |
| 1077 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 1078 | */ |
| 1079 | static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel, |
| 1080 | const unsigned char *data, size_t len) |
| 1081 | { |
| 1082 | struct quic_crypto_buf **qcb; |
| 1083 | /* The remaining byte to store in CRYPTO buffers. */ |
| 1084 | size_t cf_offset, cf_len, *nb_buf; |
| 1085 | unsigned char *pos; |
| 1086 | int ret = 0; |
| 1087 | |
| 1088 | nb_buf = &qel->tx.crypto.nb_buf; |
| 1089 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 1090 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 1091 | cf_len = len; |
| 1092 | |
| 1093 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1094 | |
| 1095 | while (len) { |
| 1096 | size_t to_copy, room; |
| 1097 | |
| 1098 | pos = (*qcb)->data + (*qcb)->sz; |
| 1099 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1100 | to_copy = len > room ? room : len; |
| 1101 | if (to_copy) { |
| 1102 | memcpy(pos, data, to_copy); |
| 1103 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1104 | qel->tx.crypto.sz += to_copy; |
| 1105 | (*qcb)->sz += to_copy; |
| 1106 | len -= to_copy; |
| 1107 | data += to_copy; |
| 1108 | } |
| 1109 | else { |
| 1110 | struct quic_crypto_buf **tmp; |
| 1111 | |
| 1112 | // FIXME: realloc! |
| 1113 | tmp = realloc(qel->tx.crypto.bufs, |
| 1114 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1115 | if (tmp) { |
| 1116 | qel->tx.crypto.bufs = tmp; |
| 1117 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1118 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1119 | if (!*qcb) { |
| 1120 | TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc); |
| 1121 | goto leave; |
| 1122 | } |
| 1123 | |
| 1124 | (*qcb)->sz = 0; |
| 1125 | ++*nb_buf; |
| 1126 | } |
| 1127 | else { |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1134 | * have been buffered. |
| 1135 | */ |
| 1136 | if (!len) { |
| 1137 | struct quic_frame *frm; |
| 1138 | struct quic_frame *found = NULL; |
| 1139 | |
| 1140 | /* There is at most one CRYPTO frame in this packet number |
| 1141 | * space. Let's look for it. |
| 1142 | */ |
| 1143 | list_for_each_entry(frm, &qel->pktns->tx.frms, list) { |
| 1144 | if (frm->type != QUIC_FT_CRYPTO) |
| 1145 | continue; |
| 1146 | |
| 1147 | /* Found */ |
| 1148 | found = frm; |
| 1149 | break; |
| 1150 | } |
| 1151 | |
| 1152 | if (found) { |
| 1153 | found->crypto.len += cf_len; |
| 1154 | } |
| 1155 | else { |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 1156 | frm = qc_frm_alloc(QUIC_FT_CRYPTO); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1157 | if (!frm) { |
| 1158 | TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc); |
| 1159 | goto leave; |
| 1160 | } |
| 1161 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1162 | frm->crypto.offset = cf_offset; |
| 1163 | frm->crypto.len = cf_len; |
| 1164 | frm->crypto.qel = qel; |
| 1165 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
| 1166 | } |
| 1167 | } |
| 1168 | ret = len == 0; |
| 1169 | leave: |
| 1170 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1171 | return ret; |
| 1172 | } |
| 1173 | |
| 1174 | /* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive |
| 1175 | * activity for <qc> will be interrupted. |
| 1176 | */ |
| 1177 | void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err) |
| 1178 | { |
| 1179 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1180 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) |
| 1181 | goto leave; |
| 1182 | |
| 1183 | TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc); |
| 1184 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 1185 | qc->err.code = err.code; |
| 1186 | qc->err.app = err.app; |
| 1187 | leave: |
| 1188 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 1189 | } |
| 1190 | |
| 1191 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1192 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1193 | { |
| 1194 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1195 | |
| 1196 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 1197 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 1198 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
| 1199 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 1200 | } |
| 1201 | quic_set_connection_close(qc, quic_err_tls(alert)); |
| 1202 | qc->flags |= QUIC_FL_CONN_TLS_ALERT; |
| 1203 | TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc); |
| 1204 | |
| 1205 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
| 1206 | } |
| 1207 | |
| 1208 | /* Set the application for <qc> QUIC connection. |
| 1209 | * Return 1 if succeeded, 0 if not. |
| 1210 | */ |
| 1211 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1212 | { |
| 1213 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1214 | qc->app_ops = &h3_ops; |
| 1215 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1216 | qc->app_ops = &hq_interop_ops; |
| 1217 | else |
| 1218 | return 0; |
| 1219 | |
| 1220 | return 1; |
| 1221 | } |
| 1222 | |
| 1223 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1224 | * wants to provide the QUIC layer with CRYPTO data. |
| 1225 | * Returns 1 if succeeded, 0 if not. |
| 1226 | */ |
| 1227 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1228 | const uint8_t *data, size_t len) |
| 1229 | { |
| 1230 | struct quic_conn *qc; |
| 1231 | enum quic_tls_enc_level tel; |
| 1232 | struct quic_enc_level *qel; |
| 1233 | int ret = 0; |
| 1234 | |
| 1235 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1236 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1237 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame^] | 1238 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 1239 | TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc); |
| 1240 | goto out; |
| 1241 | } |
| 1242 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1243 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 1244 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
| 1245 | goto out; |
| 1246 | } |
| 1247 | |
| 1248 | tel = ssl_to_quic_enc_level(level); |
| 1249 | if (tel == -1) { |
| 1250 | TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc); |
| 1251 | goto leave; |
| 1252 | } |
| 1253 | |
| 1254 | qel = &qc->els[tel]; |
| 1255 | if (!quic_crypto_data_cpy(qc, qel, data, len)) { |
| 1256 | TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc); |
| 1257 | goto leave; |
| 1258 | } |
| 1259 | |
| 1260 | TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
| 1261 | qc, &level, &len); |
| 1262 | out: |
| 1263 | ret = 1; |
| 1264 | leave: |
| 1265 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
| 1269 | int ha_quic_flush_flight(SSL *ssl) |
| 1270 | { |
| 1271 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1272 | |
| 1273 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1274 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
| 1275 | |
| 1276 | return 1; |
| 1277 | } |
| 1278 | |
| 1279 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1280 | { |
| 1281 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1282 | |
| 1283 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1284 | |
| 1285 | TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1286 | |
| 1287 | quic_set_tls_alert(qc, alert); |
| 1288 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
| 1289 | return 1; |
| 1290 | } |
| 1291 | |
| 1292 | /* QUIC TLS methods */ |
| 1293 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1294 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1295 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1296 | .flush_flight = ha_quic_flush_flight, |
| 1297 | .send_alert = ha_quic_send_alert, |
| 1298 | }; |
| 1299 | |
| 1300 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1301 | * Returns an error count. |
| 1302 | */ |
| 1303 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1304 | { |
| 1305 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1306 | int cfgerr = 0; |
| 1307 | |
| 1308 | long options = |
| 1309 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1310 | SSL_OP_SINGLE_ECDH_USE | |
| 1311 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1312 | SSL_CTX *ctx; |
| 1313 | |
| 1314 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1315 | bind_conf->initial_ctx = ctx; |
| 1316 | |
| 1317 | SSL_CTX_set_options(ctx, options); |
| 1318 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1319 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1320 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1321 | |
| 1322 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1323 | # if defined(HAVE_SSL_CLIENT_HELLO_CB) |
| 1324 | # if defined(SSL_OP_NO_ANTI_REPLAY) |
| 1325 | if (bind_conf->ssl_conf.early_data) { |
| 1326 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
| 1327 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
| 1328 | } |
| 1329 | # endif /* !SSL_OP_NO_ANTI_REPLAY */ |
| 1330 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1331 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1332 | # else /* ! HAVE_SSL_CLIENT_HELLO_CB */ |
| 1333 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1334 | # endif |
| 1335 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1336 | #endif |
| 1337 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1338 | |
| 1339 | return cfgerr; |
| 1340 | } |
| 1341 | |
| 1342 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1343 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1344 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1345 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1346 | */ |
| 1347 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1348 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1349 | { |
| 1350 | uint64_t expected_pn = largest_pn + 1; |
| 1351 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1352 | uint64_t pn_hwin = pn_win / 2; |
| 1353 | uint64_t pn_mask = pn_win - 1; |
| 1354 | uint64_t candidate_pn; |
| 1355 | |
| 1356 | |
| 1357 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1358 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1359 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1360 | candidate_pn + pn_hwin <= expected_pn) |
| 1361 | return candidate_pn + pn_win; |
| 1362 | |
| 1363 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1364 | return candidate_pn - pn_win; |
| 1365 | |
| 1366 | return candidate_pn; |
| 1367 | } |
| 1368 | |
| 1369 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1370 | * cryptographic context. |
| 1371 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1372 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1373 | * <end> points to one byte past the end of this packet. |
| 1374 | * Returns 1 if succeeded, 0 if not. |
| 1375 | */ |
| 1376 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1377 | struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
| 1378 | int64_t largest_pn, unsigned char *pn, unsigned char *byte0) |
| 1379 | { |
| 1380 | int ret, i, pnlen; |
| 1381 | uint64_t packet_number; |
| 1382 | uint32_t truncated_pn = 0; |
| 1383 | unsigned char mask[5] = {0}; |
| 1384 | unsigned char *sample; |
| 1385 | EVP_CIPHER_CTX *cctx = NULL; |
| 1386 | |
| 1387 | TRACE_ENTER(QUIC_EV_CONN_RMHP, qc); |
| 1388 | |
| 1389 | ret = 0; |
| 1390 | |
| 1391 | /* Check there is enough data in this packet. */ |
| 1392 | if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
| 1393 | TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1394 | goto leave; |
| 1395 | } |
| 1396 | |
| 1397 | cctx = EVP_CIPHER_CTX_new(); |
| 1398 | if (!cctx) { |
| 1399 | TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1400 | goto leave; |
| 1401 | } |
| 1402 | |
| 1403 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1404 | |
| 1405 | if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) { |
| 1406 | TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1407 | goto leave; |
| 1408 | } |
| 1409 | |
| 1410 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1411 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1412 | for (i = 0; i < pnlen; i++) { |
| 1413 | pn[i] ^= mask[i + 1]; |
| 1414 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1415 | } |
| 1416 | |
| 1417 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1418 | /* Store remaining information for this unprotected header */ |
| 1419 | pkt->pn = packet_number; |
| 1420 | pkt->pnl = pnlen; |
| 1421 | |
| 1422 | ret = 1; |
| 1423 | leave: |
| 1424 | if (cctx) |
| 1425 | EVP_CIPHER_CTX_free(cctx); |
| 1426 | TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc); |
| 1427 | return ret; |
| 1428 | } |
| 1429 | |
| 1430 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1431 | * address, with <payload_len> as payload length, <aad> as address of |
| 1432 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1433 | * context. |
| 1434 | * Returns 1 if succeeded, 0 if not. |
| 1435 | */ |
| 1436 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1437 | unsigned char *aad, size_t aad_len, uint64_t pn, |
| 1438 | struct quic_tls_ctx *tls_ctx, struct quic_conn *qc) |
| 1439 | { |
| 1440 | int ret = 0; |
| 1441 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 1442 | unsigned char *tx_iv = tls_ctx->tx.iv; |
| 1443 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
| 1444 | struct enc_debug_info edi; |
| 1445 | |
| 1446 | TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc); |
| 1447 | |
| 1448 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
| 1449 | TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc); |
| 1450 | goto err; |
| 1451 | } |
| 1452 | |
| 1453 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1454 | tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
| 1455 | TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc); |
| 1456 | goto err; |
| 1457 | } |
| 1458 | |
| 1459 | ret = 1; |
| 1460 | leave: |
| 1461 | TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc); |
| 1462 | return ret; |
| 1463 | |
| 1464 | err: |
| 1465 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
| 1466 | goto leave; |
| 1467 | } |
| 1468 | |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1469 | /* Decrypt <pkt> packet using encryption level <qel> for <qc> connection. |
| 1470 | * Decryption is done in place in packet buffer. |
| 1471 | * |
Ilya Shipitsin | 5fa29b8 | 2022-12-07 09:46:19 +0500 | [diff] [blame] | 1472 | * Returns 1 on success else 0. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1473 | */ |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1474 | static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel, |
| 1475 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1476 | { |
| 1477 | int ret, kp_changed; |
| 1478 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 1479 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
| 1480 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
| 1481 | unsigned char *rx_iv = tls_ctx->rx.iv; |
| 1482 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1483 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1484 | |
| 1485 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 1486 | |
| 1487 | ret = 0; |
| 1488 | kp_changed = 0; |
| 1489 | |
| 1490 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1491 | /* The two tested bits are not at the same position, |
| 1492 | * this is why they are first both inversed. |
| 1493 | */ |
| 1494 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1495 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1496 | /* The lowest packet number of a previous key phase |
| 1497 | * cannot be null if it really stores previous key phase |
| 1498 | * secrets. |
| 1499 | */ |
| 1500 | // TODO: check if BUG_ON() more suitable |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1501 | if (!qc->ku.prv_rx.pn) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1502 | TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc); |
| 1503 | goto leave; |
| 1504 | } |
| 1505 | |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1506 | rx_ctx = qc->ku.prv_rx.ctx; |
| 1507 | rx_iv = qc->ku.prv_rx.iv; |
| 1508 | rx_key = qc->ku.prv_rx.key; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1509 | } |
| 1510 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1511 | /* Next key phase */ |
| 1512 | kp_changed = 1; |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1513 | rx_ctx = qc->ku.nxt_rx.ctx; |
| 1514 | rx_iv = qc->ku.nxt_rx.iv; |
| 1515 | rx_key = qc->ku.nxt_rx.key; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) { |
| 1521 | TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1522 | goto leave; |
| 1523 | } |
| 1524 | |
| 1525 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1526 | pkt->data, pkt->aad_len, |
| 1527 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
| 1528 | if (!ret) { |
| 1529 | TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1530 | goto leave; |
| 1531 | } |
| 1532 | |
| 1533 | /* Update the keys only if the packet decryption succeeded. */ |
| 1534 | if (kp_changed) { |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1535 | quic_tls_rotate_keys(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1536 | /* Toggle the Key Phase bit */ |
| 1537 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1538 | /* Store the lowest packet number received for the current key phase */ |
| 1539 | tls_ctx->rx.pn = pkt->pn; |
| 1540 | /* Prepare the next key update */ |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1541 | if (!quic_tls_key_update(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1542 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1543 | goto leave; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | /* Update the packet length (required to parse the frames). */ |
| 1548 | pkt->len -= QUIC_TLS_TAG_LEN; |
| 1549 | ret = 1; |
| 1550 | leave: |
| 1551 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 1552 | return ret; |
| 1553 | } |
| 1554 | |
| 1555 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1556 | /* Release <frm> frame and mark its copies as acknowledged */ |
| 1557 | void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm) |
| 1558 | { |
| 1559 | uint64_t pn; |
| 1560 | struct quic_frame *origin, *f, *tmp; |
| 1561 | |
| 1562 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1563 | |
| 1564 | /* Identify this frame: a frame copy or one of its copies */ |
| 1565 | origin = frm->origin ? frm->origin : frm; |
| 1566 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1567 | * possibly a copy of <origin> |
| 1568 | */ |
| 1569 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1570 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1571 | * not release the packets (releasing the frames) at this time as |
| 1572 | * they are possibly also to be acknowledged alongside the |
| 1573 | * the current one. |
| 1574 | */ |
| 1575 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
| 1576 | if (f->pkt) { |
| 1577 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1578 | f->origin = NULL; |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1579 | LIST_DEL_INIT(&f->ref); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1580 | pn = f->pkt->pn_node.key; |
| 1581 | TRACE_DEVEL("mark frame as acked from packet", |
| 1582 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1583 | } |
| 1584 | else { |
| 1585 | TRACE_DEVEL("freeing unsent frame", |
| 1586 | QUIC_EV_CONN_PRSAFRM, qc, f); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1587 | LIST_DEL_INIT(&f->ref); |
| 1588 | qc_frm_free(&f); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1589 | } |
| 1590 | } |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1591 | LIST_DEL_INIT(&frm->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1592 | pn = frm->pkt->pn_node.key; |
| 1593 | quic_tx_packet_refdec(frm->pkt); |
| 1594 | TRACE_DEVEL("freeing frame from packet", |
| 1595 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1596 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1597 | |
| 1598 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1599 | } |
| 1600 | |
| 1601 | /* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released |
| 1602 | * and all STREAM data are acknowledged. The MUX is responsible to have set |
| 1603 | * <qc.err> before as it is reused for the CONNECTION_CLOSE frame. |
| 1604 | * |
| 1605 | * TODO this should also be called on lost packet detection |
| 1606 | */ |
| 1607 | void qc_check_close_on_released_mux(struct quic_conn *qc) |
| 1608 | { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1609 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1610 | |
| 1611 | if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) { |
| 1612 | /* Reuse errcode which should have been previously set by the MUX on release. */ |
| 1613 | quic_set_connection_close(qc, qc->err); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 1614 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 1618 | } |
| 1619 | |
| 1620 | /* Remove from <stream> the acknowledged frames. |
| 1621 | * |
| 1622 | * Returns 1 if at least one frame was removed else 0. |
| 1623 | */ |
| 1624 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1625 | struct qc_stream_desc *stream) |
| 1626 | { |
| 1627 | int ret; |
| 1628 | struct eb64_node *frm_node; |
| 1629 | |
| 1630 | TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc); |
| 1631 | |
| 1632 | ret = 0; |
| 1633 | frm_node = eb64_first(&stream->acked_frms); |
| 1634 | while (frm_node) { |
| 1635 | struct quic_stream *strm; |
| 1636 | struct quic_frame *frm; |
| 1637 | size_t offset, len; |
| 1638 | |
| 1639 | strm = eb64_entry(frm_node, struct quic_stream, offset); |
| 1640 | offset = strm->offset.key; |
| 1641 | len = strm->len; |
| 1642 | |
| 1643 | if (offset > stream->ack_offset) |
| 1644 | break; |
| 1645 | |
| 1646 | if (qc_stream_desc_ack(&stream, offset, len)) { |
| 1647 | /* cf. next comment : frame may be freed at this stage. */ |
| 1648 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1649 | qc, stream ? strm : NULL, stream); |
| 1650 | ret = 1; |
| 1651 | } |
| 1652 | |
| 1653 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1654 | * has been freed. with the stream frames tree. Nothing to do |
| 1655 | * anymore in here. |
| 1656 | */ |
| 1657 | if (!stream) { |
| 1658 | qc_check_close_on_released_mux(qc); |
| 1659 | ret = 1; |
| 1660 | goto leave; |
| 1661 | } |
| 1662 | |
| 1663 | frm_node = eb64_next(frm_node); |
| 1664 | eb64_delete(&strm->offset); |
| 1665 | |
| 1666 | frm = container_of(strm, struct quic_frame, stream); |
| 1667 | qc_release_frm(qc, frm); |
| 1668 | } |
| 1669 | |
| 1670 | leave: |
| 1671 | TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc); |
| 1672 | return ret; |
| 1673 | } |
| 1674 | |
| 1675 | /* Treat <frm> frame whose packet it is attached to has just been acknowledged. */ |
| 1676 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1677 | struct quic_frame *frm) |
| 1678 | { |
| 1679 | int stream_acked; |
| 1680 | |
| 1681 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1682 | |
| 1683 | stream_acked = 0; |
| 1684 | switch (frm->type) { |
| 1685 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1686 | { |
| 1687 | struct quic_stream *strm_frm = &frm->stream; |
| 1688 | struct eb64_node *node = NULL; |
| 1689 | struct qc_stream_desc *stream = NULL; |
| 1690 | const size_t offset = strm_frm->offset.key; |
| 1691 | const size_t len = strm_frm->len; |
| 1692 | |
| 1693 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1694 | * might be freed at this stage. Use the id to do a proper |
| 1695 | * lookup. |
| 1696 | * |
| 1697 | * TODO if lookup operation impact on the perf is noticeable, |
| 1698 | * implement a refcount on qc_stream_desc instances. |
| 1699 | */ |
| 1700 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1701 | if (!node) { |
| 1702 | TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm); |
| 1703 | qc_release_frm(qc, frm); |
| 1704 | /* early return */ |
| 1705 | goto leave; |
| 1706 | } |
| 1707 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1708 | |
| 1709 | TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
| 1710 | if (offset <= stream->ack_offset) { |
| 1711 | if (qc_stream_desc_ack(&stream, offset, len)) { |
| 1712 | stream_acked = 1; |
| 1713 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1714 | qc, strm_frm, stream); |
| 1715 | } |
| 1716 | |
| 1717 | if (!stream) { |
| 1718 | /* no need to continue if stream freed. */ |
| 1719 | TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc); |
| 1720 | qc_release_frm(qc, frm); |
| 1721 | qc_check_close_on_released_mux(qc); |
| 1722 | break; |
| 1723 | } |
| 1724 | |
| 1725 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1726 | qc, strm_frm, stream); |
| 1727 | qc_release_frm(qc, frm); |
| 1728 | } |
| 1729 | else { |
| 1730 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
| 1731 | } |
| 1732 | |
| 1733 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
| 1734 | } |
| 1735 | break; |
| 1736 | default: |
| 1737 | qc_release_frm(qc, frm); |
| 1738 | } |
| 1739 | |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 1740 | if (stream_acked) { |
| 1741 | if (qc->subs && qc->subs->events & SUB_RETRY_SEND) { |
| 1742 | tasklet_wakeup(qc->subs->tasklet); |
| 1743 | qc->subs->events &= ~SUB_RETRY_SEND; |
| 1744 | if (!qc->subs->events) |
| 1745 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1746 | } |
| 1747 | } |
| 1748 | leave: |
| 1749 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1750 | } |
| 1751 | |
| 1752 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1753 | * deallocating them, and their TX frames. |
| 1754 | * Returns the last node reached to be used for the next range. |
| 1755 | * May be NULL if <largest> node could not be found. |
| 1756 | */ |
| 1757 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1758 | struct eb_root *pkts, |
| 1759 | unsigned int *pkt_flags, |
| 1760 | struct list *newly_acked_pkts, |
| 1761 | struct eb64_node *largest_node, |
| 1762 | uint64_t largest, uint64_t smallest) |
| 1763 | { |
| 1764 | struct eb64_node *node; |
| 1765 | struct quic_tx_packet *pkt; |
| 1766 | |
| 1767 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1768 | |
| 1769 | node = largest_node ? largest_node : eb64_lookup_le(pkts, largest); |
| 1770 | while (node && node->key >= smallest) { |
| 1771 | struct quic_frame *frm, *frmbak; |
| 1772 | |
| 1773 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 1774 | *pkt_flags |= pkt->flags; |
| 1775 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
| 1776 | TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 1777 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1778 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 1779 | /* If there are others packet in the same datagram <pkt> is attached to, |
| 1780 | * detach the previous one and the next one from <pkt>. |
| 1781 | */ |
Frédéric Lécaille | 74b5f7b | 2022-11-20 18:35:35 +0100 | [diff] [blame] | 1782 | quic_tx_packet_dgram_detach(pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1783 | node = eb64_prev(node); |
| 1784 | eb64_delete(&pkt->pn_node); |
| 1785 | } |
| 1786 | |
| 1787 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1788 | return node; |
| 1789 | } |
| 1790 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1791 | /* Remove all frames from <pkt_frm_list> and reinsert them in the same order |
| 1792 | * they have been sent into <pktns_frm_list>. The loss counter of each frame is |
| 1793 | * incremented and checked if it does not exceed retransmission limit. |
| 1794 | * |
| 1795 | * Returns 1 on success, 0 if a frame loss limit is exceeded. A |
| 1796 | * CONNECTION_CLOSE is scheduled in this case. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1797 | */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1798 | static inline int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1799 | struct quic_tx_packet *pkt, |
| 1800 | struct list *pktns_frm_list) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1801 | { |
| 1802 | struct quic_frame *frm, *frmbak; |
| 1803 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1804 | struct list *pkt_frm_list = &pkt->frms; |
| 1805 | uint64_t pn = pkt->pn_node.key; |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1806 | int close = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1807 | |
| 1808 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1809 | |
| 1810 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1811 | /* First remove this frame from the packet it was attached to */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1812 | LIST_DEL_INIT(&frm->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1813 | quic_tx_packet_refdec(pkt); |
| 1814 | /* At this time, this frame is not freed but removed from its packet */ |
| 1815 | frm->pkt = NULL; |
| 1816 | /* Remove any reference to this frame */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1817 | qc_frm_unref(frm, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1818 | switch (frm->type) { |
| 1819 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1820 | { |
| 1821 | struct quic_stream *strm_frm = &frm->stream; |
| 1822 | struct eb64_node *node = NULL; |
| 1823 | struct qc_stream_desc *stream_desc; |
| 1824 | |
| 1825 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1826 | if (!node) { |
| 1827 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1828 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1829 | qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1830 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1831 | continue; |
| 1832 | } |
| 1833 | |
| 1834 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1835 | /* Do not resend this frame if in the "already acked range" */ |
| 1836 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 1837 | TRACE_DEVEL("ignored frame in already acked range", |
| 1838 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1839 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1840 | continue; |
| 1841 | } |
| 1842 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 1843 | strm_frm->offset.key = stream_desc->ack_offset; |
| 1844 | TRACE_DEVEL("updated partially acked frame", |
| 1845 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1846 | } |
| 1847 | break; |
| 1848 | } |
| 1849 | |
| 1850 | default: |
| 1851 | break; |
| 1852 | } |
| 1853 | |
| 1854 | /* Do not resend probing packet with old data */ |
| 1855 | if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) { |
| 1856 | TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 1857 | qc, frm, &pn); |
| 1858 | if (frm->origin) |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1859 | LIST_DEL_INIT(&frm->ref); |
| 1860 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1861 | continue; |
| 1862 | } |
| 1863 | |
| 1864 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 1865 | TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1866 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1867 | qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1868 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1869 | } |
| 1870 | else { |
Amaury Denoyelle | 24d5b72 | 2023-01-31 11:44:50 +0100 | [diff] [blame] | 1871 | if (++frm->loss_count >= global.tune.quic_max_frame_loss) { |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1872 | TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc); |
| 1873 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR)); |
| 1874 | close = 1; |
| 1875 | } |
| 1876 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1877 | if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) { |
| 1878 | /* Mark this STREAM frame as lost. A look up their stream descriptor |
| 1879 | * will be performed to check the stream is not consumed or released. |
| 1880 | */ |
| 1881 | frm->flags |= QUIC_FL_TX_FRAME_LOST; |
| 1882 | } |
| 1883 | LIST_APPEND(&tmp, &frm->list); |
| 1884 | TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | LIST_SPLICE(pktns_frm_list, &tmp); |
| 1889 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1890 | end: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1891 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1892 | return !close; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | /* Free <pkt> TX packet and its attached frames. |
| 1896 | * This is the responsibility of the caller to remove this packet of |
| 1897 | * any data structure it was possibly attached to. |
| 1898 | */ |
| 1899 | static inline void free_quic_tx_packet(struct quic_conn *qc, |
| 1900 | struct quic_tx_packet *pkt) |
| 1901 | { |
| 1902 | struct quic_frame *frm, *frmbak; |
| 1903 | |
| 1904 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1905 | |
| 1906 | if (!pkt) |
| 1907 | goto leave; |
| 1908 | |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1909 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1910 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1911 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1912 | |
| 1913 | leave: |
| 1914 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 1915 | } |
| 1916 | |
| 1917 | /* Free the TX packets of <pkts> list */ |
| 1918 | static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts) |
| 1919 | { |
| 1920 | struct quic_tx_packet *pkt, *tmp; |
| 1921 | |
| 1922 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1923 | |
| 1924 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 1925 | LIST_DELETE(&pkt->list); |
| 1926 | eb64_delete(&pkt->pn_node); |
| 1927 | free_quic_tx_packet(qc, pkt); |
| 1928 | } |
| 1929 | |
| 1930 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 1931 | } |
| 1932 | |
| 1933 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1934 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1935 | * updating the range which contains <largest_acked_pn>. |
| 1936 | * Never fails. |
| 1937 | */ |
| 1938 | static void qc_treat_ack_of_ack(struct quic_conn *qc, |
| 1939 | struct quic_pktns *pktns, |
| 1940 | int64_t largest_acked_pn) |
| 1941 | { |
| 1942 | struct eb64_node *ar, *next_ar; |
| 1943 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1944 | |
| 1945 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1946 | |
| 1947 | ar = eb64_first(&arngs->root); |
| 1948 | while (ar) { |
| 1949 | struct quic_arng_node *ar_node; |
| 1950 | |
| 1951 | next_ar = eb64_next(ar); |
| 1952 | ar_node = eb64_entry(ar, struct quic_arng_node, first); |
| 1953 | |
| 1954 | if ((int64_t)ar_node->first.key > largest_acked_pn) { |
| 1955 | TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc); |
| 1956 | break; |
| 1957 | } |
| 1958 | |
| 1959 | if (largest_acked_pn < ar_node->last) { |
| 1960 | eb64_delete(ar); |
| 1961 | ar_node->first.key = largest_acked_pn + 1; |
| 1962 | eb64_insert(&arngs->root, ar); |
| 1963 | break; |
| 1964 | } |
| 1965 | |
| 1966 | eb64_delete(ar); |
| 1967 | pool_free(pool_head_quic_arng, ar_node); |
| 1968 | arngs->sz--; |
| 1969 | ar = next_ar; |
| 1970 | } |
| 1971 | |
| 1972 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1973 | } |
| 1974 | |
| 1975 | /* Send a packet ack event nofication for each newly acked packet of |
| 1976 | * <newly_acked_pkts> list and free them. |
| 1977 | * Always succeeds. |
| 1978 | */ |
| 1979 | static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc, |
| 1980 | struct list *newly_acked_pkts) |
| 1981 | { |
| 1982 | struct quic_tx_packet *pkt, *tmp; |
| 1983 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1984 | |
| 1985 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1986 | |
| 1987 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1988 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
| 1989 | qc->path->prep_in_flight -= pkt->in_flight_len; |
| 1990 | qc->path->in_flight -= pkt->in_flight_len; |
| 1991 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
| 1992 | qc->path->ifae_pkts--; |
| 1993 | /* If this packet contained an ACK frame, proceed to the |
| 1994 | * acknowledging of range of acks from the largest acknowledged |
| 1995 | * packet number which was sent in an ACK frame by this packet. |
| 1996 | */ |
| 1997 | if (pkt->largest_acked_pn != -1) |
| 1998 | qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn); |
| 1999 | ev.ack.acked = pkt->in_flight_len; |
| 2000 | ev.ack.time_sent = pkt->time_sent; |
| 2001 | quic_cc_event(&qc->path->cc, &ev); |
| 2002 | LIST_DELETE(&pkt->list); |
| 2003 | eb64_delete(&pkt->pn_node); |
| 2004 | quic_tx_packet_refdec(pkt); |
| 2005 | } |
| 2006 | |
| 2007 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2008 | |
| 2009 | } |
| 2010 | |
| 2011 | /* Release all the frames attached to <pktns> packet number space */ |
| 2012 | static inline void qc_release_pktns_frms(struct quic_conn *qc, |
| 2013 | struct quic_pktns *pktns) |
| 2014 | { |
| 2015 | struct quic_frame *frm, *frmbak; |
| 2016 | |
| 2017 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2018 | |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 2019 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) |
| 2020 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2021 | |
| 2022 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2023 | } |
| 2024 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2025 | /* Handle <pkts> list of lost packets detected at <now_us> handling their TX |
| 2026 | * frames. Send a packet loss event to the congestion controller if in flight |
| 2027 | * packet have been lost. Also frees the packet in <pkts> list. |
| 2028 | * |
| 2029 | * Returns 1 on success else 0 if loss limit has been exceeded. A |
| 2030 | * CONNECTION_CLOSE was prepared to close the connection ASAP. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2031 | */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2032 | static inline int qc_release_lost_pkts(struct quic_conn *qc, |
| 2033 | struct quic_pktns *pktns, |
| 2034 | struct list *pkts, |
| 2035 | uint64_t now_us) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2036 | { |
| 2037 | struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost; |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2038 | int close = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2039 | |
| 2040 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2041 | |
| 2042 | if (LIST_ISEMPTY(pkts)) |
| 2043 | goto leave; |
| 2044 | |
| 2045 | oldest_lost = newest_lost = NULL; |
| 2046 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 2047 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2048 | |
| 2049 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
| 2050 | qc->path->prep_in_flight -= pkt->in_flight_len; |
| 2051 | qc->path->in_flight -= pkt->in_flight_len; |
| 2052 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
| 2053 | qc->path->ifae_pkts--; |
| 2054 | /* Treat the frames of this lost packet. */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2055 | if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms)) |
| 2056 | close = 1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2057 | LIST_DELETE(&pkt->list); |
| 2058 | if (!oldest_lost) { |
| 2059 | oldest_lost = newest_lost = pkt; |
| 2060 | } |
| 2061 | else { |
| 2062 | if (newest_lost != oldest_lost) |
| 2063 | quic_tx_packet_refdec(newest_lost); |
| 2064 | newest_lost = pkt; |
| 2065 | } |
| 2066 | } |
| 2067 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2068 | if (!close) { |
| 2069 | if (newest_lost) { |
| 2070 | /* Sent a congestion event to the controller */ |
| 2071 | struct quic_cc_event ev = { }; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2072 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2073 | ev.type = QUIC_CC_EVT_LOSS; |
| 2074 | ev.loss.time_sent = newest_lost->time_sent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2075 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2076 | quic_cc_event(&qc->path->cc, &ev); |
| 2077 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2078 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2079 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 2080 | * We must check if we are experiencing a persistent congestion. |
| 2081 | * If this is the case, the congestion controller must re-enter |
| 2082 | * slow start state. |
| 2083 | */ |
| 2084 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 2085 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2086 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2087 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 2088 | now_ms, qc->max_ack_delay)) |
| 2089 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 2090 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2091 | } |
| 2092 | |
Amaury Denoyelle | 3a72ba2 | 2022-11-14 11:41:34 +0100 | [diff] [blame] | 2093 | /* <oldest_lost> cannot be NULL at this stage because we have ensured |
| 2094 | * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a |
| 2095 | * possible overflow on a 0 byte region with O2 optimization. |
| 2096 | */ |
| 2097 | ALREADY_CHECKED(oldest_lost); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2098 | quic_tx_packet_refdec(oldest_lost); |
| 2099 | if (newest_lost != oldest_lost) |
| 2100 | quic_tx_packet_refdec(newest_lost); |
| 2101 | |
| 2102 | leave: |
| 2103 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2104 | return !close; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 2108 | * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e. |
| 2109 | * if the largest acked packet was newly acked and if there was at least one newly |
| 2110 | * acked ack-eliciting packet. |
| 2111 | * Return 1, if succeeded, 0 if not. |
| 2112 | */ |
| 2113 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 2114 | struct quic_frame *frm, |
| 2115 | struct quic_enc_level *qel, |
| 2116 | unsigned int *rtt_sample, |
| 2117 | const unsigned char **pos, const unsigned char *end) |
| 2118 | { |
| 2119 | struct quic_ack *ack = &frm->ack; |
| 2120 | uint64_t smallest, largest; |
| 2121 | struct eb_root *pkts; |
| 2122 | struct eb64_node *largest_node; |
| 2123 | unsigned int time_sent, pkt_flags; |
| 2124 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 2125 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 2126 | int ret = 0; |
| 2127 | |
| 2128 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2129 | |
| 2130 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 2131 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
| 2132 | qc, NULL, &ack->largest_ack); |
| 2133 | goto err; |
| 2134 | } |
| 2135 | |
| 2136 | if (ack->first_ack_range > ack->largest_ack) { |
| 2137 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
| 2138 | qc, NULL, &ack->first_ack_range); |
| 2139 | goto err; |
| 2140 | } |
| 2141 | |
| 2142 | largest = ack->largest_ack; |
| 2143 | smallest = largest - ack->first_ack_range; |
| 2144 | pkts = &qel->pktns->tx.pkts; |
| 2145 | pkt_flags = 0; |
| 2146 | largest_node = NULL; |
| 2147 | time_sent = 0; |
| 2148 | |
| 2149 | if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) { |
| 2150 | largest_node = eb64_lookup(pkts, largest); |
| 2151 | if (!largest_node) { |
| 2152 | TRACE_DEVEL("Largest acked packet not found", |
| 2153 | QUIC_EV_CONN_PRSAFRM, qc); |
| 2154 | } |
| 2155 | else { |
| 2156 | time_sent = eb64_entry(largest_node, |
| 2157 | struct quic_tx_packet, pn_node)->time_sent; |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM, |
| 2162 | qc, NULL, &largest, &smallest); |
| 2163 | do { |
| 2164 | uint64_t gap, ack_range; |
| 2165 | |
| 2166 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 2167 | largest_node, largest, smallest); |
| 2168 | if (!ack->ack_range_num--) |
| 2169 | break; |
| 2170 | |
| 2171 | if (!quic_dec_int(&gap, pos, end)) { |
| 2172 | TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc); |
| 2173 | goto err; |
| 2174 | } |
| 2175 | |
| 2176 | if (smallest < gap + 2) { |
| 2177 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
| 2178 | qc, NULL, &gap, &smallest); |
| 2179 | goto err; |
| 2180 | } |
| 2181 | |
| 2182 | largest = smallest - gap - 2; |
| 2183 | if (!quic_dec_int(&ack_range, pos, end)) { |
| 2184 | TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc); |
| 2185 | goto err; |
| 2186 | } |
| 2187 | |
| 2188 | if (largest < ack_range) { |
| 2189 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
| 2190 | qc, NULL, &largest, &ack_range); |
| 2191 | goto err; |
| 2192 | } |
| 2193 | |
| 2194 | /* Do not use this node anymore. */ |
| 2195 | largest_node = NULL; |
| 2196 | /* Next range */ |
| 2197 | smallest = largest - ack_range; |
| 2198 | |
| 2199 | TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM, |
| 2200 | qc, NULL, &largest, &smallest); |
| 2201 | } while (1); |
| 2202 | |
| 2203 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2204 | *rtt_sample = tick_remain(time_sent, now_ms); |
| 2205 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
| 2206 | } |
| 2207 | |
| 2208 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 2209 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
| 2210 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2211 | if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms)) |
| 2212 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2213 | } |
| 2214 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 2215 | if (quic_peer_validated_addr(qc)) |
| 2216 | qc->path->loss.pto_count = 0; |
| 2217 | qc_set_timer(qc); |
| 2218 | } |
| 2219 | |
| 2220 | ret = 1; |
| 2221 | leave: |
| 2222 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2223 | return ret; |
| 2224 | |
| 2225 | err: |
| 2226 | free_quic_tx_pkts(qc, &newly_acked_pkts); |
| 2227 | goto leave; |
| 2228 | } |
| 2229 | |
| 2230 | /* This function gives the detail of the SSL error. It is used only |
| 2231 | * if the debug mode and the verbose mode are activated. It dump all |
| 2232 | * the SSL error until the stack was empty. |
| 2233 | */ |
| 2234 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2235 | { |
| 2236 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2237 | while (1) { |
| 2238 | const char *func = NULL; |
| 2239 | unsigned long ret; |
| 2240 | |
| 2241 | ERR_peek_error_func(&func); |
| 2242 | ret = ERR_get_error(); |
| 2243 | if (!ret) |
| 2244 | return; |
| 2245 | |
| 2246 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 2247 | func, ERR_reason_error_string(ret)); |
| 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2253 | const char **str, int *len); |
| 2254 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2255 | /* Finalize <qc> QUIC connection: |
| 2256 | * - initialize the Initial QUIC TLS context for negotiated version, |
| 2257 | * - derive the secrets for this context, |
| 2258 | * - set them into the TLS stack, |
| 2259 | * |
| 2260 | * MUST be called after having received the remote transport parameters which |
| 2261 | * are parsed when the TLS callback for the ClientHello message is called upon |
| 2262 | * SSL_do_handshake() calls, not necessarily at the first time as this TLS |
| 2263 | * message may be splitted between packets |
| 2264 | * Return 1 if succeeded, 0 if not. |
| 2265 | */ |
| 2266 | static int qc_conn_finalize(struct quic_conn *qc, int server) |
| 2267 | { |
| 2268 | int ret = 0; |
| 2269 | |
| 2270 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 2271 | |
| 2272 | if (qc->flags & QUIC_FL_CONN_FINALIZED) |
| 2273 | goto finalized; |
| 2274 | |
| 2275 | if (qc->negotiated_version && |
| 2276 | !qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version, |
| 2277 | qc->odcid.data, qc->odcid.len, server)) |
| 2278 | goto out; |
| 2279 | |
| 2280 | /* This connection is functional (ready to send/receive) */ |
| 2281 | qc->flags |= QUIC_FL_CONN_FINALIZED; |
| 2282 | |
| 2283 | finalized: |
| 2284 | ret = 1; |
| 2285 | out: |
| 2286 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 2287 | return ret; |
| 2288 | } |
| 2289 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2290 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2291 | * from <qel> encryption level with <ctx> as QUIC connection context. |
| 2292 | * Remaining parameter are there for debugging purposes. |
| 2293 | * Return 1 if succeeded, 0 if not. |
| 2294 | */ |
| 2295 | static inline int qc_provide_cdata(struct quic_enc_level *el, |
| 2296 | struct ssl_sock_ctx *ctx, |
| 2297 | const unsigned char *data, size_t len, |
| 2298 | struct quic_rx_packet *pkt, |
| 2299 | struct quic_rx_crypto_frm *cf) |
| 2300 | { |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2301 | #ifdef DEBUG_STRICT |
| 2302 | enum ncb_ret ncb_ret; |
| 2303 | #endif |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2304 | int ssl_err, state; |
| 2305 | struct quic_conn *qc; |
| 2306 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2307 | struct ncbuf *ncbuf = &el->cstream->rx.ncbuf; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2308 | |
| 2309 | ssl_err = SSL_ERROR_NONE; |
| 2310 | qc = ctx->qc; |
| 2311 | |
| 2312 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2313 | |
| 2314 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2315 | TRACE_ERROR("SSL_provide_quic_data() error", |
| 2316 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
| 2317 | goto leave; |
| 2318 | } |
| 2319 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2320 | TRACE_PROTO("in order CRYPTO data", |
| 2321 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
| 2322 | |
| 2323 | state = qc->state; |
| 2324 | if (state < QUIC_HS_ST_COMPLETE) { |
| 2325 | ssl_err = SSL_do_handshake(ctx->ssl); |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2326 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame^] | 2327 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 2328 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc); |
| 2329 | goto leave; |
| 2330 | } |
| 2331 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2332 | /* Finalize the connection as soon as possible if the peer transport parameters |
| 2333 | * have been received. This may be useful to send packets even if this |
| 2334 | * handshake fails. |
| 2335 | */ |
| 2336 | if ((qc->flags & QUIC_FL_CONN_TX_TP_RECEIVED) && !qc_conn_finalize(qc, 1)) { |
| 2337 | TRACE_ERROR("connection finalization failed", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2338 | goto leave; |
| 2339 | } |
| 2340 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2341 | if (ssl_err != 1) { |
| 2342 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2343 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2344 | TRACE_PROTO("SSL handshake in progress", |
| 2345 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2346 | goto out; |
| 2347 | } |
| 2348 | |
| 2349 | /* TODO: Should close the connection asap */ |
| 2350 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2351 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2352 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2353 | HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail); |
| 2354 | } |
| 2355 | TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2356 | qc_ssl_dump_errors(ctx->conn); |
| 2357 | ERR_clear_error(); |
| 2358 | goto leave; |
| 2359 | } |
| 2360 | |
| 2361 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2362 | |
| 2363 | /* Check the alpn could be negotiated */ |
| 2364 | if (!qc->app_ops) { |
| 2365 | TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2366 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
| 2367 | goto leave; |
| 2368 | } |
| 2369 | |
| 2370 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2371 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2372 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2373 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2374 | } |
| 2375 | /* I/O callback switch */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2376 | qc->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2377 | if (qc_is_listener(ctx->qc)) { |
| 2378 | qc->state = QUIC_HS_ST_CONFIRMED; |
| 2379 | /* The connection is ready to be accepted. */ |
| 2380 | quic_accept_push_qc(qc); |
| 2381 | } |
| 2382 | else { |
| 2383 | qc->state = QUIC_HS_ST_COMPLETE; |
| 2384 | } |
| 2385 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 2386 | /* Prepare the next key update */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2387 | if (!quic_tls_key_update(qc)) { |
| 2388 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc); |
| 2389 | goto leave; |
| 2390 | } |
| 2391 | } else { |
| 2392 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2393 | if (ssl_err != 1) { |
| 2394 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2395 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2396 | TRACE_PROTO("SSL post handshake in progress", |
| 2397 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2398 | goto out; |
| 2399 | } |
| 2400 | |
| 2401 | TRACE_ERROR("SSL post handshake error", |
| 2402 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2403 | goto leave; |
| 2404 | } |
| 2405 | |
| 2406 | TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2407 | } |
| 2408 | |
| 2409 | out: |
| 2410 | ret = 1; |
| 2411 | leave: |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2412 | /* The CRYPTO data are consumed even in case of an error to release |
| 2413 | * the memory asap. |
| 2414 | */ |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2415 | if (!ncb_is_null(ncbuf)) { |
| 2416 | #ifdef DEBUG_STRICT |
| 2417 | ncb_ret = ncb_advance(ncbuf, len); |
| 2418 | /* ncb_advance() must always succeed. This is guaranteed as |
| 2419 | * this is only done inside a data block. If false, this will |
| 2420 | * lead to handshake failure with quic_enc_level offset shifted |
| 2421 | * from buffer data. |
| 2422 | */ |
| 2423 | BUG_ON(ncb_ret != NCB_RET_OK); |
| 2424 | #else |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2425 | ncb_advance(ncbuf, len); |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2426 | #endif |
| 2427 | } |
| 2428 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2429 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
| 2430 | return ret; |
| 2431 | } |
| 2432 | |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2433 | /* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc> |
| 2434 | * connection. <fin> is true if FIN bit is set on frame type. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2435 | * |
| 2436 | * Return 1 on success. On error, 0 is returned. In this case, the packet |
| 2437 | * containing the frame must not be acknowledged. |
| 2438 | */ |
| 2439 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2440 | struct quic_stream *strm_frm, |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2441 | struct quic_conn *qc, char fin) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2442 | { |
| 2443 | int ret; |
| 2444 | |
| 2445 | /* RFC9000 13.1. Packet Processing |
| 2446 | * |
| 2447 | * A packet MUST NOT be acknowledged until packet protection has been |
| 2448 | * successfully removed and all frames contained in the packet have |
| 2449 | * been processed. For STREAM frames, this means the data has been |
| 2450 | * enqueued in preparation to be received by the application protocol, |
| 2451 | * but it does not require that data be delivered and consumed. |
| 2452 | */ |
| 2453 | TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc); |
| 2454 | |
| 2455 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2456 | strm_frm->offset.key, fin, (char *)strm_frm->data); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2457 | |
| 2458 | /* frame rejected - packet must not be acknowledeged */ |
| 2459 | TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc); |
| 2460 | return !ret; |
| 2461 | } |
| 2462 | |
| 2463 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2464 | * for <qc> QUIC connection. |
| 2465 | * This is a best effort function which never fails even if no memory could be |
| 2466 | * allocated to duplicate these frames. |
| 2467 | */ |
| 2468 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2469 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2470 | { |
| 2471 | struct quic_frame *frm, *frmbak; |
| 2472 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2473 | |
| 2474 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2475 | |
| 2476 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2477 | struct quic_frame *dup_frm, *origin; |
| 2478 | |
| 2479 | switch (frm->type) { |
| 2480 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2481 | { |
| 2482 | struct quic_stream *strm_frm = &frm->stream; |
| 2483 | struct eb64_node *node = NULL; |
| 2484 | struct qc_stream_desc *stream_desc; |
| 2485 | |
| 2486 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2487 | if (!node) { |
| 2488 | TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2489 | continue; |
| 2490 | } |
| 2491 | |
| 2492 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2493 | /* Do not resend this frame if in the "already acked range" */ |
| 2494 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 2495 | TRACE_DEVEL("ignored frame in already acked range", |
| 2496 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2497 | continue; |
| 2498 | } |
| 2499 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 2500 | strm_frm->offset.key = stream_desc->ack_offset; |
| 2501 | TRACE_DEVEL("updated partially acked frame", |
| 2502 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2503 | } |
| 2504 | break; |
| 2505 | } |
| 2506 | |
| 2507 | default: |
| 2508 | break; |
| 2509 | } |
| 2510 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2511 | /* If <frm> is already a copy of another frame, we must take |
| 2512 | * its original frame as source for the copy. |
| 2513 | */ |
| 2514 | origin = frm->origin ? frm->origin : frm; |
| 2515 | dup_frm = qc_frm_dup(origin); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2516 | if (!dup_frm) { |
| 2517 | TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2518 | break; |
| 2519 | } |
| 2520 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2521 | TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2522 | if (origin->pkt) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2523 | TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM, |
| 2524 | qc, NULL, &origin->pkt->pn_node.key); |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2525 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2526 | else { |
| 2527 | /* <origin> is a frame which was sent from a packet detected as lost. */ |
| 2528 | TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc); |
| 2529 | } |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2530 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2531 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2532 | } |
| 2533 | |
| 2534 | LIST_SPLICE(out_frm_list, &tmp); |
| 2535 | |
| 2536 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2537 | } |
| 2538 | |
| 2539 | /* Prepare a fast retransmission from <qel> encryption level */ |
| 2540 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2541 | struct quic_enc_level *qel, |
| 2542 | struct list *frms1, struct list *frms2) |
| 2543 | { |
| 2544 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
| 2545 | struct list *frms = frms1; |
| 2546 | struct eb64_node *node; |
| 2547 | struct quic_tx_packet *pkt; |
| 2548 | |
| 2549 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2550 | |
| 2551 | BUG_ON(frms1 == frms2); |
| 2552 | |
| 2553 | pkt = NULL; |
| 2554 | node = eb64_first(pkts); |
| 2555 | start: |
| 2556 | while (node) { |
| 2557 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 2558 | node = eb64_next(node); |
| 2559 | /* Skip the empty and coalesced packets */ |
Frédéric Lécaille | 6dead91 | 2023-01-30 17:27:32 +0100 | [diff] [blame] | 2560 | TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0, |
| 2561 | "--> pn=%llu (%d %d)", (ull)pkt->pn_node.key, |
| 2562 | LIST_ISEMPTY(&pkt->frms), !!(pkt->flags & QUIC_FL_TX_PACKET_COALESCED)); |
Frédéric Lécaille | 055e826 | 2023-01-31 10:10:06 +0100 | [diff] [blame] | 2563 | if (!LIST_ISEMPTY(&pkt->frms)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2564 | break; |
| 2565 | } |
| 2566 | |
| 2567 | if (!pkt) |
| 2568 | goto leave; |
| 2569 | |
| 2570 | /* When building a packet from another one, the field which may increase the |
| 2571 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2572 | */ |
| 2573 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2574 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2575 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 2576 | goto leave; |
| 2577 | } |
| 2578 | |
| 2579 | TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 2580 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2581 | if (frms == frms1 && frms2) { |
| 2582 | frms = frms2; |
| 2583 | goto start; |
| 2584 | } |
| 2585 | leave: |
| 2586 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 2587 | } |
| 2588 | |
| 2589 | /* Prepare a fast retransmission during a handshake after a client |
| 2590 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2591 | * Initial packets send them coalescing with others (Handshake here). |
| 2592 | * (Listener only function). |
| 2593 | */ |
| 2594 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2595 | struct list *ifrms, struct list *hfrms) |
| 2596 | { |
| 2597 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2598 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2599 | |
| 2600 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2601 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2602 | struct quic_enc_level *qel = iqel; |
| 2603 | struct eb_root *pkts; |
| 2604 | struct eb64_node *node; |
| 2605 | struct quic_tx_packet *pkt; |
| 2606 | struct list *tmp = &itmp; |
| 2607 | |
| 2608 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2609 | start: |
| 2610 | pkt = NULL; |
| 2611 | pkts = &qel->pktns->tx.pkts; |
| 2612 | node = eb64_first(pkts); |
| 2613 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2614 | while (node) { |
| 2615 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | 6dead91 | 2023-01-30 17:27:32 +0100 | [diff] [blame] | 2616 | TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0, |
| 2617 | "--> pn=%llu (%d %d)", (ull)pkt->pn_node.key, |
| 2618 | LIST_ISEMPTY(&pkt->frms), !!(pkt->flags & QUIC_FL_TX_PACKET_COALESCED)); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2619 | if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED)) |
| 2620 | break; |
| 2621 | node = eb64_next(node); |
| 2622 | } |
| 2623 | |
| 2624 | if (!pkt) |
| 2625 | goto end; |
| 2626 | |
| 2627 | /* When building a packet from another one, the field which may increase the |
| 2628 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2629 | */ |
| 2630 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2631 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2632 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2633 | goto end; |
| 2634 | } |
| 2635 | |
| 2636 | qel->pktns->tx.pto_probe += 1; |
| 2637 | |
| 2638 | /* No risk to loop here, #packet per datagram is bounded */ |
| 2639 | requeue: |
| 2640 | TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2641 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
| 2642 | if (qel == iqel) { |
| 2643 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2644 | pkt = pkt->next; |
| 2645 | tmp = &htmp; |
| 2646 | hqel->pktns->tx.pto_probe += 1; |
| 2647 | TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc); |
| 2648 | goto requeue; |
| 2649 | } |
| 2650 | } |
| 2651 | |
| 2652 | end: |
| 2653 | LIST_SPLICE(ifrms, &itmp); |
| 2654 | LIST_SPLICE(hfrms, &htmp); |
| 2655 | |
| 2656 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2657 | } |
| 2658 | |
| 2659 | static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm) |
| 2660 | { |
| 2661 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 2662 | |
| 2663 | if (frm->type == QUIC_FT_CONNECTION_CLOSE) |
| 2664 | quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code); |
| 2665 | else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) { |
| 2666 | if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt) |
| 2667 | goto out; |
| 2668 | |
| 2669 | qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code); |
| 2670 | } |
| 2671 | |
| 2672 | out: |
| 2673 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 2674 | } |
| 2675 | |
| 2676 | /* Enqueue a STOP_SENDING frame to send into 1RTT packet number space |
| 2677 | * frame list to send. |
| 2678 | * Return 1 if succeeded, 0 if not. |
| 2679 | */ |
| 2680 | static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id) |
| 2681 | { |
| 2682 | int ret = 0; |
| 2683 | struct quic_frame *frm; |
| 2684 | struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2685 | uint64_t app_error_code; |
| 2686 | |
| 2687 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2688 | |
| 2689 | /* TODO: the mux may be released, we cannot have more |
| 2690 | * information about the application error code to send |
| 2691 | * at this time. |
| 2692 | */ |
| 2693 | app_error_code = H3_REQUEST_REJECTED; |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2694 | frm = qc_frm_alloc(QUIC_FT_STOP_SENDING); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2695 | if (!frm) { |
| 2696 | TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2697 | goto out; |
| 2698 | } |
| 2699 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2700 | frm->stop_sending.id = id; |
| 2701 | frm->stop_sending.app_error_code = app_error_code; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2702 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
| 2703 | ret = 1; |
| 2704 | out: |
| 2705 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2706 | return ret; |
| 2707 | } |
| 2708 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2709 | /* Release the underlying memory use by <ncbuf> non-contiguous buffer */ |
| 2710 | static void quic_free_ncbuf(struct ncbuf *ncbuf) |
| 2711 | { |
| 2712 | struct buffer buf; |
| 2713 | |
| 2714 | if (ncb_is_null(ncbuf)) |
| 2715 | return; |
| 2716 | |
| 2717 | buf = b_make(ncbuf->area, ncbuf->size, 0, 0); |
| 2718 | b_free(&buf); |
| 2719 | offer_buffers(NULL, 1); |
| 2720 | |
| 2721 | *ncbuf = NCBUF_NULL; |
| 2722 | } |
| 2723 | |
| 2724 | /* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */ |
| 2725 | static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf) |
| 2726 | { |
| 2727 | struct buffer buf = BUF_NULL; |
| 2728 | |
| 2729 | if (!ncb_is_null(ncbuf)) |
| 2730 | return ncbuf; |
| 2731 | |
| 2732 | b_alloc(&buf); |
| 2733 | BUG_ON(b_is_null(&buf)); |
| 2734 | |
| 2735 | *ncbuf = ncb_make(buf.area, buf.size, 0); |
| 2736 | ncb_init(ncbuf, 0); |
| 2737 | |
| 2738 | return ncbuf; |
| 2739 | } |
| 2740 | |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2741 | /* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn. |
| 2742 | * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the |
| 2743 | * speed up handshake completion may be run after having received duplicated |
| 2744 | * CRYPTO data. |
| 2745 | */ |
| 2746 | static int qc_handle_crypto_frm(struct quic_conn *qc, |
| 2747 | struct quic_crypto *frm, struct quic_rx_packet *pkt, |
| 2748 | struct quic_enc_level *qel, int *fast_retrans) |
| 2749 | { |
| 2750 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2751 | enum ncb_ret ncb_ret; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2752 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2753 | struct quic_rx_crypto_frm cfdebug = { |
| 2754 | .offset_node.key = frm->offset, |
| 2755 | .len = frm->len, |
| 2756 | }; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2757 | struct quic_cstream *cstream = qel->cstream; |
| 2758 | struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2759 | |
| 2760 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2761 | if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) { |
| 2762 | TRACE_PROTO("CRYPTO data discarded", |
| 2763 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2764 | goto done; |
| 2765 | } |
| 2766 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2767 | if (unlikely(frm->offset < cstream->rx.offset)) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2768 | size_t diff; |
| 2769 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2770 | if (frm->offset + frm->len <= cstream->rx.offset) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2771 | /* Nothing to do */ |
| 2772 | TRACE_PROTO("Already received CRYPTO data", |
| 2773 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2774 | if (qc_is_listener(qc) && qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] && |
| 2775 | !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP)) |
| 2776 | *fast_retrans = 1; |
| 2777 | goto done; |
| 2778 | } |
| 2779 | |
| 2780 | TRACE_PROTO("Partially already received CRYPTO data", |
| 2781 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2782 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2783 | diff = cstream->rx.offset - frm->offset; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2784 | frm->len -= diff; |
| 2785 | frm->data += diff; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2786 | frm->offset = cstream->rx.offset; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2787 | } |
| 2788 | |
Amaury Denoyelle | ff95f2d | 2022-11-18 14:50:06 +0100 | [diff] [blame] | 2789 | if (frm->offset == cstream->rx.offset && ncb_is_empty(ncbuf)) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2790 | if (!qc_provide_cdata(qel, qc->xprt_ctx, frm->data, frm->len, |
| 2791 | pkt, &cfdebug)) { |
| 2792 | // trace already emitted by function above |
| 2793 | goto leave; |
| 2794 | } |
| 2795 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2796 | cstream->rx.offset += frm->len; |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2797 | TRACE_DEVEL("increment crypto level offset", QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2798 | goto done; |
| 2799 | } |
| 2800 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2801 | if (!quic_get_ncbuf(ncbuf) || |
| 2802 | ncb_is_null(ncbuf)) { |
| 2803 | TRACE_ERROR("CRYPTO ncbuf allocation failed", QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2804 | goto leave; |
| 2805 | } |
| 2806 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2807 | /* frm->offset > cstream-trx.offset */ |
| 2808 | ncb_ret = ncb_add(ncbuf, frm->offset - cstream->rx.offset, |
| 2809 | (const char *)frm->data, frm->len, NCB_ADD_COMPARE); |
| 2810 | if (ncb_ret != NCB_RET_OK) { |
| 2811 | if (ncb_ret == NCB_RET_DATA_REJ) { |
| 2812 | TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc); |
| 2813 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION)); |
| 2814 | } |
| 2815 | else if (ncb_ret == NCB_RET_GAP_SIZE) { |
| 2816 | TRACE_ERROR("cannot bufferize frame due to gap size limit", |
| 2817 | QUIC_EV_CONN_PRSHPKT, qc); |
| 2818 | } |
| 2819 | goto leave; |
| 2820 | } |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2821 | |
| 2822 | done: |
| 2823 | ret = 1; |
| 2824 | leave: |
| 2825 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2826 | return ret; |
| 2827 | } |
| 2828 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2829 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel> |
| 2830 | * as encryption level. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2831 | * Returns 1 if succeeded, 0 if failed. |
| 2832 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2833 | static int qc_parse_pkt_frms(struct quic_conn *qc, struct quic_rx_packet *pkt, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2834 | struct quic_enc_level *qel) |
| 2835 | { |
| 2836 | struct quic_frame frm; |
| 2837 | const unsigned char *pos, *end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2838 | int fast_retrans = 0, ret = 0; |
| 2839 | |
| 2840 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2841 | /* Skip the AAD */ |
| 2842 | pos = pkt->data + pkt->aad_len; |
| 2843 | end = pkt->data + pkt->len; |
| 2844 | |
| 2845 | while (pos < end) { |
| 2846 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) { |
| 2847 | // trace already emitted by function above |
| 2848 | goto leave; |
| 2849 | } |
| 2850 | |
| 2851 | TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm); |
| 2852 | switch (frm.type) { |
| 2853 | case QUIC_FT_PADDING: |
| 2854 | break; |
| 2855 | case QUIC_FT_PING: |
| 2856 | break; |
| 2857 | case QUIC_FT_ACK: |
| 2858 | { |
| 2859 | unsigned int rtt_sample; |
| 2860 | |
| 2861 | rtt_sample = 0; |
| 2862 | if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) { |
| 2863 | // trace already emitted by function above |
| 2864 | goto leave; |
| 2865 | } |
| 2866 | |
| 2867 | if (rtt_sample) { |
| 2868 | unsigned int ack_delay; |
| 2869 | |
| 2870 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
| 2871 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
| 2872 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2873 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
| 2874 | quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc); |
| 2875 | } |
| 2876 | break; |
| 2877 | } |
| 2878 | case QUIC_FT_RESET_STREAM: |
Amaury Denoyelle | 5854fc0 | 2022-12-09 16:25:48 +0100 | [diff] [blame] | 2879 | if (qc->mux_state == QC_MUX_READY) { |
| 2880 | struct quic_reset_stream *rs = &frm.reset_stream; |
| 2881 | qcc_recv_reset_stream(qc->qcc, rs->id, rs->app_error_code, rs->final_size); |
| 2882 | } |
| 2883 | break; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2884 | case QUIC_FT_STOP_SENDING: |
| 2885 | { |
| 2886 | struct quic_stop_sending *stop_sending = &frm.stop_sending; |
| 2887 | if (qc->mux_state == QC_MUX_READY) { |
| 2888 | if (qcc_recv_stop_sending(qc->qcc, stop_sending->id, |
| 2889 | stop_sending->app_error_code)) { |
| 2890 | TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2891 | goto leave; |
| 2892 | } |
| 2893 | } |
| 2894 | break; |
| 2895 | } |
| 2896 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2897 | if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2898 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2899 | break; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2900 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2901 | { |
| 2902 | struct quic_stream *stream = &frm.stream; |
| 2903 | unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams; |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2904 | const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2905 | |
| 2906 | /* The upper layer may not be allocated. */ |
| 2907 | if (qc->mux_state != QC_MUX_READY) { |
| 2908 | if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) { |
| 2909 | TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc); |
| 2910 | break; |
| 2911 | } |
| 2912 | else { |
| 2913 | TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | d18025e | 2023-01-20 15:33:50 +0100 | [diff] [blame] | 2914 | if (qc->app_ops == &h3_ops && quic_stream_is_uni(stream->id)) { |
| 2915 | /* Do not send STOP_SENDING frames for h3 unidirectional streams. |
| 2916 | * TODO: this test should be removed when the connection closure |
| 2917 | * will be more clean. |
| 2918 | * At quic_conn level there is no mean to know that an application |
| 2919 | * want to forbid stream closure requests to receivers. This is the |
| 2920 | * case for the Control and QPACK h3 unidirectional streams. |
| 2921 | */ |
| 2922 | goto leave; |
| 2923 | } |
| 2924 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2925 | if (!qc_stop_sending_frm_enqueue(qc, stream->id)) |
| 2926 | TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2927 | /* This packet will not be acknowledged */ |
| 2928 | goto leave; |
| 2929 | } |
| 2930 | } |
| 2931 | |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2932 | if (!qc_handle_strm_frm(pkt, stream, qc, fin)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2933 | TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2934 | goto leave; |
| 2935 | } |
| 2936 | |
| 2937 | break; |
| 2938 | } |
| 2939 | case QUIC_FT_MAX_DATA: |
| 2940 | if (qc->mux_state == QC_MUX_READY) { |
| 2941 | struct quic_max_data *data = &frm.max_data; |
| 2942 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2943 | } |
| 2944 | break; |
| 2945 | case QUIC_FT_MAX_STREAM_DATA: |
| 2946 | if (qc->mux_state == QC_MUX_READY) { |
| 2947 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 2948 | if (qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2949 | data->max_stream_data)) { |
| 2950 | TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2951 | goto leave; |
| 2952 | } |
| 2953 | } |
| 2954 | break; |
| 2955 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2956 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2957 | break; |
| 2958 | case QUIC_FT_DATA_BLOCKED: |
| 2959 | HA_ATOMIC_INC(&qc->prx_counters->data_blocked); |
| 2960 | break; |
| 2961 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2962 | HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked); |
| 2963 | break; |
| 2964 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2965 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi); |
| 2966 | break; |
| 2967 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2968 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni); |
| 2969 | break; |
| 2970 | case QUIC_FT_NEW_CONNECTION_ID: |
| 2971 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2972 | /* XXX TO DO XXX */ |
| 2973 | break; |
| 2974 | case QUIC_FT_CONNECTION_CLOSE: |
| 2975 | case QUIC_FT_CONNECTION_CLOSE_APP: |
| 2976 | /* Increment the error counters */ |
| 2977 | qc_cc_err_count_inc(qc, &frm); |
| 2978 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
| 2979 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2980 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2981 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2982 | } |
| 2983 | TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 2984 | /* RFC 9000 10.2. Immediate Close: |
| 2985 | * The closing and draining connection states exist to ensure |
| 2986 | * that connections close cleanly and that delayed or reordered |
| 2987 | * packets are properly discarded. These states SHOULD persist |
| 2988 | * for at least three times the current PTO interval... |
| 2989 | * |
| 2990 | * Rearm the idle timeout only one time when entering draining |
| 2991 | * state. |
| 2992 | */ |
| 2993 | qc_idle_timer_do_rearm(qc); |
| 2994 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 2995 | qc_notify_close(qc); |
| 2996 | } |
| 2997 | break; |
| 2998 | case QUIC_FT_HANDSHAKE_DONE: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2999 | if (qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3000 | TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame", |
| 3001 | QUIC_EV_CONN_PRSHPKT, qc); |
| 3002 | goto leave; |
| 3003 | } |
| 3004 | |
| 3005 | qc->state = QUIC_HS_ST_CONFIRMED; |
| 3006 | break; |
| 3007 | default: |
| 3008 | TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc); |
| 3009 | goto leave; |
| 3010 | } |
| 3011 | } |
| 3012 | |
| 3013 | /* Flag this packet number space as having received a packet. */ |
| 3014 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
| 3015 | |
| 3016 | if (fast_retrans) { |
| 3017 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3018 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3019 | |
| 3020 | TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc); |
| 3021 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 3022 | qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP; |
| 3023 | } |
| 3024 | |
| 3025 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 3026 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 3027 | * be discarded. |
| 3028 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3029 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3030 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
| 3031 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 3032 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3033 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3034 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 3035 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3036 | qc_set_timer(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3037 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3038 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 3039 | } |
| 3040 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 3041 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | ret = 1; |
| 3046 | leave: |
| 3047 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 3048 | return ret; |
| 3049 | } |
| 3050 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3051 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3052 | /* Allocate Tx buffer from <qc> quic-conn if needed. |
| 3053 | * |
| 3054 | * Returns allocated buffer or NULL on error. |
| 3055 | */ |
| 3056 | static struct buffer *qc_txb_alloc(struct quic_conn *qc) |
| 3057 | { |
| 3058 | struct buffer *buf = &qc->tx.buf; |
| 3059 | if (!b_alloc(buf)) |
| 3060 | return NULL; |
| 3061 | |
| 3062 | return buf; |
| 3063 | } |
| 3064 | |
| 3065 | /* Free Tx buffer from <qc> if it is empty. */ |
| 3066 | static void qc_txb_release(struct quic_conn *qc) |
| 3067 | { |
| 3068 | struct buffer *buf = &qc->tx.buf; |
| 3069 | |
| 3070 | /* For the moment sending function is responsible to purge the buffer |
| 3071 | * entirely. It may change in the future but this requires to be able |
| 3072 | * to reuse old data. |
| 3073 | */ |
| 3074 | BUG_ON_HOT(buf && b_data(buf)); |
| 3075 | |
| 3076 | if (!b_data(buf)) { |
| 3077 | b_free(buf); |
| 3078 | offer_buffers(NULL, 1); |
| 3079 | } |
| 3080 | } |
| 3081 | |
| 3082 | /* Commit a datagram payload written into <buf> of length <length>. <first_pkt> |
| 3083 | * must contains the address of the first packet stored in the payload. |
| 3084 | * |
| 3085 | * Caller is responsible that there is enough space in the buffer. |
| 3086 | */ |
| 3087 | static void qc_txb_store(struct buffer *buf, uint16_t length, |
| 3088 | struct quic_tx_packet *first_pkt) |
| 3089 | { |
| 3090 | const size_t hdlen = sizeof(uint16_t) + sizeof(void *); |
| 3091 | BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */ |
| 3092 | |
| 3093 | write_u16(b_tail(buf), length); |
| 3094 | write_ptr(b_tail(buf) + sizeof(length), first_pkt); |
| 3095 | b_add(buf, hdlen + length); |
| 3096 | } |
| 3097 | |
| 3098 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 3099 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 3100 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 3101 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 3102 | * <force_ack> may be set to 1 if you want to force an ack. |
| 3103 | */ |
| 3104 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 3105 | struct quic_enc_level *qel, int cc, int probe, int force_ack) |
| 3106 | { |
| 3107 | unsigned int must_ack = force_ack || |
| 3108 | (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)); |
| 3109 | |
| 3110 | /* Do not build any more packet if the TX secrets are not available or |
| 3111 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 3112 | * and if there is no more packets to send upon PTO expiration |
| 3113 | * and if there is no more ack-eliciting frames to send or in flight |
| 3114 | * congestion control limit is reached for prepared data |
| 3115 | */ |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 3116 | if (!quic_tls_has_tx_sec(qel) || |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3117 | (!cc && !probe && !must_ack && |
| 3118 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 3119 | return 0; |
| 3120 | } |
| 3121 | |
| 3122 | return 1; |
| 3123 | } |
| 3124 | |
| 3125 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3126 | * <frms>. Each packet is stored in a distinct datagram written to <buf>. |
| 3127 | * |
| 3128 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3129 | * the address of the packet contained in the datagram. |
| 3130 | * |
| 3131 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3132 | * -1 if something wrong happened. |
| 3133 | */ |
| 3134 | static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3135 | struct list *frms) |
| 3136 | { |
| 3137 | int ret = -1; |
| 3138 | struct quic_enc_level *qel; |
| 3139 | unsigned char *end, *pos; |
| 3140 | struct quic_tx_packet *pkt; |
| 3141 | size_t total; |
| 3142 | /* Each datagram is prepended with its length followed by the address |
| 3143 | * of the first packet in the datagram. |
| 3144 | */ |
| 3145 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt); |
| 3146 | |
| 3147 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3148 | |
| 3149 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3150 | total = 0; |
| 3151 | pos = (unsigned char *)b_tail(buf); |
| 3152 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) { |
| 3153 | int err, probe, cc; |
| 3154 | |
| 3155 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
| 3156 | probe = 0; |
| 3157 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3158 | /* We do not probe if an immediate close was asked */ |
| 3159 | if (!cc) |
| 3160 | probe = qel->pktns->tx.pto_probe; |
| 3161 | |
| 3162 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0)) |
| 3163 | break; |
| 3164 | |
| 3165 | /* Leave room for the datagram header */ |
| 3166 | pos += dg_headlen; |
| 3167 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3168 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3169 | } |
| 3170 | else { |
| 3171 | end = pos + qc->path->mtu; |
| 3172 | } |
| 3173 | |
| 3174 | pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0, |
| 3175 | QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err); |
| 3176 | switch (err) { |
| 3177 | case -2: |
| 3178 | // trace already emitted by function above |
| 3179 | goto leave; |
| 3180 | case -1: |
| 3181 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 3182 | * MTU, we are here because of the congestion control window. There is |
| 3183 | * no need to try to reuse this buffer. |
| 3184 | */ |
| 3185 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
| 3186 | goto out; |
| 3187 | default: |
| 3188 | break; |
| 3189 | } |
| 3190 | |
| 3191 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 3192 | BUG_ON(!pkt); |
| 3193 | |
| 3194 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3195 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3196 | |
| 3197 | total += pkt->len; |
| 3198 | |
| 3199 | /* Write datagram header. */ |
| 3200 | qc_txb_store(buf, pkt->len, pkt); |
| 3201 | } |
| 3202 | |
| 3203 | out: |
| 3204 | ret = total; |
| 3205 | leave: |
| 3206 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3207 | return ret; |
| 3208 | } |
| 3209 | |
| 3210 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3211 | * <frms>. Several packets can be regrouped in a single datagram. The result is |
| 3212 | * written into <buf>. |
| 3213 | * |
| 3214 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3215 | * the address of first packet in the datagram. |
| 3216 | * |
| 3217 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3218 | * -1 if something wrong happened. |
| 3219 | */ |
| 3220 | static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3221 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3222 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3223 | { |
| 3224 | struct quic_enc_level *qel; |
| 3225 | unsigned char *end, *pos; |
| 3226 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 3227 | /* length of datagrams */ |
| 3228 | uint16_t dglen; |
| 3229 | size_t total; |
| 3230 | int ret = -1, padding; |
| 3231 | /* Each datagram is prepended with its length followed by the address |
| 3232 | * of the first packet in the datagram. |
| 3233 | */ |
| 3234 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt); |
| 3235 | struct list *frms; |
| 3236 | |
| 3237 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3238 | |
| 3239 | /* Currently qc_prep_pkts() does not handle buffer wrapping so the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 3240 | * caller must ensure that buf is reset. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3241 | */ |
| 3242 | BUG_ON_HOT(buf->head || buf->data); |
| 3243 | |
| 3244 | total = 0; |
| 3245 | qel = &qc->els[tel]; |
| 3246 | frms = tel_frms; |
| 3247 | dglen = 0; |
| 3248 | padding = 0; |
| 3249 | pos = (unsigned char *)b_head(buf); |
| 3250 | first_pkt = prv_pkt = NULL; |
| 3251 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
| 3252 | int err, probe, cc; |
| 3253 | enum quic_pkt_type pkt_type; |
| 3254 | struct quic_tls_ctx *tls_ctx; |
| 3255 | const struct quic_version *ver; |
| 3256 | int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 3257 | (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3258 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3259 | |
| 3260 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
| 3261 | probe = 0; |
| 3262 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3263 | /* We do not probe if an immediate close was asked */ |
| 3264 | if (!cc) |
| 3265 | probe = qel->pktns->tx.pto_probe; |
| 3266 | |
| 3267 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) { |
| 3268 | if (prv_pkt) |
| 3269 | qc_txb_store(buf, dglen, first_pkt); |
| 3270 | /* Let's select the next encryption level */ |
| 3271 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 3272 | tel = next_tel; |
| 3273 | frms = next_tel_frms; |
| 3274 | qel = &qc->els[tel]; |
| 3275 | /* Build a new datagram */ |
| 3276 | prv_pkt = NULL; |
| 3277 | TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc); |
| 3278 | continue; |
| 3279 | } |
| 3280 | break; |
| 3281 | } |
| 3282 | |
| 3283 | pkt_type = quic_tls_level_pkt_type(tel); |
| 3284 | if (!prv_pkt) { |
| 3285 | /* Leave room for the datagram header */ |
| 3286 | pos += dg_headlen; |
| 3287 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3288 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3289 | } |
| 3290 | else { |
| 3291 | end = pos + qc->path->mtu; |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | if (qc->negotiated_version) { |
| 3296 | ver = qc->negotiated_version; |
| 3297 | if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 3298 | tls_ctx = &qc->negotiated_ictx; |
| 3299 | else |
| 3300 | tls_ctx = &qel->tls_ctx; |
| 3301 | } |
| 3302 | else { |
| 3303 | ver = qc->original_version; |
| 3304 | tls_ctx = &qel->tls_ctx; |
| 3305 | } |
| 3306 | |
| 3307 | cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms, |
| 3308 | qc, ver, dglen, pkt_type, |
| 3309 | force_ack, padding, probe, cc, &err); |
| 3310 | switch (err) { |
| 3311 | case -2: |
| 3312 | // trace already emitted by function above |
| 3313 | goto leave; |
| 3314 | case -1: |
| 3315 | /* If there was already a correct packet present, set the |
| 3316 | * current datagram as prepared into <cbuf>. |
| 3317 | */ |
| 3318 | if (prv_pkt) |
| 3319 | qc_txb_store(buf, dglen, first_pkt); |
| 3320 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
| 3321 | goto out; |
| 3322 | default: |
| 3323 | break; |
| 3324 | } |
| 3325 | |
| 3326 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 3327 | BUG_ON(!cur_pkt); |
| 3328 | |
| 3329 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3330 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3331 | |
| 3332 | total += cur_pkt->len; |
| 3333 | /* keep trace of the first packet in the datagram */ |
| 3334 | if (!first_pkt) |
| 3335 | first_pkt = cur_pkt; |
Frédéric Lécaille | 74b5f7b | 2022-11-20 18:35:35 +0100 | [diff] [blame] | 3336 | /* Attach the current one to the previous one and vice versa */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3337 | if (prv_pkt) { |
| 3338 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 3339 | cur_pkt->prev = prv_pkt; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3340 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 3341 | } |
| 3342 | /* Let's say we have to build a new dgram */ |
| 3343 | prv_pkt = NULL; |
| 3344 | dglen += cur_pkt->len; |
| 3345 | /* Client: discard the Initial encryption keys as soon as |
| 3346 | * a handshake packet could be built. |
| 3347 | */ |
| 3348 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
| 3349 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 3350 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3351 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3352 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 3353 | qc_set_timer(qc); |
| 3354 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3355 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 3356 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
| 3357 | } |
| 3358 | /* If the data for the current encryption level have all been sent, |
| 3359 | * select the next level. |
| 3360 | */ |
| 3361 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
| 3362 | next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) { |
| 3363 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 3364 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 3365 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3366 | tel = next_tel; |
| 3367 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 3368 | frms = &qc->els[tel].pktns->tx.frms; |
| 3369 | else |
| 3370 | frms = next_tel_frms; |
| 3371 | qel = &qc->els[tel]; |
| 3372 | if (!LIST_ISEMPTY(frms)) { |
| 3373 | /* If there is data for the next level, do not |
| 3374 | * consume a datagram. |
| 3375 | */ |
| 3376 | prv_pkt = cur_pkt; |
| 3377 | } |
| 3378 | } |
| 3379 | |
| 3380 | /* If we have to build a new datagram, set the current datagram as |
| 3381 | * prepared into <cbuf>. |
| 3382 | */ |
| 3383 | if (!prv_pkt) { |
| 3384 | qc_txb_store(buf, dglen, first_pkt); |
| 3385 | first_pkt = NULL; |
| 3386 | dglen = 0; |
| 3387 | padding = 0; |
| 3388 | } |
| 3389 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
| 3390 | (!qc_is_listener(qc) || |
| 3391 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3392 | padding = 1; |
| 3393 | } |
| 3394 | } |
| 3395 | |
| 3396 | out: |
| 3397 | ret = total; |
| 3398 | leave: |
| 3399 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3400 | return ret; |
| 3401 | } |
| 3402 | |
| 3403 | /* Send datagrams stored in <buf>. |
| 3404 | * |
| 3405 | * This function always returns 1 for success. Even if sendto() syscall failed, |
| 3406 | * buffer is drained and packets are considered as emitted. QUIC loss detection |
| 3407 | * mechanism is used as a back door way to retry sending. |
| 3408 | */ |
| 3409 | int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx) |
| 3410 | { |
| 3411 | struct quic_conn *qc; |
| 3412 | char skip_sendto = 0; |
| 3413 | |
| 3414 | qc = ctx->qc; |
| 3415 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
| 3416 | while (b_contig_data(buf, 0)) { |
| 3417 | unsigned char *pos; |
| 3418 | struct buffer tmpbuf = { }; |
| 3419 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3420 | uint16_t dglen; |
| 3421 | size_t headlen = sizeof dglen + sizeof first_pkt; |
| 3422 | unsigned int time_sent; |
| 3423 | |
| 3424 | pos = (unsigned char *)b_head(buf); |
| 3425 | dglen = read_u16(pos); |
| 3426 | BUG_ON_HOT(!dglen); /* this should not happen */ |
| 3427 | |
| 3428 | pos += sizeof dglen; |
| 3429 | first_pkt = read_ptr(pos); |
| 3430 | pos += sizeof first_pkt; |
| 3431 | tmpbuf.area = (char *)pos; |
| 3432 | tmpbuf.size = tmpbuf.data = dglen; |
| 3433 | |
| 3434 | TRACE_DATA("send dgram", QUIC_EV_CONN_SPPKTS, qc); |
| 3435 | /* If sendto is on error just skip the call to it for the rest |
| 3436 | * of the loop but continue to purge the buffer. Data will be |
| 3437 | * transmitted when QUIC packets are detected as lost on our |
| 3438 | * side. |
| 3439 | * |
| 3440 | * TODO use fd-monitoring to detect when send operation can be |
| 3441 | * retry. This should improve the bandwidth without relying on |
| 3442 | * retransmission timer. However, it requires a major rework on |
| 3443 | * quic-conn fd management. |
| 3444 | */ |
| 3445 | if (!skip_sendto) { |
| 3446 | if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) { |
| 3447 | skip_sendto = 1; |
| 3448 | TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc); |
| 3449 | } |
| 3450 | } |
| 3451 | |
| 3452 | b_del(buf, dglen + headlen); |
| 3453 | qc->tx.bytes += tmpbuf.data; |
| 3454 | time_sent = now_ms; |
| 3455 | |
| 3456 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3457 | pkt->time_sent = time_sent; |
| 3458 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3459 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
| 3460 | qc->path->ifae_pkts++; |
| 3461 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3462 | qc_idle_timer_rearm(qc, 0); |
| 3463 | } |
| 3464 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3465 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3466 | qc->flags |= QUIC_FL_CONN_CLOSING; |
| 3467 | qc_notify_close(qc); |
| 3468 | |
| 3469 | /* RFC 9000 10.2. Immediate Close: |
| 3470 | * The closing and draining connection states exist to ensure |
| 3471 | * that connections close cleanly and that delayed or reordered |
| 3472 | * packets are properly discarded. These states SHOULD persist |
| 3473 | * for at least three times the current PTO interval... |
| 3474 | * |
| 3475 | * Rearm the idle timeout only one time when entering closing |
| 3476 | * state. |
| 3477 | */ |
| 3478 | qc_idle_timer_do_rearm(qc); |
| 3479 | if (qc->timer_task) { |
| 3480 | task_destroy(qc->timer_task); |
| 3481 | qc->timer_task = NULL; |
| 3482 | } |
| 3483 | } |
| 3484 | qc->path->in_flight += pkt->in_flight_len; |
| 3485 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3486 | if (pkt->in_flight_len) |
| 3487 | qc_set_timer(qc); |
| 3488 | TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 3489 | next_pkt = pkt->next; |
| 3490 | quic_tx_packet_refinc(pkt); |
| 3491 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
| 3492 | } |
| 3493 | } |
| 3494 | |
| 3495 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 3496 | |
| 3497 | return 1; |
| 3498 | } |
| 3499 | |
| 3500 | /* Copy into <buf> buffer a stateless reset token depending on the |
| 3501 | * <salt> salt input. This is the cluster secret which will be derived |
| 3502 | * as HKDF input secret to generate this token. |
| 3503 | * Return 1 if succeeded, 0 if not. |
| 3504 | */ |
| 3505 | static int quic_stateless_reset_token_cpy(struct quic_conn *qc, |
| 3506 | unsigned char *buf, size_t len, |
| 3507 | const unsigned char *salt, size_t saltlen) |
| 3508 | { |
| 3509 | /* Input secret */ |
| 3510 | const unsigned char *key = (const unsigned char *)global.cluster_secret; |
| 3511 | size_t keylen = strlen(global.cluster_secret); |
| 3512 | /* Info */ |
| 3513 | const unsigned char label[] = "stateless token"; |
| 3514 | size_t labellen = sizeof label - 1; |
| 3515 | int ret; |
| 3516 | |
| 3517 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3518 | |
| 3519 | ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len, |
| 3520 | key, keylen, salt, saltlen, label, labellen); |
| 3521 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3522 | return ret; |
| 3523 | } |
| 3524 | |
| 3525 | /* Initialize the stateless reset token attached to <cid> connection ID. |
| 3526 | * Returns 1 if succeeded, 0 if not. |
| 3527 | */ |
| 3528 | static int quic_stateless_reset_token_init(struct quic_conn *qc, |
| 3529 | struct quic_connection_id *quic_cid) |
| 3530 | { |
| 3531 | int ret; |
| 3532 | |
| 3533 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3534 | |
| 3535 | if (global.cluster_secret) { |
| 3536 | /* Output secret */ |
| 3537 | unsigned char *token = quic_cid->stateless_reset_token; |
| 3538 | size_t tokenlen = sizeof quic_cid->stateless_reset_token; |
| 3539 | /* Salt */ |
| 3540 | const unsigned char *cid = quic_cid->cid.data; |
| 3541 | size_t cidlen = quic_cid->cid.len; |
| 3542 | |
| 3543 | ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen); |
| 3544 | } |
| 3545 | else { |
| 3546 | /* TODO: RAND_bytes() should be replaced */ |
| 3547 | ret = RAND_bytes(quic_cid->stateless_reset_token, |
| 3548 | sizeof quic_cid->stateless_reset_token) == 1; |
| 3549 | } |
| 3550 | |
| 3551 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3552 | return ret; |
| 3553 | } |
| 3554 | |
| 3555 | /* Allocate a new CID with <seq_num> as sequence number and attach it to <root> |
| 3556 | * ebtree. |
| 3557 | * |
| 3558 | * The CID is randomly generated in part with the result altered to be |
| 3559 | * associated with the current thread ID. This means this function must only |
| 3560 | * be called by the quic_conn thread. |
| 3561 | * |
| 3562 | * Returns the new CID if succeeded, NULL if not. |
| 3563 | */ |
| 3564 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
| 3565 | struct quic_conn *qc, |
| 3566 | int seq_num) |
| 3567 | { |
| 3568 | struct quic_connection_id *cid; |
| 3569 | |
| 3570 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3571 | |
| 3572 | cid = pool_alloc(pool_head_quic_connection_id); |
| 3573 | if (!cid) { |
| 3574 | TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 3575 | goto err; |
| 3576 | } |
| 3577 | |
| 3578 | cid->cid.len = QUIC_HAP_CID_LEN; |
| 3579 | /* TODO: RAND_bytes() should be replaced */ |
| 3580 | if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) { |
| 3581 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc); |
| 3582 | goto err; |
| 3583 | } |
| 3584 | |
| 3585 | quic_pin_cid_to_tid(cid->cid.data, tid); |
| 3586 | if (quic_stateless_reset_token_init(qc, cid) != 1) { |
| 3587 | TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc); |
| 3588 | goto err; |
| 3589 | } |
| 3590 | |
| 3591 | cid->qc = qc; |
| 3592 | |
| 3593 | cid->seq_num.key = seq_num; |
| 3594 | cid->retire_prior_to = 0; |
| 3595 | /* insert the allocated CID in the quic_conn tree */ |
| 3596 | eb64_insert(root, &cid->seq_num); |
| 3597 | |
| 3598 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3599 | return cid; |
| 3600 | |
| 3601 | err: |
| 3602 | pool_free(pool_head_quic_connection_id, cid); |
| 3603 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3604 | return NULL; |
| 3605 | } |
| 3606 | |
| 3607 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3608 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3609 | * a HANDSHAKE_DONE frame. |
| 3610 | * Return 1 if succeeded, 0 if not. |
| 3611 | */ |
| 3612 | static int quic_build_post_handshake_frames(struct quic_conn *qc) |
| 3613 | { |
| 3614 | int ret = 0, i, first, max; |
| 3615 | struct quic_enc_level *qel; |
| 3616 | struct quic_frame *frm, *frmbak; |
| 3617 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3618 | struct eb64_node *node; |
| 3619 | |
| 3620 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 3621 | |
| 3622 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3623 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
| 3624 | if (qc_is_listener(qc)) { |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 3625 | frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3626 | if (!frm) { |
| 3627 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3628 | goto leave; |
| 3629 | } |
| 3630 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3631 | LIST_APPEND(&frm_list, &frm->list); |
| 3632 | } |
| 3633 | |
| 3634 | /* Initialize <max> connection IDs minus one: there is |
| 3635 | * already one connection ID used for the current connection. |
| 3636 | */ |
| 3637 | first = 1; |
| 3638 | max = qc->tx.params.active_connection_id_limit; |
| 3639 | |
| 3640 | /* TODO: check limit */ |
| 3641 | for (i = first; i < max; i++) { |
| 3642 | struct quic_connection_id *cid; |
| 3643 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 3644 | frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3645 | if (!frm) { |
| 3646 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3647 | goto err; |
| 3648 | } |
| 3649 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3650 | cid = new_quic_cid(&qc->cids, qc, i); |
| 3651 | if (!cid) { |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 3652 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3653 | TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3654 | goto err; |
| 3655 | } |
| 3656 | |
| 3657 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 3658 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
| 3659 | |
| 3660 | quic_connection_id_to_frm_cpy(frm, cid); |
| 3661 | LIST_APPEND(&frm_list, &frm->list); |
| 3662 | } |
| 3663 | |
| 3664 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
| 3665 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
| 3666 | |
| 3667 | ret = 1; |
| 3668 | leave: |
| 3669 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 3670 | return ret; |
| 3671 | |
| 3672 | err: |
| 3673 | /* free the frames */ |
| 3674 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 3675 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3676 | |
| 3677 | node = eb64_lookup_ge(&qc->cids, first); |
| 3678 | while (node) { |
| 3679 | struct quic_connection_id *cid; |
| 3680 | |
| 3681 | cid = eb64_entry(node, struct quic_connection_id, seq_num); |
| 3682 | if (cid->seq_num.key >= max) |
| 3683 | break; |
| 3684 | |
| 3685 | node = eb64_next(node); |
| 3686 | ebmb_delete(&cid->node); |
| 3687 | eb64_delete(&cid->seq_num); |
| 3688 | pool_free(pool_head_quic_connection_id, cid); |
| 3689 | } |
| 3690 | goto leave; |
| 3691 | } |
| 3692 | |
| 3693 | /* Deallocate <l> list of ACK ranges. */ |
| 3694 | void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs) |
| 3695 | { |
| 3696 | struct eb64_node *n; |
| 3697 | struct quic_arng_node *ar; |
| 3698 | |
| 3699 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 3700 | |
| 3701 | n = eb64_first(&arngs->root); |
| 3702 | while (n) { |
| 3703 | struct eb64_node *next; |
| 3704 | |
| 3705 | ar = eb64_entry(n, struct quic_arng_node, first); |
| 3706 | next = eb64_next(n); |
| 3707 | eb64_delete(n); |
| 3708 | pool_free(pool_head_quic_arng, ar); |
| 3709 | n = next; |
| 3710 | } |
| 3711 | |
| 3712 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 3713 | } |
| 3714 | |
| 3715 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3716 | * descending order. |
| 3717 | */ |
| 3718 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3719 | struct quic_arng_node *q) |
| 3720 | { |
| 3721 | return p->first.key - q->last - 2; |
| 3722 | } |
| 3723 | |
| 3724 | |
| 3725 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3726 | * encoded size until it goes below <limit>. |
| 3727 | * Returns 1 if succeeded, 0 if not (no more element to remove). |
| 3728 | */ |
| 3729 | static int quic_rm_last_ack_ranges(struct quic_conn *qc, |
| 3730 | struct quic_arngs *arngs, size_t limit) |
| 3731 | { |
| 3732 | int ret = 0; |
| 3733 | struct eb64_node *last, *prev; |
| 3734 | |
| 3735 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3736 | |
| 3737 | last = eb64_last(&arngs->root); |
| 3738 | while (last && arngs->enc_sz > limit) { |
| 3739 | struct quic_arng_node *last_node, *prev_node; |
| 3740 | |
| 3741 | prev = eb64_prev(last); |
| 3742 | if (!prev) { |
| 3743 | TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc); |
| 3744 | goto out; |
| 3745 | } |
| 3746 | |
| 3747 | last_node = eb64_entry(last, struct quic_arng_node, first); |
| 3748 | prev_node = eb64_entry(prev, struct quic_arng_node, first); |
| 3749 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3750 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3751 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3752 | --arngs->sz; |
| 3753 | eb64_delete(last); |
| 3754 | pool_free(pool_head_quic_arng, last); |
| 3755 | last = prev; |
| 3756 | } |
| 3757 | |
| 3758 | ret = 1; |
| 3759 | out: |
| 3760 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3761 | return ret; |
| 3762 | } |
| 3763 | |
| 3764 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3765 | static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs) |
| 3766 | { |
| 3767 | struct eb64_node *node, *next; |
| 3768 | struct quic_arng_node *ar, *ar_next; |
| 3769 | |
| 3770 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3771 | |
| 3772 | node = eb64_last(&arngs->root); |
| 3773 | if (!node) |
| 3774 | goto leave; |
| 3775 | |
| 3776 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 3777 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3778 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3779 | |
| 3780 | while ((next = eb64_prev(node))) { |
| 3781 | ar_next = eb64_entry(next, struct quic_arng_node, first); |
| 3782 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3783 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3784 | node = next; |
| 3785 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 3786 | } |
| 3787 | |
| 3788 | leave: |
| 3789 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3790 | } |
| 3791 | |
| 3792 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3793 | * Returns the ack range node which has been inserted if succeeded, NULL if not. |
| 3794 | */ |
| 3795 | static inline |
| 3796 | struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc, |
| 3797 | struct quic_arngs *arngs, |
| 3798 | struct quic_arng *ar) |
| 3799 | { |
| 3800 | struct quic_arng_node *new_ar; |
| 3801 | |
| 3802 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3803 | |
| 3804 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3805 | if (!new_ar) { |
| 3806 | TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc); |
| 3807 | goto leave; |
| 3808 | } |
| 3809 | |
| 3810 | new_ar->first.key = ar->first; |
| 3811 | new_ar->last = ar->last; |
| 3812 | eb64_insert(&arngs->root, &new_ar->first); |
| 3813 | arngs->sz++; |
| 3814 | |
| 3815 | leave: |
| 3816 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 3817 | return new_ar; |
| 3818 | } |
| 3819 | |
| 3820 | /* Update <arngs> tree of ACK ranges with <ar> as new ACK range value. |
| 3821 | * Note that this function computes the number of bytes required to encode |
| 3822 | * this tree of ACK ranges in descending order. |
| 3823 | * |
| 3824 | * Descending order |
| 3825 | * -------------> |
| 3826 | * range1 range2 |
| 3827 | * ..........|--------|..............|--------| |
| 3828 | * ^ ^ ^ ^ |
| 3829 | * | | | | |
| 3830 | * last1 first1 last2 first2 |
| 3831 | * ..........+--------+--------------+--------+...... |
| 3832 | * diff1 gap12 diff2 |
| 3833 | * |
| 3834 | * To encode the previous list of ranges we must encode integers as follows in |
| 3835 | * descending order: |
| 3836 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
| 3837 | * with diff1 = last1 - first1 |
| 3838 | * diff2 = last2 - first2 |
| 3839 | * gap12 = first1 - last2 - 2 (>= 0) |
| 3840 | * |
| 3841 | |
| 3842 | returns 0 on error |
| 3843 | |
| 3844 | */ |
| 3845 | int quic_update_ack_ranges_list(struct quic_conn *qc, |
| 3846 | struct quic_arngs *arngs, |
| 3847 | struct quic_arng *ar) |
| 3848 | { |
| 3849 | int ret = 0; |
| 3850 | struct eb64_node *le; |
| 3851 | struct quic_arng_node *new_node; |
| 3852 | struct eb64_node *new; |
| 3853 | |
| 3854 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3855 | |
| 3856 | new = NULL; |
| 3857 | if (eb_is_empty(&arngs->root)) { |
| 3858 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 3859 | if (new_node) |
| 3860 | ret = 1; |
| 3861 | |
| 3862 | goto leave; |
| 3863 | } |
| 3864 | |
| 3865 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3866 | if (!le) { |
| 3867 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 3868 | if (!new_node) |
| 3869 | goto leave; |
| 3870 | |
| 3871 | new = &new_node->first; |
| 3872 | } |
| 3873 | else { |
| 3874 | struct quic_arng_node *le_ar = |
| 3875 | eb64_entry(le, struct quic_arng_node, first); |
| 3876 | |
| 3877 | /* Already existing range */ |
| 3878 | if (le_ar->last >= ar->last) { |
| 3879 | ret = 1; |
| 3880 | } |
| 3881 | else if (le_ar->last + 1 >= ar->first) { |
| 3882 | le_ar->last = ar->last; |
| 3883 | new = le; |
| 3884 | new_node = le_ar; |
| 3885 | } |
| 3886 | else { |
| 3887 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 3888 | if (!new_node) |
| 3889 | goto leave; |
| 3890 | |
| 3891 | new = &new_node->first; |
| 3892 | } |
| 3893 | } |
| 3894 | |
| 3895 | /* Verify that the new inserted node does not overlap the nodes |
| 3896 | * which follow it. |
| 3897 | */ |
| 3898 | if (new) { |
| 3899 | struct eb64_node *next; |
| 3900 | struct quic_arng_node *next_node; |
| 3901 | |
| 3902 | while ((next = eb64_next(new))) { |
| 3903 | next_node = |
| 3904 | eb64_entry(next, struct quic_arng_node, first); |
| 3905 | if (new_node->last + 1 < next_node->first.key) |
| 3906 | break; |
| 3907 | |
| 3908 | if (next_node->last > new_node->last) |
| 3909 | new_node->last = next_node->last; |
| 3910 | eb64_delete(next); |
| 3911 | pool_free(pool_head_quic_arng, next_node); |
| 3912 | /* Decrement the size of these ranges. */ |
| 3913 | arngs->sz--; |
| 3914 | } |
| 3915 | } |
| 3916 | |
| 3917 | ret = 1; |
| 3918 | leave: |
| 3919 | quic_arngs_set_enc_sz(qc, arngs); |
| 3920 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 3921 | return ret; |
| 3922 | } |
| 3923 | /* Remove the header protection of packets at <el> encryption level. |
| 3924 | * Always succeeds. |
| 3925 | */ |
| 3926 | static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el) |
| 3927 | { |
| 3928 | struct quic_tls_ctx *tls_ctx; |
| 3929 | struct quic_rx_packet *pqpkt, *pkttmp; |
| 3930 | struct quic_enc_level *app_qel; |
| 3931 | |
| 3932 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3933 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3934 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
| 3935 | if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) { |
| 3936 | TRACE_DEVEL("hp not removed (handshake not completed)", |
| 3937 | QUIC_EV_CONN_ELRMHP, qc); |
| 3938 | goto out; |
| 3939 | } |
| 3940 | tls_ctx = &el->tls_ctx; |
| 3941 | list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) { |
| 3942 | if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
| 3943 | pqpkt->data + pqpkt->pn_offset, pqpkt->data)) { |
| 3944 | TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc); |
| 3945 | } |
| 3946 | else { |
| 3947 | /* The AAD includes the packet number field */ |
| 3948 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3949 | /* Store the packet into the tree of packets to decrypt. */ |
| 3950 | pqpkt->pn_node.key = pqpkt->pn; |
| 3951 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3952 | quic_rx_packet_refinc(pqpkt); |
| 3953 | TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt); |
| 3954 | } |
| 3955 | LIST_DELETE(&pqpkt->list); |
| 3956 | quic_rx_packet_refdec(pqpkt); |
| 3957 | } |
| 3958 | |
| 3959 | out: |
| 3960 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
| 3961 | } |
| 3962 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3963 | /* Process all the CRYPTO frame at <el> encryption level. This is the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 3964 | * responsibility of the called to ensure there exists a CRYPTO data |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3965 | * stream for this level. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3966 | * Return 1 if succeeded, 0 if not. |
| 3967 | */ |
| 3968 | static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc, |
| 3969 | struct quic_enc_level *el, |
| 3970 | struct ssl_sock_ctx *ctx) |
| 3971 | { |
| 3972 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3973 | struct ncbuf *ncbuf; |
| 3974 | struct quic_cstream *cstream = el->cstream; |
| 3975 | ncb_sz_t data; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3976 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3977 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3978 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3979 | BUG_ON(!cstream); |
| 3980 | ncbuf = &cstream->rx.ncbuf; |
| 3981 | if (ncb_is_null(ncbuf)) |
| 3982 | goto done; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3983 | |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 3984 | /* TODO not working if buffer is wrapping */ |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3985 | while ((data = ncb_data(ncbuf, 0))) { |
| 3986 | const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3987 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3988 | if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3989 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3990 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3991 | cstream->rx.offset += data; |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 3992 | TRACE_DEVEL("buffered crypto data were provided to TLS stack", |
| 3993 | QUIC_EV_CONN_PHPKTS, qc, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3994 | } |
| 3995 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3996 | done: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3997 | ret = 1; |
| 3998 | leave: |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 3999 | if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) { |
| 4000 | TRACE_DEVEL("freeing crypto buf", QUIC_EV_CONN_PHPKTS, qc, el); |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4001 | quic_free_ncbuf(ncbuf); |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4002 | } |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4003 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4004 | return ret; |
| 4005 | } |
| 4006 | |
| 4007 | /* Process all the packets at <el> and <next_el> encryption level. |
| 4008 | * This is the caller responsibility to check that <cur_el> is different of <next_el> |
| 4009 | * as pointer value. |
| 4010 | * Return 1 if succeeded, 0 if not. |
| 4011 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4012 | int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el, |
| 4013 | struct quic_enc_level *next_el, int force_ack) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4014 | { |
| 4015 | int ret = 0; |
| 4016 | struct eb64_node *node; |
| 4017 | int64_t largest_pn = -1; |
| 4018 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4019 | struct quic_enc_level *qel = cur_el; |
| 4020 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4021 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4022 | qel = cur_el; |
| 4023 | next_tel: |
| 4024 | if (!qel) |
| 4025 | goto out; |
| 4026 | |
| 4027 | node = eb64_first(&qel->rx.pkts); |
| 4028 | while (node) { |
| 4029 | struct quic_rx_packet *pkt; |
| 4030 | |
| 4031 | pkt = eb64_entry(node, struct quic_rx_packet, pn_node); |
| 4032 | TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT, |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4033 | qc, pkt, NULL, qc->xprt_ctx->ssl); |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 4034 | if (!qc_pkt_decrypt(qc, qel, pkt)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4035 | /* Drop the packet */ |
| 4036 | TRACE_ERROR("packet decryption failed -> dropped", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4037 | QUIC_EV_CONN_RXPKT, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4038 | } |
| 4039 | else { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4040 | if (!qc_parse_pkt_frms(qc, pkt, qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4041 | /* Drop the packet */ |
| 4042 | TRACE_ERROR("packet parsing failed -> dropped", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4043 | QUIC_EV_CONN_RXPKT, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4044 | HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing); |
| 4045 | } |
| 4046 | else { |
| 4047 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 4048 | |
| 4049 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 4050 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 4051 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
| 4052 | qc_idle_timer_rearm(qc, 1); |
| 4053 | } |
| 4054 | if (pkt->pn > largest_pn) { |
| 4055 | largest_pn = pkt->pn; |
| 4056 | largest_pn_time_received = pkt->time_received; |
| 4057 | } |
| 4058 | /* Update the list of ranges to acknowledge. */ |
| 4059 | if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar)) |
| 4060 | TRACE_ERROR("Could not update ack range list", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4061 | QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4062 | } |
| 4063 | } |
| 4064 | node = eb64_next(node); |
| 4065 | eb64_delete(&pkt->pn_node); |
| 4066 | quic_rx_packet_refdec(pkt); |
| 4067 | } |
| 4068 | |
| 4069 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 4070 | /* Update the largest packet number. */ |
| 4071 | qel->pktns->rx.largest_pn = largest_pn; |
| 4072 | /* Update the largest acknowledged packet timestamps */ |
| 4073 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 4074 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 4075 | } |
| 4076 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4077 | if (qel->cstream && !qc_treat_rx_crypto_frms(qc, qel, qc->xprt_ctx)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4078 | // trace already emitted by function above |
| 4079 | goto leave; |
| 4080 | } |
| 4081 | |
| 4082 | if (qel == cur_el) { |
| 4083 | BUG_ON(qel == next_el); |
| 4084 | qel = next_el; |
| 4085 | largest_pn = -1; |
| 4086 | goto next_tel; |
| 4087 | } |
| 4088 | |
| 4089 | out: |
| 4090 | ret = 1; |
| 4091 | leave: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4092 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4093 | return ret; |
| 4094 | } |
| 4095 | |
| 4096 | /* Check if it's possible to remove header protection for packets related to |
| 4097 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 4098 | * |
| 4099 | * Return true if the operation is possible else false. |
| 4100 | */ |
| 4101 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4102 | { |
| 4103 | int ret = 0; |
| 4104 | enum quic_tls_enc_level tel; |
| 4105 | |
| 4106 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
| 4107 | |
| 4108 | if (!qel) |
| 4109 | goto cant_rm_hp; |
| 4110 | |
| 4111 | tel = ssl_to_quic_enc_level(qel->level); |
| 4112 | |
| 4113 | /* check if tls secrets are available */ |
| 4114 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 4115 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
| 4116 | goto cant_rm_hp; |
| 4117 | } |
| 4118 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4119 | if (!quic_tls_has_rx_sec(qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4120 | TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc); |
| 4121 | goto cant_rm_hp; |
| 4122 | } |
| 4123 | |
Frédéric Lécaille | 8417beb | 2023-02-01 10:31:35 +0100 | [diff] [blame] | 4124 | if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) { |
| 4125 | TRACE_DEVEL("handshake not complete", QUIC_EV_CONN_TRMHP, qc); |
| 4126 | goto cant_rm_hp; |
| 4127 | } |
| 4128 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4129 | /* check if the connection layer is ready before using app level */ |
| 4130 | if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) && |
| 4131 | qc->mux_state == QC_MUX_NULL) { |
| 4132 | TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc); |
| 4133 | goto cant_rm_hp; |
| 4134 | } |
| 4135 | |
| 4136 | ret = 1; |
| 4137 | cant_rm_hp: |
| 4138 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc); |
| 4139 | return ret; |
| 4140 | } |
| 4141 | |
| 4142 | /* Try to send application frames from list <frms> on connection <qc>. |
| 4143 | * |
| 4144 | * Use qc_send_app_probing wrapper when probing with old data. |
| 4145 | * |
| 4146 | * Returns 1 on success. Some data might not have been sent due to congestion, |
| 4147 | * in this case they are left in <frms> input list. The caller may subscribe on |
| 4148 | * quic-conn to retry later. |
| 4149 | * |
| 4150 | * Returns 0 on critical error. |
| 4151 | * TODO review and classify more distinctly transient from definitive errors to |
| 4152 | * allow callers to properly handle it. |
| 4153 | */ |
| 4154 | static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms) |
| 4155 | { |
| 4156 | int status = 0; |
| 4157 | struct buffer *buf; |
| 4158 | |
| 4159 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4160 | |
| 4161 | buf = qc_txb_alloc(qc); |
| 4162 | if (!buf) { |
| 4163 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 4164 | goto leave; |
| 4165 | } |
| 4166 | |
| 4167 | /* Prepare and send packets until we could not further prepare packets. */ |
| 4168 | while (1) { |
| 4169 | int ret; |
| 4170 | /* Currently buf cannot be non-empty at this stage. Even if a |
| 4171 | * previous sendto() has failed it is emptied to simulate |
| 4172 | * packet emission and rely on QUIC lost detection to try to |
| 4173 | * emit it. |
| 4174 | */ |
| 4175 | BUG_ON_HOT(b_data(buf)); |
| 4176 | b_reset(buf); |
| 4177 | |
| 4178 | ret = qc_prep_app_pkts(qc, buf, frms); |
| 4179 | if (ret == -1) |
| 4180 | goto err; |
| 4181 | else if (ret == 0) |
| 4182 | goto out; |
| 4183 | |
| 4184 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
| 4185 | goto err; |
| 4186 | } |
| 4187 | |
| 4188 | out: |
| 4189 | status = 1; |
| 4190 | qc_txb_release(qc); |
| 4191 | leave: |
| 4192 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4193 | return status; |
| 4194 | |
| 4195 | err: |
| 4196 | qc_txb_release(qc); |
| 4197 | goto leave; |
| 4198 | } |
| 4199 | |
| 4200 | /* Try to send application frames from list <frms> on connection <qc>. Use this |
| 4201 | * function when probing is required. |
| 4202 | * |
| 4203 | * Returns the result from qc_send_app_pkts function. |
| 4204 | */ |
| 4205 | static forceinline int qc_send_app_probing(struct quic_conn *qc, |
| 4206 | struct list *frms) |
| 4207 | { |
| 4208 | int ret; |
| 4209 | |
| 4210 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4211 | |
| 4212 | TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc); |
| 4213 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4214 | ret = qc_send_app_pkts(qc, frms); |
| 4215 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4216 | |
| 4217 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4218 | return ret; |
| 4219 | } |
| 4220 | |
| 4221 | /* Try to send application frames from list <frms> on connection <qc>. This |
| 4222 | * function is provided for MUX upper layer usage only. |
| 4223 | * |
| 4224 | * Returns the result from qc_send_app_pkts function. |
| 4225 | */ |
| 4226 | int qc_send_mux(struct quic_conn *qc, struct list *frms) |
| 4227 | { |
| 4228 | int ret; |
| 4229 | |
| 4230 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4231 | BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */ |
| 4232 | |
| 4233 | TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc); |
| 4234 | qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4235 | ret = qc_send_app_pkts(qc, frms); |
| 4236 | qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4237 | |
| 4238 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4239 | return ret; |
| 4240 | } |
| 4241 | |
| 4242 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 4243 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
| 4244 | * QUIC connection. <old_data> is used as boolean to send data already sent but |
| 4245 | * not already acknowledged (in flight). |
| 4246 | * Returns 1 if succeeded, 0 if not. |
| 4247 | */ |
| 4248 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 4249 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 4250 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 4251 | { |
| 4252 | int ret, status = 0; |
| 4253 | struct buffer *buf = qc_txb_alloc(qc); |
| 4254 | |
| 4255 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4256 | |
| 4257 | if (!buf) { |
| 4258 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 4259 | goto leave; |
| 4260 | } |
| 4261 | |
| 4262 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4263 | * sendto() has failed it is emptied to simulate packet emission and |
| 4264 | * rely on QUIC lost detection to try to emit it. |
| 4265 | */ |
| 4266 | BUG_ON_HOT(b_data(buf)); |
| 4267 | b_reset(buf); |
| 4268 | |
| 4269 | if (old_data) { |
| 4270 | TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc); |
| 4271 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4272 | } |
| 4273 | |
| 4274 | ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms); |
| 4275 | if (ret == -1) |
| 4276 | goto out; |
| 4277 | else if (ret == 0) |
| 4278 | goto skip_send; |
| 4279 | |
| 4280 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
| 4281 | goto out; |
| 4282 | |
| 4283 | skip_send: |
| 4284 | status = 1; |
| 4285 | out: |
| 4286 | TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc); |
| 4287 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4288 | qc_txb_release(qc); |
| 4289 | leave: |
| 4290 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4291 | return status; |
| 4292 | } |
| 4293 | |
| 4294 | /* Retransmit up to two datagrams depending on packet number space */ |
| 4295 | static void qc_dgrams_retransmit(struct quic_conn *qc) |
| 4296 | { |
| 4297 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4298 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4299 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4300 | |
| 4301 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4302 | |
| 4303 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4304 | int i; |
| 4305 | |
| 4306 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 4307 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 4308 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4309 | |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4310 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
| 4311 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 4312 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 4313 | if (!LIST_ISEMPTY(&ifrms)) { |
| 4314 | iqel->pktns->tx.pto_probe = 1; |
| 4315 | if (!LIST_ISEMPTY(&hfrms)) |
| 4316 | hqel->pktns->tx.pto_probe = 1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4317 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 4318 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms); |
| 4319 | /* Put back unsent frames in their packet number spaces */ |
| 4320 | LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms); |
| 4321 | LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms); |
| 4322 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4323 | } |
| 4324 | TRACE_STATE("no more need to probe Initial packet number space", |
| 4325 | QUIC_EV_CONN_TXPKT, qc); |
| 4326 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4327 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4328 | } |
| 4329 | else { |
| 4330 | int i; |
| 4331 | |
| 4332 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4333 | hqel->pktns->tx.pto_probe = 0; |
| 4334 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
Frédéric Lécaille | 7b5d9b1 | 2022-11-28 17:21:45 +0100 | [diff] [blame] | 4335 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4336 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4337 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
| 4338 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4339 | if (!LIST_ISEMPTY(&frms1)) { |
| 4340 | hqel->pktns->tx.pto_probe = 1; |
| 4341 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 4342 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 4343 | /* Put back unsent frames into their packet number spaces */ |
| 4344 | LIST_SPLICE(&hqel->pktns->tx.frms, &frms1); |
| 4345 | } |
| 4346 | } |
| 4347 | TRACE_STATE("no more need to probe Handshake packet number space", |
| 4348 | QUIC_EV_CONN_TXPKT, qc); |
| 4349 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4350 | } |
| 4351 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4352 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 4353 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4354 | |
| 4355 | aqel->pktns->tx.pto_probe = 0; |
| 4356 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 4357 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4358 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 4359 | if (!LIST_ISEMPTY(&frms1)) { |
| 4360 | aqel->pktns->tx.pto_probe = 1; |
| 4361 | qc_send_app_probing(qc, &frms1); |
| 4362 | /* Put back unsent frames into their packet number spaces */ |
| 4363 | LIST_SPLICE(&aqel->pktns->tx.frms, &frms1); |
| 4364 | } |
| 4365 | if (!LIST_ISEMPTY(&frms2)) { |
| 4366 | aqel->pktns->tx.pto_probe = 1; |
| 4367 | qc_send_app_probing(qc, &frms2); |
| 4368 | /* Put back unsent frames into their packet number spaces */ |
| 4369 | LIST_SPLICE(&aqel->pktns->tx.frms, &frms2); |
| 4370 | } |
| 4371 | TRACE_STATE("no more need to probe 01RTT packet number space", |
| 4372 | QUIC_EV_CONN_TXPKT, qc); |
| 4373 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4374 | } |
| 4375 | } |
| 4376 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4377 | } |
| 4378 | |
| 4379 | /* QUIC connection packet handler task (post handshake) */ |
| 4380 | struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 4381 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4382 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4383 | struct quic_enc_level *qel; |
| 4384 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4385 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4386 | |
| 4387 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 4388 | TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state); |
| 4389 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 4390 | if (qc_test_fd(qc)) |
| 4391 | qc_rcv_buf(qc); |
| 4392 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4393 | /* Retranmissions */ |
| 4394 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 4395 | TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
| 4396 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 4397 | qc_dgrams_retransmit(qc); |
| 4398 | } |
| 4399 | |
| 4400 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 4401 | qc_rm_hp_pkts(qc, qel); |
| 4402 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4403 | if (!qc_treat_rx_pkts(qc, qel, NULL, 0)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4404 | TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4405 | goto out; |
| 4406 | } |
| 4407 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame^] | 4408 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 4409 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc); |
| 4410 | goto out; |
| 4411 | } |
| 4412 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4413 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4414 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) { |
| 4415 | TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc); |
| 4416 | goto out; |
| 4417 | } |
| 4418 | |
| 4419 | /* XXX TODO: how to limit the list frames to send */ |
| 4420 | if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) { |
| 4421 | TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4422 | goto out; |
| 4423 | } |
| 4424 | |
| 4425 | out: |
| 4426 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 4427 | return t; |
| 4428 | } |
| 4429 | |
| 4430 | /* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */ |
| 4431 | static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4432 | { |
| 4433 | return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) || |
| 4434 | (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) || |
| 4435 | qel->pktns->tx.pto_probe || |
| 4436 | !LIST_ISEMPTY(&qel->pktns->tx.frms); |
| 4437 | } |
| 4438 | |
| 4439 | /* QUIC connection packet handler task. */ |
| 4440 | struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state) |
| 4441 | { |
| 4442 | int ret, ssl_err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4443 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4444 | enum quic_tls_enc_level tel, next_tel; |
| 4445 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4446 | /* Early-data encryption level */ |
| 4447 | struct quic_enc_level *eqel; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4448 | struct buffer *buf = NULL; |
| 4449 | int st, force_ack, zero_rtt; |
| 4450 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4451 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4452 | eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4453 | st = qc->state; |
| 4454 | TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st); |
| 4455 | |
| 4456 | /* Retranmissions */ |
| 4457 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 4458 | TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
| 4459 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 4460 | qc_dgrams_retransmit(qc); |
| 4461 | } |
| 4462 | |
| 4463 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 4464 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
| 4465 | TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached", |
| 4466 | QUIC_EV_CONN_IO_CB, qc); |
| 4467 | /* The I/O handler has been woken up by the dgram parser (qc_lstnr_pkt_rcv()) |
| 4468 | * after the anti-amplification was reached. |
| 4469 | * |
| 4470 | * TODO: this part should be removed. This was there because the |
| 4471 | * datagram parser was not executed by only one thread. |
| 4472 | */ |
| 4473 | qc_set_timer(qc); |
Amaury Denoyelle | 5ac6b3b | 2022-12-14 18:04:06 +0100 | [diff] [blame] | 4474 | if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4475 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 4476 | } |
| 4477 | ssl_err = SSL_ERROR_NONE; |
| 4478 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4479 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4480 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 4481 | |
| 4482 | if (qc_test_fd(qc)) |
| 4483 | qc_rcv_buf(qc); |
| 4484 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4485 | if (st >= QUIC_HS_ST_COMPLETE && |
| 4486 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 4487 | TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 4488 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 4489 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 4490 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 4491 | } |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4492 | else if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, zero_rtt)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4493 | goto out; |
| 4494 | |
| 4495 | qel = &qc->els[tel]; |
| 4496 | next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel]; |
| 4497 | |
| 4498 | next_level: |
| 4499 | /* Treat packets waiting for header packet protection decryption */ |
| 4500 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 4501 | qc_rm_hp_pkts(qc, qel); |
| 4502 | |
| 4503 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 4504 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4505 | if (!qc_treat_rx_pkts(qc, qel, next_qel, force_ack)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4506 | goto out; |
| 4507 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame^] | 4508 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 4509 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc); |
| 4510 | goto out; |
| 4511 | } |
| 4512 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4513 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4514 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 4515 | goto out; |
| 4516 | |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4517 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4518 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4519 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
| 4520 | if (next_qel && next_qel == eqel && zero_rtt) { |
| 4521 | TRACE_DEVEL("select 0RTT as next encryption level", |
| 4522 | QUIC_EV_CONN_PHPKTS, qc); |
| 4523 | qel = next_qel; |
| 4524 | next_qel = NULL; |
| 4525 | goto next_level; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | st = qc->state; |
| 4529 | if (st >= QUIC_HS_ST_COMPLETE) { |
| 4530 | if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) && |
| 4531 | !quic_build_post_handshake_frames(qc)) |
| 4532 | goto out; |
| 4533 | |
| 4534 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags & |
| 4535 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 4536 | /* Discard the Handshake keys. */ |
| 4537 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4538 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 4539 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 4540 | qc_set_timer(qc); |
| 4541 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4542 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
| 4543 | } |
| 4544 | |
| 4545 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 4546 | /* There may be remaining handshake to build (acks) */ |
| 4547 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 4548 | } |
| 4549 | } |
| 4550 | |
| 4551 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 4552 | * be considered. |
| 4553 | */ |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4554 | if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4555 | goto out; |
| 4556 | |
| 4557 | if (!qc_need_sending(qc, qel) && |
| 4558 | (!next_qel || !qc_need_sending(qc, next_qel))) { |
| 4559 | goto skip_send; |
| 4560 | } |
| 4561 | |
| 4562 | buf = qc_txb_alloc(qc); |
| 4563 | if (!buf) |
| 4564 | goto out; |
| 4565 | |
| 4566 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4567 | * sendto() has failed it is emptied to simulate packet emission and |
| 4568 | * rely on QUIC lost detection to try to emit it. |
| 4569 | */ |
| 4570 | BUG_ON_HOT(b_data(buf)); |
| 4571 | b_reset(buf); |
| 4572 | |
| 4573 | ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms, |
| 4574 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
| 4575 | if (ret == -1) |
| 4576 | goto out; |
| 4577 | else if (ret == 0) |
| 4578 | goto skip_send; |
| 4579 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4580 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4581 | goto out; |
| 4582 | |
| 4583 | skip_send: |
| 4584 | /* Check if there is something to do for the next level. |
| 4585 | */ |
| 4586 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4587 | quic_tls_has_rx_sec(next_qel) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4588 | (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) { |
| 4589 | qel = next_qel; |
| 4590 | next_qel = NULL; |
| 4591 | goto next_level; |
| 4592 | } |
| 4593 | |
| 4594 | out: |
| 4595 | qc_txb_release(qc); |
| 4596 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err); |
| 4597 | return t; |
| 4598 | } |
| 4599 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4600 | /* Release the memory allocated for <cs> CRYPTO stream */ |
| 4601 | void quic_cstream_free(struct quic_cstream *cs) |
| 4602 | { |
| 4603 | if (!cs) { |
| 4604 | /* This is the case for ORTT encryption level */ |
| 4605 | return; |
| 4606 | } |
| 4607 | |
Amaury Denoyelle | bc174b2 | 2022-11-17 10:12:52 +0100 | [diff] [blame] | 4608 | quic_free_ncbuf(&cs->rx.ncbuf); |
| 4609 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4610 | qc_stream_desc_release(cs->desc); |
| 4611 | pool_free(pool_head_quic_cstream, cs); |
| 4612 | } |
| 4613 | |
| 4614 | /* Allocate a new QUIC stream for <qc>. |
| 4615 | * Return it if succeeded, NULL if not. |
| 4616 | */ |
| 4617 | struct quic_cstream *quic_cstream_new(struct quic_conn *qc) |
| 4618 | { |
| 4619 | struct quic_cstream *cs, *ret_cs = NULL; |
| 4620 | |
| 4621 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 4622 | cs = pool_alloc(pool_head_quic_cstream); |
| 4623 | if (!cs) { |
| 4624 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 4625 | goto leave; |
| 4626 | } |
| 4627 | |
| 4628 | cs->rx.offset = 0; |
| 4629 | cs->rx.ncbuf = NCBUF_NULL; |
| 4630 | cs->rx.offset = 0; |
| 4631 | |
| 4632 | cs->tx.offset = 0; |
| 4633 | cs->tx.sent_offset = 0; |
| 4634 | cs->tx.buf = BUF_NULL; |
| 4635 | cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc); |
| 4636 | if (!cs->desc) { |
| 4637 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 4638 | goto err; |
| 4639 | } |
| 4640 | |
| 4641 | ret_cs = cs; |
| 4642 | leave: |
| 4643 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 4644 | return ret_cs; |
| 4645 | |
| 4646 | err: |
| 4647 | pool_free(pool_head_quic_cstream, cs); |
| 4648 | goto leave; |
| 4649 | } |
| 4650 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4651 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
| 4652 | static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4653 | { |
| 4654 | int i; |
| 4655 | |
| 4656 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4657 | |
| 4658 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4659 | if (qel->tx.crypto.bufs[i]) { |
| 4660 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4661 | qel->tx.crypto.bufs[i] = NULL; |
| 4662 | } |
| 4663 | } |
| 4664 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4665 | quic_cstream_free(qel->cstream); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4666 | |
| 4667 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4668 | } |
| 4669 | |
| 4670 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
| 4671 | * connection allocating everything needed. |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4672 | * |
| 4673 | * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use |
| 4674 | * quic_conn_enc_level_uninit() to cleanup partially allocated content. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4675 | */ |
| 4676 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4677 | enum quic_tls_enc_level level) |
| 4678 | { |
| 4679 | int ret = 0; |
| 4680 | struct quic_enc_level *qel; |
| 4681 | |
| 4682 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4683 | |
| 4684 | qel = &qc->els[level]; |
| 4685 | qel->level = quic_to_ssl_enc_level(level); |
| 4686 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4687 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4688 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 4689 | qel->tls_ctx.flags = 0; |
| 4690 | |
| 4691 | qel->rx.pkts = EB_ROOT; |
| 4692 | LIST_INIT(&qel->rx.pqpkts); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4693 | |
| 4694 | /* Allocate only one buffer. */ |
| 4695 | /* TODO: use a pool */ |
| 4696 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4697 | if (!qel->tx.crypto.bufs) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4698 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4699 | |
| 4700 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4701 | if (!qel->tx.crypto.bufs[0]) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4702 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4703 | |
| 4704 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4705 | qel->tx.crypto.nb_buf = 1; |
| 4706 | |
| 4707 | qel->tx.crypto.sz = 0; |
| 4708 | qel->tx.crypto.offset = 0; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4709 | /* No CRYPTO data for early data TLS encryption level */ |
| 4710 | if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA) |
| 4711 | qel->cstream = NULL; |
| 4712 | else { |
| 4713 | qel->cstream = quic_cstream_new(qc); |
| 4714 | if (!qel->cstream) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4715 | goto leave; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4716 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4717 | |
| 4718 | ret = 1; |
| 4719 | leave: |
| 4720 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4721 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4722 | } |
| 4723 | |
| 4724 | /* Callback called upon loss detection and PTO timer expirations. */ |
| 4725 | struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state) |
| 4726 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4727 | struct quic_conn *qc = ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4728 | struct quic_pktns *pktns; |
| 4729 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4730 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
| 4731 | NULL, NULL, &qc->path->ifae_pkts); |
| 4732 | task->expire = TICK_ETERNITY; |
| 4733 | pktns = quic_loss_pktns(qc); |
| 4734 | if (tick_isset(pktns->tx.loss_time)) { |
| 4735 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4736 | |
| 4737 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 4738 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4739 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 4740 | if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms)) |
| 4741 | qc_set_timer(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4742 | goto out; |
| 4743 | } |
| 4744 | |
| 4745 | if (qc->path->in_flight) { |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 4746 | pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL); |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 4747 | if (qc->subs && qc->subs->events & SUB_RETRY_SEND) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4748 | pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS; |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 4749 | tasklet_wakeup(qc->subs->tasklet); |
| 4750 | qc->subs->events &= ~SUB_RETRY_SEND; |
| 4751 | if (!qc->subs->events) |
| 4752 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4753 | } |
| 4754 | else { |
| 4755 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4756 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4757 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 4758 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4759 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) { |
| 4760 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4761 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4762 | } |
| 4763 | } |
| 4764 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) { |
| 4765 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4766 | if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) { |
| 4767 | qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4768 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4769 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4770 | } |
| 4771 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) { |
| 4772 | TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4773 | } |
| 4774 | } |
| 4775 | } |
| 4776 | else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) { |
| 4777 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4778 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4779 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4780 | if (quic_tls_has_tx_sec(hel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4781 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4782 | if (quic_tls_has_tx_sec(iel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4783 | iel->pktns->tx.pto_probe = 1; |
| 4784 | } |
| 4785 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4786 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4787 | qc->path->loss.pto_count++; |
| 4788 | |
| 4789 | out: |
| 4790 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
| 4791 | |
| 4792 | return task; |
| 4793 | } |
| 4794 | |
| 4795 | /* Parse the Retry token from buffer <token> with <end> a pointer to |
| 4796 | * one byte past the end of this buffer. This will extract the ODCID |
| 4797 | * which will be stored into <odcid> |
| 4798 | * |
| 4799 | * Returns 0 on success else non-zero. |
| 4800 | */ |
| 4801 | static int parse_retry_token(struct quic_conn *qc, |
| 4802 | const unsigned char *token, const unsigned char *end, |
| 4803 | struct quic_cid *odcid) |
| 4804 | { |
| 4805 | int ret = 0; |
| 4806 | uint64_t odcid_len; |
| 4807 | uint32_t timestamp; |
| 4808 | |
| 4809 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 4810 | |
| 4811 | if (!quic_dec_int(&odcid_len, &token, end)) { |
| 4812 | TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc); |
| 4813 | goto leave; |
| 4814 | } |
| 4815 | |
| 4816 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 4817 | * When an Initial packet is sent by a client that has not previously |
| 4818 | * received an Initial or Retry packet from the server, the client |
| 4819 | * populates the Destination Connection ID field with an unpredictable |
| 4820 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 4821 | */ |
| 4822 | if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) { |
| 4823 | TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 4824 | goto leave; |
| 4825 | } |
| 4826 | |
| 4827 | if (end - token < odcid_len + sizeof timestamp) { |
| 4828 | TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 4829 | goto leave; |
| 4830 | } |
| 4831 | |
| 4832 | timestamp = ntohl(read_u32(token + odcid_len)); |
| 4833 | if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) { |
| 4834 | TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc); |
| 4835 | goto leave; |
| 4836 | } |
| 4837 | |
| 4838 | ret = 1; |
| 4839 | memcpy(odcid->data, token, odcid_len); |
| 4840 | odcid->len = odcid_len; |
| 4841 | leave: |
| 4842 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 4843 | return !ret; |
| 4844 | } |
| 4845 | |
| 4846 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 4847 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 4848 | * for QUIC servers (or haproxy listeners). |
| 4849 | * <dcid> is the destination connection ID, <scid> is the source connection ID, |
| 4850 | * <token> the token found to be used for this connection with <token_len> as |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 4851 | * length. Endpoints addresses are specified via <local_addr> and <peer_addr>. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4852 | * Returns the connection if succeeded, NULL if not. |
| 4853 | */ |
| 4854 | static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4, |
| 4855 | struct quic_cid *dcid, struct quic_cid *scid, |
| 4856 | const struct quic_cid *token_odcid, |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 4857 | struct sockaddr_storage *local_addr, |
| 4858 | struct sockaddr_storage *peer_addr, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4859 | int server, int token, void *owner) |
| 4860 | { |
| 4861 | int i; |
| 4862 | struct quic_conn *qc; |
| 4863 | /* Initial CID. */ |
| 4864 | struct quic_connection_id *icid; |
| 4865 | char *buf_area = NULL; |
| 4866 | struct listener *l = NULL; |
| 4867 | struct quic_cc_algo *cc_algo = NULL; |
| 4868 | struct quic_tls_ctx *ictx; |
| 4869 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4870 | /* TODO replace pool_zalloc by pool_alloc(). This requires special care |
| 4871 | * to properly initialized internal quic_conn members to safely use |
| 4872 | * quic_conn_release() on alloc failure. |
| 4873 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4874 | qc = pool_zalloc(pool_head_quic_conn); |
| 4875 | if (!qc) { |
| 4876 | TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 4877 | goto err; |
| 4878 | } |
| 4879 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4880 | /* Initialize in priority qc members required for a safe dealloc. */ |
| 4881 | |
| 4882 | /* required to use MTLIST_IN_LIST */ |
| 4883 | MT_LIST_INIT(&qc->accept_list); |
| 4884 | |
| 4885 | LIST_INIT(&qc->rx.pkt_list); |
| 4886 | |
Amaury Denoyelle | 4244833 | 2022-12-12 11:24:05 +0100 | [diff] [blame] | 4887 | qc_init_fd(qc); |
| 4888 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4889 | /* Now proceeds to allocation of qc members. */ |
| 4890 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4891 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 4892 | if (!buf_area) { |
| 4893 | TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc); |
| 4894 | goto err; |
| 4895 | } |
| 4896 | |
| 4897 | qc->cids = EB_ROOT; |
| 4898 | /* QUIC Server (or listener). */ |
| 4899 | if (server) { |
| 4900 | struct proxy *prx; |
| 4901 | |
| 4902 | l = owner; |
| 4903 | prx = l->bind_conf->frontend; |
| 4904 | cc_algo = l->bind_conf->quic_cc_algo; |
| 4905 | |
| 4906 | qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 4907 | &quic_stats_module); |
| 4908 | qc->flags |= QUIC_FL_CONN_LISTENER; |
| 4909 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
| 4910 | /* Copy the initial DCID with the address. */ |
| 4911 | qc->odcid.len = dcid->len; |
| 4912 | qc->odcid.addrlen = dcid->addrlen; |
| 4913 | memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen); |
| 4914 | |
| 4915 | /* copy the packet SCID to reuse it as DCID for sending */ |
| 4916 | if (scid->len) |
| 4917 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 4918 | qc->dcid.len = scid->len; |
| 4919 | qc->tx.buf = BUF_NULL; |
| 4920 | qc->li = l; |
| 4921 | } |
| 4922 | /* QUIC Client (outgoing connection to servers) */ |
| 4923 | else { |
| 4924 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
| 4925 | if (dcid->len) |
| 4926 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 4927 | qc->dcid.len = dcid->len; |
| 4928 | } |
| 4929 | qc->mux_state = QC_MUX_NULL; |
| 4930 | qc->err = quic_err_transport(QC_ERR_NO_ERROR); |
| 4931 | |
| 4932 | icid = new_quic_cid(&qc->cids, qc, 0); |
| 4933 | if (!icid) { |
| 4934 | TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc); |
| 4935 | goto err; |
| 4936 | } |
| 4937 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 4938 | if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) && |
| 4939 | is_addr(local_addr)) { |
| 4940 | TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc); |
| 4941 | qc_alloc_fd(qc, local_addr, peer_addr); |
| 4942 | } |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 4943 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4944 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4945 | if (server) |
| 4946 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
| 4947 | |
| 4948 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4949 | qc->scid = icid->cid; |
| 4950 | |
| 4951 | /* Packet number spaces initialization. */ |
| 4952 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4953 | quic_pktns_init(&qc->pktns[i]); |
| 4954 | /* QUIC encryption level context initialization. */ |
| 4955 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 4956 | if (!quic_conn_enc_level_init(qc, i)) { |
| 4957 | TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc); |
| 4958 | goto err; |
| 4959 | } |
| 4960 | /* Initialize the packet number space. */ |
| 4961 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4962 | } |
| 4963 | |
| 4964 | qc->original_version = qv; |
| 4965 | qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ? |
| 4966 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4967 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
| 4968 | /* TX part. */ |
| 4969 | LIST_INIT(&qc->tx.frms_to_send); |
| 4970 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4971 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4972 | qc->tx.bytes = 0; |
| 4973 | qc->tx.buf = BUF_NULL; |
| 4974 | /* RX part. */ |
| 4975 | qc->rx.bytes = 0; |
| 4976 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 4977 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 4978 | qc->rx.strms[i].nb_streams = 0; |
| 4979 | |
| 4980 | qc->nb_pkt_for_cc = 1; |
| 4981 | qc->nb_pkt_since_cc = 0; |
| 4982 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4983 | if (!quic_tls_ku_init(qc)) { |
| 4984 | TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc); |
| 4985 | goto err; |
| 4986 | } |
| 4987 | |
| 4988 | /* XXX TO DO: Only one path at this time. */ |
| 4989 | qc->path = &qc->paths[0]; |
| 4990 | quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc); |
| 4991 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4992 | qc->streams_by_id = EB_ROOT_UNIQUE; |
| 4993 | qc->stream_buf_count = 0; |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 4994 | memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr)); |
| 4995 | memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4996 | |
| 4997 | if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params, |
| 4998 | icid->stateless_reset_token, |
| 4999 | dcid->data, dcid->len, |
| 5000 | qc->scid.data, qc->scid.len, token_odcid)) |
| 5001 | goto err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5002 | |
| 5003 | qc->wait_event.tasklet = tasklet_new(); |
| 5004 | if (!qc->wait_event.tasklet) { |
| 5005 | TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT); |
| 5006 | goto err; |
| 5007 | } |
| 5008 | qc->wait_event.tasklet->process = quic_conn_io_cb; |
| 5009 | qc->wait_event.tasklet->context = qc; |
| 5010 | qc->wait_event.events = 0; |
| 5011 | /* Set tasklet tid based on the SCID selected by us for this |
| 5012 | * connection. The upper layer will also be binded on the same thread. |
| 5013 | */ |
Willy Tarreau | eed7826 | 2022-12-21 09:09:19 +0100 | [diff] [blame] | 5014 | qc->tid = quic_get_cid_tid(qc->scid.data, &l->rx); |
Willy Tarreau | f5a0c8a | 2022-10-13 16:14:11 +0200 | [diff] [blame] | 5015 | qc->wait_event.tasklet->tid = qc->tid; |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 5016 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5017 | |
| 5018 | if (qc_conn_alloc_ssl_ctx(qc) || |
| 5019 | !quic_conn_init_timer(qc) || |
| 5020 | !quic_conn_init_idle_timer_task(qc)) |
| 5021 | goto err; |
| 5022 | |
| 5023 | ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx; |
| 5024 | if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1)) |
| 5025 | goto err; |
| 5026 | |
| 5027 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
| 5028 | |
| 5029 | return qc; |
| 5030 | |
| 5031 | err: |
| 5032 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5033 | if (qc) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5034 | qc->rx.buf.area = NULL; |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5035 | quic_conn_release(qc); |
| 5036 | } |
| 5037 | TRACE_LEAVE(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5038 | return NULL; |
| 5039 | } |
| 5040 | |
| 5041 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 5042 | * The connection tasklet is killed. |
| 5043 | * |
| 5044 | * This function must only be called by the thread responsible of the quic_conn |
| 5045 | * tasklet. |
| 5046 | */ |
| 5047 | void quic_conn_release(struct quic_conn *qc) |
| 5048 | { |
| 5049 | int i; |
| 5050 | struct ssl_sock_ctx *conn_ctx; |
| 5051 | struct eb64_node *node; |
| 5052 | struct quic_tls_ctx *app_tls_ctx; |
| 5053 | struct quic_rx_packet *pkt, *pktback; |
| 5054 | |
| 5055 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 5056 | |
| 5057 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 5058 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 5059 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5060 | /* Close quic-conn socket fd. */ |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 5061 | qc_release_fd(qc, 0); |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5062 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5063 | /* in the unlikely (but possible) case the connection was just added to |
| 5064 | * the accept_list we must delete it from there. |
| 5065 | */ |
| 5066 | MT_LIST_DELETE(&qc->accept_list); |
| 5067 | |
| 5068 | /* free remaining stream descriptors */ |
| 5069 | node = eb64_first(&qc->streams_by_id); |
| 5070 | while (node) { |
| 5071 | struct qc_stream_desc *stream; |
| 5072 | |
| 5073 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 5074 | node = eb64_next(node); |
| 5075 | |
| 5076 | /* all streams attached to the quic-conn are released, so |
| 5077 | * qc_stream_desc_free will liberate the stream instance. |
| 5078 | */ |
| 5079 | BUG_ON(!stream->release); |
| 5080 | qc_stream_desc_free(stream, 1); |
| 5081 | } |
| 5082 | |
| 5083 | /* Purge Rx packet list. */ |
| 5084 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 5085 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 5086 | pool_free(pool_head_quic_rx_packet, pkt); |
| 5087 | } |
| 5088 | |
| 5089 | if (qc->idle_timer_task) { |
| 5090 | task_destroy(qc->idle_timer_task); |
| 5091 | qc->idle_timer_task = NULL; |
| 5092 | } |
| 5093 | |
| 5094 | if (qc->timer_task) { |
| 5095 | task_destroy(qc->timer_task); |
| 5096 | qc->timer_task = NULL; |
| 5097 | } |
| 5098 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5099 | if (qc->wait_event.tasklet) |
| 5100 | tasklet_free(qc->wait_event.tasklet); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5101 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5102 | /* remove the connection from receiver cids trees */ |
| 5103 | ebmb_delete(&qc->odcid_node); |
| 5104 | ebmb_delete(&qc->scid_node); |
| 5105 | free_quic_conn_cids(qc); |
| 5106 | |
| 5107 | conn_ctx = qc->xprt_ctx; |
| 5108 | if (conn_ctx) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5109 | SSL_free(conn_ctx->ssl); |
| 5110 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
| 5111 | } |
| 5112 | |
| 5113 | quic_tls_ku_free(qc); |
| 5114 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 5115 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
| 5116 | quic_conn_enc_level_uninit(qc, &qc->els[i]); |
| 5117 | } |
| 5118 | quic_tls_ctx_secs_free(&qc->negotiated_ictx); |
| 5119 | |
| 5120 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 5121 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 5122 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
| 5123 | |
| 5124 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 5125 | quic_pktns_tx_pkts_release(&qc->pktns[i], qc); |
| 5126 | quic_free_arngs(qc, &qc->pktns[i].rx.arngs); |
| 5127 | } |
| 5128 | |
| 5129 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 5130 | pool_free(pool_head_quic_conn, qc); |
| 5131 | TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc); |
| 5132 | |
| 5133 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 5134 | } |
| 5135 | |
| 5136 | /* Initialize the timer task of <qc> QUIC connection. |
| 5137 | * Returns 1 if succeeded, 0 if not. |
| 5138 | */ |
| 5139 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 5140 | { |
| 5141 | int ret = 0; |
| 5142 | /* Attach this task to the same thread ID used for the connection */ |
| 5143 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5144 | |
| 5145 | qc->timer_task = task_new_on(qc->tid); |
| 5146 | if (!qc->timer_task) { |
| 5147 | TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 5148 | goto leave; |
| 5149 | } |
| 5150 | |
| 5151 | qc->timer = TICK_ETERNITY; |
| 5152 | qc->timer_task->process = qc_process_timer; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5153 | qc->timer_task->context = qc; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5154 | |
| 5155 | ret = 1; |
| 5156 | leave: |
| 5157 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5158 | return ret; |
| 5159 | } |
| 5160 | |
| 5161 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 5162 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 5163 | { |
| 5164 | unsigned int expire; |
| 5165 | |
| 5166 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 5167 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 5168 | } |
| 5169 | |
| 5170 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 5171 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 5172 | */ |
| 5173 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 5174 | { |
| 5175 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5176 | |
| 5177 | if (read) { |
| 5178 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 5179 | } |
| 5180 | else { |
| 5181 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 5182 | } |
| 5183 | qc_idle_timer_do_rearm(qc); |
| 5184 | |
| 5185 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5186 | } |
| 5187 | |
| 5188 | /* The task handling the idle timeout */ |
| 5189 | struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 5190 | { |
| 5191 | struct quic_conn *qc = ctx; |
| 5192 | struct quic_counters *prx_counters = qc->prx_counters; |
| 5193 | unsigned int qc_flags = qc->flags; |
| 5194 | |
| 5195 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5196 | |
| 5197 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 5198 | * might free the quic-conn too early via quic_close(). |
| 5199 | */ |
| 5200 | qc_notify_close(qc); |
| 5201 | |
| 5202 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 5203 | * responsible to call quic_close to release it. |
| 5204 | */ |
| 5205 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 5206 | if (qc->mux_state != QC_MUX_READY) |
| 5207 | quic_conn_release(qc); |
| 5208 | |
| 5209 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 5210 | * least clean some parts of it such as the tasklet. |
| 5211 | */ |
| 5212 | |
| 5213 | if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 5214 | qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 5215 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
| 5216 | HA_ATOMIC_DEC(&prx_counters->half_open_conn); |
| 5217 | } |
| 5218 | |
| 5219 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5220 | return NULL; |
| 5221 | } |
| 5222 | |
| 5223 | /* Initialize the idle timeout task for <qc>. |
| 5224 | * Returns 1 if succeeded, 0 if not. |
| 5225 | */ |
| 5226 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 5227 | { |
| 5228 | int ret = 0; |
| 5229 | |
| 5230 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5231 | |
| 5232 | qc->idle_timer_task = task_new_here(); |
| 5233 | if (!qc->idle_timer_task) { |
| 5234 | TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 5235 | goto leave; |
| 5236 | } |
| 5237 | |
| 5238 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 5239 | qc->idle_timer_task->context = qc; |
| 5240 | qc_idle_timer_rearm(qc, 1); |
| 5241 | task_queue(qc->idle_timer_task); |
| 5242 | |
| 5243 | ret = 1; |
| 5244 | leave: |
| 5245 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5246 | return ret; |
| 5247 | } |
| 5248 | |
| 5249 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 5250 | * past one byte of this buffer. |
| 5251 | */ |
| 5252 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 5253 | struct quic_rx_packet *pkt) |
| 5254 | { |
| 5255 | int ret = 0; |
| 5256 | unsigned char dcid_len, scid_len; |
| 5257 | |
| 5258 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5259 | |
| 5260 | if (end == *buf) { |
| 5261 | TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT); |
| 5262 | goto leave; |
| 5263 | } |
| 5264 | |
| 5265 | /* Destination Connection ID Length */ |
| 5266 | dcid_len = *(*buf)++; |
| 5267 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 5268 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) { |
| 5269 | TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT); |
| 5270 | goto leave; |
| 5271 | } |
| 5272 | |
| 5273 | if (dcid_len) { |
| 5274 | /* Check that the length of this received DCID matches the CID lengths |
| 5275 | * of our implementation for non Initials packets only. |
| 5276 | */ |
| 5277 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 5278 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
| 5279 | dcid_len != QUIC_HAP_CID_LEN) { |
| 5280 | TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT); |
| 5281 | goto leave; |
| 5282 | } |
| 5283 | |
| 5284 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 5285 | } |
| 5286 | |
| 5287 | pkt->dcid.len = dcid_len; |
| 5288 | *buf += dcid_len; |
| 5289 | |
| 5290 | /* Source Connection ID Length */ |
| 5291 | scid_len = *(*buf)++; |
| 5292 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) { |
| 5293 | TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT); |
| 5294 | goto leave; |
| 5295 | } |
| 5296 | |
| 5297 | if (scid_len) |
| 5298 | memcpy(pkt->scid.data, *buf, scid_len); |
| 5299 | pkt->scid.len = scid_len; |
| 5300 | *buf += scid_len; |
| 5301 | |
| 5302 | ret = 1; |
| 5303 | leave: |
| 5304 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 5305 | return ret; |
| 5306 | } |
| 5307 | |
| 5308 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 5309 | static void qc_pkt_insert(struct quic_conn *qc, |
| 5310 | struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 5311 | { |
| 5312 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 5313 | |
| 5314 | pkt->pn_node.key = pkt->pn; |
| 5315 | quic_rx_packet_refinc(pkt); |
| 5316 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 5317 | |
| 5318 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 5319 | } |
| 5320 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5321 | /* Try to remove the header protection of <pkt> QUIC packet with <beg> the |
| 5322 | * address of the packet first byte, using the keys from encryption level <el>. |
| 5323 | * |
| 5324 | * If header protection has been successfully removed, packet data are copied |
| 5325 | * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also |
| 5326 | * proceeded, and the packet is inserted into <qc> protected packets tree. In |
| 5327 | * both cases, packet can now be considered handled by the <qc> connection. |
| 5328 | * |
| 5329 | * If header protection cannot be removed due to <el> secrets already |
| 5330 | * discarded, no operation is conducted. |
| 5331 | * |
| 5332 | * Returns 1 on success : packet data is now handled by the connection. On |
| 5333 | * error 0 is returned : packet should be dropped by the caller. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5334 | */ |
| 5335 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 5336 | struct quic_rx_packet *pkt, |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5337 | unsigned char *beg, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5338 | struct quic_enc_level **el) |
| 5339 | { |
| 5340 | int ret = 0; |
| 5341 | unsigned char *pn = NULL; /* Packet number field */ |
| 5342 | enum quic_tls_enc_level tel; |
| 5343 | struct quic_enc_level *qel; |
| 5344 | /* Only for traces. */ |
| 5345 | struct quic_rx_packet *qpkt_trace; |
| 5346 | |
| 5347 | qpkt_trace = NULL; |
| 5348 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5349 | BUG_ON(!pkt->pn_offset); |
| 5350 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5351 | /* The packet number is here. This is also the start minus |
| 5352 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 5353 | * protection. |
| 5354 | */ |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5355 | pn = beg + pkt->pn_offset; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5356 | |
| 5357 | tel = quic_packet_type_enc_level(pkt->type); |
| 5358 | qel = &qc->els[tel]; |
| 5359 | |
| 5360 | if (qc_qel_may_rm_hp(qc, qel)) { |
| 5361 | /* Note that the following function enables us to unprotect the packet |
| 5362 | * number and its length subsequently used to decrypt the entire |
| 5363 | * packets. |
| 5364 | */ |
| 5365 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 5366 | qel->pktns->rx.largest_pn, pn, beg)) { |
| 5367 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
| 5368 | goto out; |
| 5369 | } |
| 5370 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5371 | /* The AAD includes the packet number field. */ |
| 5372 | pkt->aad_len = pkt->pn_offset + pkt->pnl; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5373 | if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) { |
| 5374 | TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc); |
| 5375 | goto out; |
| 5376 | } |
| 5377 | |
| 5378 | qpkt_trace = pkt; |
| 5379 | } |
| 5380 | else { |
| 5381 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 5382 | /* If the packet number space has been discarded, this packet |
| 5383 | * will be not parsed. |
| 5384 | */ |
| 5385 | TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt); |
| 5386 | goto out; |
| 5387 | } |
| 5388 | |
| 5389 | TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5390 | LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 5391 | quic_rx_packet_refinc(pkt); |
| 5392 | } |
| 5393 | |
| 5394 | *el = qel; |
| 5395 | /* No reference counter incrementation here!!! */ |
| 5396 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 5397 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 5398 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 5399 | b_add(&qc->rx.buf, pkt->len); |
| 5400 | |
| 5401 | ret = 1; |
| 5402 | out: |
| 5403 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
| 5404 | return ret; |
| 5405 | } |
| 5406 | |
| 5407 | /* Parse the header form from <byte0> first byte of <pkt> packet to set its type. |
| 5408 | * Also set <*long_header> to 1 if this form is long, 0 if not and the version |
| 5409 | * of this packet into <*version>. |
| 5410 | */ |
| 5411 | static inline int qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 5412 | unsigned char **buf, const unsigned char *end, |
| 5413 | int *long_header, uint32_t *version) |
| 5414 | { |
| 5415 | int ret = 0; |
| 5416 | const unsigned char byte0 = **buf; |
| 5417 | |
| 5418 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5419 | |
| 5420 | (*buf)++; |
| 5421 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 5422 | unsigned char type = |
| 5423 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 5424 | |
| 5425 | *long_header = 1; |
| 5426 | /* Version */ |
| 5427 | if (!quic_read_uint32(version, (const unsigned char **)buf, end)) { |
| 5428 | TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT); |
| 5429 | goto out; |
| 5430 | } |
| 5431 | |
Frédéric Lécaille | 21c4c9b | 2023-01-13 16:37:02 +0100 | [diff] [blame] | 5432 | if (*version != QUIC_PROTOCOL_VERSION_2) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5433 | pkt->type = type; |
| 5434 | } |
| 5435 | else { |
| 5436 | switch (type) { |
| 5437 | case 0: |
| 5438 | pkt->type = QUIC_PACKET_TYPE_RETRY; |
| 5439 | break; |
| 5440 | case 1: |
| 5441 | pkt->type = QUIC_PACKET_TYPE_INITIAL; |
| 5442 | break; |
| 5443 | case 2: |
| 5444 | pkt->type = QUIC_PACKET_TYPE_0RTT; |
| 5445 | break; |
| 5446 | case 3: |
| 5447 | pkt->type = QUIC_PACKET_TYPE_HANDSHAKE; |
| 5448 | break; |
| 5449 | } |
| 5450 | } |
| 5451 | } |
| 5452 | else { |
| 5453 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 5454 | *long_header = 0; |
| 5455 | } |
| 5456 | |
| 5457 | ret = 1; |
| 5458 | out: |
| 5459 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 5460 | return ret; |
| 5461 | } |
| 5462 | |
| 5463 | /* Return the QUIC version (quic_version struct) with <version> as version number |
| 5464 | * if supported or NULL if not. |
| 5465 | */ |
| 5466 | static inline const struct quic_version *qc_supported_version(uint32_t version) |
| 5467 | { |
| 5468 | int i; |
| 5469 | |
| 5470 | for (i = 0; i < quic_versions_nb; i++) |
| 5471 | if (quic_versions[i].num == version) |
| 5472 | return &quic_versions[i]; |
| 5473 | |
| 5474 | return NULL; |
| 5475 | } |
| 5476 | |
| 5477 | /* |
| 5478 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 5479 | * address <addr>. |
| 5480 | * Implementation of RFC9000 6. Version Negotiation |
| 5481 | * |
| 5482 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 5483 | * |
| 5484 | * Returns 0 on success else non-zero |
| 5485 | */ |
| 5486 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 5487 | struct quic_rx_packet *pkt) |
| 5488 | { |
| 5489 | char buf[256]; |
| 5490 | int ret = 0, i = 0, j; |
| 5491 | uint32_t version; |
| 5492 | const socklen_t addrlen = get_addr_len(addr); |
| 5493 | |
| 5494 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5495 | /* |
| 5496 | * header form |
| 5497 | * long header, fixed bit to 0 for Version Negotiation |
| 5498 | */ |
| 5499 | /* TODO: RAND_bytes() should be replaced? */ |
| 5500 | if (RAND_bytes((unsigned char *)buf, 1) != 1) { |
| 5501 | TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT); |
| 5502 | goto out; |
| 5503 | } |
| 5504 | |
| 5505 | buf[i++] |= '\x80'; |
| 5506 | /* null version for Version Negotiation */ |
| 5507 | buf[i++] = '\x00'; |
| 5508 | buf[i++] = '\x00'; |
| 5509 | buf[i++] = '\x00'; |
| 5510 | buf[i++] = '\x00'; |
| 5511 | |
| 5512 | /* source connection id */ |
| 5513 | buf[i++] = pkt->scid.len; |
| 5514 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5515 | i += pkt->scid.len; |
| 5516 | |
| 5517 | /* destination connection id */ |
| 5518 | buf[i++] = pkt->dcid.len; |
| 5519 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 5520 | i += pkt->dcid.len; |
| 5521 | |
| 5522 | /* supported version */ |
| 5523 | for (j = 0; j < quic_versions_nb; j++) { |
| 5524 | version = htonl(quic_versions[j].num); |
| 5525 | memcpy(&buf[i], &version, sizeof(version)); |
| 5526 | i += sizeof(version); |
| 5527 | } |
| 5528 | |
| 5529 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 5530 | goto out; |
| 5531 | |
| 5532 | ret = 1; |
| 5533 | out: |
| 5534 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5535 | return !ret; |
| 5536 | } |
| 5537 | |
| 5538 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 5539 | * from <fd> UDP socket to <dst> |
| 5540 | * Return 1 if succeeded, 0 if not. |
| 5541 | */ |
| 5542 | static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr, |
| 5543 | struct quic_rx_packet *rxpkt) |
| 5544 | { |
| 5545 | int ret = 0, pktlen, rndlen; |
| 5546 | unsigned char pkt[64]; |
| 5547 | const socklen_t addrlen = get_addr_len(dstaddr); |
| 5548 | struct proxy *prx; |
| 5549 | struct quic_counters *prx_counters; |
| 5550 | |
| 5551 | TRACE_ENTER(QUIC_EV_STATELESS_RST); |
| 5552 | |
| 5553 | prx = l->bind_conf->frontend; |
| 5554 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 5555 | /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3) |
| 5556 | * The resulting minimum size of 21 bytes does not guarantee that a Stateless |
| 5557 | * Reset is difficult to distinguish from other packets if the recipient requires |
| 5558 | * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure |
| 5559 | * that all packets it sends are at least 22 bytes longer than the minimum |
| 5560 | * connection ID length that it requests the peer to include in its packets, |
| 5561 | * adding PADDING frames as necessary. This ensures that any Stateless Reset |
| 5562 | * sent by the peer is indistinguishable from a valid packet sent to the endpoint. |
| 5563 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 5564 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 5565 | * than the packet it responds to. |
| 5566 | */ |
| 5567 | |
| 5568 | /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */ |
| 5569 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0; |
| 5570 | pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen); |
| 5571 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
| 5572 | |
| 5573 | /* Put a header of random bytes */ |
| 5574 | /* TODO: RAND_bytes() should be replaced */ |
| 5575 | if (RAND_bytes(pkt, rndlen) != 1) { |
| 5576 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST); |
| 5577 | goto leave; |
| 5578 | } |
| 5579 | |
| 5580 | /* Clear the most significant bit, and set the second one */ |
| 5581 | *pkt = (*pkt & ~0x80) | 0x40; |
| 5582 | if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN, |
| 5583 | rxpkt->dcid.data, rxpkt->dcid.len)) |
| 5584 | goto leave; |
| 5585 | |
| 5586 | if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0) |
| 5587 | goto leave; |
| 5588 | |
| 5589 | ret = 1; |
| 5590 | HA_ATOMIC_INC(&prx_counters->stateless_reset_sent); |
| 5591 | TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid); |
| 5592 | leave: |
| 5593 | TRACE_LEAVE(QUIC_EV_STATELESS_RST); |
| 5594 | return ret; |
| 5595 | } |
| 5596 | |
| 5597 | /* QUIC server only function. |
| 5598 | * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address. |
| 5599 | * This is the responsibility of the caller to check <aad> size is big enough |
| 5600 | * to contain these data. |
| 5601 | * Return the number of bytes copied to <aad>. |
| 5602 | */ |
| 5603 | static int quic_generate_retry_token_aad(unsigned char *aad, |
| 5604 | uint32_t version, |
| 5605 | const struct quic_cid *cid, |
| 5606 | const struct sockaddr_storage *addr) |
| 5607 | { |
| 5608 | unsigned char *p; |
| 5609 | |
| 5610 | p = aad; |
| 5611 | memcpy(p, &version, sizeof version); |
| 5612 | p += sizeof version; |
| 5613 | p += quic_saddr_cpy(p, addr); |
| 5614 | memcpy(p, cid->data, cid->len); |
| 5615 | p += cid->len; |
| 5616 | |
| 5617 | return p - aad; |
| 5618 | } |
| 5619 | |
| 5620 | /* QUIC server only function. |
| 5621 | * Generate the token to be used in Retry packets. The token is written to |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 5622 | * <buf> with <len> as length. <odcid> is the original destination connection |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5623 | * ID and <dcid> is our side destination connection ID (or client source |
| 5624 | * connection ID). |
| 5625 | * Returns the length of the encoded token or 0 on error. |
| 5626 | */ |
| 5627 | static int quic_generate_retry_token(unsigned char *buf, size_t len, |
| 5628 | const uint32_t version, |
| 5629 | const struct quic_cid *odcid, |
| 5630 | const struct quic_cid *dcid, |
| 5631 | struct sockaddr_storage *addr) |
| 5632 | { |
| 5633 | int ret = 0; |
| 5634 | unsigned char *p; |
| 5635 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
Amaury Denoyelle | 6c94056 | 2022-10-18 11:05:02 +0200 | [diff] [blame] | 5636 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5637 | size_t aadlen; |
| 5638 | unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN]; |
| 5639 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5640 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5641 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5642 | size_t seclen = strlen(global.cluster_secret); |
| 5643 | EVP_CIPHER_CTX *ctx = NULL; |
| 5644 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 5645 | uint32_t timestamp = now_ms; |
| 5646 | |
| 5647 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5648 | |
| 5649 | /* We copy the odcid into the token, prefixed by its one byte |
| 5650 | * length, the format token byte. It is followed by an AEAD TAG, and finally |
| 5651 | * the random bytes used to derive the secret to encrypt the token. |
| 5652 | */ |
| 5653 | if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len) |
| 5654 | goto err; |
| 5655 | |
| 5656 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
| 5657 | /* TODO: RAND_bytes() should be replaced */ |
| 5658 | if (RAND_bytes(salt, sizeof salt) != 1) { |
| 5659 | TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT); |
| 5660 | goto err; |
| 5661 | } |
| 5662 | |
| 5663 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 5664 | salt, sizeof salt, sec, seclen)) { |
| 5665 | TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT); |
| 5666 | goto err; |
| 5667 | } |
| 5668 | |
| 5669 | if (!quic_tls_tx_ctx_init(&ctx, aead, key)) { |
| 5670 | TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT); |
| 5671 | goto err; |
| 5672 | } |
| 5673 | |
| 5674 | /* Token build */ |
| 5675 | p = buf; |
| 5676 | *p++ = QUIC_TOKEN_FMT_RETRY, |
| 5677 | *p++ = odcid->len; |
| 5678 | memcpy(p, odcid->data, odcid->len); |
| 5679 | p += odcid->len; |
| 5680 | write_u32(p, htonl(timestamp)); |
| 5681 | p += sizeof timestamp; |
| 5682 | |
| 5683 | /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 5684 | if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) { |
| 5685 | TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT); |
| 5686 | goto err; |
| 5687 | } |
| 5688 | |
| 5689 | p += QUIC_TLS_TAG_LEN; |
| 5690 | memcpy(p, salt, sizeof salt); |
| 5691 | p += sizeof salt; |
| 5692 | EVP_CIPHER_CTX_free(ctx); |
| 5693 | |
| 5694 | ret = p - buf; |
| 5695 | leave: |
| 5696 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5697 | return ret; |
| 5698 | |
| 5699 | err: |
| 5700 | if (ctx) |
| 5701 | EVP_CIPHER_CTX_free(ctx); |
| 5702 | goto leave; |
| 5703 | } |
| 5704 | |
| 5705 | /* QUIC server only function. |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5706 | * |
| 5707 | * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is |
| 5708 | * the UDP datagram containing <pkt> and <l> is the listener instance on which |
| 5709 | * it was received. If the token is valid, the ODCID of <qc> QUIC connection |
| 5710 | * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed |
| 5711 | * to validate the token but it can be NULL : in this case the version will be |
| 5712 | * retrieved from the packet. |
| 5713 | * |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5714 | * Return 1 if succeeded, 0 if not. |
| 5715 | */ |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5716 | |
| 5717 | static int quic_retry_token_check(struct quic_rx_packet *pkt, |
| 5718 | struct quic_dgram *dgram, |
| 5719 | struct listener *l, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5720 | struct quic_conn *qc, |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5721 | struct quic_cid *odcid) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5722 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5723 | struct proxy *prx; |
| 5724 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5725 | int ret = 0; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5726 | unsigned char *token = pkt->token; |
| 5727 | const uint64_t tokenlen = pkt->token_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5728 | unsigned char buf[128]; |
| 5729 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
Amaury Denoyelle | 6c94056 | 2022-10-18 11:05:02 +0200 | [diff] [blame] | 5730 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5731 | size_t aadlen; |
| 5732 | const unsigned char *salt; |
| 5733 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5734 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5735 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5736 | size_t seclen = strlen(global.cluster_secret); |
| 5737 | EVP_CIPHER_CTX *ctx = NULL; |
| 5738 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5739 | const struct quic_version *qv = qc ? qc->original_version : |
| 5740 | pkt->version; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5741 | |
| 5742 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 5743 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5744 | /* The caller must ensure this. */ |
| 5745 | BUG_ON(!global.cluster_secret || !pkt->token_len); |
| 5746 | |
| 5747 | prx = l->bind_conf->frontend; |
| 5748 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 5749 | |
| 5750 | if (*pkt->token != QUIC_TOKEN_FMT_RETRY) { |
| 5751 | /* TODO: New token check */ |
| 5752 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 5753 | goto leave; |
| 5754 | } |
| 5755 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5756 | if (sizeof buf < tokenlen) { |
| 5757 | TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc); |
| 5758 | goto err; |
| 5759 | } |
| 5760 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5761 | aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->scid, &dgram->saddr); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5762 | salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN; |
| 5763 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 5764 | salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) { |
| 5765 | TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc); |
| 5766 | goto err; |
| 5767 | } |
| 5768 | |
| 5769 | if (!quic_tls_rx_ctx_init(&ctx, aead, key)) { |
| 5770 | TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc); |
| 5771 | goto err; |
| 5772 | } |
| 5773 | |
| 5774 | /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 5775 | if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen, |
| 5776 | ctx, aead, key, iv)) { |
| 5777 | TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc); |
| 5778 | goto err; |
| 5779 | } |
| 5780 | |
| 5781 | if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) { |
| 5782 | TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 5783 | goto err; |
| 5784 | } |
| 5785 | |
| 5786 | EVP_CIPHER_CTX_free(ctx); |
| 5787 | |
| 5788 | ret = 1; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5789 | HA_ATOMIC_INC(&prx_counters->retry_validated); |
| 5790 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5791 | leave: |
| 5792 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 5793 | return ret; |
| 5794 | |
| 5795 | err: |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5796 | HA_ATOMIC_INC(&prx_counters->retry_error); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5797 | if (ctx) |
| 5798 | EVP_CIPHER_CTX_free(ctx); |
| 5799 | goto leave; |
| 5800 | } |
| 5801 | |
| 5802 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 5803 | * the Initial <pkt> packet. |
| 5804 | * |
| 5805 | * Returns 0 on success else non-zero. |
| 5806 | */ |
| 5807 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 5808 | struct quic_rx_packet *pkt, const struct quic_version *qv) |
| 5809 | { |
| 5810 | int ret = 0; |
| 5811 | unsigned char buf[128]; |
| 5812 | int i = 0, token_len; |
| 5813 | const socklen_t addrlen = get_addr_len(addr); |
| 5814 | struct quic_cid scid; |
| 5815 | |
| 5816 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5817 | |
| 5818 | /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */ |
| 5819 | buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) | |
| 5820 | (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT); |
| 5821 | /* version */ |
| 5822 | buf[i++] = *((unsigned char *)&qv->num + 3); |
| 5823 | buf[i++] = *((unsigned char *)&qv->num + 2); |
| 5824 | buf[i++] = *((unsigned char *)&qv->num + 1); |
| 5825 | buf[i++] = *(unsigned char *)&qv->num; |
| 5826 | |
| 5827 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 5828 | buf[i++] = pkt->scid.len; |
| 5829 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5830 | i += pkt->scid.len; |
| 5831 | |
| 5832 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 5833 | scid.len = QUIC_HAP_CID_LEN; |
| 5834 | /* TODO: RAND_bytes() should be replaced */ |
| 5835 | if (RAND_bytes(scid.data, scid.len) != 1) { |
| 5836 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT); |
| 5837 | goto out; |
| 5838 | } |
| 5839 | |
| 5840 | buf[i++] = scid.len; |
| 5841 | memcpy(&buf[i], scid.data, scid.len); |
| 5842 | i += scid.len; |
| 5843 | |
| 5844 | /* token */ |
| 5845 | if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num, |
| 5846 | &pkt->dcid, &pkt->scid, addr))) { |
| 5847 | TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT); |
| 5848 | goto out; |
| 5849 | } |
| 5850 | |
| 5851 | i += token_len; |
| 5852 | |
| 5853 | /* token integrity tag */ |
| 5854 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 5855 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 5856 | pkt->dcid.len, buf, i, qv)) { |
| 5857 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 5858 | goto out; |
| 5859 | } |
| 5860 | |
| 5861 | i += QUIC_TLS_TAG_LEN; |
| 5862 | |
| 5863 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) { |
| 5864 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 5865 | goto out; |
| 5866 | } |
| 5867 | |
| 5868 | ret = 1; |
| 5869 | out: |
| 5870 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5871 | return !ret; |
| 5872 | } |
| 5873 | |
| 5874 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 5875 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 5876 | * concatenated to the <pkt> DCID field. |
| 5877 | * |
| 5878 | * Returns the instance or NULL if not found. |
| 5879 | */ |
| 5880 | static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt, |
| 5881 | struct listener *l, |
| 5882 | struct sockaddr_storage *saddr) |
| 5883 | { |
| 5884 | struct quic_conn *qc = NULL; |
| 5885 | struct ebmb_node *node; |
| 5886 | struct quic_connection_id *id; |
| 5887 | /* set if the quic_conn is found in the second DCID tree */ |
| 5888 | |
| 5889 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5890 | |
| 5891 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 5892 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 5893 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 5894 | /* DCIDs of first packets coming from multiple clients may have |
| 5895 | * the same values. Let's distinguish them by concatenating the |
| 5896 | * socket addresses. |
| 5897 | */ |
| 5898 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 5899 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
| 5900 | pkt->dcid.len + pkt->dcid.addrlen); |
| 5901 | if (node) { |
| 5902 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 5903 | goto end; |
| 5904 | } |
| 5905 | } |
| 5906 | |
| 5907 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 5908 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 5909 | * used. |
| 5910 | */ |
| 5911 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len); |
| 5912 | if (!node) |
| 5913 | goto end; |
| 5914 | |
| 5915 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 5916 | qc = id->qc; |
| 5917 | |
| 5918 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 5919 | * If already done, this is a noop. |
| 5920 | */ |
| 5921 | if (qc) |
| 5922 | ebmb_delete(&qc->odcid_node); |
| 5923 | |
| 5924 | end: |
| 5925 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 5926 | return qc; |
| 5927 | } |
| 5928 | |
| 5929 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 5930 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 5931 | * parameters of this session. |
| 5932 | * This is the responsibility of the caller to check the validity of all the |
| 5933 | * pointers passed as parameter to this function. |
| 5934 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 5935 | * CO_ER_SSL_NO_MEM. |
| 5936 | */ |
| 5937 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 5938 | unsigned char *params, size_t params_len) |
| 5939 | { |
| 5940 | int retry, ret = -1; |
| 5941 | |
| 5942 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5943 | |
| 5944 | retry = 1; |
| 5945 | retry: |
| 5946 | *ssl = SSL_new(ssl_ctx); |
| 5947 | if (!*ssl) { |
| 5948 | if (!retry--) |
| 5949 | goto err; |
| 5950 | |
| 5951 | pool_gc(NULL); |
| 5952 | goto retry; |
| 5953 | } |
| 5954 | |
| 5955 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 5956 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) { |
| 5957 | SSL_free(*ssl); |
| 5958 | *ssl = NULL; |
| 5959 | if (!retry--) |
| 5960 | goto err; |
| 5961 | |
| 5962 | pool_gc(NULL); |
| 5963 | goto retry; |
| 5964 | } |
| 5965 | |
| 5966 | ret = 0; |
| 5967 | leave: |
| 5968 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5969 | return ret; |
| 5970 | |
| 5971 | err: |
| 5972 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 5973 | goto leave; |
| 5974 | } |
| 5975 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5976 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 5977 | * used to process <qc> received packets. The allocated context is stored in |
| 5978 | * <qc.xprt_ctx>. |
| 5979 | * |
| 5980 | * Returns 0 on success else non-zero. |
| 5981 | */ |
| 5982 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 5983 | { |
| 5984 | int ret = 0; |
| 5985 | struct bind_conf *bc = qc->li->bind_conf; |
| 5986 | struct ssl_sock_ctx *ctx = NULL; |
| 5987 | |
| 5988 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5989 | |
| 5990 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 5991 | if (!ctx) { |
| 5992 | TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT); |
| 5993 | goto err; |
| 5994 | } |
| 5995 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5996 | ctx->subs = NULL; |
| 5997 | ctx->xprt_ctx = NULL; |
| 5998 | ctx->qc = qc; |
| 5999 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6000 | if (qc_is_listener(qc)) { |
| 6001 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 6002 | qc->enc_params, qc->enc_params_len) == -1) { |
| 6003 | goto err; |
| 6004 | } |
| 6005 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6006 | /* Enabling 0-RTT */ |
| 6007 | if (bc->ssl_conf.early_data) |
| 6008 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 6009 | #endif |
| 6010 | |
| 6011 | SSL_set_accept_state(ctx->ssl); |
| 6012 | } |
| 6013 | |
| 6014 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 6015 | |
| 6016 | /* Store the allocated context in <qc>. */ |
| 6017 | qc->xprt_ctx = ctx; |
| 6018 | |
| 6019 | ret = 1; |
| 6020 | leave: |
| 6021 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 6022 | return !ret; |
| 6023 | |
| 6024 | err: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6025 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 6026 | goto leave; |
| 6027 | } |
| 6028 | |
| 6029 | /* Check that all the bytes between <buf> included and <end> address |
| 6030 | * excluded are null. This is the responsibility of the caller to |
| 6031 | * check that there is at least one byte between <buf> end <end>. |
| 6032 | * Return 1 if this all the bytes are null, 0 if not. |
| 6033 | */ |
| 6034 | static inline int quic_padding_check(const unsigned char *buf, |
| 6035 | const unsigned char *end) |
| 6036 | { |
| 6037 | while (buf < end && !*buf) |
| 6038 | buf++; |
| 6039 | |
| 6040 | return buf == end; |
| 6041 | } |
| 6042 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6043 | /* Find the associated connection to the packet <pkt> or create a new one if |
| 6044 | * this is an Initial packet. <dgram> is the datagram containing the packet and |
| 6045 | * <l> is the listener instance on which it was received. |
| 6046 | * |
| 6047 | * Returns the quic-conn instance or NULL. |
| 6048 | */ |
| 6049 | static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt, |
| 6050 | struct quic_dgram *dgram, |
| 6051 | struct listener *l) |
| 6052 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6053 | struct quic_cid token_odcid = { .len = 0 }; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6054 | struct quic_conn *qc = NULL; |
| 6055 | struct proxy *prx; |
| 6056 | struct quic_counters *prx_counters; |
| 6057 | |
| 6058 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 6059 | |
| 6060 | prx = l->bind_conf->frontend; |
| 6061 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6062 | |
| 6063 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
| 6064 | |
| 6065 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 6066 | BUG_ON(!pkt->version); /* This must not happen. */ |
| 6067 | |
| 6068 | if (global.cluster_secret && pkt->token_len) { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6069 | if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid)) |
| 6070 | goto err; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6071 | } |
| 6072 | |
| 6073 | if (!qc) { |
| 6074 | int ipv4; |
| 6075 | |
| 6076 | if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) && |
| 6077 | HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) { |
| 6078 | TRACE_PROTO("Initial without token, sending retry", |
| 6079 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6080 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) { |
| 6081 | TRACE_ERROR("Error during Retry generation", |
| 6082 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6083 | goto out; |
| 6084 | } |
| 6085 | |
| 6086 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 6087 | goto out; |
| 6088 | } |
| 6089 | |
| 6090 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 6091 | * When an Initial packet is sent by a client that has not previously |
| 6092 | * received an Initial or Retry packet from the server, the client |
| 6093 | * populates the Destination Connection ID field with an unpredictable |
| 6094 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 6095 | */ |
| 6096 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 6097 | TRACE_PROTO("dropped packet", |
| 6098 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6099 | goto err; |
| 6100 | } |
| 6101 | |
| 6102 | pkt->saddr = dgram->saddr; |
| 6103 | ipv4 = dgram->saddr.ss_family == AF_INET; |
| 6104 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6105 | qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid, |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6106 | &dgram->daddr, &pkt->saddr, 1, |
| 6107 | !!pkt->token_len, l); |
| 6108 | if (qc == NULL) |
| 6109 | goto err; |
| 6110 | |
| 6111 | HA_ATOMIC_INC(&prx_counters->half_open_conn); |
| 6112 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
| 6113 | ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
| 6114 | qc->odcid.len + qc->odcid.addrlen); |
| 6115 | } |
| 6116 | } |
| 6117 | else if (!qc) { |
| 6118 | TRACE_PROTO("No connection on a non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6119 | if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt)) |
| 6120 | TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc); |
| 6121 | goto err; |
| 6122 | } |
| 6123 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6124 | out: |
| 6125 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6126 | return qc; |
| 6127 | |
| 6128 | err: |
| 6129 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
| 6130 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 6131 | return NULL; |
| 6132 | } |
| 6133 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6134 | /* Parse a QUIC packet starting at <buf>. Data won't be read after <end> even |
| 6135 | * if the packet is incomplete. This function will populate fields of <pkt> |
| 6136 | * instance, most notably its length. <dgram> is the UDP datagram which |
| 6137 | * contains the parsed packet. <l> is the listener instance on which it was |
| 6138 | * received. |
| 6139 | * |
| 6140 | * Returns 0 on success else non-zero. Packet length is guaranteed to be set to |
| 6141 | * the real packet value or to cover all data between <buf> and <end> : this is |
| 6142 | * useful to reject a whole datagram. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6143 | */ |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6144 | static int quic_rx_pkt_parse(struct quic_rx_packet *pkt, |
| 6145 | unsigned char *buf, const unsigned char *end, |
| 6146 | struct quic_dgram *dgram, struct listener *l) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6147 | { |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6148 | const unsigned char *beg = buf; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6149 | struct proxy *prx; |
| 6150 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6151 | int long_header = 0; |
Willy Tarreau | 33a6870 | 2022-11-24 09:16:41 +0100 | [diff] [blame] | 6152 | uint32_t version = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6153 | const struct quic_version *qv = NULL; |
| 6154 | |
| 6155 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 6156 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6157 | prx = l->bind_conf->frontend; |
| 6158 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6159 | /* This ist only to please to traces and distinguish the |
| 6160 | * packet with parsed packet number from others. |
| 6161 | */ |
| 6162 | pkt->pn_node.key = (uint64_t)-1; |
| 6163 | if (end <= buf) { |
| 6164 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6165 | goto drop; |
| 6166 | } |
| 6167 | |
| 6168 | /* Fixed bit */ |
| 6169 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6170 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
| 6171 | quic_padding_check(buf, end)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6172 | /* Some browsers may pad the remaining datagram space with null bytes. |
| 6173 | * That is what we called add padding out of QUIC packets. Such |
| 6174 | * datagrams must be considered as valid. But we can only consume |
| 6175 | * the remaining space. |
| 6176 | */ |
| 6177 | pkt->len = end - buf; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6178 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6179 | } |
| 6180 | |
| 6181 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6182 | goto drop; |
| 6183 | } |
| 6184 | |
| 6185 | /* Header form */ |
| 6186 | if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) { |
| 6187 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6188 | goto drop; |
| 6189 | } |
| 6190 | |
| 6191 | if (long_header) { |
| 6192 | uint64_t len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6193 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6194 | TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6195 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
| 6196 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6197 | goto drop; |
| 6198 | } |
| 6199 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6200 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 6201 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6202 | TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6203 | HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram); |
| 6204 | goto drop; |
| 6205 | } |
| 6206 | |
| 6207 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6208 | * they must have the same DCID. |
| 6209 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6210 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6211 | (pkt->dcid.len != dgram->dcid_len || |
| 6212 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6213 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6214 | goto drop; |
| 6215 | } |
| 6216 | |
| 6217 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 6218 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) { |
| 6219 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6220 | goto drop; |
| 6221 | } |
| 6222 | |
| 6223 | /* RFC9000 6. Version Negotiation */ |
| 6224 | qv = qc_supported_version(version); |
| 6225 | if (!qv) { |
| 6226 | /* unsupported version, send Negotiation packet */ |
| 6227 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
| 6228 | TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6229 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6230 | } |
| 6231 | |
| 6232 | TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6233 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6234 | } |
Amaury Denoyelle | 0eae572 | 2022-10-17 18:05:18 +0200 | [diff] [blame] | 6235 | pkt->version = qv; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6236 | |
| 6237 | /* For Initial packets, and for servers (QUIC clients connections), |
| 6238 | * there is no Initial connection IDs storage. |
| 6239 | */ |
| 6240 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 6241 | uint64_t token_len; |
| 6242 | |
| 6243 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 6244 | end - buf < token_len) { |
| 6245 | TRACE_PROTO("Packet dropped", |
| 6246 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6247 | goto drop; |
| 6248 | } |
| 6249 | |
| 6250 | /* TODO Retry should be automatically activated if |
| 6251 | * suspect network usage is detected. |
| 6252 | */ |
| 6253 | if (global.cluster_secret && !token_len) { |
| 6254 | if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) { |
| 6255 | TRACE_PROTO("Initial without token, sending retry", |
Amaury Denoyelle | 90121b3 | 2022-09-27 10:35:29 +0200 | [diff] [blame] | 6256 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6257 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) { |
| 6258 | TRACE_PROTO("Error during Retry generation", |
Amaury Denoyelle | 90121b3 | 2022-09-27 10:35:29 +0200 | [diff] [blame] | 6259 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6260 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6261 | } |
| 6262 | |
| 6263 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6264 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6265 | } |
| 6266 | } |
| 6267 | else if (!global.cluster_secret && token_len) { |
| 6268 | /* Impossible case: a token was received without configured |
| 6269 | * cluster secret. |
| 6270 | */ |
| 6271 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, |
| 6272 | NULL, NULL, NULL, qv); |
| 6273 | goto drop; |
| 6274 | } |
| 6275 | |
| 6276 | pkt->token = buf; |
| 6277 | pkt->token_len = token_len; |
| 6278 | buf += pkt->token_len; |
| 6279 | } |
| 6280 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
| 6281 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
| 6282 | TRACE_PROTO("Packet dropped", |
| 6283 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6284 | goto drop; |
| 6285 | } |
| 6286 | } |
| 6287 | |
| 6288 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 6289 | end - buf < len) { |
| 6290 | TRACE_PROTO("Packet dropped", |
| 6291 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6292 | goto drop; |
| 6293 | } |
| 6294 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6295 | /* Packet Number is stored here. Packet Length totalizes the |
| 6296 | * rest of the content. |
| 6297 | */ |
| 6298 | pkt->pn_offset = buf - beg; |
| 6299 | pkt->len = pkt->pn_offset + len; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6300 | |
| 6301 | /* Interrupt parsing after packet length retrieval : this |
| 6302 | * ensures that only the packet is dropped but not the whole |
| 6303 | * datagram. |
| 6304 | */ |
| 6305 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
| 6306 | TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT); |
| 6307 | goto drop; |
| 6308 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6309 | } |
| 6310 | else { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6311 | TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6312 | if (end - buf < QUIC_HAP_CID_LEN) { |
| 6313 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6314 | goto drop; |
| 6315 | } |
| 6316 | |
| 6317 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
| 6318 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
| 6319 | |
| 6320 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6321 | * they must have the same DCID. |
| 6322 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6323 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6324 | (pkt->dcid.len != dgram->dcid_len || |
| 6325 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6326 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6327 | goto drop; |
| 6328 | } |
| 6329 | |
| 6330 | buf += QUIC_HAP_CID_LEN; |
| 6331 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6332 | pkt->pn_offset = buf - beg; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6333 | /* A short packet is the last one of a UDP datagram. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6334 | pkt->len = end - beg; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6335 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6336 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6337 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv); |
| 6338 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6339 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6340 | drop: |
| 6341 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6342 | drop_silent: |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6343 | if (!pkt->len) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6344 | pkt->len = end - beg; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6345 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv); |
| 6346 | return -1; |
| 6347 | } |
| 6348 | |
| 6349 | /* Check if received packet <pkt> should be drop due to <qc> already in closing |
| 6350 | * state. This can be true if a CONNECTION_CLOSE has already been emitted for |
| 6351 | * this connection. |
| 6352 | * |
| 6353 | * Returns false if connection is not in closing state else true. The caller |
| 6354 | * should drop the whole datagram in the last case to not mess up <qc> |
| 6355 | * CONNECTION_CLOSE rate limit counter. |
| 6356 | */ |
| 6357 | static int qc_rx_check_closing(struct quic_conn *qc, |
| 6358 | struct quic_rx_packet *pkt) |
| 6359 | { |
| 6360 | if (!(qc->flags & QUIC_FL_CONN_CLOSING)) |
| 6361 | return 0; |
| 6362 | |
| 6363 | TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 6364 | |
| 6365 | /* Check if CONNECTION_CLOSE rate reemission is reached. */ |
| 6366 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 6367 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 6368 | qc->nb_pkt_for_cc++; |
| 6369 | qc->nb_pkt_since_cc = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6370 | } |
| 6371 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6372 | return 1; |
| 6373 | } |
| 6374 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6375 | /* React to a connection migration initiated on <qc> by a client with the new |
| 6376 | * path addresses <peer_addr>/<local_addr>. |
| 6377 | * |
| 6378 | * Returns 0 on success else non-zero. |
| 6379 | */ |
| 6380 | static int qc_handle_conn_migration(struct quic_conn *qc, |
| 6381 | const struct sockaddr_storage *peer_addr, |
| 6382 | const struct sockaddr_storage *local_addr) |
| 6383 | { |
| 6384 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6385 | |
Frédéric Lécaille | 6fc8697 | 2023-01-12 08:29:23 +0100 | [diff] [blame] | 6386 | /* RFC 9000. Connection Migration |
| 6387 | * |
| 6388 | * If the peer sent the disable_active_migration transport parameter, |
| 6389 | * an endpoint also MUST NOT send packets (including probing packets; |
| 6390 | * see Section 9.1) from a different local address to the address the peer |
| 6391 | * used during the handshake, unless the endpoint has acted on a |
| 6392 | * preferred_address transport parameter from the peer. |
| 6393 | */ |
| 6394 | if (qc->li->bind_conf->quic_params.disable_active_migration) { |
| 6395 | TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc); |
| 6396 | goto err; |
| 6397 | } |
| 6398 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6399 | /* RFC 9000 9. Connection Migration |
| 6400 | * |
Amaury Denoyelle | eb6be98 | 2022-11-21 11:14:45 +0100 | [diff] [blame] | 6401 | * The design of QUIC relies on endpoints retaining a stable address for |
| 6402 | * the duration of the handshake. An endpoint MUST NOT initiate |
| 6403 | * connection migration before the handshake is confirmed, as defined in |
| 6404 | * Section 4.1.2 of [QUIC-TLS]. |
| 6405 | */ |
| 6406 | if (qc->state < QUIC_HS_ST_COMPLETE) { |
| 6407 | TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc); |
| 6408 | goto err; |
| 6409 | } |
| 6410 | |
| 6411 | /* RFC 9000 9. Connection Migration |
| 6412 | * |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6413 | * TODO |
| 6414 | * An endpoint MUST |
| 6415 | * perform path validation (Section 8.2) if it detects any change to a |
| 6416 | * peer's address, unless it has previously validated that address. |
| 6417 | */ |
| 6418 | |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 6419 | /* Update quic-conn owned socket if in used. |
| 6420 | * TODO try to reuse it instead of closing and opening a new one. |
| 6421 | */ |
| 6422 | if (qc_test_fd(qc)) { |
| 6423 | /* TODO try to reuse socket instead of closing it and opening a new one. */ |
| 6424 | TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc); |
| 6425 | qc_release_fd(qc, 1); |
| 6426 | qc_alloc_fd(qc, local_addr, peer_addr); |
| 6427 | } |
| 6428 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6429 | qc->local_addr = *local_addr; |
| 6430 | qc->peer_addr = *peer_addr; |
| 6431 | HA_ATOMIC_INC(&qc->prx_counters->conn_migration_done); |
| 6432 | |
| 6433 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6434 | return 0; |
| 6435 | |
| 6436 | err: |
| 6437 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6438 | return 1; |
| 6439 | } |
| 6440 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6441 | /* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied |
| 6442 | * into <qc> receive buffer after header protection removal procedure. |
| 6443 | * |
| 6444 | * <dgram> must be set to the datagram which contains the QUIC packet. <beg> |
| 6445 | * must point to packet buffer first byte. |
| 6446 | * |
| 6447 | * <tasklist_head> may be non-NULL when the caller treat several datagrams for |
| 6448 | * different quic-conn. In this case, each quic-conn tasklet will be appended |
| 6449 | * to it in order to be woken up after the current task. |
| 6450 | * |
| 6451 | * The caller can safely removed the packet data. If packet refcount was not |
| 6452 | * incremented by this function, it means that the connection did not handled |
| 6453 | * it and it should be freed by the caller. |
| 6454 | */ |
| 6455 | static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt, |
| 6456 | struct quic_dgram *dgram, unsigned char *beg, |
| 6457 | struct list **tasklist_head) |
| 6458 | { |
| 6459 | const struct quic_version *qv = pkt->version; |
| 6460 | struct quic_enc_level *qel = NULL; |
| 6461 | size_t b_cspace; |
| 6462 | int io_cb_wakeup = 1; |
| 6463 | |
Amaury Denoyelle | 3f474e6 | 2022-11-24 17:15:08 +0100 | [diff] [blame] | 6464 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 6465 | |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6466 | if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST && |
| 6467 | !quic_peer_validated_addr(qc) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6468 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
| 6469 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 6470 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6471 | /* Reset the anti-amplification bit. It will be set again |
| 6472 | * when sending the next packet if reached again. |
| 6473 | */ |
| 6474 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 6475 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
| 6476 | io_cb_wakeup = 1; |
| 6477 | } |
| 6478 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6479 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 6480 | TRACE_PROTO("Connection error", |
| 6481 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6482 | goto out; |
| 6483 | } |
| 6484 | |
| 6485 | pkt->raw_len = pkt->len; |
| 6486 | quic_rx_pkts_del(qc); |
| 6487 | b_cspace = b_contig_space(&qc->rx.buf); |
| 6488 | if (b_cspace < pkt->len) { |
| 6489 | /* Do not consume buf if space not at the end. */ |
| 6490 | if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) { |
| 6491 | TRACE_PROTO("Packet dropped", |
| 6492 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6493 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6494 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6495 | } |
| 6496 | |
| 6497 | /* Let us consume the remaining contiguous space. */ |
| 6498 | if (b_cspace) { |
| 6499 | b_putchr(&qc->rx.buf, 0x00); |
| 6500 | b_cspace--; |
| 6501 | } |
| 6502 | b_add(&qc->rx.buf, b_cspace); |
| 6503 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 6504 | TRACE_PROTO("Too big packet", |
| 6505 | QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6506 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6507 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6508 | } |
| 6509 | } |
| 6510 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6511 | if (!qc_try_rm_hp(qc, pkt, beg, &qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6512 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6513 | goto drop; |
| 6514 | } |
| 6515 | |
| 6516 | TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 6517 | if (pkt->aad_len) |
| 6518 | qc_pkt_insert(qc, pkt, qel); |
| 6519 | out: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 6520 | *tasklist_head = tasklet_wakeup_after(*tasklist_head, |
| 6521 | qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6522 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6523 | drop_silent: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6524 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6525 | return; |
| 6526 | |
| 6527 | drop: |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6528 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6529 | err: |
| 6530 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 6531 | * This cannot be done by this thread. |
| 6532 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 6533 | if (io_cb_wakeup) |
| 6534 | tasklet_wakeup(qc->wait_event.tasklet); |
| 6535 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6536 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
| 6537 | } |
| 6538 | |
| 6539 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 6540 | * Return 1 if enough room to build this header, 0 if not. |
| 6541 | */ |
| 6542 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 6543 | int type, size_t pn_len, |
| 6544 | struct quic_conn *qc, const struct quic_version *ver) |
| 6545 | { |
| 6546 | int ret = 0; |
| 6547 | |
| 6548 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6549 | |
| 6550 | if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) { |
| 6551 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6552 | goto leave; |
| 6553 | } |
| 6554 | |
| 6555 | type = quic_pkt_type(type, ver->num); |
| 6556 | /* #0 byte flags */ |
| 6557 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 6558 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 6559 | /* Version */ |
| 6560 | quic_write_uint32(buf, end, ver->num); |
| 6561 | *(*buf)++ = qc->dcid.len; |
| 6562 | /* Destination connection ID */ |
| 6563 | if (qc->dcid.len) { |
| 6564 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6565 | *buf += qc->dcid.len; |
| 6566 | } |
| 6567 | /* Source connection ID */ |
| 6568 | *(*buf)++ = qc->scid.len; |
| 6569 | if (qc->scid.len) { |
| 6570 | memcpy(*buf, qc->scid.data, qc->scid.len); |
| 6571 | *buf += qc->scid.len; |
| 6572 | } |
| 6573 | |
| 6574 | ret = 1; |
| 6575 | leave: |
| 6576 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6577 | return ret; |
| 6578 | } |
| 6579 | |
| 6580 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 6581 | * Return 1 if enough room to build this header, 0 if not. |
| 6582 | */ |
| 6583 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 6584 | size_t pn_len, struct quic_conn *qc, |
| 6585 | unsigned char tls_flags) |
| 6586 | { |
| 6587 | int ret = 0; |
| 6588 | |
| 6589 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6590 | |
| 6591 | if (end - *buf < 1 + qc->dcid.len) { |
| 6592 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6593 | goto leave; |
| 6594 | } |
| 6595 | |
| 6596 | /* #0 byte flags */ |
| 6597 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 6598 | ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1); |
| 6599 | /* Destination connection ID */ |
| 6600 | if (qc->dcid.len) { |
| 6601 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6602 | *buf += qc->dcid.len; |
| 6603 | } |
| 6604 | |
| 6605 | ret = 1; |
| 6606 | leave: |
| 6607 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6608 | return ret; |
| 6609 | } |
| 6610 | |
| 6611 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 6612 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 6613 | * with <aead> as AEAD cipher and <key> as secret key. |
| 6614 | * Returns 1 if succeeded or 0 if failed. |
| 6615 | */ |
| 6616 | static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf, |
| 6617 | unsigned char *pn, size_t pnlen, |
| 6618 | struct quic_tls_ctx *tls_ctx) |
| 6619 | |
| 6620 | { |
| 6621 | int i, ret = 0; |
| 6622 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 6623 | * and at most 4 bytes for the packet number |
| 6624 | */ |
| 6625 | unsigned char mask[5] = {0}; |
| 6626 | EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx; |
| 6627 | |
| 6628 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6629 | |
| 6630 | if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) { |
| 6631 | TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc); |
| 6632 | goto out; |
| 6633 | } |
| 6634 | |
| 6635 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 6636 | for (i = 0; i < pnlen; i++) |
| 6637 | pn[i] ^= mask[i + 1]; |
| 6638 | |
| 6639 | ret = 1; |
| 6640 | out: |
| 6641 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6642 | return ret; |
| 6643 | } |
| 6644 | |
| 6645 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 6646 | * ACK ranges if needed to a value below <limit> in bytes. |
| 6647 | * Return 1 if succeeded, 0 if not. |
| 6648 | */ |
| 6649 | static int quic_ack_frm_reduce_sz(struct quic_conn *qc, |
| 6650 | struct quic_frame *ack_frm, size_t limit) |
| 6651 | { |
| 6652 | size_t room, ack_delay_sz; |
| 6653 | int ret = 0; |
| 6654 | |
| 6655 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6656 | |
| 6657 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 6658 | /* A frame is made of 1 byte for the frame type. */ |
| 6659 | room = limit - ack_delay_sz - 1; |
| 6660 | if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room)) |
| 6661 | goto leave; |
| 6662 | |
| 6663 | ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz; |
| 6664 | leave: |
| 6665 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6666 | return ret; |
| 6667 | } |
| 6668 | |
| 6669 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 6670 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 6671 | * with <room> as available room, and <*len> the packet Length field initialized |
| 6672 | * with the number of bytes already present in this buffer which must be taken |
| 6673 | * into an account for the Length packet field value. <headlen> is the number of |
| 6674 | * bytes already present in this packet before building frames. |
| 6675 | * |
| 6676 | * Update consequently <*len> to reflect the size of these frames built |
| 6677 | * by this function. Also attach these frames to <l> frame list. |
| 6678 | * Return 1 if at least one ack-eleciting frame could be built, 0 if not. |
| 6679 | */ |
| 6680 | static inline int qc_build_frms(struct list *outlist, struct list *inlist, |
| 6681 | size_t room, size_t *len, size_t headlen, |
| 6682 | struct quic_enc_level *qel, |
| 6683 | struct quic_conn *qc) |
| 6684 | { |
| 6685 | int ret; |
| 6686 | struct quic_frame *cf, *cfbak; |
| 6687 | |
| 6688 | TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc); |
| 6689 | |
| 6690 | ret = 0; |
| 6691 | if (*len > room) |
| 6692 | goto leave; |
| 6693 | |
| 6694 | /* If we are not probing we must take into an account the congestion |
| 6695 | * control window. |
| 6696 | */ |
| 6697 | if (!qel->pktns->tx.pto_probe) { |
| 6698 | size_t remain = quic_path_prep_data(qc->path); |
| 6699 | |
| 6700 | if (headlen > remain) |
| 6701 | goto leave; |
| 6702 | |
| 6703 | room = QUIC_MIN(room, remain - headlen); |
| 6704 | } |
| 6705 | |
| 6706 | TRACE_PROTO("************** frames build (headlen)", |
| 6707 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
| 6708 | |
| 6709 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 6710 | * returned by this function only if at least one frame could be built |
| 6711 | * in the switch/case block. |
| 6712 | */ |
| 6713 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
| 6714 | /* header length, data length, frame length. */ |
| 6715 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
| 6716 | |
| 6717 | if (!room) |
| 6718 | break; |
| 6719 | |
| 6720 | switch (cf->type) { |
| 6721 | case QUIC_FT_CRYPTO: |
| 6722 | TRACE_DEVEL(" New CRYPTO frame build (room, len)", |
| 6723 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
| 6724 | /* Compute the length of this CRYPTO frame header */ |
| 6725 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 6726 | /* Compute the data length of this CRyPTO frame. */ |
| 6727 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 6728 | TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)", |
| 6729 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen); |
| 6730 | if (!dlen) |
| 6731 | continue; |
| 6732 | |
| 6733 | /* CRYPTO frame length. */ |
| 6734 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 6735 | TRACE_DEVEL(" CRYPTO frame length (flen)", |
| 6736 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 6737 | /* Add the CRYPTO data length and its encoded length to the packet |
| 6738 | * length and the length of this length. |
| 6739 | */ |
| 6740 | *len += flen; |
| 6741 | room -= flen; |
| 6742 | if (dlen == cf->crypto.len) { |
| 6743 | /* <cf> CRYPTO data have been consumed. */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 6744 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6745 | LIST_APPEND(outlist, &cf->list); |
| 6746 | } |
| 6747 | else { |
| 6748 | struct quic_frame *new_cf; |
| 6749 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 6750 | new_cf = qc_frm_alloc(QUIC_FT_CRYPTO); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6751 | if (!new_cf) { |
| 6752 | TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
| 6753 | continue; |
| 6754 | } |
| 6755 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6756 | new_cf->crypto.len = dlen; |
| 6757 | new_cf->crypto.offset = cf->crypto.offset; |
| 6758 | new_cf->crypto.qel = qel; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 6759 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6760 | if (cf->origin) { |
| 6761 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 6762 | /* This <cf> frame was duplicated */ |
| 6763 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 6764 | new_cf->origin = cf->origin; |
Frédéric Lécaille | dd41946 | 2023-01-26 15:07:39 +0100 | [diff] [blame] | 6765 | /* Detach the remaining CRYPTO frame from its original frame */ |
| 6766 | LIST_DEL_INIT(&cf->ref); |
| 6767 | cf->origin = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6768 | } |
| 6769 | LIST_APPEND(outlist, &new_cf->list); |
| 6770 | /* Consume <dlen> bytes of the current frame. */ |
| 6771 | cf->crypto.len -= dlen; |
| 6772 | cf->crypto.offset += dlen; |
| 6773 | } |
| 6774 | break; |
| 6775 | |
| 6776 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 6777 | if (cf->flags & QUIC_FL_TX_FRAME_LOST) { |
| 6778 | struct eb64_node *node = NULL; |
| 6779 | struct qc_stream_desc *stream_desc = NULL; |
| 6780 | struct quic_stream *strm = &cf->stream; |
| 6781 | |
| 6782 | /* As this frame has been already lost, ensure the stream is always |
| 6783 | * available or the range of this frame is not consumed before |
| 6784 | * resending it. |
| 6785 | */ |
| 6786 | node = eb64_lookup(&qc->streams_by_id, strm->id); |
| 6787 | if (!node) { |
| 6788 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 6789 | qc_frm_free(&cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6790 | continue; |
| 6791 | } |
| 6792 | |
| 6793 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 6794 | if (strm->offset.key + strm->len <= stream_desc->ack_offset) { |
| 6795 | TRACE_DEVEL("ignored frame frame in already acked range", |
| 6796 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 6797 | qc_frm_free(&cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6798 | continue; |
| 6799 | } |
| 6800 | else if (strm->offset.key < stream_desc->ack_offset) { |
| 6801 | strm->offset.key = stream_desc->ack_offset; |
| 6802 | TRACE_DEVEL("updated partially acked frame", |
| 6803 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
| 6804 | } |
| 6805 | } |
| 6806 | /* Note that these frames are accepted in short packets only without |
| 6807 | * "Length" packet field. Here, <*len> is used only to compute the |
| 6808 | * sum of the lengths of the already built frames for this packet. |
| 6809 | * |
| 6810 | * Compute the length of this STREAM frame "header" made a all the field |
| 6811 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 6812 | */ |
| 6813 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
| 6814 | ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0); |
| 6815 | /* Compute the data length of this STREAM frame. */ |
| 6816 | avail_room = room - hlen - *len; |
| 6817 | if ((ssize_t)avail_room <= 0) |
| 6818 | continue; |
| 6819 | |
| 6820 | TRACE_DEVEL(" New STREAM frame build (room, len)", |
| 6821 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
| 6822 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 6823 | dlen = max_available_room(avail_room, &dlen_sz); |
| 6824 | if (dlen > cf->stream.len) { |
| 6825 | dlen = cf->stream.len; |
| 6826 | } |
| 6827 | dlen_sz = quic_int_getsize(dlen); |
| 6828 | flen = hlen + dlen_sz + dlen; |
| 6829 | } |
| 6830 | else { |
| 6831 | dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len); |
| 6832 | flen = hlen + dlen; |
| 6833 | } |
| 6834 | TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)", |
| 6835 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen); |
| 6836 | TRACE_DEVEL(" STREAM frame length (flen)", |
| 6837 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 6838 | /* Add the STREAM data length and its encoded length to the packet |
| 6839 | * length and the length of this length. |
| 6840 | */ |
| 6841 | *len += flen; |
| 6842 | room -= flen; |
| 6843 | if (dlen == cf->stream.len) { |
| 6844 | /* <cf> STREAM data have been consumed. */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 6845 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6846 | LIST_APPEND(outlist, &cf->list); |
| 6847 | |
| 6848 | /* Do not notify MUX on retransmission. */ |
| 6849 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 6850 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 6851 | cf->stream.len, |
| 6852 | cf->stream.offset.key); |
| 6853 | } |
| 6854 | } |
| 6855 | else { |
| 6856 | struct quic_frame *new_cf; |
| 6857 | struct buffer cf_buf; |
| 6858 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 6859 | new_cf = qc_frm_alloc(cf->type); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6860 | if (!new_cf) { |
| 6861 | TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
| 6862 | continue; |
| 6863 | } |
| 6864 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6865 | new_cf->stream.stream = cf->stream.stream; |
| 6866 | new_cf->stream.buf = cf->stream.buf; |
| 6867 | new_cf->stream.id = cf->stream.id; |
Amaury Denoyelle | 1dac018 | 2023-02-02 16:45:07 +0100 | [diff] [blame] | 6868 | new_cf->stream.offset = cf->stream.offset; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6869 | new_cf->stream.len = dlen; |
| 6870 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 6871 | /* FIN bit reset */ |
| 6872 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 6873 | new_cf->stream.data = cf->stream.data; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 6874 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6875 | if (cf->origin) { |
| 6876 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 6877 | /* This <cf> frame was duplicated */ |
| 6878 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 6879 | new_cf->origin = cf->origin; |
Frédéric Lécaille | dd41946 | 2023-01-26 15:07:39 +0100 | [diff] [blame] | 6880 | /* Detach this STREAM frame from its origin */ |
| 6881 | LIST_DEL_INIT(&cf->ref); |
| 6882 | cf->origin = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6883 | } |
| 6884 | LIST_APPEND(outlist, &new_cf->list); |
| 6885 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 6886 | /* Consume <dlen> bytes of the current frame. */ |
| 6887 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 6888 | b_size(cf->stream.buf), |
| 6889 | (char *)cf->stream.data - b_orig(cf->stream.buf), 0); |
| 6890 | cf->stream.len -= dlen; |
| 6891 | cf->stream.offset.key += dlen; |
| 6892 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
| 6893 | |
| 6894 | /* Do not notify MUX on retransmission. */ |
| 6895 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 6896 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 6897 | new_cf->stream.len, |
| 6898 | new_cf->stream.offset.key); |
| 6899 | } |
| 6900 | } |
| 6901 | |
| 6902 | /* TODO the MUX is notified about the frame sending via |
| 6903 | * previous qcc_streams_sent_done call. However, the |
| 6904 | * sending can fail later, for example if the sendto |
| 6905 | * system call returns an error. As the MUX has been |
| 6906 | * notified, the transport layer is responsible to |
| 6907 | * bufferize and resent the announced data later. |
| 6908 | */ |
| 6909 | |
| 6910 | break; |
| 6911 | |
| 6912 | default: |
| 6913 | flen = qc_frm_len(cf); |
| 6914 | BUG_ON(!flen); |
| 6915 | if (flen > room) |
| 6916 | continue; |
| 6917 | |
| 6918 | *len += flen; |
| 6919 | room -= flen; |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 6920 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6921 | LIST_APPEND(outlist, &cf->list); |
| 6922 | break; |
| 6923 | } |
| 6924 | |
| 6925 | /* Successful status as soon as a frame could be built */ |
| 6926 | ret = 1; |
| 6927 | } |
| 6928 | |
| 6929 | leave: |
| 6930 | TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc); |
| 6931 | return ret; |
| 6932 | } |
| 6933 | |
| 6934 | /* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out> |
| 6935 | * is used as return parameter and should be zero'ed by the caller. |
| 6936 | */ |
| 6937 | static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel, |
| 6938 | struct quic_frame *out) |
| 6939 | { |
| 6940 | /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels |
| 6941 | * |
| 6942 | * A CONNECTION_CLOSE frame should be sent in several packets with |
| 6943 | * different encryption levels depending on the client context. This is |
| 6944 | * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for |
| 6945 | * more details on how to implement it. |
| 6946 | */ |
| 6947 | TRACE_ENTER(QUIC_EV_CONN_BFRM, qc); |
| 6948 | |
| 6949 | |
| 6950 | if (qc->err.app) { |
| 6951 | if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 6952 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 6953 | /* RFC 9000 10.2.3. Immediate Close during the Handshake |
| 6954 | * |
| 6955 | * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake |
| 6956 | * packet could expose application state or be used to alter application |
| 6957 | * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a |
| 6958 | * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or |
| 6959 | * Handshake packets. Otherwise, information about the application |
| 6960 | * state might be revealed. Endpoints MUST clear the value of the |
| 6961 | * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when |
| 6962 | * converting to a CONNECTION_CLOSE of type 0x1c. |
| 6963 | */ |
| 6964 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 6965 | out->connection_close.error_code = QC_ERR_APPLICATION_ERROR; |
| 6966 | out->connection_close.reason_phrase_len = 0; |
| 6967 | } |
| 6968 | else { |
| 6969 | out->type = QUIC_FT_CONNECTION_CLOSE_APP; |
| 6970 | out->connection_close.error_code = qc->err.code; |
| 6971 | } |
| 6972 | } |
| 6973 | else { |
| 6974 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 6975 | out->connection_close.error_code = qc->err.code; |
| 6976 | } |
| 6977 | TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc); |
| 6978 | |
| 6979 | } |
| 6980 | |
| 6981 | /* This function builds a clear packet from <pkt> information (its type) |
| 6982 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 6983 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 6984 | * filling the buffer with as much frames as possible from <frms> list of |
| 6985 | * prebuilt frames. |
| 6986 | * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are |
| 6987 | * reserved so that to ensure there is enough room to build this AEAD TAG after |
| 6988 | * having returned from this function. |
| 6989 | * This function also updates the value of <buf_pn> pointer to point to the packet |
| 6990 | * number field in this packet. <pn_len> will also have the packet number |
| 6991 | * length as value. |
| 6992 | * |
| 6993 | * Return 1 if succeeded (enough room to buile this packet), O if not. |
| 6994 | */ |
| 6995 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 6996 | size_t dglen, struct quic_tx_packet *pkt, |
| 6997 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
| 6998 | int force_ack, int padding, int cc, int probe, |
| 6999 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 7000 | const struct quic_version *ver, struct list *frms) |
| 7001 | { |
| 7002 | unsigned char *beg, *payload; |
| 7003 | size_t len, len_sz, len_frms, padding_len; |
| 7004 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 7005 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
| 7006 | struct quic_frame cc_frm = { }; |
| 7007 | size_t ack_frm_len, head_len; |
| 7008 | int64_t rx_largest_acked_pn; |
| 7009 | int add_ping_frm; |
| 7010 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 7011 | struct quic_frame *cf; |
| 7012 | int must_ack, ret = 0; |
| 7013 | int nb_aepkts_since_last_ack; |
| 7014 | |
| 7015 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 7016 | |
| 7017 | /* Length field value with CRYPTO frames if present. */ |
| 7018 | len_frms = 0; |
| 7019 | beg = pos; |
| 7020 | /* When not probing, and no immediate close is required, reduce the size of this |
| 7021 | * buffer to respect the congestion controller window. |
| 7022 | * This size will be limited if we have ack-eliciting frames to send from <frms>. |
| 7023 | */ |
| 7024 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
| 7025 | size_t path_room; |
| 7026 | |
| 7027 | path_room = quic_path_prep_data(qc->path); |
| 7028 | if (end - beg > path_room) |
| 7029 | end = beg + path_room; |
| 7030 | } |
| 7031 | |
| 7032 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 7033 | * length field if any. |
| 7034 | */ |
| 7035 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 7036 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 7037 | goto no_room; |
| 7038 | |
| 7039 | end -= QUIC_TLS_TAG_LEN; |
| 7040 | rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn; |
| 7041 | /* packet number length */ |
| 7042 | *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn); |
| 7043 | /* Build the header */ |
| 7044 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
| 7045 | !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) || |
| 7046 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
| 7047 | !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver))) |
| 7048 | goto no_room; |
| 7049 | |
| 7050 | /* Encode the token length (0) for an Initial packet. */ |
| 7051 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
| 7052 | *pos++ = 0; |
| 7053 | head_len = pos - beg; |
| 7054 | /* Build an ACK frame if required. */ |
| 7055 | ack_frm_len = 0; |
| 7056 | nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack; |
| 7057 | must_ack = !qel->pktns->tx.pto_probe && |
| 7058 | (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 7059 | (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK))); |
| 7060 | if (must_ack) { |
| 7061 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
| 7062 | BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root)); |
| 7063 | ack_frm.tx_ack.arngs = arngs; |
| 7064 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 7065 | qel->pktns->tx.ack_delay = |
| 7066 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 7067 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 7068 | } |
| 7069 | ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay; |
| 7070 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 7071 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 7072 | * that from here, we do not know if we will have to send a PING frame. |
| 7073 | * This will be decided after having computed the ack-eliciting frames |
| 7074 | * to be added to this packet. |
| 7075 | */ |
| 7076 | ack_frm_len = quic_ack_frm_reduce_sz(qc, &ack_frm, end - 1 - *pn_len - pos); |
| 7077 | if (!ack_frm_len) |
| 7078 | goto no_room; |
| 7079 | } |
| 7080 | |
| 7081 | /* Length field value without the ack-eliciting frames. */ |
| 7082 | len = ack_frm_len + *pn_len; |
| 7083 | len_frms = 0; |
| 7084 | if (!cc && !LIST_ISEMPTY(frms)) { |
| 7085 | ssize_t room = end - pos; |
| 7086 | |
| 7087 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 7088 | /* Initialize the length of the frames built below to <len>. |
| 7089 | * If any frame could be successfully built by qc_build_frms(), |
| 7090 | * we will have len_frms > len. |
| 7091 | */ |
| 7092 | len_frms = len; |
| 7093 | if (!qc_build_frms(&frm_list, frms, |
| 7094 | end - pos, &len_frms, pos - beg, qel, qc)) { |
| 7095 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
| 7096 | qc, NULL, NULL, &room); |
| 7097 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 7098 | goto no_room; |
| 7099 | } |
| 7100 | } |
| 7101 | |
| 7102 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 7103 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 7104 | * for the encryption tag. It must be taken into an account for the length |
| 7105 | * of this packet. |
| 7106 | */ |
| 7107 | if (len_frms) |
| 7108 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 7109 | else |
| 7110 | len += QUIC_TLS_TAG_LEN; |
| 7111 | /* CONNECTION_CLOSE frame */ |
| 7112 | if (cc) { |
| 7113 | qc_build_cc_frm(qc, qel, &cc_frm); |
| 7114 | len += qc_frm_len(&cc_frm); |
| 7115 | } |
| 7116 | add_ping_frm = 0; |
| 7117 | padding_len = 0; |
| 7118 | len_sz = quic_int_getsize(len); |
| 7119 | /* Add this packet size to <dglen> */ |
| 7120 | dglen += head_len + len_sz + len; |
| 7121 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 7122 | /* This is a maximum padding size */ |
| 7123 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 7124 | /* The length field value is of this packet is <len> + <padding_len> |
| 7125 | * the size of which may be greater than the initial computed size |
| 7126 | * <len_sz>. So, let's deduce the difference between these to packet |
| 7127 | * sizes from <padding_len>. |
| 7128 | */ |
| 7129 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 7130 | len += padding_len; |
| 7131 | } |
| 7132 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
| 7133 | if (qel->pktns->tx.pto_probe) { |
| 7134 | /* If we cannot send a frame, we send a PING frame. */ |
| 7135 | add_ping_frm = 1; |
| 7136 | len += 1; |
| 7137 | } |
| 7138 | /* If there is no frame at all to follow, add at least a PADDING frame. */ |
| 7139 | if (!ack_frm_len && !cc) |
| 7140 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 7141 | } |
| 7142 | |
| 7143 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 7144 | goto no_room; |
| 7145 | |
| 7146 | /* Packet number field address. */ |
| 7147 | *buf_pn = pos; |
| 7148 | |
| 7149 | /* Packet number encoding. */ |
| 7150 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 7151 | goto no_room; |
| 7152 | |
| 7153 | /* payload building (ack-eliciting or not frames) */ |
| 7154 | payload = pos; |
| 7155 | if (ack_frm_len) { |
| 7156 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 7157 | goto no_room; |
| 7158 | |
| 7159 | pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns); |
| 7160 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
| 7161 | } |
| 7162 | |
| 7163 | /* Ack-eliciting frames */ |
| 7164 | if (!LIST_ISEMPTY(&frm_list)) { |
| 7165 | struct quic_frame *tmp_cf; |
| 7166 | list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) { |
| 7167 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
| 7168 | ssize_t room = end - pos; |
| 7169 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
| 7170 | qc, NULL, NULL, &room); |
| 7171 | /* Note that <cf> was added from <frms> to <frm_list> list by |
| 7172 | * qc_build_frms(). |
| 7173 | */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7174 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7175 | LIST_INSERT(frms, &cf->list); |
| 7176 | continue; |
| 7177 | } |
| 7178 | |
| 7179 | quic_tx_packet_refinc(pkt); |
| 7180 | cf->pkt = pkt; |
| 7181 | } |
| 7182 | } |
| 7183 | |
| 7184 | /* Build a PING frame if needed. */ |
| 7185 | if (add_ping_frm) { |
| 7186 | frm.type = QUIC_FT_PING; |
| 7187 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 7188 | goto no_room; |
| 7189 | } |
| 7190 | |
| 7191 | /* Build a CONNECTION_CLOSE frame if needed. */ |
| 7192 | if (cc) { |
| 7193 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 7194 | goto no_room; |
| 7195 | |
| 7196 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 7197 | } |
| 7198 | |
| 7199 | /* Build a PADDING frame if needed. */ |
| 7200 | if (padding_len) { |
| 7201 | frm.type = QUIC_FT_PADDING; |
| 7202 | frm.padding.len = padding_len; |
| 7203 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 7204 | goto no_room; |
| 7205 | } |
| 7206 | |
| 7207 | if (pos == payload) { |
| 7208 | /* No payload was built because of congestion control */ |
| 7209 | TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc); |
| 7210 | goto no_room; |
| 7211 | } |
| 7212 | |
| 7213 | /* If this packet is ack-eliciting and we are probing let's |
| 7214 | * decrement the PTO probe counter. |
| 7215 | */ |
| 7216 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 7217 | qel->pktns->tx.pto_probe) |
| 7218 | qel->pktns->tx.pto_probe--; |
| 7219 | |
| 7220 | pkt->len = pos - beg; |
| 7221 | LIST_SPLICE(&pkt->frms, &frm_list); |
| 7222 | |
| 7223 | ret = 1; |
| 7224 | TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt); |
| 7225 | leave: |
| 7226 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 7227 | return ret; |
| 7228 | |
| 7229 | no_room: |
| 7230 | /* Replace the pre-built frames which could not be add to this packet */ |
| 7231 | LIST_SPLICE(frms, &frm_list); |
| 7232 | TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 7233 | goto leave; |
| 7234 | } |
| 7235 | |
| 7236 | static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type) |
| 7237 | { |
| 7238 | pkt->type = type; |
| 7239 | pkt->len = 0; |
| 7240 | pkt->in_flight_len = 0; |
| 7241 | pkt->pn_node.key = (uint64_t)-1; |
| 7242 | LIST_INIT(&pkt->frms); |
| 7243 | pkt->time_sent = TICK_ETERNITY; |
| 7244 | pkt->next = NULL; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 7245 | pkt->prev = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7246 | pkt->largest_acked_pn = -1; |
| 7247 | pkt->flags = 0; |
| 7248 | pkt->refcnt = 0; |
| 7249 | } |
| 7250 | |
| 7251 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 7252 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 7253 | * of prebuilt frames. |
| 7254 | * |
| 7255 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 7256 | * -1 if there was not enough room to build a packet. |
| 7257 | * XXX NOTE XXX |
| 7258 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 7259 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 7260 | * control window limitation. |
| 7261 | */ |
| 7262 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 7263 | const unsigned char *buf_end, |
| 7264 | struct quic_enc_level *qel, |
| 7265 | struct quic_tls_ctx *tls_ctx, struct list *frms, |
| 7266 | struct quic_conn *qc, const struct quic_version *ver, |
| 7267 | size_t dglen, int pkt_type, int force_ack, |
| 7268 | int padding, int probe, int cc, int *err) |
| 7269 | { |
| 7270 | struct quic_tx_packet *ret_pkt = NULL; |
| 7271 | /* The pointer to the packet number field. */ |
| 7272 | unsigned char *buf_pn; |
| 7273 | unsigned char *beg, *end, *payload; |
| 7274 | int64_t pn; |
| 7275 | size_t pn_len, payload_len, aad_len; |
| 7276 | struct quic_tx_packet *pkt; |
| 7277 | |
| 7278 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel); |
| 7279 | *err = 0; |
| 7280 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 7281 | if (!pkt) { |
| 7282 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc); |
| 7283 | *err = -2; |
| 7284 | goto err; |
| 7285 | } |
| 7286 | |
| 7287 | quic_tx_packet_init(pkt, pkt_type); |
| 7288 | beg = *pos; |
| 7289 | pn_len = 0; |
| 7290 | buf_pn = NULL; |
| 7291 | |
| 7292 | pn = qel->pktns->tx.next_pn + 1; |
| 7293 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
| 7294 | force_ack, padding, cc, probe, qel, qc, ver, frms)) { |
| 7295 | // trace already emitted by function above |
| 7296 | *err = -1; |
| 7297 | goto err; |
| 7298 | } |
| 7299 | |
| 7300 | end = beg + pkt->len; |
| 7301 | payload = buf_pn + pn_len; |
| 7302 | payload_len = end - payload; |
| 7303 | aad_len = payload - beg; |
| 7304 | |
| 7305 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) { |
| 7306 | // trace already emitted by function above |
| 7307 | *err = -2; |
| 7308 | goto err; |
| 7309 | } |
| 7310 | |
| 7311 | end += QUIC_TLS_TAG_LEN; |
| 7312 | pkt->len += QUIC_TLS_TAG_LEN; |
| 7313 | if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) { |
| 7314 | // trace already emitted by function above |
| 7315 | *err = -2; |
| 7316 | goto err; |
| 7317 | } |
| 7318 | |
| 7319 | /* Consume a packet number */ |
| 7320 | qel->pktns->tx.next_pn++; |
| 7321 | qc->tx.prep_bytes += pkt->len; |
| 7322 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) { |
| 7323 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 7324 | TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc); |
| 7325 | } |
| 7326 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 7327 | *pos = end; |
| 7328 | /* Attach the built packet to its tree. */ |
| 7329 | pkt->pn_node.key = pn; |
| 7330 | /* Set the packet in fligth length for in flight packet only. */ |
| 7331 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
| 7332 | pkt->in_flight_len = pkt->len; |
| 7333 | qc->path->prep_in_flight += pkt->len; |
| 7334 | } |
| 7335 | /* Always reset this flags */ |
| 7336 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 7337 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 7338 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 7339 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 7340 | } |
| 7341 | |
| 7342 | pkt->pktns = qel->pktns; |
| 7343 | |
| 7344 | ret_pkt = pkt; |
| 7345 | leave: |
| 7346 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt); |
| 7347 | return ret_pkt; |
| 7348 | |
| 7349 | err: |
| 7350 | /* TODO: what about the frames which have been built |
| 7351 | * for this packet. |
| 7352 | */ |
| 7353 | free_quic_tx_packet(qc, pkt); |
| 7354 | goto leave; |
| 7355 | } |
| 7356 | |
| 7357 | |
| 7358 | static void __quic_conn_init(void) |
| 7359 | { |
| 7360 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 7361 | } |
| 7362 | INITCALL0(STG_REGISTER, __quic_conn_init); |
| 7363 | |
| 7364 | static void __quic_conn_deinit(void) |
| 7365 | { |
| 7366 | BIO_meth_free(ha_quic_meth); |
| 7367 | } |
| 7368 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
| 7369 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7370 | /* Handle a new <dgram> received. Parse each QUIC packets and copied their |
| 7371 | * content to a quic-conn instance. The datagram content can be released after |
| 7372 | * this function. |
| 7373 | * |
| 7374 | * If datagram has been received on a quic-conn owned FD, <from_qc> must be set |
| 7375 | * to the connection instance. <li> is the attached listener. The caller is |
| 7376 | * responsible to ensure that the first packet is destined to this connection |
| 7377 | * by comparing CIDs. |
| 7378 | * |
| 7379 | * If datagram has been received on a receiver FD, <from_qc> will be NULL. This |
| 7380 | * function will thus retrieve the connection from the CID tree or allocate a |
| 7381 | * new one if possible. <li> is the listener attached to the receiver. |
| 7382 | * |
| 7383 | * Returns 0 on success else non-zero. If an error happens, some packets from |
| 7384 | * the datagram may not have been parsed. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7385 | */ |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7386 | int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc, |
| 7387 | struct listener *li) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7388 | { |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7389 | struct quic_rx_packet *pkt; |
| 7390 | struct quic_conn *qc = NULL; |
| 7391 | unsigned char *pos, *end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7392 | struct list *tasklist_head = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7393 | |
| 7394 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 7395 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7396 | pos = dgram->buf; |
| 7397 | end = pos + dgram->len; |
| 7398 | do { |
| 7399 | /* TODO replace zalloc -> alloc. */ |
| 7400 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 7401 | if (!pkt) { |
| 7402 | TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT); |
| 7403 | goto err; |
| 7404 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7405 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7406 | pkt->version = NULL; |
| 7407 | pkt->pn_offset = 0; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7408 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7409 | /* Set flag if pkt is the first one in dgram. */ |
| 7410 | if (pos == dgram->buf) |
| 7411 | pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7412 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7413 | LIST_INIT(&pkt->qc_rx_pkt_list); |
| 7414 | pkt->time_received = now_ms; |
| 7415 | quic_rx_packet_refinc(pkt); |
| 7416 | if (quic_rx_pkt_parse(pkt, pos, end, dgram, li)) |
| 7417 | goto next; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7418 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7419 | /* Search quic-conn instance for first packet of the datagram. |
| 7420 | * quic_rx_packet_parse() is responsible to discard packets |
| 7421 | * with different DCID as the first one in the same datagram. |
| 7422 | */ |
| 7423 | if (!qc) { |
| 7424 | qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li); |
| 7425 | /* qc is NULL if receiving a non Initial packet for an |
| 7426 | * unknown connection. |
| 7427 | */ |
| 7428 | if (!qc) { |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7429 | /* Skip the entire datagram. */ |
| 7430 | pkt->len = end - pos; |
| 7431 | goto next; |
| 7432 | } |
| 7433 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7434 | dgram->qc = qc; |
| 7435 | } |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7436 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7437 | if (qc_rx_check_closing(qc, pkt)) { |
| 7438 | /* Skip the entire datagram. */ |
| 7439 | pkt->len = end - pos; |
| 7440 | goto next; |
| 7441 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7442 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7443 | /* Detect QUIC connection migration. */ |
Frédéric Lécaille | f676954 | 2023-01-12 10:36:26 +0100 | [diff] [blame] | 7444 | if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) { |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7445 | if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) { |
| 7446 | /* Skip the entire datagram. */ |
| 7447 | TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc); |
| 7448 | pkt->len = end - pos; |
| 7449 | goto next; |
| 7450 | } |
| 7451 | } |
| 7452 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7453 | qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7454 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7455 | next: |
| 7456 | pos += pkt->len; |
| 7457 | quic_rx_packet_refdec(pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7458 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7459 | /* Free rejected packets */ |
| 7460 | if (!pkt->refcnt) { |
| 7461 | BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list)); |
| 7462 | pool_free(pool_head_quic_rx_packet, pkt); |
| 7463 | } |
| 7464 | } while (pos < end); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7465 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7466 | /* Increasing the received bytes counter by the UDP datagram length |
| 7467 | * if this datagram could be associated to a connection. |
| 7468 | */ |
| 7469 | if (dgram->qc) |
| 7470 | dgram->qc->rx.bytes += dgram->len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7471 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7472 | /* This must never happen. */ |
| 7473 | BUG_ON(pos > end); |
| 7474 | BUG_ON(pos < end || pos > dgram->buf + dgram->len); |
| 7475 | /* Mark this datagram as consumed */ |
| 7476 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7477 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7478 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 7479 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7480 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7481 | err: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7482 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7483 | return -1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7484 | } |
| 7485 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 7486 | /* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local |
| 7487 | * CIDs. This can be used to determine if a datagram is addressed to the right |
| 7488 | * connection instance. |
| 7489 | * |
| 7490 | * Returns a boolean value. |
| 7491 | */ |
| 7492 | int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len) |
| 7493 | { |
| 7494 | struct ebmb_node *node; |
| 7495 | struct quic_connection_id *id; |
| 7496 | |
| 7497 | /* For ODCID, address is concatenated to it after qc.odcid.len so this |
| 7498 | * comparison is safe. |
| 7499 | */ |
| 7500 | if ((qc->scid.len == dcid_len && |
| 7501 | memcmp(qc->scid.data, dcid, dcid_len) == 0) || |
| 7502 | (qc->odcid.len == dcid_len && |
| 7503 | memcmp(qc->odcid.data, dcid, dcid_len)) == 0) { |
| 7504 | return 1; |
| 7505 | } |
| 7506 | |
| 7507 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, dcid, dcid_len); |
| 7508 | if (node) { |
| 7509 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 7510 | if (qc == id->qc) |
| 7511 | return 1; |
| 7512 | } |
| 7513 | |
| 7514 | return 0; |
| 7515 | } |
| 7516 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7517 | /* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 7518 | * Returns 1 if succeeded, 0 if not. |
| 7519 | */ |
| 7520 | int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 7521 | unsigned char **dcid, size_t *dcid_len) |
| 7522 | { |
| 7523 | int ret = 0, long_header; |
| 7524 | size_t minlen, skip; |
| 7525 | |
| 7526 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 7527 | |
| 7528 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
| 7529 | TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT); |
| 7530 | goto err; |
| 7531 | } |
| 7532 | |
| 7533 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
| 7534 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 7535 | QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN; |
| 7536 | skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF; |
| 7537 | if (end - buf < minlen) |
| 7538 | goto err; |
| 7539 | |
| 7540 | buf += skip; |
| 7541 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 7542 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 7543 | goto err; |
| 7544 | |
| 7545 | *dcid = buf; |
| 7546 | |
| 7547 | ret = 1; |
| 7548 | leave: |
| 7549 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 7550 | return ret; |
| 7551 | |
| 7552 | err: |
| 7553 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT); |
| 7554 | goto leave; |
| 7555 | } |
| 7556 | |
| 7557 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 7558 | void qc_notify_close(struct quic_conn *qc) |
| 7559 | { |
| 7560 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 7561 | |
| 7562 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 7563 | goto leave; |
| 7564 | |
| 7565 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 7566 | /* wake up the MUX */ |
| 7567 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) { |
| 7568 | TRACE_STATE("connection closure notidfied to mux", |
| 7569 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7570 | qc->conn->mux->wake(qc->conn); |
| 7571 | } |
| 7572 | else |
| 7573 | TRACE_STATE("connection closure not notidfied to mux", |
| 7574 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7575 | leave: |
| 7576 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 7577 | } |
| 7578 | |
| 7579 | /* |
| 7580 | * Local variables: |
| 7581 | * c-indent-level: 8 |
| 7582 | * c-basic-offset: 8 |
| 7583 | * End: |
| 7584 | */ |