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 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 37 | #include <haproxy/applet-t.h> |
| 38 | #include <haproxy/cli.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 39 | #include <haproxy/connection.h> |
| 40 | #include <haproxy/fd.h> |
| 41 | #include <haproxy/freq_ctr.h> |
| 42 | #include <haproxy/global.h> |
| 43 | #include <haproxy/h3.h> |
| 44 | #include <haproxy/hq_interop.h> |
| 45 | #include <haproxy/log.h> |
| 46 | #include <haproxy/mux_quic.h> |
| 47 | #include <haproxy/ncbuf.h> |
| 48 | #include <haproxy/pipe.h> |
| 49 | #include <haproxy/proxy.h> |
| 50 | #include <haproxy/quic_cc.h> |
| 51 | #include <haproxy/quic_frame.h> |
| 52 | #include <haproxy/quic_enc.h> |
| 53 | #include <haproxy/quic_loss.h> |
| 54 | #include <haproxy/quic_sock.h> |
| 55 | #include <haproxy/quic_stats.h> |
| 56 | #include <haproxy/quic_stream.h> |
| 57 | #include <haproxy/quic_tp.h> |
| 58 | #include <haproxy/cbuf.h> |
| 59 | #include <haproxy/proto_quic.h> |
| 60 | #include <haproxy/quic_tls.h> |
| 61 | #include <haproxy/ssl_sock.h> |
| 62 | #include <haproxy/task.h> |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 63 | #include <haproxy/thread.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 64 | #include <haproxy/trace.h> |
| 65 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 66 | /* incremented by each "show quic". */ |
| 67 | static unsigned int qc_epoch = 0; |
| 68 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 69 | /* list of supported QUIC versions by this implementation */ |
| 70 | const struct quic_version quic_versions[] = { |
| 71 | { |
| 72 | .num = QUIC_PROTOCOL_VERSION_DRAFT_29, |
| 73 | .initial_salt = initial_salt_draft_29, |
| 74 | .initial_salt_len = sizeof initial_salt_draft_29, |
| 75 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
| 76 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
| 77 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
| 78 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
| 79 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
| 80 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
| 81 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
| 82 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
| 83 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT, |
| 84 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT, |
| 85 | }, |
| 86 | { |
| 87 | .num = QUIC_PROTOCOL_VERSION_1, |
| 88 | .initial_salt = initial_salt_v1, |
| 89 | .initial_salt_len = sizeof initial_salt_v1, |
| 90 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
| 91 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
| 92 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
| 93 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
| 94 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
| 95 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
| 96 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
| 97 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
| 98 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1, |
| 99 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1, |
| 100 | }, |
| 101 | { |
Frédéric Lécaille | 21c4c9b | 2023-01-13 16:37:02 +0100 | [diff] [blame] | 102 | .num = QUIC_PROTOCOL_VERSION_2, |
| 103 | .initial_salt = initial_salt_v2, |
| 104 | .initial_salt_len = sizeof initial_salt_v2, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 105 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2, |
| 106 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1, |
| 107 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2, |
| 108 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1, |
| 109 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2, |
| 110 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1, |
| 111 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2, |
| 112 | .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] | 113 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2, |
| 114 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 115 | }, |
| 116 | }; |
| 117 | |
| 118 | /* The total number of supported versions */ |
| 119 | const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions; |
| 120 | /* Listener only preferred version */ |
| 121 | const struct quic_version *preferred_version; |
| 122 | |
| 123 | /* trace source and events */ |
| 124 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 125 | const struct trace_source *src, |
| 126 | const struct ist where, const struct ist func, |
| 127 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 128 | |
| 129 | static const struct trace_event quic_trace_events[] = { |
| 130 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 131 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 132 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 133 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 134 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 135 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 136 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
| 137 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
| 138 | { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" }, |
| 139 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 140 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 141 | { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" }, |
| 142 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 143 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 144 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 145 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 146 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 147 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 148 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 149 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 150 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 151 | { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" }, |
| 152 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 153 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 154 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 155 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 156 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
| 157 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 158 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 159 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 160 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 161 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 162 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
| 163 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
| 164 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 165 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
| 166 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 167 | { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." }, |
| 168 | { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."}, |
| 169 | { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"}, |
| 170 | { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"}, |
| 171 | { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"}, |
| 172 | { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"}, |
| 173 | { .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] | 174 | { .mask = QUIC_EV_CONN_RCV, .name = "conn_recv", .desc = "RX on connection" }, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 175 | { /* end */ } |
| 176 | }; |
| 177 | |
| 178 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 179 | /* arg1 */ { /* already used by the connection */ }, |
| 180 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 181 | /* arg3 */ { }, |
| 182 | /* arg4 */ { } |
| 183 | }; |
| 184 | |
| 185 | static const struct name_desc quic_trace_decoding[] = { |
| 186 | #define QUIC_VERB_CLEAN 1 |
| 187 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 188 | { /* end */ } |
| 189 | }; |
| 190 | |
| 191 | |
| 192 | struct trace_source trace_quic = { |
| 193 | .name = IST("quic"), |
| 194 | .desc = "QUIC xprt", |
| 195 | .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */ |
| 196 | .default_cb = quic_trace, |
| 197 | .known_events = quic_trace_events, |
| 198 | .lockon_args = quic_trace_lockon_args, |
| 199 | .decoding = quic_trace_decoding, |
| 200 | .report_events = ~0, /* report everything by default */ |
| 201 | }; |
| 202 | |
| 203 | #define TRACE_SOURCE &trace_quic |
| 204 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 205 | |
| 206 | static BIO_METHOD *ha_quic_meth; |
| 207 | |
| 208 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ); |
| 209 | DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ); |
| 210 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
| 211 | "quic_conn_ctx", sizeof(struct ssl_sock_ctx)); |
| 212 | DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn)); |
| 213 | DECLARE_POOL(pool_head_quic_connection_id, |
| 214 | "quic_connnection_id", sizeof(struct quic_connection_id)); |
| 215 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
| 216 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet)); |
| 217 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet)); |
| 218 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm)); |
| 219 | 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] | 220 | 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] | 221 | DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame)); |
| 222 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node)); |
| 223 | |
| 224 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
| 225 | struct quic_enc_level *qel, struct quic_tls_ctx *ctx, |
| 226 | struct list *frms, struct quic_conn *qc, |
| 227 | const struct quic_version *ver, size_t dglen, int pkt_type, |
| 228 | int force_ack, int padding, int probe, int cc, int *err); |
| 229 | struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state); |
| 230 | static void qc_idle_timer_do_rearm(struct quic_conn *qc); |
| 231 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read); |
| 232 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc); |
| 233 | static int quic_conn_init_timer(struct quic_conn *qc); |
| 234 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc); |
| 235 | |
| 236 | /* Only for debug purpose */ |
| 237 | struct enc_debug_info { |
| 238 | unsigned char *payload; |
| 239 | size_t payload_len; |
| 240 | unsigned char *aad; |
| 241 | size_t aad_len; |
| 242 | uint64_t pn; |
| 243 | }; |
| 244 | |
| 245 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 246 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 247 | unsigned char *payload, size_t payload_len, |
| 248 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 249 | { |
| 250 | edi->payload = payload; |
| 251 | edi->payload_len = payload_len; |
| 252 | edi->aad = aad; |
| 253 | edi->aad_len = aad_len; |
| 254 | edi->pn = pn; |
| 255 | } |
| 256 | |
| 257 | /* Trace callback for QUIC. |
| 258 | * These traces always expect that arg1, if non-null, is of type connection. |
| 259 | */ |
| 260 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 261 | const struct ist where, const struct ist func, |
| 262 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 263 | { |
| 264 | const struct quic_conn *qc = a1; |
| 265 | |
| 266 | if (qc) { |
| 267 | const struct quic_tls_ctx *tls_ctx; |
| 268 | |
| 269 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
| 270 | if (mask & QUIC_EV_CONN_INIT) { |
| 271 | chunk_appendf(&trace_buf, "\n odcid"); |
| 272 | quic_cid_dump(&trace_buf, &qc->odcid); |
| 273 | chunk_appendf(&trace_buf, "\n dcid"); |
| 274 | quic_cid_dump(&trace_buf, &qc->dcid); |
| 275 | chunk_appendf(&trace_buf, "\n scid"); |
| 276 | quic_cid_dump(&trace_buf, &qc->scid); |
| 277 | } |
| 278 | |
| 279 | if (mask & QUIC_EV_TRANSP_PARAMS) { |
| 280 | const struct quic_transport_params *p = a2; |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 281 | |
| 282 | if (p) |
| 283 | quic_transport_params_dump(&trace_buf, qc, p); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 287 | const enum ssl_encryption_level_t *level = a2; |
| 288 | const size_t *len = a3; |
| 289 | |
| 290 | if (level) { |
| 291 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 292 | |
| 293 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 294 | } |
| 295 | if (len) |
| 296 | chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len); |
| 297 | } |
| 298 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 299 | /* Initial read & write secrets. */ |
| 300 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 301 | const unsigned char *rx_sec = a2; |
| 302 | const unsigned char *tx_sec = a3; |
| 303 | |
| 304 | tls_ctx = &qc->els[level].tls_ctx; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 305 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 306 | if (rx_sec) |
| 307 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 308 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
| 309 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 310 | if (tx_sec) |
| 311 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 312 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 313 | } |
| 314 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 315 | const enum ssl_encryption_level_t *level = a2; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 316 | |
| 317 | if (level) { |
| 318 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 319 | |
| 320 | 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] | 321 | if (quic_tls_has_rx_sec(&qc->els[lvl])) { |
| 322 | tls_ctx = &qc->els[lvl].tls_ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 323 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 324 | } |
| 325 | else |
| 326 | chunk_appendf(&trace_buf, " (none)"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 331 | const enum ssl_encryption_level_t *level = a2; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 332 | |
| 333 | if (level) { |
| 334 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 335 | |
| 336 | 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] | 337 | if (quic_tls_has_tx_sec(&qc->els[lvl])) { |
| 338 | tls_ctx = &qc->els[lvl].tls_ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 339 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 340 | } |
| 341 | else |
| 342 | chunk_appendf(&trace_buf, " (none)"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | } |
| 346 | |
| 347 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 348 | const struct list *l = a2; |
| 349 | |
| 350 | if (l) { |
| 351 | const struct quic_frame *frm; |
| 352 | list_for_each_entry(frm, l, list) { |
| 353 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 354 | chunk_frm_appendf(&trace_buf, frm); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) { |
| 360 | const struct quic_tx_packet *pkt = a2; |
| 361 | const struct quic_enc_level *qel = a3; |
| 362 | const ssize_t *room = a4; |
| 363 | |
| 364 | if (qel) { |
| 365 | const struct quic_pktns *pktns = qel->pktns; |
| 366 | chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu " |
| 367 | "if=%llu pp=%u", |
| 368 | quic_enc_level_char_from_qel(qel, qc), |
| 369 | (unsigned long long)qc->path->cwnd, |
| 370 | (unsigned long long)qc->path->prep_in_flight, |
| 371 | (unsigned long long)qc->path->in_flight, |
| 372 | (unsigned long long)pktns->tx.in_flight, |
| 373 | pktns->tx.pto_probe); |
| 374 | } |
| 375 | if (pkt) { |
| 376 | const struct quic_frame *frm; |
| 377 | if (pkt->pn_node.key != (uint64_t)-1) |
| 378 | chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key); |
| 379 | list_for_each_entry(frm, &pkt->frms, list) { |
| 380 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 381 | chunk_frm_appendf(&trace_buf, frm); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (room) { |
| 386 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 387 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 388 | (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | if (mask & QUIC_EV_CONN_IO_CB) { |
| 393 | const enum quic_handshake_state *state = a2; |
| 394 | const int *err = a3; |
| 395 | |
| 396 | if (state) |
| 397 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 398 | if (err) |
| 399 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 400 | } |
| 401 | |
| 402 | if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) { |
| 403 | const struct quic_rx_packet *pkt = a2; |
| 404 | const unsigned long *pktlen = a3; |
| 405 | const SSL *ssl = a4; |
| 406 | |
| 407 | if (pkt) { |
| 408 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 409 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 410 | chunk_appendf(&trace_buf, " kp=%d", |
| 411 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 412 | chunk_appendf(&trace_buf, " el=%c", |
| 413 | quic_packet_type_enc_level_char(pkt->type)); |
| 414 | if (pkt->pnl) |
| 415 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 416 | (unsigned long long)pkt->pn); |
| 417 | if (pkt->token_len) |
| 418 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 419 | (unsigned long long)pkt->token_len); |
| 420 | if (pkt->aad_len) |
| 421 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 422 | (unsigned long long)pkt->aad_len); |
| 423 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 424 | pkt->flags, (unsigned long long)pkt->len); |
| 425 | } |
| 426 | if (pktlen) |
| 427 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 428 | if (ssl) { |
| 429 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 430 | chunk_appendf(&trace_buf, " el=%c", |
| 431 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 436 | const struct quic_rx_packet *pkt = a2; |
| 437 | const struct quic_rx_crypto_frm *cf = a3; |
| 438 | const SSL *ssl = a4; |
| 439 | |
| 440 | if (pkt) |
| 441 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 442 | quic_packet_type_enc_level_char(pkt->type), |
| 443 | (unsigned long long)pkt->pn); |
| 444 | if (cf) |
| 445 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 446 | (unsigned long long)cf->offset_node.key, |
| 447 | (unsigned long long)cf->len); |
| 448 | if (ssl) { |
| 449 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 450 | chunk_appendf(&trace_buf, " rel=%c", |
| 451 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 452 | } |
| 453 | |
| 454 | if (qc->err.code) |
| 455 | chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code); |
| 456 | } |
| 457 | |
| 458 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 459 | const struct quic_frame *frm = a2; |
| 460 | |
| 461 | if (frm) |
| 462 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 463 | } |
| 464 | |
| 465 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 466 | const struct quic_enc_level *qel = a2; |
| 467 | |
| 468 | if (qel) { |
| 469 | const struct quic_pktns *pktns = qel->pktns; |
| 470 | chunk_appendf(&trace_buf, |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 471 | " 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] | 472 | quic_enc_level_char_from_qel(qel, qc), |
| 473 | quic_hdshk_state_str(qc->state), |
| 474 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
| 475 | (unsigned long long)qc->path->cwnd, |
| 476 | (unsigned long long)qc->path->prep_in_flight, |
| 477 | (unsigned long long)qc->path->in_flight, |
| 478 | (unsigned long long)pktns->tx.in_flight, |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 479 | pktns->tx.pto_probe, |
| 480 | qel->cstream ? (unsigned long long)qel->cstream->rx.offset : 0); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 485 | const struct enc_debug_info *edi = a2; |
| 486 | |
| 487 | if (edi) |
| 488 | chunk_appendf(&trace_buf, |
| 489 | " payload=@%p payload_len=%llu" |
| 490 | " aad=@%p aad_len=%llu pn=%llu", |
| 491 | edi->payload, (unsigned long long)edi->payload_len, |
| 492 | edi->aad, (unsigned long long)edi->aad_len, |
| 493 | (unsigned long long)edi->pn); |
| 494 | } |
| 495 | |
| 496 | if (mask & QUIC_EV_CONN_RMHP) { |
| 497 | const struct quic_rx_packet *pkt = a2; |
| 498 | |
| 499 | if (pkt) { |
| 500 | const int *ret = a3; |
| 501 | |
| 502 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 503 | if (ret && *ret) |
| 504 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 505 | pkt->pnl, (unsigned long long)pkt->pn); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
| 510 | const struct quic_frame *frm = a2; |
| 511 | const unsigned long *val1 = a3; |
| 512 | const unsigned long *val2 = a4; |
| 513 | |
| 514 | if (frm) { |
| 515 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 516 | chunk_frm_appendf(&trace_buf, frm); |
| 517 | } |
| 518 | if (val1) |
| 519 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 520 | if (val2) |
| 521 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 522 | } |
| 523 | |
| 524 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 525 | const struct quic_stream *s = a2; |
| 526 | const struct qc_stream_desc *stream = a3; |
| 527 | |
| 528 | if (s) |
| 529 | chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len); |
| 530 | if (stream) |
| 531 | chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset); |
| 532 | } |
| 533 | |
| 534 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 535 | const unsigned int *rtt_sample = a2; |
| 536 | const unsigned int *ack_delay = a3; |
| 537 | const struct quic_loss *ql = a4; |
| 538 | |
| 539 | if (rtt_sample) |
| 540 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 541 | if (ack_delay) |
| 542 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 543 | if (ql) |
| 544 | chunk_appendf(&trace_buf, |
| 545 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 546 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 547 | } |
| 548 | if (mask & QUIC_EV_CONN_CC) { |
| 549 | const struct quic_cc_event *ev = a2; |
| 550 | const struct quic_cc *cc = a3; |
| 551 | |
| 552 | if (a2) |
| 553 | quic_cc_event_trace(&trace_buf, ev); |
| 554 | if (a3) |
| 555 | quic_cc_state_trace(&trace_buf, cc); |
| 556 | } |
| 557 | |
| 558 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 559 | const struct quic_pktns *pktns = a2; |
| 560 | const struct list *lost_pkts = a3; |
| 561 | |
| 562 | if (pktns) { |
| 563 | chunk_appendf(&trace_buf, " pktns=%s", |
| 564 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 565 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 566 | if (pktns->tx.loss_time) |
| 567 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 568 | TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time))); |
| 569 | } |
| 570 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 571 | struct quic_tx_packet *pkt; |
| 572 | |
| 573 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 574 | list_for_each_entry(pkt, lost_pkts, list) |
| 575 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) { |
| 580 | const struct quic_pktns *pktns = a2; |
| 581 | const int *duration = a3; |
| 582 | const uint64_t *ifae_pkts = a4; |
| 583 | |
| 584 | if (ifae_pkts) |
| 585 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 586 | (unsigned long long)*ifae_pkts); |
| 587 | if (pktns) { |
| 588 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
| 589 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 590 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 591 | pktns->tx.pto_probe); |
| 592 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 593 | if (pktns->tx.in_flight) |
| 594 | chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight); |
| 595 | if (pktns->tx.loss_time) |
| 596 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 597 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 598 | } |
| 599 | if (mask & QUIC_EV_CONN_SPTO) { |
| 600 | if (pktns->tx.time_of_last_eliciting) |
| 601 | chunk_appendf(&trace_buf, " tole=%dms", |
| 602 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 603 | if (duration) |
| 604 | chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration)); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) { |
| 609 | chunk_appendf(&trace_buf, |
| 610 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 615 | const struct quic_tx_packet *pkt = a2; |
| 616 | |
| 617 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 618 | (unsigned long long)qc->path->cwnd, |
| 619 | (unsigned long long)qc->path->prep_in_flight, |
| 620 | (unsigned long long)qc->path->in_flight); |
| 621 | if (pkt) { |
| 622 | const struct quic_frame *frm; |
| 623 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
| 624 | (unsigned long)pkt->pn_node.key, |
| 625 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 626 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 627 | (unsigned long long)pkt->in_flight_len); |
| 628 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 629 | (unsigned long long)qc->rx.bytes, |
| 630 | (unsigned long long)qc->tx.bytes); |
| 631 | list_for_each_entry(frm, &pkt->frms, list) { |
| 632 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 633 | chunk_frm_appendf(&trace_buf, frm); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 639 | const uint8_t *alert = a2; |
| 640 | const enum ssl_encryption_level_t *level = a3; |
| 641 | |
| 642 | if (alert) |
| 643 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 644 | if (level) |
| 645 | chunk_appendf(&trace_buf, " el=%c", |
| 646 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 647 | } |
| 648 | |
| 649 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 650 | const size_t *sz1 = a2; |
| 651 | const size_t *sz2 = a3; |
| 652 | const size_t *sz3 = a4; |
| 653 | |
| 654 | if (sz1) |
| 655 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 656 | if (sz2) |
| 657 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 658 | if (sz3) |
| 659 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 660 | } |
| 661 | |
| 662 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 663 | const struct quic_frame *frm = a2; |
| 664 | |
| 665 | if (frm) { |
| 666 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 667 | chunk_frm_appendf(&trace_buf, frm); |
| 668 | } |
| 669 | } |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 670 | |
| 671 | if (mask & QUIC_EV_CONN_ELEVELSEL) { |
| 672 | const enum quic_handshake_state *state = a2; |
| 673 | const enum quic_tls_enc_level *level = a3; |
| 674 | const enum quic_tls_enc_level *next_level = a4; |
| 675 | |
| 676 | if (state) |
| 677 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(qc->state)); |
| 678 | if (level) |
| 679 | chunk_appendf(&trace_buf, " level=%c", quic_enc_level_char(*level)); |
| 680 | if (next_level) |
| 681 | chunk_appendf(&trace_buf, " next_level=%c", quic_enc_level_char(*next_level)); |
| 682 | |
| 683 | } |
Amaury Denoyelle | 5b41486 | 2022-10-24 17:40:37 +0200 | [diff] [blame] | 684 | |
| 685 | if (mask & QUIC_EV_CONN_RCV) { |
| 686 | const struct quic_dgram *dgram = a2; |
| 687 | |
| 688 | if (dgram) |
| 689 | chunk_appendf(&trace_buf, " dgram.len=%zu", dgram->len); |
| 690 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 691 | } |
| 692 | if (mask & QUIC_EV_CONN_LPKT) { |
| 693 | const struct quic_rx_packet *pkt = a2; |
| 694 | const uint64_t *len = a3; |
| 695 | const struct quic_version *ver = a4; |
| 696 | |
| 697 | if (pkt) { |
| 698 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 699 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 700 | if (pkt->pn_node.key != (uint64_t)-1) |
| 701 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 702 | } |
| 703 | |
| 704 | if (len) |
| 705 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
| 706 | |
| 707 | if (ver) |
| 708 | chunk_appendf(&trace_buf, " ver=0x%08x", ver->num); |
| 709 | } |
| 710 | |
| 711 | if (mask & QUIC_EV_STATELESS_RST) { |
| 712 | const struct quic_cid *cid = a2; |
| 713 | |
| 714 | if (cid) |
| 715 | quic_cid_dump(&trace_buf, cid); |
| 716 | } |
| 717 | |
| 718 | } |
| 719 | |
| 720 | /* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */ |
| 721 | static inline int quic_peer_validated_addr(struct quic_conn *qc) |
| 722 | { |
| 723 | struct quic_pktns *hdshk_pktns, *app_pktns; |
| 724 | |
| 725 | if (!qc_is_listener(qc)) |
| 726 | return 1; |
| 727 | |
| 728 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 729 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 730 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 731 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 732 | qc->state >= QUIC_HS_ST_COMPLETE) |
| 733 | return 1; |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 738 | /* To be called to kill a connection as soon as possible (without sending any packet). */ |
| 739 | void qc_kill_conn(struct quic_conn *qc) |
| 740 | { |
| 741 | qc->flags |= QUIC_FL_CONN_TO_KILL; |
| 742 | task_wakeup(qc->idle_timer_task, TASK_WOKEN_OTHER); |
| 743 | } |
| 744 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 745 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 746 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 747 | */ |
| 748 | static inline void qc_set_timer(struct quic_conn *qc) |
| 749 | { |
| 750 | struct quic_pktns *pktns; |
| 751 | unsigned int pto; |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 752 | int handshake_confirmed; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 753 | |
| 754 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
| 755 | NULL, NULL, &qc->path->ifae_pkts); |
| 756 | |
| 757 | pktns = quic_loss_pktns(qc); |
| 758 | if (tick_isset(pktns->tx.loss_time)) { |
| 759 | qc->timer = pktns->tx.loss_time; |
| 760 | goto out; |
| 761 | } |
| 762 | |
| 763 | /* anti-amplification: the timer must be |
| 764 | * cancelled for a server which reached the anti-amplification limit. |
| 765 | */ |
| 766 | if (!quic_peer_validated_addr(qc) && |
| 767 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
| 768 | TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc); |
| 769 | qc->timer = TICK_ETERNITY; |
| 770 | goto out; |
| 771 | } |
| 772 | |
| 773 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
| 774 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
| 775 | /* Timer cancellation. */ |
| 776 | qc->timer = TICK_ETERNITY; |
| 777 | goto out; |
| 778 | } |
| 779 | |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 780 | handshake_confirmed = qc->state >= QUIC_HS_ST_CONFIRMED; |
| 781 | pktns = quic_pto_pktns(qc, handshake_confirmed, &pto); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 782 | if (tick_isset(pto)) |
| 783 | qc->timer = pto; |
| 784 | out: |
| 785 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
| 786 | if (tick_is_expired(qc->timer, now_ms)) { |
| 787 | TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc); |
| 788 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 789 | } |
| 790 | else { |
| 791 | TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc); |
| 792 | task_schedule(qc->timer_task, qc->timer); |
| 793 | } |
| 794 | } |
| 795 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
| 796 | } |
| 797 | |
| 798 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 799 | * connection. |
| 800 | * Return 1 if succeeded, 0 if not. |
| 801 | */ |
| 802 | static int quic_tls_key_update(struct quic_conn *qc) |
| 803 | { |
| 804 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 805 | struct quic_tls_secrets *rx, *tx; |
| 806 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 807 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 808 | const struct quic_version *ver = |
| 809 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
| 810 | int ret = 0; |
| 811 | |
| 812 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
| 813 | |
| 814 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 815 | rx = &tls_ctx->rx; |
| 816 | tx = &tls_ctx->tx; |
| 817 | nxt_rx = &qc->ku.nxt_rx; |
| 818 | nxt_tx = &qc->ku.nxt_tx; |
| 819 | |
| 820 | /* Prepare new RX secrets */ |
| 821 | if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen, |
| 822 | rx->secret, rx->secretlen)) { |
| 823 | TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
| 824 | goto leave; |
| 825 | } |
| 826 | |
| 827 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver, |
| 828 | nxt_rx->key, nxt_rx->keylen, |
| 829 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 830 | nxt_rx->secret, nxt_rx->secretlen)) { |
| 831 | TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 832 | goto leave; |
| 833 | } |
| 834 | |
| 835 | /* Prepare new TX secrets */ |
| 836 | if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen, |
| 837 | tx->secret, tx->secretlen)) { |
| 838 | TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
| 839 | goto leave; |
| 840 | } |
| 841 | |
| 842 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver, |
| 843 | nxt_tx->key, nxt_tx->keylen, |
| 844 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 845 | nxt_tx->secret, nxt_tx->secretlen)) { |
| 846 | TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 847 | goto leave; |
| 848 | } |
| 849 | |
| 850 | if (nxt_rx->ctx) { |
| 851 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 852 | nxt_rx->ctx = NULL; |
| 853 | } |
| 854 | |
| 855 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
| 856 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 857 | goto leave; |
| 858 | } |
| 859 | |
| 860 | if (nxt_tx->ctx) { |
| 861 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 862 | nxt_tx->ctx = NULL; |
| 863 | } |
| 864 | |
| 865 | if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
| 866 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 867 | goto leave; |
| 868 | } |
| 869 | |
| 870 | ret = 1; |
| 871 | leave: |
| 872 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc); |
| 873 | return ret; |
| 874 | } |
| 875 | |
| 876 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 877 | * Must be used after having updated them. |
| 878 | * Always succeeds. |
| 879 | */ |
| 880 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 881 | { |
| 882 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 883 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 884 | EVP_CIPHER_CTX *curr_ctx; |
| 885 | |
| 886 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 887 | |
| 888 | /* Rotate the RX secrets */ |
| 889 | curr_ctx = tls_ctx->rx.ctx; |
| 890 | curr_secret = tls_ctx->rx.secret; |
| 891 | curr_iv = tls_ctx->rx.iv; |
| 892 | curr_key = tls_ctx->rx.key; |
| 893 | |
| 894 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
| 895 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 896 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 897 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 898 | |
| 899 | qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx; |
| 900 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 901 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 902 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 903 | |
| 904 | qc->ku.prv_rx.ctx = curr_ctx; |
| 905 | qc->ku.prv_rx.secret = curr_secret; |
| 906 | qc->ku.prv_rx.iv = curr_iv; |
| 907 | qc->ku.prv_rx.key = curr_key; |
| 908 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 909 | |
| 910 | /* Update the TX secrets */ |
| 911 | curr_ctx = tls_ctx->tx.ctx; |
| 912 | curr_secret = tls_ctx->tx.secret; |
| 913 | curr_iv = tls_ctx->tx.iv; |
| 914 | curr_key = tls_ctx->tx.key; |
| 915 | |
| 916 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
| 917 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 918 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 919 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 920 | |
| 921 | qc->ku.nxt_tx.ctx = curr_ctx; |
| 922 | qc->ku.nxt_tx.secret = curr_secret; |
| 923 | qc->ku.nxt_tx.iv = curr_iv; |
| 924 | qc->ku.nxt_tx.key = curr_key; |
| 925 | |
| 926 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 927 | } |
| 928 | |
| 929 | /* returns 0 on error, 1 on success */ |
| 930 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 931 | const uint8_t *read_secret, |
| 932 | const uint8_t *write_secret, size_t secret_len) |
| 933 | { |
| 934 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 935 | struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 936 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 937 | struct quic_tls_secrets *rx = NULL, *tx = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 938 | const struct quic_version *ver = |
| 939 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
| 940 | int ret = 0; |
| 941 | |
| 942 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
| 943 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 944 | |
| 945 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 946 | TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc); |
| 947 | goto out; |
| 948 | } |
| 949 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 950 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 951 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 952 | goto out; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 955 | if (!read_secret) |
| 956 | goto write; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 957 | |
| 958 | rx = &tls_ctx->rx; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 959 | if (!quic_tls_secrets_keys_alloc(rx)) { |
| 960 | TRACE_ERROR("RX keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 961 | goto leave; |
| 962 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 963 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 964 | rx->aead = tls_aead(cipher); |
| 965 | rx->md = tls_md(cipher); |
| 966 | rx->hp = tls_hp(cipher); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 967 | |
| 968 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen, |
| 969 | rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key, |
| 970 | read_secret, secret_len)) { |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 971 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 972 | goto leave; |
| 973 | } |
| 974 | |
| 975 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 976 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 977 | goto leave; |
| 978 | } |
| 979 | |
| 980 | if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) { |
| 981 | TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 982 | goto leave; |
| 983 | } |
| 984 | |
| 985 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 986 | * listener. Note that a listener derives only RX secrets for this |
| 987 | * level. |
| 988 | */ |
| 989 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) { |
| 990 | TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc); |
| 991 | quic_accept_push_qc(qc); |
| 992 | } |
| 993 | |
| 994 | write: |
| 995 | |
| 996 | if (!write_secret) |
| 997 | goto out; |
| 998 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 999 | tx = &tls_ctx->tx; |
| 1000 | if (!quic_tls_secrets_keys_alloc(tx)) { |
| 1001 | TRACE_ERROR("TX keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 1002 | goto leave; |
| 1003 | } |
| 1004 | |
| 1005 | tx->aead = tls_aead(cipher); |
| 1006 | tx->md = tls_md(cipher); |
| 1007 | tx->hp = tls_hp(cipher); |
| 1008 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1009 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen, |
| 1010 | tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key, |
| 1011 | write_secret, secret_len)) { |
| 1012 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 1013 | goto leave; |
| 1014 | } |
| 1015 | |
| 1016 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 1017 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 1018 | goto leave; |
| 1019 | } |
| 1020 | |
| 1021 | if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) { |
| 1022 | TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 1023 | goto leave; |
| 1024 | } |
| 1025 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 1026 | if (level == ssl_encryption_handshake && qc_is_listener(qc)) { |
| 1027 | qc->enc_params_len = |
| 1028 | quic_transport_params_encode(qc->enc_params, |
| 1029 | qc->enc_params + sizeof qc->enc_params, |
| 1030 | &qc->rx.params, ver, 1); |
| 1031 | if (!qc->enc_params_len) { |
| 1032 | TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_RWSEC); |
| 1033 | goto leave; |
| 1034 | } |
| 1035 | |
| 1036 | if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) { |
| 1037 | TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_RWSEC); |
| 1038 | goto leave; |
| 1039 | } |
| 1040 | } |
| 1041 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1042 | if (level == ssl_encryption_application) { |
| 1043 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 1044 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 1045 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 1046 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1047 | if (rx) { |
| 1048 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
| 1049 | TRACE_ERROR("Could not allocate RX Application secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 1050 | goto leave; |
| 1051 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1052 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1053 | memcpy(rx->secret, read_secret, secret_len); |
| 1054 | rx->secretlen = secret_len; |
| 1055 | } |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1056 | |
| 1057 | if (tx) { |
| 1058 | if (!(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
| 1059 | TRACE_ERROR("Could not allocate TX Application secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 1060 | goto leave; |
| 1061 | } |
| 1062 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1063 | memcpy(tx->secret, write_secret, secret_len); |
| 1064 | tx->secretlen = secret_len; |
| 1065 | } |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1066 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1067 | /* Initialize all the secret keys lengths */ |
| 1068 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | out: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1072 | ret = 1; |
| 1073 | leave: |
| 1074 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
| 1078 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 1079 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 1080 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 1081 | * It fails (returns 0) only if it could not managed to allocate enough CRYPTO |
| 1082 | * buffers to store all the data. |
| 1083 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 1084 | */ |
| 1085 | static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel, |
| 1086 | const unsigned char *data, size_t len) |
| 1087 | { |
| 1088 | struct quic_crypto_buf **qcb; |
| 1089 | /* The remaining byte to store in CRYPTO buffers. */ |
| 1090 | size_t cf_offset, cf_len, *nb_buf; |
| 1091 | unsigned char *pos; |
| 1092 | int ret = 0; |
| 1093 | |
| 1094 | nb_buf = &qel->tx.crypto.nb_buf; |
| 1095 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 1096 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 1097 | cf_len = len; |
| 1098 | |
| 1099 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1100 | |
| 1101 | while (len) { |
| 1102 | size_t to_copy, room; |
| 1103 | |
| 1104 | pos = (*qcb)->data + (*qcb)->sz; |
| 1105 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1106 | to_copy = len > room ? room : len; |
| 1107 | if (to_copy) { |
| 1108 | memcpy(pos, data, to_copy); |
| 1109 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1110 | qel->tx.crypto.sz += to_copy; |
| 1111 | (*qcb)->sz += to_copy; |
| 1112 | len -= to_copy; |
| 1113 | data += to_copy; |
| 1114 | } |
| 1115 | else { |
| 1116 | struct quic_crypto_buf **tmp; |
| 1117 | |
| 1118 | // FIXME: realloc! |
| 1119 | tmp = realloc(qel->tx.crypto.bufs, |
| 1120 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1121 | if (tmp) { |
| 1122 | qel->tx.crypto.bufs = tmp; |
| 1123 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1124 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1125 | if (!*qcb) { |
| 1126 | TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc); |
| 1127 | goto leave; |
| 1128 | } |
| 1129 | |
| 1130 | (*qcb)->sz = 0; |
| 1131 | ++*nb_buf; |
| 1132 | } |
| 1133 | else { |
| 1134 | break; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1140 | * have been buffered. |
| 1141 | */ |
| 1142 | if (!len) { |
| 1143 | struct quic_frame *frm; |
| 1144 | struct quic_frame *found = NULL; |
| 1145 | |
| 1146 | /* There is at most one CRYPTO frame in this packet number |
| 1147 | * space. Let's look for it. |
| 1148 | */ |
| 1149 | list_for_each_entry(frm, &qel->pktns->tx.frms, list) { |
| 1150 | if (frm->type != QUIC_FT_CRYPTO) |
| 1151 | continue; |
| 1152 | |
| 1153 | /* Found */ |
| 1154 | found = frm; |
| 1155 | break; |
| 1156 | } |
| 1157 | |
| 1158 | if (found) { |
| 1159 | found->crypto.len += cf_len; |
| 1160 | } |
| 1161 | else { |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 1162 | frm = qc_frm_alloc(QUIC_FT_CRYPTO); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1163 | if (!frm) { |
| 1164 | TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc); |
| 1165 | goto leave; |
| 1166 | } |
| 1167 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1168 | frm->crypto.offset = cf_offset; |
| 1169 | frm->crypto.len = cf_len; |
| 1170 | frm->crypto.qel = qel; |
| 1171 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
| 1172 | } |
| 1173 | } |
| 1174 | ret = len == 0; |
| 1175 | leave: |
| 1176 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1177 | return ret; |
| 1178 | } |
| 1179 | |
| 1180 | /* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive |
| 1181 | * activity for <qc> will be interrupted. |
| 1182 | */ |
| 1183 | void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err) |
| 1184 | { |
| 1185 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1186 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) |
| 1187 | goto leave; |
| 1188 | |
| 1189 | TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc); |
| 1190 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 1191 | qc->err.code = err.code; |
| 1192 | qc->err.app = err.app; |
| 1193 | leave: |
| 1194 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 1195 | } |
| 1196 | |
| 1197 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1198 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1199 | { |
| 1200 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1201 | |
| 1202 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 1203 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 1204 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
| 1205 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 1206 | } |
| 1207 | quic_set_connection_close(qc, quic_err_tls(alert)); |
| 1208 | qc->flags |= QUIC_FL_CONN_TLS_ALERT; |
| 1209 | TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc); |
| 1210 | |
| 1211 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
| 1212 | } |
| 1213 | |
| 1214 | /* Set the application for <qc> QUIC connection. |
| 1215 | * Return 1 if succeeded, 0 if not. |
| 1216 | */ |
| 1217 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1218 | { |
| 1219 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1220 | qc->app_ops = &h3_ops; |
| 1221 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1222 | qc->app_ops = &hq_interop_ops; |
| 1223 | else |
| 1224 | return 0; |
| 1225 | |
| 1226 | return 1; |
| 1227 | } |
| 1228 | |
| 1229 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1230 | * wants to provide the QUIC layer with CRYPTO data. |
| 1231 | * Returns 1 if succeeded, 0 if not. |
| 1232 | */ |
| 1233 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1234 | const uint8_t *data, size_t len) |
| 1235 | { |
| 1236 | struct quic_conn *qc; |
| 1237 | enum quic_tls_enc_level tel; |
| 1238 | struct quic_enc_level *qel; |
| 1239 | int ret = 0; |
| 1240 | |
| 1241 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1242 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1243 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 1244 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 1245 | TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc); |
| 1246 | goto out; |
| 1247 | } |
| 1248 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1249 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 1250 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
| 1251 | goto out; |
| 1252 | } |
| 1253 | |
| 1254 | tel = ssl_to_quic_enc_level(level); |
| 1255 | if (tel == -1) { |
| 1256 | TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc); |
| 1257 | goto leave; |
| 1258 | } |
| 1259 | |
| 1260 | qel = &qc->els[tel]; |
| 1261 | if (!quic_crypto_data_cpy(qc, qel, data, len)) { |
| 1262 | TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc); |
| 1263 | goto leave; |
| 1264 | } |
| 1265 | |
| 1266 | TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
| 1267 | qc, &level, &len); |
| 1268 | out: |
| 1269 | ret = 1; |
| 1270 | leave: |
| 1271 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1272 | return ret; |
| 1273 | } |
| 1274 | |
| 1275 | int ha_quic_flush_flight(SSL *ssl) |
| 1276 | { |
| 1277 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1278 | |
| 1279 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1280 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
| 1281 | |
| 1282 | return 1; |
| 1283 | } |
| 1284 | |
| 1285 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1286 | { |
| 1287 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1288 | |
| 1289 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1290 | |
| 1291 | TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1292 | |
| 1293 | quic_set_tls_alert(qc, alert); |
| 1294 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
| 1295 | return 1; |
| 1296 | } |
| 1297 | |
| 1298 | /* QUIC TLS methods */ |
| 1299 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1300 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1301 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1302 | .flush_flight = ha_quic_flush_flight, |
| 1303 | .send_alert = ha_quic_send_alert, |
| 1304 | }; |
| 1305 | |
| 1306 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1307 | * Returns an error count. |
| 1308 | */ |
| 1309 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1310 | { |
| 1311 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1312 | int cfgerr = 0; |
| 1313 | |
| 1314 | long options = |
| 1315 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1316 | SSL_OP_SINGLE_ECDH_USE | |
| 1317 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1318 | SSL_CTX *ctx; |
| 1319 | |
| 1320 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1321 | bind_conf->initial_ctx = ctx; |
| 1322 | |
| 1323 | SSL_CTX_set_options(ctx, options); |
| 1324 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1325 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1326 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1327 | |
| 1328 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1329 | # if defined(HAVE_SSL_CLIENT_HELLO_CB) |
| 1330 | # if defined(SSL_OP_NO_ANTI_REPLAY) |
| 1331 | if (bind_conf->ssl_conf.early_data) { |
| 1332 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
| 1333 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
| 1334 | } |
| 1335 | # endif /* !SSL_OP_NO_ANTI_REPLAY */ |
| 1336 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1337 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1338 | # else /* ! HAVE_SSL_CLIENT_HELLO_CB */ |
| 1339 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1340 | # endif |
| 1341 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1342 | #endif |
| 1343 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1344 | |
| 1345 | return cfgerr; |
| 1346 | } |
| 1347 | |
| 1348 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1349 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1350 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1351 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1352 | */ |
| 1353 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1354 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1355 | { |
| 1356 | uint64_t expected_pn = largest_pn + 1; |
| 1357 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1358 | uint64_t pn_hwin = pn_win / 2; |
| 1359 | uint64_t pn_mask = pn_win - 1; |
| 1360 | uint64_t candidate_pn; |
| 1361 | |
| 1362 | |
| 1363 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1364 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1365 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1366 | candidate_pn + pn_hwin <= expected_pn) |
| 1367 | return candidate_pn + pn_win; |
| 1368 | |
| 1369 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1370 | return candidate_pn - pn_win; |
| 1371 | |
| 1372 | return candidate_pn; |
| 1373 | } |
| 1374 | |
| 1375 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1376 | * cryptographic context. |
| 1377 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1378 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1379 | * <end> points to one byte past the end of this packet. |
| 1380 | * Returns 1 if succeeded, 0 if not. |
| 1381 | */ |
| 1382 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1383 | struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
| 1384 | int64_t largest_pn, unsigned char *pn, unsigned char *byte0) |
| 1385 | { |
| 1386 | int ret, i, pnlen; |
| 1387 | uint64_t packet_number; |
| 1388 | uint32_t truncated_pn = 0; |
| 1389 | unsigned char mask[5] = {0}; |
| 1390 | unsigned char *sample; |
| 1391 | EVP_CIPHER_CTX *cctx = NULL; |
| 1392 | |
| 1393 | TRACE_ENTER(QUIC_EV_CONN_RMHP, qc); |
| 1394 | |
| 1395 | ret = 0; |
| 1396 | |
| 1397 | /* Check there is enough data in this packet. */ |
| 1398 | if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
| 1399 | TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1400 | goto leave; |
| 1401 | } |
| 1402 | |
| 1403 | cctx = EVP_CIPHER_CTX_new(); |
| 1404 | if (!cctx) { |
| 1405 | TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1406 | goto leave; |
| 1407 | } |
| 1408 | |
| 1409 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1410 | |
| 1411 | if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) { |
| 1412 | TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1413 | goto leave; |
| 1414 | } |
| 1415 | |
| 1416 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1417 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1418 | for (i = 0; i < pnlen; i++) { |
| 1419 | pn[i] ^= mask[i + 1]; |
| 1420 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1421 | } |
| 1422 | |
| 1423 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1424 | /* Store remaining information for this unprotected header */ |
| 1425 | pkt->pn = packet_number; |
| 1426 | pkt->pnl = pnlen; |
| 1427 | |
| 1428 | ret = 1; |
| 1429 | leave: |
| 1430 | if (cctx) |
| 1431 | EVP_CIPHER_CTX_free(cctx); |
| 1432 | TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc); |
| 1433 | return ret; |
| 1434 | } |
| 1435 | |
| 1436 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1437 | * address, with <payload_len> as payload length, <aad> as address of |
| 1438 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1439 | * context. |
| 1440 | * Returns 1 if succeeded, 0 if not. |
| 1441 | */ |
| 1442 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1443 | unsigned char *aad, size_t aad_len, uint64_t pn, |
| 1444 | struct quic_tls_ctx *tls_ctx, struct quic_conn *qc) |
| 1445 | { |
| 1446 | int ret = 0; |
| 1447 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 1448 | unsigned char *tx_iv = tls_ctx->tx.iv; |
| 1449 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
| 1450 | struct enc_debug_info edi; |
| 1451 | |
| 1452 | TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc); |
| 1453 | |
| 1454 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
| 1455 | TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc); |
| 1456 | goto err; |
| 1457 | } |
| 1458 | |
| 1459 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1460 | tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
| 1461 | TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc); |
| 1462 | goto err; |
| 1463 | } |
| 1464 | |
| 1465 | ret = 1; |
| 1466 | leave: |
| 1467 | TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc); |
| 1468 | return ret; |
| 1469 | |
| 1470 | err: |
| 1471 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
| 1472 | goto leave; |
| 1473 | } |
| 1474 | |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1475 | /* Decrypt <pkt> packet using encryption level <qel> for <qc> connection. |
| 1476 | * Decryption is done in place in packet buffer. |
| 1477 | * |
Ilya Shipitsin | 5fa29b8 | 2022-12-07 09:46:19 +0500 | [diff] [blame] | 1478 | * Returns 1 on success else 0. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1479 | */ |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1480 | static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel, |
| 1481 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1482 | { |
| 1483 | int ret, kp_changed; |
| 1484 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 1485 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
| 1486 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
| 1487 | unsigned char *rx_iv = tls_ctx->rx.iv; |
| 1488 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1489 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1490 | |
| 1491 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 1492 | |
| 1493 | ret = 0; |
| 1494 | kp_changed = 0; |
| 1495 | |
| 1496 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1497 | /* The two tested bits are not at the same position, |
| 1498 | * this is why they are first both inversed. |
| 1499 | */ |
| 1500 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1501 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1502 | /* The lowest packet number of a previous key phase |
| 1503 | * cannot be null if it really stores previous key phase |
| 1504 | * secrets. |
| 1505 | */ |
| 1506 | // TODO: check if BUG_ON() more suitable |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1507 | if (!qc->ku.prv_rx.pn) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1508 | TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc); |
| 1509 | goto leave; |
| 1510 | } |
| 1511 | |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1512 | rx_ctx = qc->ku.prv_rx.ctx; |
| 1513 | rx_iv = qc->ku.prv_rx.iv; |
| 1514 | rx_key = qc->ku.prv_rx.key; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1515 | } |
| 1516 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1517 | /* Next key phase */ |
| 1518 | kp_changed = 1; |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1519 | rx_ctx = qc->ku.nxt_rx.ctx; |
| 1520 | rx_iv = qc->ku.nxt_rx.iv; |
| 1521 | rx_key = qc->ku.nxt_rx.key; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) { |
| 1527 | TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1528 | goto leave; |
| 1529 | } |
| 1530 | |
| 1531 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1532 | pkt->data, pkt->aad_len, |
| 1533 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
| 1534 | if (!ret) { |
| 1535 | TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1536 | goto leave; |
| 1537 | } |
| 1538 | |
| 1539 | /* Update the keys only if the packet decryption succeeded. */ |
| 1540 | if (kp_changed) { |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1541 | quic_tls_rotate_keys(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1542 | /* Toggle the Key Phase bit */ |
| 1543 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1544 | /* Store the lowest packet number received for the current key phase */ |
| 1545 | tls_ctx->rx.pn = pkt->pn; |
| 1546 | /* Prepare the next key update */ |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1547 | if (!quic_tls_key_update(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1548 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1549 | goto leave; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | /* Update the packet length (required to parse the frames). */ |
| 1554 | pkt->len -= QUIC_TLS_TAG_LEN; |
| 1555 | ret = 1; |
| 1556 | leave: |
| 1557 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 1558 | return ret; |
| 1559 | } |
| 1560 | |
| 1561 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1562 | /* Release <frm> frame and mark its copies as acknowledged */ |
| 1563 | void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm) |
| 1564 | { |
| 1565 | uint64_t pn; |
| 1566 | struct quic_frame *origin, *f, *tmp; |
| 1567 | |
| 1568 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1569 | |
| 1570 | /* Identify this frame: a frame copy or one of its copies */ |
| 1571 | origin = frm->origin ? frm->origin : frm; |
| 1572 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1573 | * possibly a copy of <origin> |
| 1574 | */ |
| 1575 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1576 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1577 | * not release the packets (releasing the frames) at this time as |
| 1578 | * they are possibly also to be acknowledged alongside the |
| 1579 | * the current one. |
| 1580 | */ |
| 1581 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
| 1582 | if (f->pkt) { |
| 1583 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1584 | f->origin = NULL; |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1585 | LIST_DEL_INIT(&f->ref); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1586 | pn = f->pkt->pn_node.key; |
| 1587 | TRACE_DEVEL("mark frame as acked from packet", |
| 1588 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1589 | } |
| 1590 | else { |
| 1591 | TRACE_DEVEL("freeing unsent frame", |
| 1592 | QUIC_EV_CONN_PRSAFRM, qc, f); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1593 | LIST_DEL_INIT(&f->ref); |
| 1594 | qc_frm_free(&f); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1595 | } |
| 1596 | } |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1597 | LIST_DEL_INIT(&frm->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1598 | pn = frm->pkt->pn_node.key; |
| 1599 | quic_tx_packet_refdec(frm->pkt); |
| 1600 | TRACE_DEVEL("freeing frame from packet", |
| 1601 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1602 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1603 | |
| 1604 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1605 | } |
| 1606 | |
| 1607 | /* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released |
| 1608 | * and all STREAM data are acknowledged. The MUX is responsible to have set |
| 1609 | * <qc.err> before as it is reused for the CONNECTION_CLOSE frame. |
| 1610 | * |
| 1611 | * TODO this should also be called on lost packet detection |
| 1612 | */ |
| 1613 | void qc_check_close_on_released_mux(struct quic_conn *qc) |
| 1614 | { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1615 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1616 | |
| 1617 | if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) { |
| 1618 | /* Reuse errcode which should have been previously set by the MUX on release. */ |
| 1619 | quic_set_connection_close(qc, qc->err); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 1620 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 1624 | } |
| 1625 | |
| 1626 | /* Remove from <stream> the acknowledged frames. |
| 1627 | * |
| 1628 | * Returns 1 if at least one frame was removed else 0. |
| 1629 | */ |
| 1630 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1631 | struct qc_stream_desc *stream) |
| 1632 | { |
| 1633 | int ret; |
| 1634 | struct eb64_node *frm_node; |
| 1635 | |
| 1636 | TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc); |
| 1637 | |
| 1638 | ret = 0; |
| 1639 | frm_node = eb64_first(&stream->acked_frms); |
| 1640 | while (frm_node) { |
| 1641 | struct quic_stream *strm; |
| 1642 | struct quic_frame *frm; |
| 1643 | size_t offset, len; |
| 1644 | |
| 1645 | strm = eb64_entry(frm_node, struct quic_stream, offset); |
| 1646 | offset = strm->offset.key; |
| 1647 | len = strm->len; |
| 1648 | |
| 1649 | if (offset > stream->ack_offset) |
| 1650 | break; |
| 1651 | |
| 1652 | if (qc_stream_desc_ack(&stream, offset, len)) { |
| 1653 | /* cf. next comment : frame may be freed at this stage. */ |
| 1654 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1655 | qc, stream ? strm : NULL, stream); |
| 1656 | ret = 1; |
| 1657 | } |
| 1658 | |
| 1659 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1660 | * has been freed. with the stream frames tree. Nothing to do |
| 1661 | * anymore in here. |
| 1662 | */ |
| 1663 | if (!stream) { |
| 1664 | qc_check_close_on_released_mux(qc); |
| 1665 | ret = 1; |
| 1666 | goto leave; |
| 1667 | } |
| 1668 | |
| 1669 | frm_node = eb64_next(frm_node); |
| 1670 | eb64_delete(&strm->offset); |
| 1671 | |
| 1672 | frm = container_of(strm, struct quic_frame, stream); |
| 1673 | qc_release_frm(qc, frm); |
| 1674 | } |
| 1675 | |
| 1676 | leave: |
| 1677 | TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc); |
| 1678 | return ret; |
| 1679 | } |
| 1680 | |
| 1681 | /* Treat <frm> frame whose packet it is attached to has just been acknowledged. */ |
| 1682 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1683 | struct quic_frame *frm) |
| 1684 | { |
| 1685 | int stream_acked; |
| 1686 | |
| 1687 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1688 | |
| 1689 | stream_acked = 0; |
| 1690 | switch (frm->type) { |
| 1691 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1692 | { |
| 1693 | struct quic_stream *strm_frm = &frm->stream; |
| 1694 | struct eb64_node *node = NULL; |
| 1695 | struct qc_stream_desc *stream = NULL; |
| 1696 | const size_t offset = strm_frm->offset.key; |
| 1697 | const size_t len = strm_frm->len; |
| 1698 | |
| 1699 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1700 | * might be freed at this stage. Use the id to do a proper |
| 1701 | * lookup. |
| 1702 | * |
| 1703 | * TODO if lookup operation impact on the perf is noticeable, |
| 1704 | * implement a refcount on qc_stream_desc instances. |
| 1705 | */ |
| 1706 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1707 | if (!node) { |
| 1708 | TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm); |
| 1709 | qc_release_frm(qc, frm); |
| 1710 | /* early return */ |
| 1711 | goto leave; |
| 1712 | } |
| 1713 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1714 | |
| 1715 | TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
| 1716 | if (offset <= stream->ack_offset) { |
| 1717 | if (qc_stream_desc_ack(&stream, offset, len)) { |
| 1718 | stream_acked = 1; |
| 1719 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1720 | qc, strm_frm, stream); |
| 1721 | } |
| 1722 | |
| 1723 | if (!stream) { |
| 1724 | /* no need to continue if stream freed. */ |
| 1725 | TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc); |
| 1726 | qc_release_frm(qc, frm); |
| 1727 | qc_check_close_on_released_mux(qc); |
| 1728 | break; |
| 1729 | } |
| 1730 | |
| 1731 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1732 | qc, strm_frm, stream); |
| 1733 | qc_release_frm(qc, frm); |
| 1734 | } |
| 1735 | else { |
| 1736 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
| 1737 | } |
| 1738 | |
| 1739 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
| 1740 | } |
| 1741 | break; |
| 1742 | default: |
| 1743 | qc_release_frm(qc, frm); |
| 1744 | } |
| 1745 | |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 1746 | if (stream_acked) { |
| 1747 | if (qc->subs && qc->subs->events & SUB_RETRY_SEND) { |
| 1748 | tasklet_wakeup(qc->subs->tasklet); |
| 1749 | qc->subs->events &= ~SUB_RETRY_SEND; |
| 1750 | if (!qc->subs->events) |
| 1751 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1752 | } |
| 1753 | } |
| 1754 | leave: |
| 1755 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1756 | } |
| 1757 | |
| 1758 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1759 | * deallocating them, and their TX frames. |
| 1760 | * Returns the last node reached to be used for the next range. |
| 1761 | * May be NULL if <largest> node could not be found. |
| 1762 | */ |
| 1763 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1764 | struct eb_root *pkts, |
| 1765 | unsigned int *pkt_flags, |
| 1766 | struct list *newly_acked_pkts, |
| 1767 | struct eb64_node *largest_node, |
| 1768 | uint64_t largest, uint64_t smallest) |
| 1769 | { |
| 1770 | struct eb64_node *node; |
| 1771 | struct quic_tx_packet *pkt; |
| 1772 | |
| 1773 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1774 | |
| 1775 | node = largest_node ? largest_node : eb64_lookup_le(pkts, largest); |
| 1776 | while (node && node->key >= smallest) { |
| 1777 | struct quic_frame *frm, *frmbak; |
| 1778 | |
| 1779 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 1780 | *pkt_flags |= pkt->flags; |
| 1781 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
| 1782 | TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 1783 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1784 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 1785 | /* If there are others packet in the same datagram <pkt> is attached to, |
| 1786 | * detach the previous one and the next one from <pkt>. |
| 1787 | */ |
Frédéric Lécaille | 74b5f7b | 2022-11-20 18:35:35 +0100 | [diff] [blame] | 1788 | quic_tx_packet_dgram_detach(pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1789 | node = eb64_prev(node); |
| 1790 | eb64_delete(&pkt->pn_node); |
| 1791 | } |
| 1792 | |
| 1793 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1794 | return node; |
| 1795 | } |
| 1796 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1797 | /* Remove all frames from <pkt_frm_list> and reinsert them in the same order |
| 1798 | * they have been sent into <pktns_frm_list>. The loss counter of each frame is |
| 1799 | * incremented and checked if it does not exceed retransmission limit. |
| 1800 | * |
| 1801 | * Returns 1 on success, 0 if a frame loss limit is exceeded. A |
| 1802 | * CONNECTION_CLOSE is scheduled in this case. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1803 | */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1804 | static inline int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1805 | struct quic_tx_packet *pkt, |
| 1806 | struct list *pktns_frm_list) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1807 | { |
| 1808 | struct quic_frame *frm, *frmbak; |
| 1809 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1810 | struct list *pkt_frm_list = &pkt->frms; |
| 1811 | uint64_t pn = pkt->pn_node.key; |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1812 | int close = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1813 | |
| 1814 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1815 | |
| 1816 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1817 | /* First remove this frame from the packet it was attached to */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1818 | LIST_DEL_INIT(&frm->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1819 | quic_tx_packet_refdec(pkt); |
| 1820 | /* At this time, this frame is not freed but removed from its packet */ |
| 1821 | frm->pkt = NULL; |
| 1822 | /* Remove any reference to this frame */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1823 | qc_frm_unref(frm, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1824 | switch (frm->type) { |
| 1825 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1826 | { |
| 1827 | struct quic_stream *strm_frm = &frm->stream; |
| 1828 | struct eb64_node *node = NULL; |
| 1829 | struct qc_stream_desc *stream_desc; |
| 1830 | |
| 1831 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1832 | if (!node) { |
| 1833 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1834 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1835 | qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1836 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1837 | continue; |
| 1838 | } |
| 1839 | |
| 1840 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1841 | /* Do not resend this frame if in the "already acked range" */ |
| 1842 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 1843 | TRACE_DEVEL("ignored frame in already acked range", |
| 1844 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1845 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1846 | continue; |
| 1847 | } |
| 1848 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 1849 | strm_frm->offset.key = stream_desc->ack_offset; |
| 1850 | TRACE_DEVEL("updated partially acked frame", |
| 1851 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1852 | } |
| 1853 | break; |
| 1854 | } |
| 1855 | |
| 1856 | default: |
| 1857 | break; |
| 1858 | } |
| 1859 | |
| 1860 | /* Do not resend probing packet with old data */ |
| 1861 | if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) { |
| 1862 | TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 1863 | qc, frm, &pn); |
| 1864 | if (frm->origin) |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1865 | LIST_DEL_INIT(&frm->ref); |
| 1866 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1867 | continue; |
| 1868 | } |
| 1869 | |
| 1870 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 1871 | TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1872 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1873 | qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1874 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1875 | } |
| 1876 | else { |
Amaury Denoyelle | 24d5b72 | 2023-01-31 11:44:50 +0100 | [diff] [blame] | 1877 | if (++frm->loss_count >= global.tune.quic_max_frame_loss) { |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1878 | TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc); |
| 1879 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR)); |
| 1880 | close = 1; |
| 1881 | } |
| 1882 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1883 | if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) { |
| 1884 | /* Mark this STREAM frame as lost. A look up their stream descriptor |
| 1885 | * will be performed to check the stream is not consumed or released. |
| 1886 | */ |
| 1887 | frm->flags |= QUIC_FL_TX_FRAME_LOST; |
| 1888 | } |
| 1889 | LIST_APPEND(&tmp, &frm->list); |
| 1890 | TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | LIST_SPLICE(pktns_frm_list, &tmp); |
| 1895 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1896 | end: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1897 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1898 | return !close; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /* Free <pkt> TX packet and its attached frames. |
| 1902 | * This is the responsibility of the caller to remove this packet of |
| 1903 | * any data structure it was possibly attached to. |
| 1904 | */ |
| 1905 | static inline void free_quic_tx_packet(struct quic_conn *qc, |
| 1906 | struct quic_tx_packet *pkt) |
| 1907 | { |
| 1908 | struct quic_frame *frm, *frmbak; |
| 1909 | |
| 1910 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1911 | |
| 1912 | if (!pkt) |
| 1913 | goto leave; |
| 1914 | |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1915 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1916 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1917 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1918 | |
| 1919 | leave: |
| 1920 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 1921 | } |
| 1922 | |
| 1923 | /* Free the TX packets of <pkts> list */ |
| 1924 | static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts) |
| 1925 | { |
| 1926 | struct quic_tx_packet *pkt, *tmp; |
| 1927 | |
| 1928 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1929 | |
| 1930 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 1931 | LIST_DELETE(&pkt->list); |
| 1932 | eb64_delete(&pkt->pn_node); |
| 1933 | free_quic_tx_packet(qc, pkt); |
| 1934 | } |
| 1935 | |
| 1936 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 1937 | } |
| 1938 | |
| 1939 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1940 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1941 | * updating the range which contains <largest_acked_pn>. |
| 1942 | * Never fails. |
| 1943 | */ |
| 1944 | static void qc_treat_ack_of_ack(struct quic_conn *qc, |
| 1945 | struct quic_pktns *pktns, |
| 1946 | int64_t largest_acked_pn) |
| 1947 | { |
| 1948 | struct eb64_node *ar, *next_ar; |
| 1949 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1950 | |
| 1951 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1952 | |
| 1953 | ar = eb64_first(&arngs->root); |
| 1954 | while (ar) { |
| 1955 | struct quic_arng_node *ar_node; |
| 1956 | |
| 1957 | next_ar = eb64_next(ar); |
| 1958 | ar_node = eb64_entry(ar, struct quic_arng_node, first); |
| 1959 | |
| 1960 | if ((int64_t)ar_node->first.key > largest_acked_pn) { |
| 1961 | TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc); |
| 1962 | break; |
| 1963 | } |
| 1964 | |
| 1965 | if (largest_acked_pn < ar_node->last) { |
| 1966 | eb64_delete(ar); |
| 1967 | ar_node->first.key = largest_acked_pn + 1; |
| 1968 | eb64_insert(&arngs->root, ar); |
| 1969 | break; |
| 1970 | } |
| 1971 | |
| 1972 | eb64_delete(ar); |
| 1973 | pool_free(pool_head_quic_arng, ar_node); |
| 1974 | arngs->sz--; |
| 1975 | ar = next_ar; |
| 1976 | } |
| 1977 | |
| 1978 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1979 | } |
| 1980 | |
| 1981 | /* Send a packet ack event nofication for each newly acked packet of |
| 1982 | * <newly_acked_pkts> list and free them. |
| 1983 | * Always succeeds. |
| 1984 | */ |
| 1985 | static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc, |
| 1986 | struct list *newly_acked_pkts) |
| 1987 | { |
| 1988 | struct quic_tx_packet *pkt, *tmp; |
| 1989 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1990 | |
| 1991 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1992 | |
| 1993 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1994 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
| 1995 | qc->path->prep_in_flight -= pkt->in_flight_len; |
| 1996 | qc->path->in_flight -= pkt->in_flight_len; |
| 1997 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
| 1998 | qc->path->ifae_pkts--; |
| 1999 | /* If this packet contained an ACK frame, proceed to the |
| 2000 | * acknowledging of range of acks from the largest acknowledged |
| 2001 | * packet number which was sent in an ACK frame by this packet. |
| 2002 | */ |
| 2003 | if (pkt->largest_acked_pn != -1) |
| 2004 | qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn); |
| 2005 | ev.ack.acked = pkt->in_flight_len; |
| 2006 | ev.ack.time_sent = pkt->time_sent; |
| 2007 | quic_cc_event(&qc->path->cc, &ev); |
| 2008 | LIST_DELETE(&pkt->list); |
| 2009 | eb64_delete(&pkt->pn_node); |
| 2010 | quic_tx_packet_refdec(pkt); |
| 2011 | } |
| 2012 | |
| 2013 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2014 | |
| 2015 | } |
| 2016 | |
| 2017 | /* Release all the frames attached to <pktns> packet number space */ |
| 2018 | static inline void qc_release_pktns_frms(struct quic_conn *qc, |
| 2019 | struct quic_pktns *pktns) |
| 2020 | { |
| 2021 | struct quic_frame *frm, *frmbak; |
| 2022 | |
| 2023 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2024 | |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 2025 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) |
| 2026 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2027 | |
| 2028 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2029 | } |
| 2030 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2031 | /* Handle <pkts> list of lost packets detected at <now_us> handling their TX |
| 2032 | * frames. Send a packet loss event to the congestion controller if in flight |
| 2033 | * packet have been lost. Also frees the packet in <pkts> list. |
| 2034 | * |
| 2035 | * Returns 1 on success else 0 if loss limit has been exceeded. A |
| 2036 | * CONNECTION_CLOSE was prepared to close the connection ASAP. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2037 | */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2038 | static inline int qc_release_lost_pkts(struct quic_conn *qc, |
| 2039 | struct quic_pktns *pktns, |
| 2040 | struct list *pkts, |
| 2041 | uint64_t now_us) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2042 | { |
| 2043 | struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost; |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2044 | int close = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2045 | |
| 2046 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2047 | |
| 2048 | if (LIST_ISEMPTY(pkts)) |
| 2049 | goto leave; |
| 2050 | |
| 2051 | oldest_lost = newest_lost = NULL; |
| 2052 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 2053 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2054 | |
| 2055 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
| 2056 | qc->path->prep_in_flight -= pkt->in_flight_len; |
| 2057 | qc->path->in_flight -= pkt->in_flight_len; |
| 2058 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
| 2059 | qc->path->ifae_pkts--; |
| 2060 | /* Treat the frames of this lost packet. */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2061 | if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms)) |
| 2062 | close = 1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2063 | LIST_DELETE(&pkt->list); |
| 2064 | if (!oldest_lost) { |
| 2065 | oldest_lost = newest_lost = pkt; |
| 2066 | } |
| 2067 | else { |
| 2068 | if (newest_lost != oldest_lost) |
| 2069 | quic_tx_packet_refdec(newest_lost); |
| 2070 | newest_lost = pkt; |
| 2071 | } |
| 2072 | } |
| 2073 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2074 | if (!close) { |
| 2075 | if (newest_lost) { |
| 2076 | /* Sent a congestion event to the controller */ |
| 2077 | struct quic_cc_event ev = { }; |
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 | ev.type = QUIC_CC_EVT_LOSS; |
| 2080 | ev.loss.time_sent = newest_lost->time_sent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2081 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2082 | quic_cc_event(&qc->path->cc, &ev); |
| 2083 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2084 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2085 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 2086 | * We must check if we are experiencing a persistent congestion. |
| 2087 | * If this is the case, the congestion controller must re-enter |
| 2088 | * slow start state. |
| 2089 | */ |
| 2090 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 2091 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2092 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2093 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 2094 | now_ms, qc->max_ack_delay)) |
| 2095 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 2096 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2097 | } |
| 2098 | |
Amaury Denoyelle | 3a72ba2 | 2022-11-14 11:41:34 +0100 | [diff] [blame] | 2099 | /* <oldest_lost> cannot be NULL at this stage because we have ensured |
| 2100 | * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a |
| 2101 | * possible overflow on a 0 byte region with O2 optimization. |
| 2102 | */ |
| 2103 | ALREADY_CHECKED(oldest_lost); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2104 | quic_tx_packet_refdec(oldest_lost); |
| 2105 | if (newest_lost != oldest_lost) |
| 2106 | quic_tx_packet_refdec(newest_lost); |
| 2107 | |
| 2108 | leave: |
| 2109 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2110 | return !close; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 2114 | * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e. |
| 2115 | * if the largest acked packet was newly acked and if there was at least one newly |
| 2116 | * acked ack-eliciting packet. |
| 2117 | * Return 1, if succeeded, 0 if not. |
| 2118 | */ |
| 2119 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 2120 | struct quic_frame *frm, |
| 2121 | struct quic_enc_level *qel, |
| 2122 | unsigned int *rtt_sample, |
| 2123 | const unsigned char **pos, const unsigned char *end) |
| 2124 | { |
| 2125 | struct quic_ack *ack = &frm->ack; |
| 2126 | uint64_t smallest, largest; |
| 2127 | struct eb_root *pkts; |
| 2128 | struct eb64_node *largest_node; |
| 2129 | unsigned int time_sent, pkt_flags; |
| 2130 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 2131 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 2132 | int ret = 0; |
| 2133 | |
| 2134 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2135 | |
| 2136 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 2137 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
| 2138 | qc, NULL, &ack->largest_ack); |
| 2139 | goto err; |
| 2140 | } |
| 2141 | |
| 2142 | if (ack->first_ack_range > ack->largest_ack) { |
| 2143 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
| 2144 | qc, NULL, &ack->first_ack_range); |
| 2145 | goto err; |
| 2146 | } |
| 2147 | |
| 2148 | largest = ack->largest_ack; |
| 2149 | smallest = largest - ack->first_ack_range; |
| 2150 | pkts = &qel->pktns->tx.pkts; |
| 2151 | pkt_flags = 0; |
| 2152 | largest_node = NULL; |
| 2153 | time_sent = 0; |
| 2154 | |
| 2155 | if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) { |
| 2156 | largest_node = eb64_lookup(pkts, largest); |
| 2157 | if (!largest_node) { |
| 2158 | TRACE_DEVEL("Largest acked packet not found", |
| 2159 | QUIC_EV_CONN_PRSAFRM, qc); |
| 2160 | } |
| 2161 | else { |
| 2162 | time_sent = eb64_entry(largest_node, |
| 2163 | struct quic_tx_packet, pn_node)->time_sent; |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM, |
| 2168 | qc, NULL, &largest, &smallest); |
| 2169 | do { |
| 2170 | uint64_t gap, ack_range; |
| 2171 | |
| 2172 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 2173 | largest_node, largest, smallest); |
| 2174 | if (!ack->ack_range_num--) |
| 2175 | break; |
| 2176 | |
| 2177 | if (!quic_dec_int(&gap, pos, end)) { |
| 2178 | TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc); |
| 2179 | goto err; |
| 2180 | } |
| 2181 | |
| 2182 | if (smallest < gap + 2) { |
| 2183 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
| 2184 | qc, NULL, &gap, &smallest); |
| 2185 | goto err; |
| 2186 | } |
| 2187 | |
| 2188 | largest = smallest - gap - 2; |
| 2189 | if (!quic_dec_int(&ack_range, pos, end)) { |
| 2190 | TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc); |
| 2191 | goto err; |
| 2192 | } |
| 2193 | |
| 2194 | if (largest < ack_range) { |
| 2195 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
| 2196 | qc, NULL, &largest, &ack_range); |
| 2197 | goto err; |
| 2198 | } |
| 2199 | |
| 2200 | /* Do not use this node anymore. */ |
| 2201 | largest_node = NULL; |
| 2202 | /* Next range */ |
| 2203 | smallest = largest - ack_range; |
| 2204 | |
| 2205 | TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM, |
| 2206 | qc, NULL, &largest, &smallest); |
| 2207 | } while (1); |
| 2208 | |
| 2209 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2210 | *rtt_sample = tick_remain(time_sent, now_ms); |
| 2211 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
| 2212 | } |
| 2213 | |
| 2214 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 2215 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
| 2216 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2217 | if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms)) |
| 2218 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2219 | } |
| 2220 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 2221 | if (quic_peer_validated_addr(qc)) |
| 2222 | qc->path->loss.pto_count = 0; |
| 2223 | qc_set_timer(qc); |
| 2224 | } |
| 2225 | |
| 2226 | ret = 1; |
| 2227 | leave: |
| 2228 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2229 | return ret; |
| 2230 | |
| 2231 | err: |
| 2232 | free_quic_tx_pkts(qc, &newly_acked_pkts); |
| 2233 | goto leave; |
| 2234 | } |
| 2235 | |
| 2236 | /* This function gives the detail of the SSL error. It is used only |
| 2237 | * if the debug mode and the verbose mode are activated. It dump all |
| 2238 | * the SSL error until the stack was empty. |
| 2239 | */ |
| 2240 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2241 | { |
| 2242 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2243 | while (1) { |
| 2244 | const char *func = NULL; |
| 2245 | unsigned long ret; |
| 2246 | |
| 2247 | ERR_peek_error_func(&func); |
| 2248 | ret = ERR_get_error(); |
| 2249 | if (!ret) |
| 2250 | return; |
| 2251 | |
| 2252 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 2253 | func, ERR_reason_error_string(ret)); |
| 2254 | } |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2259 | const char **str, int *len); |
| 2260 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2261 | /* Finalize <qc> QUIC connection: |
| 2262 | * - initialize the Initial QUIC TLS context for negotiated version, |
| 2263 | * - derive the secrets for this context, |
| 2264 | * - set them into the TLS stack, |
| 2265 | * |
| 2266 | * MUST be called after having received the remote transport parameters which |
| 2267 | * are parsed when the TLS callback for the ClientHello message is called upon |
| 2268 | * SSL_do_handshake() calls, not necessarily at the first time as this TLS |
| 2269 | * message may be splitted between packets |
| 2270 | * Return 1 if succeeded, 0 if not. |
| 2271 | */ |
| 2272 | static int qc_conn_finalize(struct quic_conn *qc, int server) |
| 2273 | { |
| 2274 | int ret = 0; |
| 2275 | |
| 2276 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 2277 | |
| 2278 | if (qc->flags & QUIC_FL_CONN_FINALIZED) |
| 2279 | goto finalized; |
| 2280 | |
| 2281 | if (qc->negotiated_version && |
| 2282 | !qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version, |
| 2283 | qc->odcid.data, qc->odcid.len, server)) |
| 2284 | goto out; |
| 2285 | |
| 2286 | /* This connection is functional (ready to send/receive) */ |
| 2287 | qc->flags |= QUIC_FL_CONN_FINALIZED; |
| 2288 | |
| 2289 | finalized: |
| 2290 | ret = 1; |
| 2291 | out: |
| 2292 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 2293 | return ret; |
| 2294 | } |
| 2295 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2296 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2297 | * from <qel> encryption level with <ctx> as QUIC connection context. |
| 2298 | * Remaining parameter are there for debugging purposes. |
| 2299 | * Return 1 if succeeded, 0 if not. |
| 2300 | */ |
| 2301 | static inline int qc_provide_cdata(struct quic_enc_level *el, |
| 2302 | struct ssl_sock_ctx *ctx, |
| 2303 | const unsigned char *data, size_t len, |
| 2304 | struct quic_rx_packet *pkt, |
| 2305 | struct quic_rx_crypto_frm *cf) |
| 2306 | { |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2307 | #ifdef DEBUG_STRICT |
| 2308 | enum ncb_ret ncb_ret; |
| 2309 | #endif |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2310 | int ssl_err, state; |
| 2311 | struct quic_conn *qc; |
| 2312 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2313 | struct ncbuf *ncbuf = &el->cstream->rx.ncbuf; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2314 | |
| 2315 | ssl_err = SSL_ERROR_NONE; |
| 2316 | qc = ctx->qc; |
| 2317 | |
| 2318 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2319 | |
| 2320 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2321 | TRACE_ERROR("SSL_provide_quic_data() error", |
| 2322 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
| 2323 | goto leave; |
| 2324 | } |
| 2325 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2326 | TRACE_PROTO("in order CRYPTO data", |
| 2327 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
| 2328 | |
| 2329 | state = qc->state; |
| 2330 | if (state < QUIC_HS_ST_COMPLETE) { |
| 2331 | ssl_err = SSL_do_handshake(ctx->ssl); |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2332 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 2333 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 2334 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc); |
| 2335 | goto leave; |
| 2336 | } |
| 2337 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2338 | /* Finalize the connection as soon as possible if the peer transport parameters |
| 2339 | * have been received. This may be useful to send packets even if this |
| 2340 | * handshake fails. |
| 2341 | */ |
| 2342 | if ((qc->flags & QUIC_FL_CONN_TX_TP_RECEIVED) && !qc_conn_finalize(qc, 1)) { |
| 2343 | TRACE_ERROR("connection finalization failed", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2344 | goto leave; |
| 2345 | } |
| 2346 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2347 | if (ssl_err != 1) { |
| 2348 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2349 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2350 | TRACE_PROTO("SSL handshake in progress", |
| 2351 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2352 | goto out; |
| 2353 | } |
| 2354 | |
| 2355 | /* TODO: Should close the connection asap */ |
| 2356 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2357 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2358 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2359 | HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail); |
| 2360 | } |
| 2361 | TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2362 | qc_ssl_dump_errors(ctx->conn); |
| 2363 | ERR_clear_error(); |
| 2364 | goto leave; |
| 2365 | } |
| 2366 | |
| 2367 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2368 | |
| 2369 | /* Check the alpn could be negotiated */ |
| 2370 | if (!qc->app_ops) { |
| 2371 | TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2372 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
| 2373 | goto leave; |
| 2374 | } |
| 2375 | |
| 2376 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2377 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2378 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2379 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2380 | } |
| 2381 | /* I/O callback switch */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2382 | qc->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2383 | if (qc_is_listener(ctx->qc)) { |
| 2384 | qc->state = QUIC_HS_ST_CONFIRMED; |
| 2385 | /* The connection is ready to be accepted. */ |
| 2386 | quic_accept_push_qc(qc); |
| 2387 | } |
| 2388 | else { |
| 2389 | qc->state = QUIC_HS_ST_COMPLETE; |
| 2390 | } |
| 2391 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 2392 | /* Prepare the next key update */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2393 | if (!quic_tls_key_update(qc)) { |
| 2394 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc); |
| 2395 | goto leave; |
| 2396 | } |
| 2397 | } else { |
| 2398 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2399 | if (ssl_err != 1) { |
| 2400 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2401 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2402 | TRACE_PROTO("SSL post handshake in progress", |
| 2403 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2404 | goto out; |
| 2405 | } |
| 2406 | |
| 2407 | TRACE_ERROR("SSL post handshake error", |
| 2408 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2409 | goto leave; |
| 2410 | } |
| 2411 | |
| 2412 | TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2413 | } |
| 2414 | |
| 2415 | out: |
| 2416 | ret = 1; |
| 2417 | leave: |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2418 | /* The CRYPTO data are consumed even in case of an error to release |
| 2419 | * the memory asap. |
| 2420 | */ |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2421 | if (!ncb_is_null(ncbuf)) { |
| 2422 | #ifdef DEBUG_STRICT |
| 2423 | ncb_ret = ncb_advance(ncbuf, len); |
| 2424 | /* ncb_advance() must always succeed. This is guaranteed as |
| 2425 | * this is only done inside a data block. If false, this will |
| 2426 | * lead to handshake failure with quic_enc_level offset shifted |
| 2427 | * from buffer data. |
| 2428 | */ |
| 2429 | BUG_ON(ncb_ret != NCB_RET_OK); |
| 2430 | #else |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2431 | ncb_advance(ncbuf, len); |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2432 | #endif |
| 2433 | } |
| 2434 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2435 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
| 2436 | return ret; |
| 2437 | } |
| 2438 | |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2439 | /* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc> |
| 2440 | * connection. <fin> is true if FIN bit is set on frame type. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2441 | * |
| 2442 | * Return 1 on success. On error, 0 is returned. In this case, the packet |
| 2443 | * containing the frame must not be acknowledged. |
| 2444 | */ |
| 2445 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2446 | struct quic_stream *strm_frm, |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2447 | struct quic_conn *qc, char fin) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2448 | { |
| 2449 | int ret; |
| 2450 | |
| 2451 | /* RFC9000 13.1. Packet Processing |
| 2452 | * |
| 2453 | * A packet MUST NOT be acknowledged until packet protection has been |
| 2454 | * successfully removed and all frames contained in the packet have |
| 2455 | * been processed. For STREAM frames, this means the data has been |
| 2456 | * enqueued in preparation to be received by the application protocol, |
| 2457 | * but it does not require that data be delivered and consumed. |
| 2458 | */ |
| 2459 | TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc); |
| 2460 | |
| 2461 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2462 | strm_frm->offset.key, fin, (char *)strm_frm->data); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2463 | |
| 2464 | /* frame rejected - packet must not be acknowledeged */ |
| 2465 | TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc); |
| 2466 | return !ret; |
| 2467 | } |
| 2468 | |
| 2469 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2470 | * for <qc> QUIC connection. |
| 2471 | * This is a best effort function which never fails even if no memory could be |
| 2472 | * allocated to duplicate these frames. |
| 2473 | */ |
| 2474 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2475 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2476 | { |
| 2477 | struct quic_frame *frm, *frmbak; |
| 2478 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2479 | |
| 2480 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2481 | |
| 2482 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2483 | struct quic_frame *dup_frm, *origin; |
| 2484 | |
| 2485 | switch (frm->type) { |
| 2486 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2487 | { |
| 2488 | struct quic_stream *strm_frm = &frm->stream; |
| 2489 | struct eb64_node *node = NULL; |
| 2490 | struct qc_stream_desc *stream_desc; |
| 2491 | |
| 2492 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2493 | if (!node) { |
| 2494 | TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2495 | continue; |
| 2496 | } |
| 2497 | |
| 2498 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2499 | /* Do not resend this frame if in the "already acked range" */ |
| 2500 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 2501 | TRACE_DEVEL("ignored frame in already acked range", |
| 2502 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2503 | continue; |
| 2504 | } |
| 2505 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 2506 | strm_frm->offset.key = stream_desc->ack_offset; |
| 2507 | TRACE_DEVEL("updated partially acked frame", |
| 2508 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2509 | } |
| 2510 | break; |
| 2511 | } |
| 2512 | |
| 2513 | default: |
| 2514 | break; |
| 2515 | } |
| 2516 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2517 | /* If <frm> is already a copy of another frame, we must take |
| 2518 | * its original frame as source for the copy. |
| 2519 | */ |
| 2520 | origin = frm->origin ? frm->origin : frm; |
| 2521 | dup_frm = qc_frm_dup(origin); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2522 | if (!dup_frm) { |
| 2523 | TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2524 | break; |
| 2525 | } |
| 2526 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2527 | TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2528 | if (origin->pkt) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2529 | TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM, |
| 2530 | qc, NULL, &origin->pkt->pn_node.key); |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2531 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2532 | else { |
| 2533 | /* <origin> is a frame which was sent from a packet detected as lost. */ |
| 2534 | TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc); |
| 2535 | } |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2536 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2537 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2538 | } |
| 2539 | |
| 2540 | LIST_SPLICE(out_frm_list, &tmp); |
| 2541 | |
| 2542 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2543 | } |
| 2544 | |
| 2545 | /* Prepare a fast retransmission from <qel> encryption level */ |
| 2546 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2547 | struct quic_enc_level *qel, |
| 2548 | struct list *frms1, struct list *frms2) |
| 2549 | { |
| 2550 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
| 2551 | struct list *frms = frms1; |
| 2552 | struct eb64_node *node; |
| 2553 | struct quic_tx_packet *pkt; |
| 2554 | |
| 2555 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2556 | |
| 2557 | BUG_ON(frms1 == frms2); |
| 2558 | |
| 2559 | pkt = NULL; |
| 2560 | node = eb64_first(pkts); |
| 2561 | start: |
| 2562 | while (node) { |
| 2563 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 2564 | node = eb64_next(node); |
| 2565 | /* Skip the empty and coalesced packets */ |
Frédéric Lécaille | 6dead91 | 2023-01-30 17:27:32 +0100 | [diff] [blame] | 2566 | TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0, |
| 2567 | "--> pn=%llu (%d %d)", (ull)pkt->pn_node.key, |
| 2568 | 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] | 2569 | if (!LIST_ISEMPTY(&pkt->frms)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2570 | break; |
| 2571 | } |
| 2572 | |
| 2573 | if (!pkt) |
| 2574 | goto leave; |
| 2575 | |
| 2576 | /* When building a packet from another one, the field which may increase the |
| 2577 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2578 | */ |
| 2579 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2580 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2581 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 2582 | goto leave; |
| 2583 | } |
| 2584 | |
| 2585 | TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 2586 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2587 | if (frms == frms1 && frms2) { |
| 2588 | frms = frms2; |
| 2589 | goto start; |
| 2590 | } |
| 2591 | leave: |
| 2592 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 2593 | } |
| 2594 | |
| 2595 | /* Prepare a fast retransmission during a handshake after a client |
| 2596 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2597 | * Initial packets send them coalescing with others (Handshake here). |
| 2598 | * (Listener only function). |
| 2599 | */ |
| 2600 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2601 | struct list *ifrms, struct list *hfrms) |
| 2602 | { |
| 2603 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2604 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2605 | |
| 2606 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2607 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2608 | struct quic_enc_level *qel = iqel; |
| 2609 | struct eb_root *pkts; |
| 2610 | struct eb64_node *node; |
| 2611 | struct quic_tx_packet *pkt; |
| 2612 | struct list *tmp = &itmp; |
| 2613 | |
| 2614 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2615 | start: |
| 2616 | pkt = NULL; |
| 2617 | pkts = &qel->pktns->tx.pkts; |
| 2618 | node = eb64_first(pkts); |
| 2619 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2620 | while (node) { |
| 2621 | 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] | 2622 | TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0, |
| 2623 | "--> pn=%llu (%d %d)", (ull)pkt->pn_node.key, |
| 2624 | LIST_ISEMPTY(&pkt->frms), !!(pkt->flags & QUIC_FL_TX_PACKET_COALESCED)); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2625 | if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED)) |
| 2626 | break; |
| 2627 | node = eb64_next(node); |
| 2628 | } |
| 2629 | |
| 2630 | if (!pkt) |
| 2631 | goto end; |
| 2632 | |
| 2633 | /* When building a packet from another one, the field which may increase the |
| 2634 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2635 | */ |
| 2636 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2637 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2638 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2639 | goto end; |
| 2640 | } |
| 2641 | |
| 2642 | qel->pktns->tx.pto_probe += 1; |
| 2643 | |
| 2644 | /* No risk to loop here, #packet per datagram is bounded */ |
| 2645 | requeue: |
| 2646 | TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2647 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
| 2648 | if (qel == iqel) { |
| 2649 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2650 | pkt = pkt->next; |
| 2651 | tmp = &htmp; |
| 2652 | hqel->pktns->tx.pto_probe += 1; |
| 2653 | TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc); |
| 2654 | goto requeue; |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | end: |
| 2659 | LIST_SPLICE(ifrms, &itmp); |
| 2660 | LIST_SPLICE(hfrms, &htmp); |
| 2661 | |
| 2662 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2663 | } |
| 2664 | |
| 2665 | static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm) |
| 2666 | { |
| 2667 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 2668 | |
| 2669 | if (frm->type == QUIC_FT_CONNECTION_CLOSE) |
| 2670 | quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code); |
| 2671 | else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) { |
| 2672 | if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt) |
| 2673 | goto out; |
| 2674 | |
| 2675 | qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code); |
| 2676 | } |
| 2677 | |
| 2678 | out: |
| 2679 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 2680 | } |
| 2681 | |
| 2682 | /* Enqueue a STOP_SENDING frame to send into 1RTT packet number space |
| 2683 | * frame list to send. |
| 2684 | * Return 1 if succeeded, 0 if not. |
| 2685 | */ |
| 2686 | static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id) |
| 2687 | { |
| 2688 | int ret = 0; |
| 2689 | struct quic_frame *frm; |
| 2690 | struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2691 | uint64_t app_error_code; |
| 2692 | |
| 2693 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2694 | |
| 2695 | /* TODO: the mux may be released, we cannot have more |
| 2696 | * information about the application error code to send |
| 2697 | * at this time. |
| 2698 | */ |
| 2699 | app_error_code = H3_REQUEST_REJECTED; |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2700 | frm = qc_frm_alloc(QUIC_FT_STOP_SENDING); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2701 | if (!frm) { |
| 2702 | TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2703 | goto out; |
| 2704 | } |
| 2705 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2706 | frm->stop_sending.id = id; |
| 2707 | frm->stop_sending.app_error_code = app_error_code; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2708 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
| 2709 | ret = 1; |
| 2710 | out: |
| 2711 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2712 | return ret; |
| 2713 | } |
| 2714 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2715 | /* Release the underlying memory use by <ncbuf> non-contiguous buffer */ |
| 2716 | static void quic_free_ncbuf(struct ncbuf *ncbuf) |
| 2717 | { |
| 2718 | struct buffer buf; |
| 2719 | |
| 2720 | if (ncb_is_null(ncbuf)) |
| 2721 | return; |
| 2722 | |
| 2723 | buf = b_make(ncbuf->area, ncbuf->size, 0, 0); |
| 2724 | b_free(&buf); |
| 2725 | offer_buffers(NULL, 1); |
| 2726 | |
| 2727 | *ncbuf = NCBUF_NULL; |
| 2728 | } |
| 2729 | |
| 2730 | /* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */ |
| 2731 | static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf) |
| 2732 | { |
| 2733 | struct buffer buf = BUF_NULL; |
| 2734 | |
| 2735 | if (!ncb_is_null(ncbuf)) |
| 2736 | return ncbuf; |
| 2737 | |
| 2738 | b_alloc(&buf); |
| 2739 | BUG_ON(b_is_null(&buf)); |
| 2740 | |
| 2741 | *ncbuf = ncb_make(buf.area, buf.size, 0); |
| 2742 | ncb_init(ncbuf, 0); |
| 2743 | |
| 2744 | return ncbuf; |
| 2745 | } |
| 2746 | |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2747 | /* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn. |
| 2748 | * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the |
| 2749 | * speed up handshake completion may be run after having received duplicated |
| 2750 | * CRYPTO data. |
| 2751 | */ |
| 2752 | static int qc_handle_crypto_frm(struct quic_conn *qc, |
| 2753 | struct quic_crypto *frm, struct quic_rx_packet *pkt, |
| 2754 | struct quic_enc_level *qel, int *fast_retrans) |
| 2755 | { |
| 2756 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2757 | enum ncb_ret ncb_ret; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2758 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2759 | struct quic_rx_crypto_frm cfdebug = { |
| 2760 | .offset_node.key = frm->offset, |
| 2761 | .len = frm->len, |
| 2762 | }; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2763 | struct quic_cstream *cstream = qel->cstream; |
| 2764 | struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2765 | |
| 2766 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2767 | if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) { |
| 2768 | TRACE_PROTO("CRYPTO data discarded", |
| 2769 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2770 | goto done; |
| 2771 | } |
| 2772 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2773 | if (unlikely(frm->offset < cstream->rx.offset)) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2774 | size_t diff; |
| 2775 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2776 | if (frm->offset + frm->len <= cstream->rx.offset) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2777 | /* Nothing to do */ |
| 2778 | TRACE_PROTO("Already received CRYPTO data", |
| 2779 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2780 | if (qc_is_listener(qc) && qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] && |
| 2781 | !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP)) |
| 2782 | *fast_retrans = 1; |
| 2783 | goto done; |
| 2784 | } |
| 2785 | |
| 2786 | TRACE_PROTO("Partially already received CRYPTO data", |
| 2787 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2788 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2789 | diff = cstream->rx.offset - frm->offset; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2790 | frm->len -= diff; |
| 2791 | frm->data += diff; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2792 | frm->offset = cstream->rx.offset; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2793 | } |
| 2794 | |
Amaury Denoyelle | ff95f2d | 2022-11-18 14:50:06 +0100 | [diff] [blame] | 2795 | 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] | 2796 | if (!qc_provide_cdata(qel, qc->xprt_ctx, frm->data, frm->len, |
| 2797 | pkt, &cfdebug)) { |
| 2798 | // trace already emitted by function above |
| 2799 | goto leave; |
| 2800 | } |
| 2801 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2802 | cstream->rx.offset += frm->len; |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2803 | 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] | 2804 | goto done; |
| 2805 | } |
| 2806 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2807 | if (!quic_get_ncbuf(ncbuf) || |
| 2808 | ncb_is_null(ncbuf)) { |
| 2809 | 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] | 2810 | goto leave; |
| 2811 | } |
| 2812 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2813 | /* frm->offset > cstream-trx.offset */ |
| 2814 | ncb_ret = ncb_add(ncbuf, frm->offset - cstream->rx.offset, |
| 2815 | (const char *)frm->data, frm->len, NCB_ADD_COMPARE); |
| 2816 | if (ncb_ret != NCB_RET_OK) { |
| 2817 | if (ncb_ret == NCB_RET_DATA_REJ) { |
| 2818 | TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc); |
| 2819 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION)); |
| 2820 | } |
| 2821 | else if (ncb_ret == NCB_RET_GAP_SIZE) { |
| 2822 | TRACE_ERROR("cannot bufferize frame due to gap size limit", |
| 2823 | QUIC_EV_CONN_PRSHPKT, qc); |
| 2824 | } |
| 2825 | goto leave; |
| 2826 | } |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2827 | |
| 2828 | done: |
| 2829 | ret = 1; |
| 2830 | leave: |
| 2831 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2832 | return ret; |
| 2833 | } |
| 2834 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2835 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel> |
| 2836 | * as encryption level. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2837 | * Returns 1 if succeeded, 0 if failed. |
| 2838 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2839 | 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] | 2840 | struct quic_enc_level *qel) |
| 2841 | { |
| 2842 | struct quic_frame frm; |
| 2843 | const unsigned char *pos, *end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2844 | int fast_retrans = 0, ret = 0; |
| 2845 | |
| 2846 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2847 | /* Skip the AAD */ |
| 2848 | pos = pkt->data + pkt->aad_len; |
| 2849 | end = pkt->data + pkt->len; |
| 2850 | |
| 2851 | while (pos < end) { |
| 2852 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) { |
| 2853 | // trace already emitted by function above |
| 2854 | goto leave; |
| 2855 | } |
| 2856 | |
| 2857 | TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm); |
| 2858 | switch (frm.type) { |
| 2859 | case QUIC_FT_PADDING: |
| 2860 | break; |
| 2861 | case QUIC_FT_PING: |
| 2862 | break; |
| 2863 | case QUIC_FT_ACK: |
| 2864 | { |
| 2865 | unsigned int rtt_sample; |
| 2866 | |
| 2867 | rtt_sample = 0; |
| 2868 | if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) { |
| 2869 | // trace already emitted by function above |
| 2870 | goto leave; |
| 2871 | } |
| 2872 | |
| 2873 | if (rtt_sample) { |
| 2874 | unsigned int ack_delay; |
| 2875 | |
| 2876 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
| 2877 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
| 2878 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2879 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
| 2880 | quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc); |
| 2881 | } |
| 2882 | break; |
| 2883 | } |
| 2884 | case QUIC_FT_RESET_STREAM: |
Amaury Denoyelle | 5854fc0 | 2022-12-09 16:25:48 +0100 | [diff] [blame] | 2885 | if (qc->mux_state == QC_MUX_READY) { |
| 2886 | struct quic_reset_stream *rs = &frm.reset_stream; |
| 2887 | qcc_recv_reset_stream(qc->qcc, rs->id, rs->app_error_code, rs->final_size); |
| 2888 | } |
| 2889 | break; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2890 | case QUIC_FT_STOP_SENDING: |
| 2891 | { |
| 2892 | struct quic_stop_sending *stop_sending = &frm.stop_sending; |
| 2893 | if (qc->mux_state == QC_MUX_READY) { |
| 2894 | if (qcc_recv_stop_sending(qc->qcc, stop_sending->id, |
| 2895 | stop_sending->app_error_code)) { |
| 2896 | TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2897 | goto leave; |
| 2898 | } |
| 2899 | } |
| 2900 | break; |
| 2901 | } |
| 2902 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2903 | if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2904 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2905 | break; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2906 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2907 | { |
| 2908 | struct quic_stream *stream = &frm.stream; |
| 2909 | 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] | 2910 | const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2911 | |
| 2912 | /* The upper layer may not be allocated. */ |
| 2913 | if (qc->mux_state != QC_MUX_READY) { |
| 2914 | if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) { |
| 2915 | TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc); |
| 2916 | break; |
| 2917 | } |
| 2918 | else { |
| 2919 | 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] | 2920 | if (qc->app_ops == &h3_ops && quic_stream_is_uni(stream->id)) { |
| 2921 | /* Do not send STOP_SENDING frames for h3 unidirectional streams. |
| 2922 | * TODO: this test should be removed when the connection closure |
| 2923 | * will be more clean. |
| 2924 | * At quic_conn level there is no mean to know that an application |
| 2925 | * want to forbid stream closure requests to receivers. This is the |
| 2926 | * case for the Control and QPACK h3 unidirectional streams. |
| 2927 | */ |
| 2928 | goto leave; |
| 2929 | } |
| 2930 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2931 | if (!qc_stop_sending_frm_enqueue(qc, stream->id)) |
| 2932 | TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2933 | /* This packet will not be acknowledged */ |
| 2934 | goto leave; |
| 2935 | } |
| 2936 | } |
| 2937 | |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2938 | if (!qc_handle_strm_frm(pkt, stream, qc, fin)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2939 | TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2940 | goto leave; |
| 2941 | } |
| 2942 | |
| 2943 | break; |
| 2944 | } |
| 2945 | case QUIC_FT_MAX_DATA: |
| 2946 | if (qc->mux_state == QC_MUX_READY) { |
| 2947 | struct quic_max_data *data = &frm.max_data; |
| 2948 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2949 | } |
| 2950 | break; |
| 2951 | case QUIC_FT_MAX_STREAM_DATA: |
| 2952 | if (qc->mux_state == QC_MUX_READY) { |
| 2953 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 2954 | if (qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2955 | data->max_stream_data)) { |
| 2956 | TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2957 | goto leave; |
| 2958 | } |
| 2959 | } |
| 2960 | break; |
| 2961 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2962 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2963 | break; |
| 2964 | case QUIC_FT_DATA_BLOCKED: |
| 2965 | HA_ATOMIC_INC(&qc->prx_counters->data_blocked); |
| 2966 | break; |
| 2967 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2968 | HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked); |
| 2969 | break; |
| 2970 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2971 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi); |
| 2972 | break; |
| 2973 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2974 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni); |
| 2975 | break; |
| 2976 | case QUIC_FT_NEW_CONNECTION_ID: |
| 2977 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2978 | /* XXX TO DO XXX */ |
| 2979 | break; |
| 2980 | case QUIC_FT_CONNECTION_CLOSE: |
| 2981 | case QUIC_FT_CONNECTION_CLOSE_APP: |
| 2982 | /* Increment the error counters */ |
| 2983 | qc_cc_err_count_inc(qc, &frm); |
| 2984 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
| 2985 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2986 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2987 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2988 | } |
| 2989 | TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 2990 | /* RFC 9000 10.2. Immediate Close: |
| 2991 | * The closing and draining connection states exist to ensure |
| 2992 | * that connections close cleanly and that delayed or reordered |
| 2993 | * packets are properly discarded. These states SHOULD persist |
| 2994 | * for at least three times the current PTO interval... |
| 2995 | * |
| 2996 | * Rearm the idle timeout only one time when entering draining |
| 2997 | * state. |
| 2998 | */ |
| 2999 | qc_idle_timer_do_rearm(qc); |
| 3000 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3001 | qc_notify_close(qc); |
| 3002 | } |
| 3003 | break; |
| 3004 | case QUIC_FT_HANDSHAKE_DONE: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3005 | if (qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3006 | TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame", |
| 3007 | QUIC_EV_CONN_PRSHPKT, qc); |
| 3008 | goto leave; |
| 3009 | } |
| 3010 | |
| 3011 | qc->state = QUIC_HS_ST_CONFIRMED; |
| 3012 | break; |
| 3013 | default: |
| 3014 | TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc); |
| 3015 | goto leave; |
| 3016 | } |
| 3017 | } |
| 3018 | |
| 3019 | /* Flag this packet number space as having received a packet. */ |
| 3020 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
| 3021 | |
| 3022 | if (fast_retrans) { |
| 3023 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3024 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3025 | |
| 3026 | TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc); |
| 3027 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 3028 | qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP; |
| 3029 | } |
| 3030 | |
| 3031 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 3032 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 3033 | * be discarded. |
| 3034 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3035 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3036 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
| 3037 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 3038 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3039 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3040 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 3041 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3042 | qc_set_timer(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3043 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3044 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 3045 | } |
| 3046 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 3047 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3048 | } |
| 3049 | } |
| 3050 | |
| 3051 | ret = 1; |
| 3052 | leave: |
| 3053 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 3054 | return ret; |
| 3055 | } |
| 3056 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3057 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3058 | /* Allocate Tx buffer from <qc> quic-conn if needed. |
| 3059 | * |
| 3060 | * Returns allocated buffer or NULL on error. |
| 3061 | */ |
| 3062 | static struct buffer *qc_txb_alloc(struct quic_conn *qc) |
| 3063 | { |
| 3064 | struct buffer *buf = &qc->tx.buf; |
| 3065 | if (!b_alloc(buf)) |
| 3066 | return NULL; |
| 3067 | |
| 3068 | return buf; |
| 3069 | } |
| 3070 | |
| 3071 | /* Free Tx buffer from <qc> if it is empty. */ |
| 3072 | static void qc_txb_release(struct quic_conn *qc) |
| 3073 | { |
| 3074 | struct buffer *buf = &qc->tx.buf; |
| 3075 | |
| 3076 | /* For the moment sending function is responsible to purge the buffer |
| 3077 | * entirely. It may change in the future but this requires to be able |
| 3078 | * to reuse old data. |
| 3079 | */ |
| 3080 | BUG_ON_HOT(buf && b_data(buf)); |
| 3081 | |
| 3082 | if (!b_data(buf)) { |
| 3083 | b_free(buf); |
| 3084 | offer_buffers(NULL, 1); |
| 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | /* Commit a datagram payload written into <buf> of length <length>. <first_pkt> |
| 3089 | * must contains the address of the first packet stored in the payload. |
| 3090 | * |
| 3091 | * Caller is responsible that there is enough space in the buffer. |
| 3092 | */ |
| 3093 | static void qc_txb_store(struct buffer *buf, uint16_t length, |
| 3094 | struct quic_tx_packet *first_pkt) |
| 3095 | { |
| 3096 | const size_t hdlen = sizeof(uint16_t) + sizeof(void *); |
| 3097 | BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */ |
| 3098 | |
| 3099 | write_u16(b_tail(buf), length); |
| 3100 | write_ptr(b_tail(buf) + sizeof(length), first_pkt); |
| 3101 | b_add(buf, hdlen + length); |
| 3102 | } |
| 3103 | |
| 3104 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 3105 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 3106 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 3107 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 3108 | * <force_ack> may be set to 1 if you want to force an ack. |
| 3109 | */ |
| 3110 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 3111 | struct quic_enc_level *qel, int cc, int probe, int force_ack) |
| 3112 | { |
| 3113 | unsigned int must_ack = force_ack || |
| 3114 | (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)); |
| 3115 | |
| 3116 | /* Do not build any more packet if the TX secrets are not available or |
| 3117 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 3118 | * and if there is no more packets to send upon PTO expiration |
| 3119 | * and if there is no more ack-eliciting frames to send or in flight |
| 3120 | * congestion control limit is reached for prepared data |
| 3121 | */ |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 3122 | if (!quic_tls_has_tx_sec(qel) || |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3123 | (!cc && !probe && !must_ack && |
| 3124 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 3125 | return 0; |
| 3126 | } |
| 3127 | |
| 3128 | return 1; |
| 3129 | } |
| 3130 | |
| 3131 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3132 | * <frms>. Each packet is stored in a distinct datagram written to <buf>. |
| 3133 | * |
| 3134 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3135 | * the address of the packet contained in the datagram. |
| 3136 | * |
| 3137 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3138 | * -1 if something wrong happened. |
| 3139 | */ |
| 3140 | static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3141 | struct list *frms) |
| 3142 | { |
| 3143 | int ret = -1; |
| 3144 | struct quic_enc_level *qel; |
| 3145 | unsigned char *end, *pos; |
| 3146 | struct quic_tx_packet *pkt; |
| 3147 | size_t total; |
| 3148 | /* Each datagram is prepended with its length followed by the address |
| 3149 | * of the first packet in the datagram. |
| 3150 | */ |
| 3151 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt); |
| 3152 | |
| 3153 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3154 | |
| 3155 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3156 | total = 0; |
| 3157 | pos = (unsigned char *)b_tail(buf); |
| 3158 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) { |
| 3159 | int err, probe, cc; |
| 3160 | |
| 3161 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
| 3162 | probe = 0; |
| 3163 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3164 | /* We do not probe if an immediate close was asked */ |
| 3165 | if (!cc) |
| 3166 | probe = qel->pktns->tx.pto_probe; |
| 3167 | |
| 3168 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0)) |
| 3169 | break; |
| 3170 | |
| 3171 | /* Leave room for the datagram header */ |
| 3172 | pos += dg_headlen; |
| 3173 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3174 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3175 | } |
| 3176 | else { |
| 3177 | end = pos + qc->path->mtu; |
| 3178 | } |
| 3179 | |
| 3180 | pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0, |
| 3181 | QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err); |
| 3182 | switch (err) { |
| 3183 | case -2: |
| 3184 | // trace already emitted by function above |
| 3185 | goto leave; |
| 3186 | case -1: |
| 3187 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 3188 | * MTU, we are here because of the congestion control window. There is |
| 3189 | * no need to try to reuse this buffer. |
| 3190 | */ |
| 3191 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
| 3192 | goto out; |
| 3193 | default: |
| 3194 | break; |
| 3195 | } |
| 3196 | |
| 3197 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 3198 | BUG_ON(!pkt); |
| 3199 | |
| 3200 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3201 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3202 | |
| 3203 | total += pkt->len; |
| 3204 | |
| 3205 | /* Write datagram header. */ |
| 3206 | qc_txb_store(buf, pkt->len, pkt); |
| 3207 | } |
| 3208 | |
| 3209 | out: |
| 3210 | ret = total; |
| 3211 | leave: |
| 3212 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3213 | return ret; |
| 3214 | } |
| 3215 | |
| 3216 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3217 | * <frms>. Several packets can be regrouped in a single datagram. The result is |
| 3218 | * written into <buf>. |
| 3219 | * |
| 3220 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3221 | * the address of first packet in the datagram. |
| 3222 | * |
| 3223 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3224 | * -1 if something wrong happened. |
| 3225 | */ |
| 3226 | static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3227 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3228 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3229 | { |
| 3230 | struct quic_enc_level *qel; |
| 3231 | unsigned char *end, *pos; |
| 3232 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 3233 | /* length of datagrams */ |
| 3234 | uint16_t dglen; |
| 3235 | size_t total; |
| 3236 | int ret = -1, padding; |
| 3237 | /* Each datagram is prepended with its length followed by the address |
| 3238 | * of the first packet in the datagram. |
| 3239 | */ |
| 3240 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt); |
| 3241 | struct list *frms; |
| 3242 | |
| 3243 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3244 | |
| 3245 | /* Currently qc_prep_pkts() does not handle buffer wrapping so the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 3246 | * caller must ensure that buf is reset. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3247 | */ |
| 3248 | BUG_ON_HOT(buf->head || buf->data); |
| 3249 | |
| 3250 | total = 0; |
| 3251 | qel = &qc->els[tel]; |
| 3252 | frms = tel_frms; |
| 3253 | dglen = 0; |
| 3254 | padding = 0; |
| 3255 | pos = (unsigned char *)b_head(buf); |
| 3256 | first_pkt = prv_pkt = NULL; |
| 3257 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
| 3258 | int err, probe, cc; |
| 3259 | enum quic_pkt_type pkt_type; |
| 3260 | struct quic_tls_ctx *tls_ctx; |
| 3261 | const struct quic_version *ver; |
| 3262 | int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 3263 | (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3264 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3265 | |
| 3266 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
| 3267 | probe = 0; |
| 3268 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3269 | /* We do not probe if an immediate close was asked */ |
| 3270 | if (!cc) |
| 3271 | probe = qel->pktns->tx.pto_probe; |
| 3272 | |
| 3273 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) { |
| 3274 | if (prv_pkt) |
| 3275 | qc_txb_store(buf, dglen, first_pkt); |
| 3276 | /* Let's select the next encryption level */ |
| 3277 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 3278 | tel = next_tel; |
| 3279 | frms = next_tel_frms; |
| 3280 | qel = &qc->els[tel]; |
| 3281 | /* Build a new datagram */ |
| 3282 | prv_pkt = NULL; |
| 3283 | TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc); |
| 3284 | continue; |
| 3285 | } |
| 3286 | break; |
| 3287 | } |
| 3288 | |
| 3289 | pkt_type = quic_tls_level_pkt_type(tel); |
| 3290 | if (!prv_pkt) { |
| 3291 | /* Leave room for the datagram header */ |
| 3292 | pos += dg_headlen; |
| 3293 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3294 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3295 | } |
| 3296 | else { |
| 3297 | end = pos + qc->path->mtu; |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | if (qc->negotiated_version) { |
| 3302 | ver = qc->negotiated_version; |
| 3303 | if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 3304 | tls_ctx = &qc->negotiated_ictx; |
| 3305 | else |
| 3306 | tls_ctx = &qel->tls_ctx; |
| 3307 | } |
| 3308 | else { |
| 3309 | ver = qc->original_version; |
| 3310 | tls_ctx = &qel->tls_ctx; |
| 3311 | } |
| 3312 | |
| 3313 | cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms, |
| 3314 | qc, ver, dglen, pkt_type, |
| 3315 | force_ack, padding, probe, cc, &err); |
| 3316 | switch (err) { |
| 3317 | case -2: |
| 3318 | // trace already emitted by function above |
| 3319 | goto leave; |
| 3320 | case -1: |
| 3321 | /* If there was already a correct packet present, set the |
| 3322 | * current datagram as prepared into <cbuf>. |
| 3323 | */ |
| 3324 | if (prv_pkt) |
| 3325 | qc_txb_store(buf, dglen, first_pkt); |
| 3326 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
| 3327 | goto out; |
| 3328 | default: |
| 3329 | break; |
| 3330 | } |
| 3331 | |
| 3332 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 3333 | BUG_ON(!cur_pkt); |
| 3334 | |
| 3335 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3336 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3337 | |
| 3338 | total += cur_pkt->len; |
| 3339 | /* keep trace of the first packet in the datagram */ |
| 3340 | if (!first_pkt) |
| 3341 | first_pkt = cur_pkt; |
Frédéric Lécaille | 74b5f7b | 2022-11-20 18:35:35 +0100 | [diff] [blame] | 3342 | /* Attach the current one to the previous one and vice versa */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3343 | if (prv_pkt) { |
| 3344 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 3345 | cur_pkt->prev = prv_pkt; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3346 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 3347 | } |
| 3348 | /* Let's say we have to build a new dgram */ |
| 3349 | prv_pkt = NULL; |
| 3350 | dglen += cur_pkt->len; |
| 3351 | /* Client: discard the Initial encryption keys as soon as |
| 3352 | * a handshake packet could be built. |
| 3353 | */ |
| 3354 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
| 3355 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 3356 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3357 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3358 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 3359 | qc_set_timer(qc); |
| 3360 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3361 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 3362 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
| 3363 | } |
| 3364 | /* If the data for the current encryption level have all been sent, |
| 3365 | * select the next level. |
| 3366 | */ |
| 3367 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
| 3368 | next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) { |
| 3369 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 3370 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 3371 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3372 | tel = next_tel; |
| 3373 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 3374 | frms = &qc->els[tel].pktns->tx.frms; |
| 3375 | else |
| 3376 | frms = next_tel_frms; |
| 3377 | qel = &qc->els[tel]; |
| 3378 | if (!LIST_ISEMPTY(frms)) { |
| 3379 | /* If there is data for the next level, do not |
| 3380 | * consume a datagram. |
| 3381 | */ |
| 3382 | prv_pkt = cur_pkt; |
| 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | /* If we have to build a new datagram, set the current datagram as |
| 3387 | * prepared into <cbuf>. |
| 3388 | */ |
| 3389 | if (!prv_pkt) { |
| 3390 | qc_txb_store(buf, dglen, first_pkt); |
| 3391 | first_pkt = NULL; |
| 3392 | dglen = 0; |
| 3393 | padding = 0; |
| 3394 | } |
| 3395 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
| 3396 | (!qc_is_listener(qc) || |
| 3397 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3398 | padding = 1; |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | out: |
| 3403 | ret = total; |
| 3404 | leave: |
| 3405 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3406 | return ret; |
| 3407 | } |
| 3408 | |
| 3409 | /* Send datagrams stored in <buf>. |
| 3410 | * |
| 3411 | * This function always returns 1 for success. Even if sendto() syscall failed, |
| 3412 | * buffer is drained and packets are considered as emitted. QUIC loss detection |
| 3413 | * mechanism is used as a back door way to retry sending. |
| 3414 | */ |
| 3415 | int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx) |
| 3416 | { |
| 3417 | struct quic_conn *qc; |
| 3418 | char skip_sendto = 0; |
| 3419 | |
| 3420 | qc = ctx->qc; |
| 3421 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
| 3422 | while (b_contig_data(buf, 0)) { |
| 3423 | unsigned char *pos; |
| 3424 | struct buffer tmpbuf = { }; |
| 3425 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3426 | uint16_t dglen; |
| 3427 | size_t headlen = sizeof dglen + sizeof first_pkt; |
| 3428 | unsigned int time_sent; |
| 3429 | |
| 3430 | pos = (unsigned char *)b_head(buf); |
| 3431 | dglen = read_u16(pos); |
| 3432 | BUG_ON_HOT(!dglen); /* this should not happen */ |
| 3433 | |
| 3434 | pos += sizeof dglen; |
| 3435 | first_pkt = read_ptr(pos); |
| 3436 | pos += sizeof first_pkt; |
| 3437 | tmpbuf.area = (char *)pos; |
| 3438 | tmpbuf.size = tmpbuf.data = dglen; |
| 3439 | |
| 3440 | TRACE_DATA("send dgram", QUIC_EV_CONN_SPPKTS, qc); |
| 3441 | /* If sendto is on error just skip the call to it for the rest |
| 3442 | * of the loop but continue to purge the buffer. Data will be |
| 3443 | * transmitted when QUIC packets are detected as lost on our |
| 3444 | * side. |
| 3445 | * |
| 3446 | * TODO use fd-monitoring to detect when send operation can be |
| 3447 | * retry. This should improve the bandwidth without relying on |
| 3448 | * retransmission timer. However, it requires a major rework on |
| 3449 | * quic-conn fd management. |
| 3450 | */ |
| 3451 | if (!skip_sendto) { |
| 3452 | if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) { |
| 3453 | skip_sendto = 1; |
| 3454 | TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc); |
| 3455 | } |
| 3456 | } |
| 3457 | |
| 3458 | b_del(buf, dglen + headlen); |
| 3459 | qc->tx.bytes += tmpbuf.data; |
| 3460 | time_sent = now_ms; |
| 3461 | |
| 3462 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3463 | pkt->time_sent = time_sent; |
| 3464 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3465 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
| 3466 | qc->path->ifae_pkts++; |
| 3467 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3468 | qc_idle_timer_rearm(qc, 0); |
| 3469 | } |
| 3470 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3471 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3472 | qc->flags |= QUIC_FL_CONN_CLOSING; |
| 3473 | qc_notify_close(qc); |
| 3474 | |
| 3475 | /* RFC 9000 10.2. Immediate Close: |
| 3476 | * The closing and draining connection states exist to ensure |
| 3477 | * that connections close cleanly and that delayed or reordered |
| 3478 | * packets are properly discarded. These states SHOULD persist |
| 3479 | * for at least three times the current PTO interval... |
| 3480 | * |
| 3481 | * Rearm the idle timeout only one time when entering closing |
| 3482 | * state. |
| 3483 | */ |
| 3484 | qc_idle_timer_do_rearm(qc); |
| 3485 | if (qc->timer_task) { |
| 3486 | task_destroy(qc->timer_task); |
| 3487 | qc->timer_task = NULL; |
| 3488 | } |
| 3489 | } |
| 3490 | qc->path->in_flight += pkt->in_flight_len; |
| 3491 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3492 | if (pkt->in_flight_len) |
| 3493 | qc_set_timer(qc); |
| 3494 | TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 3495 | next_pkt = pkt->next; |
| 3496 | quic_tx_packet_refinc(pkt); |
| 3497 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
| 3498 | } |
| 3499 | } |
| 3500 | |
| 3501 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 3502 | |
| 3503 | return 1; |
| 3504 | } |
| 3505 | |
| 3506 | /* Copy into <buf> buffer a stateless reset token depending on the |
| 3507 | * <salt> salt input. This is the cluster secret which will be derived |
| 3508 | * as HKDF input secret to generate this token. |
| 3509 | * Return 1 if succeeded, 0 if not. |
| 3510 | */ |
| 3511 | static int quic_stateless_reset_token_cpy(struct quic_conn *qc, |
| 3512 | unsigned char *buf, size_t len, |
| 3513 | const unsigned char *salt, size_t saltlen) |
| 3514 | { |
| 3515 | /* Input secret */ |
| 3516 | const unsigned char *key = (const unsigned char *)global.cluster_secret; |
| 3517 | size_t keylen = strlen(global.cluster_secret); |
| 3518 | /* Info */ |
| 3519 | const unsigned char label[] = "stateless token"; |
| 3520 | size_t labellen = sizeof label - 1; |
| 3521 | int ret; |
| 3522 | |
| 3523 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3524 | |
| 3525 | ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len, |
| 3526 | key, keylen, salt, saltlen, label, labellen); |
| 3527 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3528 | return ret; |
| 3529 | } |
| 3530 | |
| 3531 | /* Initialize the stateless reset token attached to <cid> connection ID. |
| 3532 | * Returns 1 if succeeded, 0 if not. |
| 3533 | */ |
| 3534 | static int quic_stateless_reset_token_init(struct quic_conn *qc, |
| 3535 | struct quic_connection_id *quic_cid) |
| 3536 | { |
| 3537 | int ret; |
| 3538 | |
| 3539 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3540 | |
| 3541 | if (global.cluster_secret) { |
| 3542 | /* Output secret */ |
| 3543 | unsigned char *token = quic_cid->stateless_reset_token; |
| 3544 | size_t tokenlen = sizeof quic_cid->stateless_reset_token; |
| 3545 | /* Salt */ |
| 3546 | const unsigned char *cid = quic_cid->cid.data; |
| 3547 | size_t cidlen = quic_cid->cid.len; |
| 3548 | |
| 3549 | ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen); |
| 3550 | } |
| 3551 | else { |
| 3552 | /* TODO: RAND_bytes() should be replaced */ |
| 3553 | ret = RAND_bytes(quic_cid->stateless_reset_token, |
| 3554 | sizeof quic_cid->stateless_reset_token) == 1; |
| 3555 | } |
| 3556 | |
| 3557 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3558 | return ret; |
| 3559 | } |
| 3560 | |
| 3561 | /* Allocate a new CID with <seq_num> as sequence number and attach it to <root> |
| 3562 | * ebtree. |
| 3563 | * |
| 3564 | * The CID is randomly generated in part with the result altered to be |
| 3565 | * associated with the current thread ID. This means this function must only |
| 3566 | * be called by the quic_conn thread. |
| 3567 | * |
| 3568 | * Returns the new CID if succeeded, NULL if not. |
| 3569 | */ |
| 3570 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
| 3571 | struct quic_conn *qc, |
| 3572 | int seq_num) |
| 3573 | { |
| 3574 | struct quic_connection_id *cid; |
| 3575 | |
| 3576 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3577 | |
| 3578 | cid = pool_alloc(pool_head_quic_connection_id); |
| 3579 | if (!cid) { |
| 3580 | TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 3581 | goto err; |
| 3582 | } |
| 3583 | |
| 3584 | cid->cid.len = QUIC_HAP_CID_LEN; |
| 3585 | /* TODO: RAND_bytes() should be replaced */ |
| 3586 | if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) { |
| 3587 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc); |
| 3588 | goto err; |
| 3589 | } |
| 3590 | |
| 3591 | quic_pin_cid_to_tid(cid->cid.data, tid); |
| 3592 | if (quic_stateless_reset_token_init(qc, cid) != 1) { |
| 3593 | TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc); |
| 3594 | goto err; |
| 3595 | } |
| 3596 | |
| 3597 | cid->qc = qc; |
| 3598 | |
| 3599 | cid->seq_num.key = seq_num; |
| 3600 | cid->retire_prior_to = 0; |
| 3601 | /* insert the allocated CID in the quic_conn tree */ |
| 3602 | eb64_insert(root, &cid->seq_num); |
| 3603 | |
| 3604 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3605 | return cid; |
| 3606 | |
| 3607 | err: |
| 3608 | pool_free(pool_head_quic_connection_id, cid); |
| 3609 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3610 | return NULL; |
| 3611 | } |
| 3612 | |
| 3613 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3614 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3615 | * a HANDSHAKE_DONE frame. |
| 3616 | * Return 1 if succeeded, 0 if not. |
| 3617 | */ |
| 3618 | static int quic_build_post_handshake_frames(struct quic_conn *qc) |
| 3619 | { |
| 3620 | int ret = 0, i, first, max; |
| 3621 | struct quic_enc_level *qel; |
| 3622 | struct quic_frame *frm, *frmbak; |
| 3623 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3624 | struct eb64_node *node; |
| 3625 | |
| 3626 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 3627 | |
| 3628 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3629 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
| 3630 | if (qc_is_listener(qc)) { |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 3631 | frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3632 | if (!frm) { |
| 3633 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3634 | goto leave; |
| 3635 | } |
| 3636 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3637 | LIST_APPEND(&frm_list, &frm->list); |
| 3638 | } |
| 3639 | |
| 3640 | /* Initialize <max> connection IDs minus one: there is |
| 3641 | * already one connection ID used for the current connection. |
| 3642 | */ |
| 3643 | first = 1; |
| 3644 | max = qc->tx.params.active_connection_id_limit; |
| 3645 | |
| 3646 | /* TODO: check limit */ |
| 3647 | for (i = first; i < max; i++) { |
| 3648 | struct quic_connection_id *cid; |
| 3649 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 3650 | frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3651 | if (!frm) { |
| 3652 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3653 | goto err; |
| 3654 | } |
| 3655 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3656 | cid = new_quic_cid(&qc->cids, qc, i); |
| 3657 | if (!cid) { |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 3658 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3659 | TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3660 | goto err; |
| 3661 | } |
| 3662 | |
| 3663 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 3664 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
| 3665 | |
| 3666 | quic_connection_id_to_frm_cpy(frm, cid); |
| 3667 | LIST_APPEND(&frm_list, &frm->list); |
| 3668 | } |
| 3669 | |
| 3670 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
| 3671 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
| 3672 | |
| 3673 | ret = 1; |
| 3674 | leave: |
| 3675 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 3676 | return ret; |
| 3677 | |
| 3678 | err: |
| 3679 | /* free the frames */ |
| 3680 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 3681 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3682 | |
| 3683 | node = eb64_lookup_ge(&qc->cids, first); |
| 3684 | while (node) { |
| 3685 | struct quic_connection_id *cid; |
| 3686 | |
| 3687 | cid = eb64_entry(node, struct quic_connection_id, seq_num); |
| 3688 | if (cid->seq_num.key >= max) |
| 3689 | break; |
| 3690 | |
| 3691 | node = eb64_next(node); |
| 3692 | ebmb_delete(&cid->node); |
| 3693 | eb64_delete(&cid->seq_num); |
| 3694 | pool_free(pool_head_quic_connection_id, cid); |
| 3695 | } |
| 3696 | goto leave; |
| 3697 | } |
| 3698 | |
| 3699 | /* Deallocate <l> list of ACK ranges. */ |
| 3700 | void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs) |
| 3701 | { |
| 3702 | struct eb64_node *n; |
| 3703 | struct quic_arng_node *ar; |
| 3704 | |
| 3705 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 3706 | |
| 3707 | n = eb64_first(&arngs->root); |
| 3708 | while (n) { |
| 3709 | struct eb64_node *next; |
| 3710 | |
| 3711 | ar = eb64_entry(n, struct quic_arng_node, first); |
| 3712 | next = eb64_next(n); |
| 3713 | eb64_delete(n); |
| 3714 | pool_free(pool_head_quic_arng, ar); |
| 3715 | n = next; |
| 3716 | } |
| 3717 | |
| 3718 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 3719 | } |
| 3720 | |
| 3721 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3722 | * descending order. |
| 3723 | */ |
| 3724 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3725 | struct quic_arng_node *q) |
| 3726 | { |
| 3727 | return p->first.key - q->last - 2; |
| 3728 | } |
| 3729 | |
| 3730 | |
| 3731 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3732 | * encoded size until it goes below <limit>. |
| 3733 | * Returns 1 if succeeded, 0 if not (no more element to remove). |
| 3734 | */ |
| 3735 | static int quic_rm_last_ack_ranges(struct quic_conn *qc, |
| 3736 | struct quic_arngs *arngs, size_t limit) |
| 3737 | { |
| 3738 | int ret = 0; |
| 3739 | struct eb64_node *last, *prev; |
| 3740 | |
| 3741 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3742 | |
| 3743 | last = eb64_last(&arngs->root); |
| 3744 | while (last && arngs->enc_sz > limit) { |
| 3745 | struct quic_arng_node *last_node, *prev_node; |
| 3746 | |
| 3747 | prev = eb64_prev(last); |
| 3748 | if (!prev) { |
| 3749 | TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc); |
| 3750 | goto out; |
| 3751 | } |
| 3752 | |
| 3753 | last_node = eb64_entry(last, struct quic_arng_node, first); |
| 3754 | prev_node = eb64_entry(prev, struct quic_arng_node, first); |
| 3755 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3756 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3757 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3758 | --arngs->sz; |
| 3759 | eb64_delete(last); |
| 3760 | pool_free(pool_head_quic_arng, last); |
| 3761 | last = prev; |
| 3762 | } |
| 3763 | |
| 3764 | ret = 1; |
| 3765 | out: |
| 3766 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3767 | return ret; |
| 3768 | } |
| 3769 | |
| 3770 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3771 | static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs) |
| 3772 | { |
| 3773 | struct eb64_node *node, *next; |
| 3774 | struct quic_arng_node *ar, *ar_next; |
| 3775 | |
| 3776 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3777 | |
| 3778 | node = eb64_last(&arngs->root); |
| 3779 | if (!node) |
| 3780 | goto leave; |
| 3781 | |
| 3782 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 3783 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3784 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3785 | |
| 3786 | while ((next = eb64_prev(node))) { |
| 3787 | ar_next = eb64_entry(next, struct quic_arng_node, first); |
| 3788 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3789 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3790 | node = next; |
| 3791 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 3792 | } |
| 3793 | |
| 3794 | leave: |
| 3795 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3796 | } |
| 3797 | |
| 3798 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3799 | * Returns the ack range node which has been inserted if succeeded, NULL if not. |
| 3800 | */ |
| 3801 | static inline |
| 3802 | struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc, |
| 3803 | struct quic_arngs *arngs, |
| 3804 | struct quic_arng *ar) |
| 3805 | { |
| 3806 | struct quic_arng_node *new_ar; |
| 3807 | |
| 3808 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3809 | |
| 3810 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3811 | if (!new_ar) { |
| 3812 | TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc); |
| 3813 | goto leave; |
| 3814 | } |
| 3815 | |
| 3816 | new_ar->first.key = ar->first; |
| 3817 | new_ar->last = ar->last; |
| 3818 | eb64_insert(&arngs->root, &new_ar->first); |
| 3819 | arngs->sz++; |
| 3820 | |
| 3821 | leave: |
| 3822 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 3823 | return new_ar; |
| 3824 | } |
| 3825 | |
| 3826 | /* Update <arngs> tree of ACK ranges with <ar> as new ACK range value. |
| 3827 | * Note that this function computes the number of bytes required to encode |
| 3828 | * this tree of ACK ranges in descending order. |
| 3829 | * |
| 3830 | * Descending order |
| 3831 | * -------------> |
| 3832 | * range1 range2 |
| 3833 | * ..........|--------|..............|--------| |
| 3834 | * ^ ^ ^ ^ |
| 3835 | * | | | | |
| 3836 | * last1 first1 last2 first2 |
| 3837 | * ..........+--------+--------------+--------+...... |
| 3838 | * diff1 gap12 diff2 |
| 3839 | * |
| 3840 | * To encode the previous list of ranges we must encode integers as follows in |
| 3841 | * descending order: |
| 3842 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
| 3843 | * with diff1 = last1 - first1 |
| 3844 | * diff2 = last2 - first2 |
| 3845 | * gap12 = first1 - last2 - 2 (>= 0) |
| 3846 | * |
| 3847 | |
| 3848 | returns 0 on error |
| 3849 | |
| 3850 | */ |
| 3851 | int quic_update_ack_ranges_list(struct quic_conn *qc, |
| 3852 | struct quic_arngs *arngs, |
| 3853 | struct quic_arng *ar) |
| 3854 | { |
| 3855 | int ret = 0; |
| 3856 | struct eb64_node *le; |
| 3857 | struct quic_arng_node *new_node; |
| 3858 | struct eb64_node *new; |
| 3859 | |
| 3860 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3861 | |
| 3862 | new = NULL; |
| 3863 | if (eb_is_empty(&arngs->root)) { |
| 3864 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 3865 | if (new_node) |
| 3866 | ret = 1; |
| 3867 | |
| 3868 | goto leave; |
| 3869 | } |
| 3870 | |
| 3871 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3872 | if (!le) { |
| 3873 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 3874 | if (!new_node) |
| 3875 | goto leave; |
| 3876 | |
| 3877 | new = &new_node->first; |
| 3878 | } |
| 3879 | else { |
| 3880 | struct quic_arng_node *le_ar = |
| 3881 | eb64_entry(le, struct quic_arng_node, first); |
| 3882 | |
| 3883 | /* Already existing range */ |
| 3884 | if (le_ar->last >= ar->last) { |
| 3885 | ret = 1; |
| 3886 | } |
| 3887 | else if (le_ar->last + 1 >= ar->first) { |
| 3888 | le_ar->last = ar->last; |
| 3889 | new = le; |
| 3890 | new_node = le_ar; |
| 3891 | } |
| 3892 | else { |
| 3893 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 3894 | if (!new_node) |
| 3895 | goto leave; |
| 3896 | |
| 3897 | new = &new_node->first; |
| 3898 | } |
| 3899 | } |
| 3900 | |
| 3901 | /* Verify that the new inserted node does not overlap the nodes |
| 3902 | * which follow it. |
| 3903 | */ |
| 3904 | if (new) { |
| 3905 | struct eb64_node *next; |
| 3906 | struct quic_arng_node *next_node; |
| 3907 | |
| 3908 | while ((next = eb64_next(new))) { |
| 3909 | next_node = |
| 3910 | eb64_entry(next, struct quic_arng_node, first); |
| 3911 | if (new_node->last + 1 < next_node->first.key) |
| 3912 | break; |
| 3913 | |
| 3914 | if (next_node->last > new_node->last) |
| 3915 | new_node->last = next_node->last; |
| 3916 | eb64_delete(next); |
| 3917 | pool_free(pool_head_quic_arng, next_node); |
| 3918 | /* Decrement the size of these ranges. */ |
| 3919 | arngs->sz--; |
| 3920 | } |
| 3921 | } |
| 3922 | |
| 3923 | ret = 1; |
| 3924 | leave: |
| 3925 | quic_arngs_set_enc_sz(qc, arngs); |
| 3926 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 3927 | return ret; |
| 3928 | } |
| 3929 | /* Remove the header protection of packets at <el> encryption level. |
| 3930 | * Always succeeds. |
| 3931 | */ |
| 3932 | static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el) |
| 3933 | { |
| 3934 | struct quic_tls_ctx *tls_ctx; |
| 3935 | struct quic_rx_packet *pqpkt, *pkttmp; |
| 3936 | struct quic_enc_level *app_qel; |
| 3937 | |
| 3938 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3939 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3940 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
| 3941 | if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) { |
| 3942 | TRACE_DEVEL("hp not removed (handshake not completed)", |
| 3943 | QUIC_EV_CONN_ELRMHP, qc); |
| 3944 | goto out; |
| 3945 | } |
| 3946 | tls_ctx = &el->tls_ctx; |
| 3947 | list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) { |
| 3948 | if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
| 3949 | pqpkt->data + pqpkt->pn_offset, pqpkt->data)) { |
| 3950 | TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc); |
| 3951 | } |
| 3952 | else { |
| 3953 | /* The AAD includes the packet number field */ |
| 3954 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3955 | /* Store the packet into the tree of packets to decrypt. */ |
| 3956 | pqpkt->pn_node.key = pqpkt->pn; |
| 3957 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3958 | quic_rx_packet_refinc(pqpkt); |
| 3959 | TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt); |
| 3960 | } |
| 3961 | LIST_DELETE(&pqpkt->list); |
| 3962 | quic_rx_packet_refdec(pqpkt); |
| 3963 | } |
| 3964 | |
| 3965 | out: |
| 3966 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
| 3967 | } |
| 3968 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3969 | /* Process all the CRYPTO frame at <el> encryption level. This is the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 3970 | * 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] | 3971 | * stream for this level. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3972 | * Return 1 if succeeded, 0 if not. |
| 3973 | */ |
| 3974 | static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc, |
| 3975 | struct quic_enc_level *el, |
| 3976 | struct ssl_sock_ctx *ctx) |
| 3977 | { |
| 3978 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3979 | struct ncbuf *ncbuf; |
| 3980 | struct quic_cstream *cstream = el->cstream; |
| 3981 | ncb_sz_t data; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3982 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3983 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3984 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3985 | BUG_ON(!cstream); |
| 3986 | ncbuf = &cstream->rx.ncbuf; |
| 3987 | if (ncb_is_null(ncbuf)) |
| 3988 | goto done; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3989 | |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 3990 | /* TODO not working if buffer is wrapping */ |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3991 | while ((data = ncb_data(ncbuf, 0))) { |
| 3992 | const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3993 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3994 | if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3995 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3996 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3997 | cstream->rx.offset += data; |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 3998 | TRACE_DEVEL("buffered crypto data were provided to TLS stack", |
| 3999 | QUIC_EV_CONN_PHPKTS, qc, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4000 | } |
| 4001 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4002 | done: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4003 | ret = 1; |
| 4004 | leave: |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4005 | if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) { |
| 4006 | 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] | 4007 | quic_free_ncbuf(ncbuf); |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4008 | } |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4009 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4010 | return ret; |
| 4011 | } |
| 4012 | |
| 4013 | /* Process all the packets at <el> and <next_el> encryption level. |
| 4014 | * This is the caller responsibility to check that <cur_el> is different of <next_el> |
| 4015 | * as pointer value. |
| 4016 | * Return 1 if succeeded, 0 if not. |
| 4017 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4018 | int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el, |
| 4019 | struct quic_enc_level *next_el, int force_ack) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4020 | { |
| 4021 | int ret = 0; |
| 4022 | struct eb64_node *node; |
| 4023 | int64_t largest_pn = -1; |
| 4024 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4025 | struct quic_enc_level *qel = cur_el; |
| 4026 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4027 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4028 | qel = cur_el; |
| 4029 | next_tel: |
| 4030 | if (!qel) |
| 4031 | goto out; |
| 4032 | |
| 4033 | node = eb64_first(&qel->rx.pkts); |
| 4034 | while (node) { |
| 4035 | struct quic_rx_packet *pkt; |
| 4036 | |
| 4037 | pkt = eb64_entry(node, struct quic_rx_packet, pn_node); |
| 4038 | TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT, |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4039 | qc, pkt, NULL, qc->xprt_ctx->ssl); |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 4040 | if (!qc_pkt_decrypt(qc, qel, pkt)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4041 | /* Drop the packet */ |
| 4042 | TRACE_ERROR("packet decryption 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 | } |
| 4045 | else { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4046 | if (!qc_parse_pkt_frms(qc, pkt, qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4047 | /* Drop the packet */ |
| 4048 | TRACE_ERROR("packet parsing failed -> dropped", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4049 | QUIC_EV_CONN_RXPKT, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4050 | HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing); |
| 4051 | } |
| 4052 | else { |
| 4053 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 4054 | |
| 4055 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 4056 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 4057 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
| 4058 | qc_idle_timer_rearm(qc, 1); |
| 4059 | } |
| 4060 | if (pkt->pn > largest_pn) { |
| 4061 | largest_pn = pkt->pn; |
| 4062 | largest_pn_time_received = pkt->time_received; |
| 4063 | } |
| 4064 | /* Update the list of ranges to acknowledge. */ |
| 4065 | if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar)) |
| 4066 | TRACE_ERROR("Could not update ack range list", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4067 | QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4068 | } |
| 4069 | } |
| 4070 | node = eb64_next(node); |
| 4071 | eb64_delete(&pkt->pn_node); |
| 4072 | quic_rx_packet_refdec(pkt); |
| 4073 | } |
| 4074 | |
| 4075 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 4076 | /* Update the largest packet number. */ |
| 4077 | qel->pktns->rx.largest_pn = largest_pn; |
| 4078 | /* Update the largest acknowledged packet timestamps */ |
| 4079 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 4080 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 4081 | } |
| 4082 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4083 | 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] | 4084 | // trace already emitted by function above |
| 4085 | goto leave; |
| 4086 | } |
| 4087 | |
| 4088 | if (qel == cur_el) { |
| 4089 | BUG_ON(qel == next_el); |
| 4090 | qel = next_el; |
| 4091 | largest_pn = -1; |
| 4092 | goto next_tel; |
| 4093 | } |
| 4094 | |
| 4095 | out: |
| 4096 | ret = 1; |
| 4097 | leave: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4098 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4099 | return ret; |
| 4100 | } |
| 4101 | |
| 4102 | /* Check if it's possible to remove header protection for packets related to |
| 4103 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 4104 | * |
| 4105 | * Return true if the operation is possible else false. |
| 4106 | */ |
| 4107 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4108 | { |
| 4109 | int ret = 0; |
| 4110 | enum quic_tls_enc_level tel; |
| 4111 | |
| 4112 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
| 4113 | |
| 4114 | if (!qel) |
| 4115 | goto cant_rm_hp; |
| 4116 | |
| 4117 | tel = ssl_to_quic_enc_level(qel->level); |
| 4118 | |
| 4119 | /* check if tls secrets are available */ |
| 4120 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 4121 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
| 4122 | goto cant_rm_hp; |
| 4123 | } |
| 4124 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4125 | if (!quic_tls_has_rx_sec(qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4126 | TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc); |
| 4127 | goto cant_rm_hp; |
| 4128 | } |
| 4129 | |
Frédéric Lécaille | 8417beb | 2023-02-01 10:31:35 +0100 | [diff] [blame] | 4130 | if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) { |
| 4131 | TRACE_DEVEL("handshake not complete", QUIC_EV_CONN_TRMHP, qc); |
| 4132 | goto cant_rm_hp; |
| 4133 | } |
| 4134 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4135 | /* check if the connection layer is ready before using app level */ |
| 4136 | if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) && |
| 4137 | qc->mux_state == QC_MUX_NULL) { |
| 4138 | TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc); |
| 4139 | goto cant_rm_hp; |
| 4140 | } |
| 4141 | |
| 4142 | ret = 1; |
| 4143 | cant_rm_hp: |
| 4144 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc); |
| 4145 | return ret; |
| 4146 | } |
| 4147 | |
| 4148 | /* Try to send application frames from list <frms> on connection <qc>. |
| 4149 | * |
| 4150 | * Use qc_send_app_probing wrapper when probing with old data. |
| 4151 | * |
| 4152 | * Returns 1 on success. Some data might not have been sent due to congestion, |
| 4153 | * in this case they are left in <frms> input list. The caller may subscribe on |
| 4154 | * quic-conn to retry later. |
| 4155 | * |
| 4156 | * Returns 0 on critical error. |
| 4157 | * TODO review and classify more distinctly transient from definitive errors to |
| 4158 | * allow callers to properly handle it. |
| 4159 | */ |
| 4160 | static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms) |
| 4161 | { |
| 4162 | int status = 0; |
| 4163 | struct buffer *buf; |
| 4164 | |
| 4165 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4166 | |
| 4167 | buf = qc_txb_alloc(qc); |
| 4168 | if (!buf) { |
| 4169 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 4170 | goto leave; |
| 4171 | } |
| 4172 | |
| 4173 | /* Prepare and send packets until we could not further prepare packets. */ |
| 4174 | while (1) { |
| 4175 | int ret; |
| 4176 | /* Currently buf cannot be non-empty at this stage. Even if a |
| 4177 | * previous sendto() has failed it is emptied to simulate |
| 4178 | * packet emission and rely on QUIC lost detection to try to |
| 4179 | * emit it. |
| 4180 | */ |
| 4181 | BUG_ON_HOT(b_data(buf)); |
| 4182 | b_reset(buf); |
| 4183 | |
| 4184 | ret = qc_prep_app_pkts(qc, buf, frms); |
| 4185 | if (ret == -1) |
| 4186 | goto err; |
| 4187 | else if (ret == 0) |
| 4188 | goto out; |
| 4189 | |
| 4190 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
| 4191 | goto err; |
| 4192 | } |
| 4193 | |
| 4194 | out: |
| 4195 | status = 1; |
| 4196 | qc_txb_release(qc); |
| 4197 | leave: |
| 4198 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4199 | return status; |
| 4200 | |
| 4201 | err: |
| 4202 | qc_txb_release(qc); |
| 4203 | goto leave; |
| 4204 | } |
| 4205 | |
| 4206 | /* Try to send application frames from list <frms> on connection <qc>. Use this |
| 4207 | * function when probing is required. |
| 4208 | * |
| 4209 | * Returns the result from qc_send_app_pkts function. |
| 4210 | */ |
| 4211 | static forceinline int qc_send_app_probing(struct quic_conn *qc, |
| 4212 | struct list *frms) |
| 4213 | { |
| 4214 | int ret; |
| 4215 | |
| 4216 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4217 | |
| 4218 | TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc); |
| 4219 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4220 | ret = qc_send_app_pkts(qc, frms); |
| 4221 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4222 | |
| 4223 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4224 | return ret; |
| 4225 | } |
| 4226 | |
| 4227 | /* Try to send application frames from list <frms> on connection <qc>. This |
| 4228 | * function is provided for MUX upper layer usage only. |
| 4229 | * |
| 4230 | * Returns the result from qc_send_app_pkts function. |
| 4231 | */ |
| 4232 | int qc_send_mux(struct quic_conn *qc, struct list *frms) |
| 4233 | { |
| 4234 | int ret; |
| 4235 | |
| 4236 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4237 | BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */ |
| 4238 | |
| 4239 | TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc); |
| 4240 | qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4241 | ret = qc_send_app_pkts(qc, frms); |
| 4242 | qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4243 | |
| 4244 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4245 | return ret; |
| 4246 | } |
| 4247 | |
| 4248 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 4249 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
| 4250 | * QUIC connection. <old_data> is used as boolean to send data already sent but |
| 4251 | * not already acknowledged (in flight). |
| 4252 | * Returns 1 if succeeded, 0 if not. |
| 4253 | */ |
| 4254 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 4255 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 4256 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 4257 | { |
| 4258 | int ret, status = 0; |
| 4259 | struct buffer *buf = qc_txb_alloc(qc); |
| 4260 | |
| 4261 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4262 | |
| 4263 | if (!buf) { |
| 4264 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 4265 | goto leave; |
| 4266 | } |
| 4267 | |
| 4268 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4269 | * sendto() has failed it is emptied to simulate packet emission and |
| 4270 | * rely on QUIC lost detection to try to emit it. |
| 4271 | */ |
| 4272 | BUG_ON_HOT(b_data(buf)); |
| 4273 | b_reset(buf); |
| 4274 | |
| 4275 | if (old_data) { |
| 4276 | TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc); |
| 4277 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4278 | } |
| 4279 | |
| 4280 | ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms); |
| 4281 | if (ret == -1) |
| 4282 | goto out; |
| 4283 | else if (ret == 0) |
| 4284 | goto skip_send; |
| 4285 | |
| 4286 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
| 4287 | goto out; |
| 4288 | |
| 4289 | skip_send: |
| 4290 | status = 1; |
| 4291 | out: |
| 4292 | TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc); |
| 4293 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4294 | qc_txb_release(qc); |
| 4295 | leave: |
| 4296 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4297 | return status; |
| 4298 | } |
| 4299 | |
| 4300 | /* Retransmit up to two datagrams depending on packet number space */ |
| 4301 | static void qc_dgrams_retransmit(struct quic_conn *qc) |
| 4302 | { |
| 4303 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4304 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4305 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4306 | |
| 4307 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4308 | |
| 4309 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4310 | int i; |
| 4311 | |
| 4312 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 4313 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 4314 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4315 | |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4316 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
| 4317 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 4318 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 4319 | if (!LIST_ISEMPTY(&ifrms)) { |
| 4320 | iqel->pktns->tx.pto_probe = 1; |
| 4321 | if (!LIST_ISEMPTY(&hfrms)) |
| 4322 | hqel->pktns->tx.pto_probe = 1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4323 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 4324 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms); |
| 4325 | /* Put back unsent frames in their packet number spaces */ |
| 4326 | LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms); |
| 4327 | LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms); |
| 4328 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4329 | } |
| 4330 | TRACE_STATE("no more need to probe Initial packet number space", |
| 4331 | QUIC_EV_CONN_TXPKT, qc); |
| 4332 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4333 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4334 | } |
| 4335 | else { |
| 4336 | int i; |
| 4337 | |
| 4338 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4339 | hqel->pktns->tx.pto_probe = 0; |
| 4340 | 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] | 4341 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4342 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4343 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
| 4344 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4345 | if (!LIST_ISEMPTY(&frms1)) { |
| 4346 | hqel->pktns->tx.pto_probe = 1; |
| 4347 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 4348 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 4349 | /* Put back unsent frames into their packet number spaces */ |
| 4350 | LIST_SPLICE(&hqel->pktns->tx.frms, &frms1); |
| 4351 | } |
| 4352 | } |
| 4353 | TRACE_STATE("no more need to probe Handshake packet number space", |
| 4354 | QUIC_EV_CONN_TXPKT, qc); |
| 4355 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4356 | } |
| 4357 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4358 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 4359 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4360 | |
| 4361 | aqel->pktns->tx.pto_probe = 0; |
| 4362 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 4363 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4364 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 4365 | if (!LIST_ISEMPTY(&frms1)) { |
| 4366 | aqel->pktns->tx.pto_probe = 1; |
| 4367 | qc_send_app_probing(qc, &frms1); |
| 4368 | /* Put back unsent frames into their packet number spaces */ |
| 4369 | LIST_SPLICE(&aqel->pktns->tx.frms, &frms1); |
| 4370 | } |
| 4371 | if (!LIST_ISEMPTY(&frms2)) { |
| 4372 | aqel->pktns->tx.pto_probe = 1; |
| 4373 | qc_send_app_probing(qc, &frms2); |
| 4374 | /* Put back unsent frames into their packet number spaces */ |
| 4375 | LIST_SPLICE(&aqel->pktns->tx.frms, &frms2); |
| 4376 | } |
| 4377 | TRACE_STATE("no more need to probe 01RTT packet number space", |
| 4378 | QUIC_EV_CONN_TXPKT, qc); |
| 4379 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4380 | } |
| 4381 | } |
| 4382 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4383 | } |
| 4384 | |
| 4385 | /* QUIC connection packet handler task (post handshake) */ |
| 4386 | struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 4387 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4388 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4389 | struct quic_enc_level *qel; |
| 4390 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4391 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4392 | |
| 4393 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 4394 | TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state); |
| 4395 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 4396 | if (qc_test_fd(qc)) |
| 4397 | qc_rcv_buf(qc); |
| 4398 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4399 | /* Retranmissions */ |
| 4400 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 4401 | TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
| 4402 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 4403 | qc_dgrams_retransmit(qc); |
| 4404 | } |
| 4405 | |
| 4406 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 4407 | qc_rm_hp_pkts(qc, qel); |
| 4408 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4409 | if (!qc_treat_rx_pkts(qc, qel, NULL, 0)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4410 | TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4411 | goto out; |
| 4412 | } |
| 4413 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 4414 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 4415 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc); |
| 4416 | goto out; |
| 4417 | } |
| 4418 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4419 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4420 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) { |
| 4421 | TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc); |
| 4422 | goto out; |
| 4423 | } |
| 4424 | |
| 4425 | /* XXX TODO: how to limit the list frames to send */ |
| 4426 | if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) { |
| 4427 | TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4428 | goto out; |
| 4429 | } |
| 4430 | |
| 4431 | out: |
| 4432 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 4433 | return t; |
| 4434 | } |
| 4435 | |
| 4436 | /* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */ |
| 4437 | static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4438 | { |
| 4439 | return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) || |
| 4440 | (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) || |
| 4441 | qel->pktns->tx.pto_probe || |
| 4442 | !LIST_ISEMPTY(&qel->pktns->tx.frms); |
| 4443 | } |
| 4444 | |
| 4445 | /* QUIC connection packet handler task. */ |
| 4446 | struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state) |
| 4447 | { |
| 4448 | int ret, ssl_err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4449 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4450 | enum quic_tls_enc_level tel, next_tel; |
| 4451 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4452 | /* Early-data encryption level */ |
| 4453 | struct quic_enc_level *eqel; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4454 | struct buffer *buf = NULL; |
| 4455 | int st, force_ack, zero_rtt; |
| 4456 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4457 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4458 | eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4459 | st = qc->state; |
| 4460 | TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st); |
| 4461 | |
| 4462 | /* Retranmissions */ |
| 4463 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 4464 | TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
| 4465 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 4466 | qc_dgrams_retransmit(qc); |
| 4467 | } |
| 4468 | |
| 4469 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 4470 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
| 4471 | TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached", |
| 4472 | QUIC_EV_CONN_IO_CB, qc); |
| 4473 | /* The I/O handler has been woken up by the dgram parser (qc_lstnr_pkt_rcv()) |
| 4474 | * after the anti-amplification was reached. |
| 4475 | * |
| 4476 | * TODO: this part should be removed. This was there because the |
| 4477 | * datagram parser was not executed by only one thread. |
| 4478 | */ |
| 4479 | qc_set_timer(qc); |
Amaury Denoyelle | 5ac6b3b | 2022-12-14 18:04:06 +0100 | [diff] [blame] | 4480 | 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] | 4481 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 4482 | } |
| 4483 | ssl_err = SSL_ERROR_NONE; |
| 4484 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4485 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4486 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 4487 | |
| 4488 | if (qc_test_fd(qc)) |
| 4489 | qc_rcv_buf(qc); |
| 4490 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4491 | if (st >= QUIC_HS_ST_COMPLETE && |
| 4492 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 4493 | TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 4494 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 4495 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 4496 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 4497 | } |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4498 | 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] | 4499 | goto out; |
| 4500 | |
| 4501 | qel = &qc->els[tel]; |
| 4502 | next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel]; |
| 4503 | |
| 4504 | next_level: |
| 4505 | /* Treat packets waiting for header packet protection decryption */ |
| 4506 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 4507 | qc_rm_hp_pkts(qc, qel); |
| 4508 | |
| 4509 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 4510 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4511 | if (!qc_treat_rx_pkts(qc, qel, next_qel, force_ack)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4512 | goto out; |
| 4513 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 4514 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 4515 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc); |
| 4516 | goto out; |
| 4517 | } |
| 4518 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4519 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4520 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 4521 | goto out; |
| 4522 | |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4523 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4524 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4525 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
| 4526 | if (next_qel && next_qel == eqel && zero_rtt) { |
| 4527 | TRACE_DEVEL("select 0RTT as next encryption level", |
| 4528 | QUIC_EV_CONN_PHPKTS, qc); |
| 4529 | qel = next_qel; |
| 4530 | next_qel = NULL; |
| 4531 | goto next_level; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | st = qc->state; |
| 4535 | if (st >= QUIC_HS_ST_COMPLETE) { |
| 4536 | if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) && |
| 4537 | !quic_build_post_handshake_frames(qc)) |
| 4538 | goto out; |
| 4539 | |
| 4540 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags & |
| 4541 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 4542 | /* Discard the Handshake keys. */ |
| 4543 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4544 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 4545 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 4546 | qc_set_timer(qc); |
| 4547 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4548 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
| 4549 | } |
| 4550 | |
| 4551 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 4552 | /* There may be remaining handshake to build (acks) */ |
| 4553 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 4554 | } |
| 4555 | } |
| 4556 | |
| 4557 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 4558 | * be considered. |
| 4559 | */ |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4560 | if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4561 | goto out; |
| 4562 | |
| 4563 | if (!qc_need_sending(qc, qel) && |
| 4564 | (!next_qel || !qc_need_sending(qc, next_qel))) { |
| 4565 | goto skip_send; |
| 4566 | } |
| 4567 | |
| 4568 | buf = qc_txb_alloc(qc); |
| 4569 | if (!buf) |
| 4570 | goto out; |
| 4571 | |
| 4572 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4573 | * sendto() has failed it is emptied to simulate packet emission and |
| 4574 | * rely on QUIC lost detection to try to emit it. |
| 4575 | */ |
| 4576 | BUG_ON_HOT(b_data(buf)); |
| 4577 | b_reset(buf); |
| 4578 | |
| 4579 | ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms, |
| 4580 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
| 4581 | if (ret == -1) |
| 4582 | goto out; |
| 4583 | else if (ret == 0) |
| 4584 | goto skip_send; |
| 4585 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4586 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4587 | goto out; |
| 4588 | |
| 4589 | skip_send: |
| 4590 | /* Check if there is something to do for the next level. |
| 4591 | */ |
| 4592 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4593 | quic_tls_has_rx_sec(next_qel) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4594 | (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) { |
| 4595 | qel = next_qel; |
| 4596 | next_qel = NULL; |
| 4597 | goto next_level; |
| 4598 | } |
| 4599 | |
| 4600 | out: |
| 4601 | qc_txb_release(qc); |
| 4602 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err); |
| 4603 | return t; |
| 4604 | } |
| 4605 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4606 | /* Release the memory allocated for <cs> CRYPTO stream */ |
| 4607 | void quic_cstream_free(struct quic_cstream *cs) |
| 4608 | { |
| 4609 | if (!cs) { |
| 4610 | /* This is the case for ORTT encryption level */ |
| 4611 | return; |
| 4612 | } |
| 4613 | |
Amaury Denoyelle | bc174b2 | 2022-11-17 10:12:52 +0100 | [diff] [blame] | 4614 | quic_free_ncbuf(&cs->rx.ncbuf); |
| 4615 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4616 | qc_stream_desc_release(cs->desc); |
| 4617 | pool_free(pool_head_quic_cstream, cs); |
| 4618 | } |
| 4619 | |
| 4620 | /* Allocate a new QUIC stream for <qc>. |
| 4621 | * Return it if succeeded, NULL if not. |
| 4622 | */ |
| 4623 | struct quic_cstream *quic_cstream_new(struct quic_conn *qc) |
| 4624 | { |
| 4625 | struct quic_cstream *cs, *ret_cs = NULL; |
| 4626 | |
| 4627 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 4628 | cs = pool_alloc(pool_head_quic_cstream); |
| 4629 | if (!cs) { |
| 4630 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 4631 | goto leave; |
| 4632 | } |
| 4633 | |
| 4634 | cs->rx.offset = 0; |
| 4635 | cs->rx.ncbuf = NCBUF_NULL; |
| 4636 | cs->rx.offset = 0; |
| 4637 | |
| 4638 | cs->tx.offset = 0; |
| 4639 | cs->tx.sent_offset = 0; |
| 4640 | cs->tx.buf = BUF_NULL; |
| 4641 | cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc); |
| 4642 | if (!cs->desc) { |
| 4643 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 4644 | goto err; |
| 4645 | } |
| 4646 | |
| 4647 | ret_cs = cs; |
| 4648 | leave: |
| 4649 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 4650 | return ret_cs; |
| 4651 | |
| 4652 | err: |
| 4653 | pool_free(pool_head_quic_cstream, cs); |
| 4654 | goto leave; |
| 4655 | } |
| 4656 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4657 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
| 4658 | static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4659 | { |
| 4660 | int i; |
| 4661 | |
| 4662 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4663 | |
| 4664 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4665 | if (qel->tx.crypto.bufs[i]) { |
| 4666 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4667 | qel->tx.crypto.bufs[i] = NULL; |
| 4668 | } |
| 4669 | } |
| 4670 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4671 | quic_cstream_free(qel->cstream); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4672 | |
| 4673 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4674 | } |
| 4675 | |
| 4676 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
| 4677 | * connection allocating everything needed. |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4678 | * |
| 4679 | * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use |
| 4680 | * quic_conn_enc_level_uninit() to cleanup partially allocated content. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4681 | */ |
| 4682 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4683 | enum quic_tls_enc_level level) |
| 4684 | { |
| 4685 | int ret = 0; |
| 4686 | struct quic_enc_level *qel; |
| 4687 | |
| 4688 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4689 | |
| 4690 | qel = &qc->els[level]; |
| 4691 | qel->level = quic_to_ssl_enc_level(level); |
| 4692 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4693 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4694 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 4695 | qel->tls_ctx.flags = 0; |
| 4696 | |
| 4697 | qel->rx.pkts = EB_ROOT; |
| 4698 | LIST_INIT(&qel->rx.pqpkts); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4699 | |
| 4700 | /* Allocate only one buffer. */ |
| 4701 | /* TODO: use a pool */ |
| 4702 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4703 | if (!qel->tx.crypto.bufs) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4704 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4705 | |
| 4706 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4707 | if (!qel->tx.crypto.bufs[0]) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4708 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4709 | |
| 4710 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4711 | qel->tx.crypto.nb_buf = 1; |
| 4712 | |
| 4713 | qel->tx.crypto.sz = 0; |
| 4714 | qel->tx.crypto.offset = 0; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4715 | /* No CRYPTO data for early data TLS encryption level */ |
| 4716 | if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA) |
| 4717 | qel->cstream = NULL; |
| 4718 | else { |
| 4719 | qel->cstream = quic_cstream_new(qc); |
| 4720 | if (!qel->cstream) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4721 | goto leave; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4722 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4723 | |
| 4724 | ret = 1; |
| 4725 | leave: |
| 4726 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4727 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4728 | } |
| 4729 | |
| 4730 | /* Callback called upon loss detection and PTO timer expirations. */ |
| 4731 | struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state) |
| 4732 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4733 | struct quic_conn *qc = ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4734 | struct quic_pktns *pktns; |
| 4735 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4736 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
| 4737 | NULL, NULL, &qc->path->ifae_pkts); |
| 4738 | task->expire = TICK_ETERNITY; |
| 4739 | pktns = quic_loss_pktns(qc); |
| 4740 | if (tick_isset(pktns->tx.loss_time)) { |
| 4741 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4742 | |
| 4743 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 4744 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4745 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 4746 | if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms)) |
| 4747 | qc_set_timer(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4748 | goto out; |
| 4749 | } |
| 4750 | |
| 4751 | if (qc->path->in_flight) { |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 4752 | pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL); |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 4753 | if (qc->subs && qc->subs->events & SUB_RETRY_SEND) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4754 | pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS; |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 4755 | tasklet_wakeup(qc->subs->tasklet); |
| 4756 | qc->subs->events &= ~SUB_RETRY_SEND; |
| 4757 | if (!qc->subs->events) |
| 4758 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4759 | } |
| 4760 | else { |
| 4761 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4762 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4763 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 4764 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4765 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) { |
| 4766 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4767 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4768 | } |
| 4769 | } |
| 4770 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) { |
| 4771 | 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] | 4772 | if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) { |
| 4773 | qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4774 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4775 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4776 | } |
| 4777 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) { |
| 4778 | TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4779 | } |
| 4780 | } |
| 4781 | } |
| 4782 | else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) { |
| 4783 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4784 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4785 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4786 | if (quic_tls_has_tx_sec(hel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4787 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4788 | if (quic_tls_has_tx_sec(iel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4789 | iel->pktns->tx.pto_probe = 1; |
| 4790 | } |
| 4791 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4792 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4793 | qc->path->loss.pto_count++; |
| 4794 | |
| 4795 | out: |
| 4796 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
| 4797 | |
| 4798 | return task; |
| 4799 | } |
| 4800 | |
| 4801 | /* Parse the Retry token from buffer <token> with <end> a pointer to |
| 4802 | * one byte past the end of this buffer. This will extract the ODCID |
| 4803 | * which will be stored into <odcid> |
| 4804 | * |
| 4805 | * Returns 0 on success else non-zero. |
| 4806 | */ |
| 4807 | static int parse_retry_token(struct quic_conn *qc, |
| 4808 | const unsigned char *token, const unsigned char *end, |
| 4809 | struct quic_cid *odcid) |
| 4810 | { |
| 4811 | int ret = 0; |
| 4812 | uint64_t odcid_len; |
| 4813 | uint32_t timestamp; |
| 4814 | |
| 4815 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 4816 | |
| 4817 | if (!quic_dec_int(&odcid_len, &token, end)) { |
| 4818 | TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc); |
| 4819 | goto leave; |
| 4820 | } |
| 4821 | |
| 4822 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 4823 | * When an Initial packet is sent by a client that has not previously |
| 4824 | * received an Initial or Retry packet from the server, the client |
| 4825 | * populates the Destination Connection ID field with an unpredictable |
| 4826 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 4827 | */ |
| 4828 | if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) { |
| 4829 | TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 4830 | goto leave; |
| 4831 | } |
| 4832 | |
| 4833 | if (end - token < odcid_len + sizeof timestamp) { |
| 4834 | TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 4835 | goto leave; |
| 4836 | } |
| 4837 | |
| 4838 | timestamp = ntohl(read_u32(token + odcid_len)); |
| 4839 | if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) { |
| 4840 | TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc); |
| 4841 | goto leave; |
| 4842 | } |
| 4843 | |
| 4844 | ret = 1; |
| 4845 | memcpy(odcid->data, token, odcid_len); |
| 4846 | odcid->len = odcid_len; |
| 4847 | leave: |
| 4848 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 4849 | return !ret; |
| 4850 | } |
| 4851 | |
| 4852 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 4853 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 4854 | * for QUIC servers (or haproxy listeners). |
| 4855 | * <dcid> is the destination connection ID, <scid> is the source connection ID, |
| 4856 | * <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] | 4857 | * length. Endpoints addresses are specified via <local_addr> and <peer_addr>. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4858 | * Returns the connection if succeeded, NULL if not. |
| 4859 | */ |
| 4860 | static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4, |
| 4861 | struct quic_cid *dcid, struct quic_cid *scid, |
| 4862 | const struct quic_cid *token_odcid, |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 4863 | struct sockaddr_storage *local_addr, |
| 4864 | struct sockaddr_storage *peer_addr, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4865 | int server, int token, void *owner) |
| 4866 | { |
| 4867 | int i; |
| 4868 | struct quic_conn *qc; |
| 4869 | /* Initial CID. */ |
| 4870 | struct quic_connection_id *icid; |
| 4871 | char *buf_area = NULL; |
| 4872 | struct listener *l = NULL; |
| 4873 | struct quic_cc_algo *cc_algo = NULL; |
| 4874 | struct quic_tls_ctx *ictx; |
| 4875 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4876 | /* TODO replace pool_zalloc by pool_alloc(). This requires special care |
| 4877 | * to properly initialized internal quic_conn members to safely use |
| 4878 | * quic_conn_release() on alloc failure. |
| 4879 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4880 | qc = pool_zalloc(pool_head_quic_conn); |
| 4881 | if (!qc) { |
| 4882 | TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 4883 | goto err; |
| 4884 | } |
| 4885 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4886 | /* Initialize in priority qc members required for a safe dealloc. */ |
| 4887 | |
| 4888 | /* required to use MTLIST_IN_LIST */ |
| 4889 | MT_LIST_INIT(&qc->accept_list); |
| 4890 | |
| 4891 | LIST_INIT(&qc->rx.pkt_list); |
| 4892 | |
Amaury Denoyelle | 4244833 | 2022-12-12 11:24:05 +0100 | [diff] [blame] | 4893 | qc_init_fd(qc); |
| 4894 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 4895 | LIST_INIT(&qc->back_refs); |
| 4896 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4897 | /* Now proceeds to allocation of qc members. */ |
| 4898 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4899 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 4900 | if (!buf_area) { |
| 4901 | TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc); |
| 4902 | goto err; |
| 4903 | } |
| 4904 | |
| 4905 | qc->cids = EB_ROOT; |
| 4906 | /* QUIC Server (or listener). */ |
| 4907 | if (server) { |
| 4908 | struct proxy *prx; |
| 4909 | |
| 4910 | l = owner; |
| 4911 | prx = l->bind_conf->frontend; |
| 4912 | cc_algo = l->bind_conf->quic_cc_algo; |
| 4913 | |
| 4914 | qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 4915 | &quic_stats_module); |
| 4916 | qc->flags |= QUIC_FL_CONN_LISTENER; |
| 4917 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
| 4918 | /* Copy the initial DCID with the address. */ |
| 4919 | qc->odcid.len = dcid->len; |
| 4920 | qc->odcid.addrlen = dcid->addrlen; |
| 4921 | memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen); |
| 4922 | |
| 4923 | /* copy the packet SCID to reuse it as DCID for sending */ |
| 4924 | if (scid->len) |
| 4925 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 4926 | qc->dcid.len = scid->len; |
| 4927 | qc->tx.buf = BUF_NULL; |
| 4928 | qc->li = l; |
| 4929 | } |
| 4930 | /* QUIC Client (outgoing connection to servers) */ |
| 4931 | else { |
| 4932 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
| 4933 | if (dcid->len) |
| 4934 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 4935 | qc->dcid.len = dcid->len; |
| 4936 | } |
| 4937 | qc->mux_state = QC_MUX_NULL; |
| 4938 | qc->err = quic_err_transport(QC_ERR_NO_ERROR); |
| 4939 | |
| 4940 | icid = new_quic_cid(&qc->cids, qc, 0); |
| 4941 | if (!icid) { |
| 4942 | TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc); |
| 4943 | goto err; |
| 4944 | } |
| 4945 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 4946 | if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) && |
| 4947 | is_addr(local_addr)) { |
| 4948 | TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc); |
| 4949 | qc_alloc_fd(qc, local_addr, peer_addr); |
| 4950 | } |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 4951 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4952 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4953 | if (server) |
| 4954 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
| 4955 | |
| 4956 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4957 | qc->scid = icid->cid; |
| 4958 | |
| 4959 | /* Packet number spaces initialization. */ |
| 4960 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4961 | quic_pktns_init(&qc->pktns[i]); |
| 4962 | /* QUIC encryption level context initialization. */ |
| 4963 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 4964 | if (!quic_conn_enc_level_init(qc, i)) { |
| 4965 | TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc); |
| 4966 | goto err; |
| 4967 | } |
| 4968 | /* Initialize the packet number space. */ |
| 4969 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4970 | } |
| 4971 | |
| 4972 | qc->original_version = qv; |
| 4973 | qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ? |
| 4974 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4975 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
| 4976 | /* TX part. */ |
| 4977 | LIST_INIT(&qc->tx.frms_to_send); |
| 4978 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4979 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4980 | qc->tx.bytes = 0; |
| 4981 | qc->tx.buf = BUF_NULL; |
| 4982 | /* RX part. */ |
| 4983 | qc->rx.bytes = 0; |
| 4984 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 4985 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 4986 | qc->rx.strms[i].nb_streams = 0; |
| 4987 | |
| 4988 | qc->nb_pkt_for_cc = 1; |
| 4989 | qc->nb_pkt_since_cc = 0; |
| 4990 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4991 | if (!quic_tls_ku_init(qc)) { |
| 4992 | TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc); |
| 4993 | goto err; |
| 4994 | } |
| 4995 | |
| 4996 | /* XXX TO DO: Only one path at this time. */ |
| 4997 | qc->path = &qc->paths[0]; |
| 4998 | quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc); |
| 4999 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5000 | qc->streams_by_id = EB_ROOT_UNIQUE; |
| 5001 | qc->stream_buf_count = 0; |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 5002 | memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr)); |
| 5003 | memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5004 | |
| 5005 | if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params, |
| 5006 | icid->stateless_reset_token, |
| 5007 | dcid->data, dcid->len, |
| 5008 | qc->scid.data, qc->scid.len, token_odcid)) |
| 5009 | goto err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5010 | |
| 5011 | qc->wait_event.tasklet = tasklet_new(); |
| 5012 | if (!qc->wait_event.tasklet) { |
| 5013 | TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT); |
| 5014 | goto err; |
| 5015 | } |
| 5016 | qc->wait_event.tasklet->process = quic_conn_io_cb; |
| 5017 | qc->wait_event.tasklet->context = qc; |
| 5018 | qc->wait_event.events = 0; |
| 5019 | /* Set tasklet tid based on the SCID selected by us for this |
| 5020 | * connection. The upper layer will also be binded on the same thread. |
| 5021 | */ |
Willy Tarreau | eed7826 | 2022-12-21 09:09:19 +0100 | [diff] [blame] | 5022 | qc->tid = quic_get_cid_tid(qc->scid.data, &l->rx); |
Willy Tarreau | f5a0c8a | 2022-10-13 16:14:11 +0200 | [diff] [blame] | 5023 | qc->wait_event.tasklet->tid = qc->tid; |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 5024 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5025 | |
| 5026 | if (qc_conn_alloc_ssl_ctx(qc) || |
| 5027 | !quic_conn_init_timer(qc) || |
| 5028 | !quic_conn_init_idle_timer_task(qc)) |
| 5029 | goto err; |
| 5030 | |
| 5031 | ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx; |
| 5032 | if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1)) |
| 5033 | goto err; |
| 5034 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5035 | LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx); |
| 5036 | qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch); |
| 5037 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5038 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
| 5039 | |
| 5040 | return qc; |
| 5041 | |
| 5042 | err: |
| 5043 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5044 | if (qc) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5045 | qc->rx.buf.area = NULL; |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5046 | quic_conn_release(qc); |
| 5047 | } |
| 5048 | TRACE_LEAVE(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5049 | return NULL; |
| 5050 | } |
| 5051 | |
| 5052 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 5053 | * The connection tasklet is killed. |
| 5054 | * |
| 5055 | * This function must only be called by the thread responsible of the quic_conn |
| 5056 | * tasklet. |
| 5057 | */ |
| 5058 | void quic_conn_release(struct quic_conn *qc) |
| 5059 | { |
| 5060 | int i; |
| 5061 | struct ssl_sock_ctx *conn_ctx; |
| 5062 | struct eb64_node *node; |
| 5063 | struct quic_tls_ctx *app_tls_ctx; |
| 5064 | struct quic_rx_packet *pkt, *pktback; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5065 | struct bref *bref, *back; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5066 | |
| 5067 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 5068 | |
| 5069 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 5070 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 5071 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5072 | /* Close quic-conn socket fd. */ |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 5073 | qc_release_fd(qc, 0); |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5074 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5075 | /* in the unlikely (but possible) case the connection was just added to |
| 5076 | * the accept_list we must delete it from there. |
| 5077 | */ |
| 5078 | MT_LIST_DELETE(&qc->accept_list); |
| 5079 | |
| 5080 | /* free remaining stream descriptors */ |
| 5081 | node = eb64_first(&qc->streams_by_id); |
| 5082 | while (node) { |
| 5083 | struct qc_stream_desc *stream; |
| 5084 | |
| 5085 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 5086 | node = eb64_next(node); |
| 5087 | |
| 5088 | /* all streams attached to the quic-conn are released, so |
| 5089 | * qc_stream_desc_free will liberate the stream instance. |
| 5090 | */ |
| 5091 | BUG_ON(!stream->release); |
| 5092 | qc_stream_desc_free(stream, 1); |
| 5093 | } |
| 5094 | |
| 5095 | /* Purge Rx packet list. */ |
| 5096 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 5097 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 5098 | pool_free(pool_head_quic_rx_packet, pkt); |
| 5099 | } |
| 5100 | |
| 5101 | if (qc->idle_timer_task) { |
| 5102 | task_destroy(qc->idle_timer_task); |
| 5103 | qc->idle_timer_task = NULL; |
| 5104 | } |
| 5105 | |
| 5106 | if (qc->timer_task) { |
| 5107 | task_destroy(qc->timer_task); |
| 5108 | qc->timer_task = NULL; |
| 5109 | } |
| 5110 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5111 | if (qc->wait_event.tasklet) |
| 5112 | tasklet_free(qc->wait_event.tasklet); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5113 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5114 | /* remove the connection from receiver cids trees */ |
| 5115 | ebmb_delete(&qc->odcid_node); |
| 5116 | ebmb_delete(&qc->scid_node); |
| 5117 | free_quic_conn_cids(qc); |
| 5118 | |
| 5119 | conn_ctx = qc->xprt_ctx; |
| 5120 | if (conn_ctx) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5121 | SSL_free(conn_ctx->ssl); |
| 5122 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
| 5123 | } |
| 5124 | |
| 5125 | quic_tls_ku_free(qc); |
| 5126 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 5127 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
| 5128 | quic_conn_enc_level_uninit(qc, &qc->els[i]); |
| 5129 | } |
| 5130 | quic_tls_ctx_secs_free(&qc->negotiated_ictx); |
| 5131 | |
| 5132 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 5133 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 5134 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
| 5135 | |
| 5136 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 5137 | quic_pktns_tx_pkts_release(&qc->pktns[i], qc); |
| 5138 | quic_free_arngs(qc, &qc->pktns[i].rx.arngs); |
| 5139 | } |
| 5140 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5141 | /* Detach CLI context watchers currently dumping this connection. |
| 5142 | * Reattach them to the next quic_conn instance. |
| 5143 | */ |
| 5144 | list_for_each_entry_safe(bref, back, &qc->back_refs, users) { |
| 5145 | /* Remove watcher from this quic_conn instance. */ |
| 5146 | LIST_DEL_INIT(&bref->users); |
| 5147 | |
| 5148 | /* Attach it to next instance unless it was the last list element. */ |
| 5149 | if (qc->el_th_ctx.n != &th_ctx->quic_conns) { |
| 5150 | struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx, |
| 5151 | struct quic_conn *, |
| 5152 | el_th_ctx); |
| 5153 | LIST_APPEND(&next->back_refs, &bref->users); |
| 5154 | } |
| 5155 | bref->ref = qc->el_th_ctx.n; |
| 5156 | __ha_barrier_store(); |
| 5157 | } |
| 5158 | /* Remove quic_conn from global ha_thread_ctx list. */ |
| 5159 | LIST_DELETE(&qc->el_th_ctx); |
| 5160 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5161 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 5162 | pool_free(pool_head_quic_conn, qc); |
| 5163 | TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc); |
| 5164 | |
| 5165 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 5166 | } |
| 5167 | |
| 5168 | /* Initialize the timer task of <qc> QUIC connection. |
| 5169 | * Returns 1 if succeeded, 0 if not. |
| 5170 | */ |
| 5171 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 5172 | { |
| 5173 | int ret = 0; |
| 5174 | /* Attach this task to the same thread ID used for the connection */ |
| 5175 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5176 | |
| 5177 | qc->timer_task = task_new_on(qc->tid); |
| 5178 | if (!qc->timer_task) { |
| 5179 | TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 5180 | goto leave; |
| 5181 | } |
| 5182 | |
| 5183 | qc->timer = TICK_ETERNITY; |
| 5184 | qc->timer_task->process = qc_process_timer; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5185 | qc->timer_task->context = qc; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5186 | |
| 5187 | ret = 1; |
| 5188 | leave: |
| 5189 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5190 | return ret; |
| 5191 | } |
| 5192 | |
| 5193 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 5194 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 5195 | { |
| 5196 | unsigned int expire; |
| 5197 | |
| 5198 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 5199 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 5200 | } |
| 5201 | |
| 5202 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 5203 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 5204 | */ |
| 5205 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 5206 | { |
| 5207 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5208 | |
| 5209 | if (read) { |
| 5210 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 5211 | } |
| 5212 | else { |
| 5213 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 5214 | } |
| 5215 | qc_idle_timer_do_rearm(qc); |
| 5216 | |
| 5217 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5218 | } |
| 5219 | |
| 5220 | /* The task handling the idle timeout */ |
| 5221 | struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 5222 | { |
| 5223 | struct quic_conn *qc = ctx; |
| 5224 | struct quic_counters *prx_counters = qc->prx_counters; |
| 5225 | unsigned int qc_flags = qc->flags; |
| 5226 | |
| 5227 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5228 | |
| 5229 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 5230 | * might free the quic-conn too early via quic_close(). |
| 5231 | */ |
| 5232 | qc_notify_close(qc); |
| 5233 | |
| 5234 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 5235 | * responsible to call quic_close to release it. |
| 5236 | */ |
| 5237 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 5238 | if (qc->mux_state != QC_MUX_READY) |
| 5239 | quic_conn_release(qc); |
| 5240 | |
| 5241 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 5242 | * least clean some parts of it such as the tasklet. |
| 5243 | */ |
| 5244 | |
| 5245 | if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 5246 | qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 5247 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
| 5248 | HA_ATOMIC_DEC(&prx_counters->half_open_conn); |
| 5249 | } |
| 5250 | |
| 5251 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5252 | return NULL; |
| 5253 | } |
| 5254 | |
| 5255 | /* Initialize the idle timeout task for <qc>. |
| 5256 | * Returns 1 if succeeded, 0 if not. |
| 5257 | */ |
| 5258 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 5259 | { |
| 5260 | int ret = 0; |
| 5261 | |
| 5262 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5263 | |
| 5264 | qc->idle_timer_task = task_new_here(); |
| 5265 | if (!qc->idle_timer_task) { |
| 5266 | TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 5267 | goto leave; |
| 5268 | } |
| 5269 | |
| 5270 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 5271 | qc->idle_timer_task->context = qc; |
| 5272 | qc_idle_timer_rearm(qc, 1); |
| 5273 | task_queue(qc->idle_timer_task); |
| 5274 | |
| 5275 | ret = 1; |
| 5276 | leave: |
| 5277 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5278 | return ret; |
| 5279 | } |
| 5280 | |
| 5281 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 5282 | * past one byte of this buffer. |
| 5283 | */ |
| 5284 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 5285 | struct quic_rx_packet *pkt) |
| 5286 | { |
| 5287 | int ret = 0; |
| 5288 | unsigned char dcid_len, scid_len; |
| 5289 | |
| 5290 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5291 | |
| 5292 | if (end == *buf) { |
| 5293 | TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT); |
| 5294 | goto leave; |
| 5295 | } |
| 5296 | |
| 5297 | /* Destination Connection ID Length */ |
| 5298 | dcid_len = *(*buf)++; |
| 5299 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 5300 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) { |
| 5301 | TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT); |
| 5302 | goto leave; |
| 5303 | } |
| 5304 | |
| 5305 | if (dcid_len) { |
| 5306 | /* Check that the length of this received DCID matches the CID lengths |
| 5307 | * of our implementation for non Initials packets only. |
| 5308 | */ |
| 5309 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 5310 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
| 5311 | dcid_len != QUIC_HAP_CID_LEN) { |
| 5312 | TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT); |
| 5313 | goto leave; |
| 5314 | } |
| 5315 | |
| 5316 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 5317 | } |
| 5318 | |
| 5319 | pkt->dcid.len = dcid_len; |
| 5320 | *buf += dcid_len; |
| 5321 | |
| 5322 | /* Source Connection ID Length */ |
| 5323 | scid_len = *(*buf)++; |
| 5324 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) { |
| 5325 | TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT); |
| 5326 | goto leave; |
| 5327 | } |
| 5328 | |
| 5329 | if (scid_len) |
| 5330 | memcpy(pkt->scid.data, *buf, scid_len); |
| 5331 | pkt->scid.len = scid_len; |
| 5332 | *buf += scid_len; |
| 5333 | |
| 5334 | ret = 1; |
| 5335 | leave: |
| 5336 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 5337 | return ret; |
| 5338 | } |
| 5339 | |
| 5340 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 5341 | static void qc_pkt_insert(struct quic_conn *qc, |
| 5342 | struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 5343 | { |
| 5344 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 5345 | |
| 5346 | pkt->pn_node.key = pkt->pn; |
| 5347 | quic_rx_packet_refinc(pkt); |
| 5348 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 5349 | |
| 5350 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 5351 | } |
| 5352 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5353 | /* Try to remove the header protection of <pkt> QUIC packet with <beg> the |
| 5354 | * address of the packet first byte, using the keys from encryption level <el>. |
| 5355 | * |
| 5356 | * If header protection has been successfully removed, packet data are copied |
| 5357 | * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also |
| 5358 | * proceeded, and the packet is inserted into <qc> protected packets tree. In |
| 5359 | * both cases, packet can now be considered handled by the <qc> connection. |
| 5360 | * |
| 5361 | * If header protection cannot be removed due to <el> secrets already |
| 5362 | * discarded, no operation is conducted. |
| 5363 | * |
| 5364 | * Returns 1 on success : packet data is now handled by the connection. On |
| 5365 | * error 0 is returned : packet should be dropped by the caller. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5366 | */ |
| 5367 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 5368 | struct quic_rx_packet *pkt, |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5369 | unsigned char *beg, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5370 | struct quic_enc_level **el) |
| 5371 | { |
| 5372 | int ret = 0; |
| 5373 | unsigned char *pn = NULL; /* Packet number field */ |
| 5374 | enum quic_tls_enc_level tel; |
| 5375 | struct quic_enc_level *qel; |
| 5376 | /* Only for traces. */ |
| 5377 | struct quic_rx_packet *qpkt_trace; |
| 5378 | |
| 5379 | qpkt_trace = NULL; |
| 5380 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5381 | BUG_ON(!pkt->pn_offset); |
| 5382 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5383 | /* The packet number is here. This is also the start minus |
| 5384 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 5385 | * protection. |
| 5386 | */ |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5387 | pn = beg + pkt->pn_offset; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5388 | |
| 5389 | tel = quic_packet_type_enc_level(pkt->type); |
| 5390 | qel = &qc->els[tel]; |
| 5391 | |
| 5392 | if (qc_qel_may_rm_hp(qc, qel)) { |
| 5393 | /* Note that the following function enables us to unprotect the packet |
| 5394 | * number and its length subsequently used to decrypt the entire |
| 5395 | * packets. |
| 5396 | */ |
| 5397 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 5398 | qel->pktns->rx.largest_pn, pn, beg)) { |
| 5399 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
| 5400 | goto out; |
| 5401 | } |
| 5402 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5403 | /* The AAD includes the packet number field. */ |
| 5404 | pkt->aad_len = pkt->pn_offset + pkt->pnl; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5405 | if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) { |
| 5406 | TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc); |
| 5407 | goto out; |
| 5408 | } |
| 5409 | |
| 5410 | qpkt_trace = pkt; |
| 5411 | } |
| 5412 | else { |
| 5413 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 5414 | /* If the packet number space has been discarded, this packet |
| 5415 | * will be not parsed. |
| 5416 | */ |
| 5417 | TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt); |
| 5418 | goto out; |
| 5419 | } |
| 5420 | |
| 5421 | TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5422 | LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 5423 | quic_rx_packet_refinc(pkt); |
| 5424 | } |
| 5425 | |
| 5426 | *el = qel; |
| 5427 | /* No reference counter incrementation here!!! */ |
| 5428 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 5429 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 5430 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 5431 | b_add(&qc->rx.buf, pkt->len); |
| 5432 | |
| 5433 | ret = 1; |
| 5434 | out: |
| 5435 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
| 5436 | return ret; |
| 5437 | } |
| 5438 | |
| 5439 | /* Parse the header form from <byte0> first byte of <pkt> packet to set its type. |
| 5440 | * Also set <*long_header> to 1 if this form is long, 0 if not and the version |
| 5441 | * of this packet into <*version>. |
| 5442 | */ |
| 5443 | static inline int qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 5444 | unsigned char **buf, const unsigned char *end, |
| 5445 | int *long_header, uint32_t *version) |
| 5446 | { |
| 5447 | int ret = 0; |
| 5448 | const unsigned char byte0 = **buf; |
| 5449 | |
| 5450 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5451 | |
| 5452 | (*buf)++; |
| 5453 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 5454 | unsigned char type = |
| 5455 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 5456 | |
| 5457 | *long_header = 1; |
| 5458 | /* Version */ |
| 5459 | if (!quic_read_uint32(version, (const unsigned char **)buf, end)) { |
| 5460 | TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT); |
| 5461 | goto out; |
| 5462 | } |
| 5463 | |
Frédéric Lécaille | 21c4c9b | 2023-01-13 16:37:02 +0100 | [diff] [blame] | 5464 | if (*version != QUIC_PROTOCOL_VERSION_2) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5465 | pkt->type = type; |
| 5466 | } |
| 5467 | else { |
| 5468 | switch (type) { |
| 5469 | case 0: |
| 5470 | pkt->type = QUIC_PACKET_TYPE_RETRY; |
| 5471 | break; |
| 5472 | case 1: |
| 5473 | pkt->type = QUIC_PACKET_TYPE_INITIAL; |
| 5474 | break; |
| 5475 | case 2: |
| 5476 | pkt->type = QUIC_PACKET_TYPE_0RTT; |
| 5477 | break; |
| 5478 | case 3: |
| 5479 | pkt->type = QUIC_PACKET_TYPE_HANDSHAKE; |
| 5480 | break; |
| 5481 | } |
| 5482 | } |
| 5483 | } |
| 5484 | else { |
| 5485 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 5486 | *long_header = 0; |
| 5487 | } |
| 5488 | |
| 5489 | ret = 1; |
| 5490 | out: |
| 5491 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 5492 | return ret; |
| 5493 | } |
| 5494 | |
| 5495 | /* Return the QUIC version (quic_version struct) with <version> as version number |
| 5496 | * if supported or NULL if not. |
| 5497 | */ |
| 5498 | static inline const struct quic_version *qc_supported_version(uint32_t version) |
| 5499 | { |
| 5500 | int i; |
| 5501 | |
| 5502 | for (i = 0; i < quic_versions_nb; i++) |
| 5503 | if (quic_versions[i].num == version) |
| 5504 | return &quic_versions[i]; |
| 5505 | |
| 5506 | return NULL; |
| 5507 | } |
| 5508 | |
| 5509 | /* |
| 5510 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 5511 | * address <addr>. |
| 5512 | * Implementation of RFC9000 6. Version Negotiation |
| 5513 | * |
| 5514 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 5515 | * |
| 5516 | * Returns 0 on success else non-zero |
| 5517 | */ |
| 5518 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 5519 | struct quic_rx_packet *pkt) |
| 5520 | { |
| 5521 | char buf[256]; |
| 5522 | int ret = 0, i = 0, j; |
| 5523 | uint32_t version; |
| 5524 | const socklen_t addrlen = get_addr_len(addr); |
| 5525 | |
| 5526 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5527 | /* |
| 5528 | * header form |
| 5529 | * long header, fixed bit to 0 for Version Negotiation |
| 5530 | */ |
| 5531 | /* TODO: RAND_bytes() should be replaced? */ |
| 5532 | if (RAND_bytes((unsigned char *)buf, 1) != 1) { |
| 5533 | TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT); |
| 5534 | goto out; |
| 5535 | } |
| 5536 | |
| 5537 | buf[i++] |= '\x80'; |
| 5538 | /* null version for Version Negotiation */ |
| 5539 | buf[i++] = '\x00'; |
| 5540 | buf[i++] = '\x00'; |
| 5541 | buf[i++] = '\x00'; |
| 5542 | buf[i++] = '\x00'; |
| 5543 | |
| 5544 | /* source connection id */ |
| 5545 | buf[i++] = pkt->scid.len; |
| 5546 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5547 | i += pkt->scid.len; |
| 5548 | |
| 5549 | /* destination connection id */ |
| 5550 | buf[i++] = pkt->dcid.len; |
| 5551 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 5552 | i += pkt->dcid.len; |
| 5553 | |
| 5554 | /* supported version */ |
| 5555 | for (j = 0; j < quic_versions_nb; j++) { |
| 5556 | version = htonl(quic_versions[j].num); |
| 5557 | memcpy(&buf[i], &version, sizeof(version)); |
| 5558 | i += sizeof(version); |
| 5559 | } |
| 5560 | |
| 5561 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 5562 | goto out; |
| 5563 | |
| 5564 | ret = 1; |
| 5565 | out: |
| 5566 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5567 | return !ret; |
| 5568 | } |
| 5569 | |
| 5570 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 5571 | * from <fd> UDP socket to <dst> |
| 5572 | * Return 1 if succeeded, 0 if not. |
| 5573 | */ |
| 5574 | static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr, |
| 5575 | struct quic_rx_packet *rxpkt) |
| 5576 | { |
| 5577 | int ret = 0, pktlen, rndlen; |
| 5578 | unsigned char pkt[64]; |
| 5579 | const socklen_t addrlen = get_addr_len(dstaddr); |
| 5580 | struct proxy *prx; |
| 5581 | struct quic_counters *prx_counters; |
| 5582 | |
| 5583 | TRACE_ENTER(QUIC_EV_STATELESS_RST); |
| 5584 | |
| 5585 | prx = l->bind_conf->frontend; |
| 5586 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 5587 | /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3) |
| 5588 | * The resulting minimum size of 21 bytes does not guarantee that a Stateless |
| 5589 | * Reset is difficult to distinguish from other packets if the recipient requires |
| 5590 | * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure |
| 5591 | * that all packets it sends are at least 22 bytes longer than the minimum |
| 5592 | * connection ID length that it requests the peer to include in its packets, |
| 5593 | * adding PADDING frames as necessary. This ensures that any Stateless Reset |
| 5594 | * sent by the peer is indistinguishable from a valid packet sent to the endpoint. |
| 5595 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 5596 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 5597 | * than the packet it responds to. |
| 5598 | */ |
| 5599 | |
| 5600 | /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */ |
| 5601 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0; |
| 5602 | pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen); |
| 5603 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
| 5604 | |
| 5605 | /* Put a header of random bytes */ |
| 5606 | /* TODO: RAND_bytes() should be replaced */ |
| 5607 | if (RAND_bytes(pkt, rndlen) != 1) { |
| 5608 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST); |
| 5609 | goto leave; |
| 5610 | } |
| 5611 | |
| 5612 | /* Clear the most significant bit, and set the second one */ |
| 5613 | *pkt = (*pkt & ~0x80) | 0x40; |
| 5614 | if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN, |
| 5615 | rxpkt->dcid.data, rxpkt->dcid.len)) |
| 5616 | goto leave; |
| 5617 | |
| 5618 | if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0) |
| 5619 | goto leave; |
| 5620 | |
| 5621 | ret = 1; |
| 5622 | HA_ATOMIC_INC(&prx_counters->stateless_reset_sent); |
| 5623 | TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid); |
| 5624 | leave: |
| 5625 | TRACE_LEAVE(QUIC_EV_STATELESS_RST); |
| 5626 | return ret; |
| 5627 | } |
| 5628 | |
| 5629 | /* QUIC server only function. |
| 5630 | * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address. |
| 5631 | * This is the responsibility of the caller to check <aad> size is big enough |
| 5632 | * to contain these data. |
| 5633 | * Return the number of bytes copied to <aad>. |
| 5634 | */ |
| 5635 | static int quic_generate_retry_token_aad(unsigned char *aad, |
| 5636 | uint32_t version, |
| 5637 | const struct quic_cid *cid, |
| 5638 | const struct sockaddr_storage *addr) |
| 5639 | { |
| 5640 | unsigned char *p; |
| 5641 | |
| 5642 | p = aad; |
| 5643 | memcpy(p, &version, sizeof version); |
| 5644 | p += sizeof version; |
| 5645 | p += quic_saddr_cpy(p, addr); |
| 5646 | memcpy(p, cid->data, cid->len); |
| 5647 | p += cid->len; |
| 5648 | |
| 5649 | return p - aad; |
| 5650 | } |
| 5651 | |
| 5652 | /* QUIC server only function. |
| 5653 | * 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] | 5654 | * <buf> with <len> as length. <odcid> is the original destination connection |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5655 | * ID and <dcid> is our side destination connection ID (or client source |
| 5656 | * connection ID). |
| 5657 | * Returns the length of the encoded token or 0 on error. |
| 5658 | */ |
| 5659 | static int quic_generate_retry_token(unsigned char *buf, size_t len, |
| 5660 | const uint32_t version, |
| 5661 | const struct quic_cid *odcid, |
| 5662 | const struct quic_cid *dcid, |
| 5663 | struct sockaddr_storage *addr) |
| 5664 | { |
| 5665 | int ret = 0; |
| 5666 | unsigned char *p; |
| 5667 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
Amaury Denoyelle | 6c94056 | 2022-10-18 11:05:02 +0200 | [diff] [blame] | 5668 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5669 | size_t aadlen; |
| 5670 | unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN]; |
| 5671 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5672 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5673 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5674 | size_t seclen = strlen(global.cluster_secret); |
| 5675 | EVP_CIPHER_CTX *ctx = NULL; |
| 5676 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 5677 | uint32_t timestamp = now_ms; |
| 5678 | |
| 5679 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5680 | |
| 5681 | /* We copy the odcid into the token, prefixed by its one byte |
| 5682 | * length, the format token byte. It is followed by an AEAD TAG, and finally |
| 5683 | * the random bytes used to derive the secret to encrypt the token. |
| 5684 | */ |
| 5685 | if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len) |
| 5686 | goto err; |
| 5687 | |
| 5688 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
| 5689 | /* TODO: RAND_bytes() should be replaced */ |
| 5690 | if (RAND_bytes(salt, sizeof salt) != 1) { |
| 5691 | TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT); |
| 5692 | goto err; |
| 5693 | } |
| 5694 | |
| 5695 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 5696 | salt, sizeof salt, sec, seclen)) { |
| 5697 | TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT); |
| 5698 | goto err; |
| 5699 | } |
| 5700 | |
| 5701 | if (!quic_tls_tx_ctx_init(&ctx, aead, key)) { |
| 5702 | TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT); |
| 5703 | goto err; |
| 5704 | } |
| 5705 | |
| 5706 | /* Token build */ |
| 5707 | p = buf; |
| 5708 | *p++ = QUIC_TOKEN_FMT_RETRY, |
| 5709 | *p++ = odcid->len; |
| 5710 | memcpy(p, odcid->data, odcid->len); |
| 5711 | p += odcid->len; |
| 5712 | write_u32(p, htonl(timestamp)); |
| 5713 | p += sizeof timestamp; |
| 5714 | |
| 5715 | /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 5716 | if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) { |
| 5717 | TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT); |
| 5718 | goto err; |
| 5719 | } |
| 5720 | |
| 5721 | p += QUIC_TLS_TAG_LEN; |
| 5722 | memcpy(p, salt, sizeof salt); |
| 5723 | p += sizeof salt; |
| 5724 | EVP_CIPHER_CTX_free(ctx); |
| 5725 | |
| 5726 | ret = p - buf; |
| 5727 | leave: |
| 5728 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5729 | return ret; |
| 5730 | |
| 5731 | err: |
| 5732 | if (ctx) |
| 5733 | EVP_CIPHER_CTX_free(ctx); |
| 5734 | goto leave; |
| 5735 | } |
| 5736 | |
| 5737 | /* QUIC server only function. |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5738 | * |
| 5739 | * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is |
| 5740 | * the UDP datagram containing <pkt> and <l> is the listener instance on which |
| 5741 | * it was received. If the token is valid, the ODCID of <qc> QUIC connection |
| 5742 | * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed |
| 5743 | * to validate the token but it can be NULL : in this case the version will be |
| 5744 | * retrieved from the packet. |
| 5745 | * |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5746 | * Return 1 if succeeded, 0 if not. |
| 5747 | */ |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5748 | |
| 5749 | static int quic_retry_token_check(struct quic_rx_packet *pkt, |
| 5750 | struct quic_dgram *dgram, |
| 5751 | struct listener *l, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5752 | struct quic_conn *qc, |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5753 | struct quic_cid *odcid) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5754 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5755 | struct proxy *prx; |
| 5756 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5757 | int ret = 0; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5758 | unsigned char *token = pkt->token; |
| 5759 | const uint64_t tokenlen = pkt->token_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5760 | unsigned char buf[128]; |
| 5761 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
Amaury Denoyelle | 6c94056 | 2022-10-18 11:05:02 +0200 | [diff] [blame] | 5762 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5763 | size_t aadlen; |
| 5764 | const unsigned char *salt; |
| 5765 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5766 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5767 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5768 | size_t seclen = strlen(global.cluster_secret); |
| 5769 | EVP_CIPHER_CTX *ctx = NULL; |
| 5770 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5771 | const struct quic_version *qv = qc ? qc->original_version : |
| 5772 | pkt->version; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5773 | |
| 5774 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 5775 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5776 | /* The caller must ensure this. */ |
| 5777 | BUG_ON(!global.cluster_secret || !pkt->token_len); |
| 5778 | |
| 5779 | prx = l->bind_conf->frontend; |
| 5780 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 5781 | |
| 5782 | if (*pkt->token != QUIC_TOKEN_FMT_RETRY) { |
| 5783 | /* TODO: New token check */ |
| 5784 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 5785 | goto leave; |
| 5786 | } |
| 5787 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5788 | if (sizeof buf < tokenlen) { |
| 5789 | TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc); |
| 5790 | goto err; |
| 5791 | } |
| 5792 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5793 | 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] | 5794 | salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN; |
| 5795 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 5796 | salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) { |
| 5797 | TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc); |
| 5798 | goto err; |
| 5799 | } |
| 5800 | |
| 5801 | if (!quic_tls_rx_ctx_init(&ctx, aead, key)) { |
| 5802 | TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc); |
| 5803 | goto err; |
| 5804 | } |
| 5805 | |
| 5806 | /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 5807 | if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen, |
| 5808 | ctx, aead, key, iv)) { |
| 5809 | TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc); |
| 5810 | goto err; |
| 5811 | } |
| 5812 | |
| 5813 | if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) { |
| 5814 | TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 5815 | goto err; |
| 5816 | } |
| 5817 | |
| 5818 | EVP_CIPHER_CTX_free(ctx); |
| 5819 | |
| 5820 | ret = 1; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5821 | HA_ATOMIC_INC(&prx_counters->retry_validated); |
| 5822 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5823 | leave: |
| 5824 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 5825 | return ret; |
| 5826 | |
| 5827 | err: |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5828 | HA_ATOMIC_INC(&prx_counters->retry_error); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5829 | if (ctx) |
| 5830 | EVP_CIPHER_CTX_free(ctx); |
| 5831 | goto leave; |
| 5832 | } |
| 5833 | |
| 5834 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 5835 | * the Initial <pkt> packet. |
| 5836 | * |
| 5837 | * Returns 0 on success else non-zero. |
| 5838 | */ |
| 5839 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 5840 | struct quic_rx_packet *pkt, const struct quic_version *qv) |
| 5841 | { |
| 5842 | int ret = 0; |
| 5843 | unsigned char buf[128]; |
| 5844 | int i = 0, token_len; |
| 5845 | const socklen_t addrlen = get_addr_len(addr); |
| 5846 | struct quic_cid scid; |
| 5847 | |
| 5848 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5849 | |
| 5850 | /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */ |
| 5851 | buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) | |
| 5852 | (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT); |
| 5853 | /* version */ |
| 5854 | buf[i++] = *((unsigned char *)&qv->num + 3); |
| 5855 | buf[i++] = *((unsigned char *)&qv->num + 2); |
| 5856 | buf[i++] = *((unsigned char *)&qv->num + 1); |
| 5857 | buf[i++] = *(unsigned char *)&qv->num; |
| 5858 | |
| 5859 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 5860 | buf[i++] = pkt->scid.len; |
| 5861 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5862 | i += pkt->scid.len; |
| 5863 | |
| 5864 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 5865 | scid.len = QUIC_HAP_CID_LEN; |
| 5866 | /* TODO: RAND_bytes() should be replaced */ |
| 5867 | if (RAND_bytes(scid.data, scid.len) != 1) { |
| 5868 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT); |
| 5869 | goto out; |
| 5870 | } |
| 5871 | |
| 5872 | buf[i++] = scid.len; |
| 5873 | memcpy(&buf[i], scid.data, scid.len); |
| 5874 | i += scid.len; |
| 5875 | |
| 5876 | /* token */ |
| 5877 | if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num, |
| 5878 | &pkt->dcid, &pkt->scid, addr))) { |
| 5879 | TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT); |
| 5880 | goto out; |
| 5881 | } |
| 5882 | |
| 5883 | i += token_len; |
| 5884 | |
| 5885 | /* token integrity tag */ |
| 5886 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 5887 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 5888 | pkt->dcid.len, buf, i, qv)) { |
| 5889 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 5890 | goto out; |
| 5891 | } |
| 5892 | |
| 5893 | i += QUIC_TLS_TAG_LEN; |
| 5894 | |
| 5895 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) { |
| 5896 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 5897 | goto out; |
| 5898 | } |
| 5899 | |
| 5900 | ret = 1; |
| 5901 | out: |
| 5902 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5903 | return !ret; |
| 5904 | } |
| 5905 | |
| 5906 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 5907 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 5908 | * concatenated to the <pkt> DCID field. |
| 5909 | * |
| 5910 | * Returns the instance or NULL if not found. |
| 5911 | */ |
| 5912 | static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt, |
| 5913 | struct listener *l, |
| 5914 | struct sockaddr_storage *saddr) |
| 5915 | { |
| 5916 | struct quic_conn *qc = NULL; |
| 5917 | struct ebmb_node *node; |
| 5918 | struct quic_connection_id *id; |
| 5919 | /* set if the quic_conn is found in the second DCID tree */ |
| 5920 | |
| 5921 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5922 | |
| 5923 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 5924 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 5925 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 5926 | /* DCIDs of first packets coming from multiple clients may have |
| 5927 | * the same values. Let's distinguish them by concatenating the |
| 5928 | * socket addresses. |
| 5929 | */ |
| 5930 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 5931 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
| 5932 | pkt->dcid.len + pkt->dcid.addrlen); |
| 5933 | if (node) { |
| 5934 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 5935 | goto end; |
| 5936 | } |
| 5937 | } |
| 5938 | |
| 5939 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 5940 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 5941 | * used. |
| 5942 | */ |
| 5943 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len); |
| 5944 | if (!node) |
| 5945 | goto end; |
| 5946 | |
| 5947 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 5948 | qc = id->qc; |
| 5949 | |
| 5950 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 5951 | * If already done, this is a noop. |
| 5952 | */ |
| 5953 | if (qc) |
| 5954 | ebmb_delete(&qc->odcid_node); |
| 5955 | |
| 5956 | end: |
| 5957 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 5958 | return qc; |
| 5959 | } |
| 5960 | |
| 5961 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 5962 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 5963 | * parameters of this session. |
| 5964 | * This is the responsibility of the caller to check the validity of all the |
| 5965 | * pointers passed as parameter to this function. |
| 5966 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 5967 | * CO_ER_SSL_NO_MEM. |
| 5968 | */ |
| 5969 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 5970 | unsigned char *params, size_t params_len) |
| 5971 | { |
| 5972 | int retry, ret = -1; |
| 5973 | |
| 5974 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5975 | |
| 5976 | retry = 1; |
| 5977 | retry: |
| 5978 | *ssl = SSL_new(ssl_ctx); |
| 5979 | if (!*ssl) { |
| 5980 | if (!retry--) |
| 5981 | goto err; |
| 5982 | |
| 5983 | pool_gc(NULL); |
| 5984 | goto retry; |
| 5985 | } |
| 5986 | |
| 5987 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 5988 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) { |
| 5989 | SSL_free(*ssl); |
| 5990 | *ssl = NULL; |
| 5991 | if (!retry--) |
| 5992 | goto err; |
| 5993 | |
| 5994 | pool_gc(NULL); |
| 5995 | goto retry; |
| 5996 | } |
| 5997 | |
| 5998 | ret = 0; |
| 5999 | leave: |
| 6000 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 6001 | return ret; |
| 6002 | |
| 6003 | err: |
| 6004 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 6005 | goto leave; |
| 6006 | } |
| 6007 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6008 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 6009 | * used to process <qc> received packets. The allocated context is stored in |
| 6010 | * <qc.xprt_ctx>. |
| 6011 | * |
| 6012 | * Returns 0 on success else non-zero. |
| 6013 | */ |
| 6014 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 6015 | { |
| 6016 | int ret = 0; |
| 6017 | struct bind_conf *bc = qc->li->bind_conf; |
| 6018 | struct ssl_sock_ctx *ctx = NULL; |
| 6019 | |
| 6020 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 6021 | |
| 6022 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 6023 | if (!ctx) { |
| 6024 | TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT); |
| 6025 | goto err; |
| 6026 | } |
| 6027 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6028 | ctx->subs = NULL; |
| 6029 | ctx->xprt_ctx = NULL; |
| 6030 | ctx->qc = qc; |
| 6031 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6032 | if (qc_is_listener(qc)) { |
| 6033 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 6034 | qc->enc_params, qc->enc_params_len) == -1) { |
| 6035 | goto err; |
| 6036 | } |
| 6037 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6038 | /* Enabling 0-RTT */ |
| 6039 | if (bc->ssl_conf.early_data) |
| 6040 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 6041 | #endif |
| 6042 | |
| 6043 | SSL_set_accept_state(ctx->ssl); |
| 6044 | } |
| 6045 | |
| 6046 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 6047 | |
| 6048 | /* Store the allocated context in <qc>. */ |
| 6049 | qc->xprt_ctx = ctx; |
| 6050 | |
| 6051 | ret = 1; |
| 6052 | leave: |
| 6053 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 6054 | return !ret; |
| 6055 | |
| 6056 | err: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6057 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 6058 | goto leave; |
| 6059 | } |
| 6060 | |
| 6061 | /* Check that all the bytes between <buf> included and <end> address |
| 6062 | * excluded are null. This is the responsibility of the caller to |
| 6063 | * check that there is at least one byte between <buf> end <end>. |
| 6064 | * Return 1 if this all the bytes are null, 0 if not. |
| 6065 | */ |
| 6066 | static inline int quic_padding_check(const unsigned char *buf, |
| 6067 | const unsigned char *end) |
| 6068 | { |
| 6069 | while (buf < end && !*buf) |
| 6070 | buf++; |
| 6071 | |
| 6072 | return buf == end; |
| 6073 | } |
| 6074 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6075 | /* Find the associated connection to the packet <pkt> or create a new one if |
| 6076 | * this is an Initial packet. <dgram> is the datagram containing the packet and |
| 6077 | * <l> is the listener instance on which it was received. |
| 6078 | * |
| 6079 | * Returns the quic-conn instance or NULL. |
| 6080 | */ |
| 6081 | static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt, |
| 6082 | struct quic_dgram *dgram, |
| 6083 | struct listener *l) |
| 6084 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6085 | struct quic_cid token_odcid = { .len = 0 }; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6086 | struct quic_conn *qc = NULL; |
| 6087 | struct proxy *prx; |
| 6088 | struct quic_counters *prx_counters; |
| 6089 | |
| 6090 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 6091 | |
| 6092 | prx = l->bind_conf->frontend; |
| 6093 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6094 | |
| 6095 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
| 6096 | |
| 6097 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 6098 | BUG_ON(!pkt->version); /* This must not happen. */ |
| 6099 | |
| 6100 | if (global.cluster_secret && pkt->token_len) { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6101 | if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid)) |
| 6102 | goto err; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6103 | } |
| 6104 | |
| 6105 | if (!qc) { |
| 6106 | int ipv4; |
| 6107 | |
| 6108 | if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) && |
| 6109 | HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) { |
| 6110 | TRACE_PROTO("Initial without token, sending retry", |
| 6111 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6112 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) { |
| 6113 | TRACE_ERROR("Error during Retry generation", |
| 6114 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6115 | goto out; |
| 6116 | } |
| 6117 | |
| 6118 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 6119 | goto out; |
| 6120 | } |
| 6121 | |
| 6122 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 6123 | * When an Initial packet is sent by a client that has not previously |
| 6124 | * received an Initial or Retry packet from the server, the client |
| 6125 | * populates the Destination Connection ID field with an unpredictable |
| 6126 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 6127 | */ |
| 6128 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 6129 | TRACE_PROTO("dropped packet", |
| 6130 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6131 | goto err; |
| 6132 | } |
| 6133 | |
| 6134 | pkt->saddr = dgram->saddr; |
| 6135 | ipv4 = dgram->saddr.ss_family == AF_INET; |
| 6136 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6137 | 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] | 6138 | &dgram->daddr, &pkt->saddr, 1, |
| 6139 | !!pkt->token_len, l); |
| 6140 | if (qc == NULL) |
| 6141 | goto err; |
| 6142 | |
| 6143 | HA_ATOMIC_INC(&prx_counters->half_open_conn); |
| 6144 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
| 6145 | ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
| 6146 | qc->odcid.len + qc->odcid.addrlen); |
| 6147 | } |
| 6148 | } |
| 6149 | else if (!qc) { |
| 6150 | TRACE_PROTO("No connection on a non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6151 | if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt)) |
| 6152 | TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc); |
| 6153 | goto err; |
| 6154 | } |
| 6155 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6156 | out: |
| 6157 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6158 | return qc; |
| 6159 | |
| 6160 | err: |
| 6161 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
| 6162 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 6163 | return NULL; |
| 6164 | } |
| 6165 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6166 | /* Parse a QUIC packet starting at <buf>. Data won't be read after <end> even |
| 6167 | * if the packet is incomplete. This function will populate fields of <pkt> |
| 6168 | * instance, most notably its length. <dgram> is the UDP datagram which |
| 6169 | * contains the parsed packet. <l> is the listener instance on which it was |
| 6170 | * received. |
| 6171 | * |
| 6172 | * Returns 0 on success else non-zero. Packet length is guaranteed to be set to |
| 6173 | * the real packet value or to cover all data between <buf> and <end> : this is |
| 6174 | * useful to reject a whole datagram. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6175 | */ |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6176 | static int quic_rx_pkt_parse(struct quic_rx_packet *pkt, |
| 6177 | unsigned char *buf, const unsigned char *end, |
| 6178 | struct quic_dgram *dgram, struct listener *l) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6179 | { |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6180 | const unsigned char *beg = buf; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6181 | struct proxy *prx; |
| 6182 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6183 | int long_header = 0; |
Willy Tarreau | 33a6870 | 2022-11-24 09:16:41 +0100 | [diff] [blame] | 6184 | uint32_t version = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6185 | const struct quic_version *qv = NULL; |
| 6186 | |
| 6187 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 6188 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6189 | prx = l->bind_conf->frontend; |
| 6190 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6191 | /* This ist only to please to traces and distinguish the |
| 6192 | * packet with parsed packet number from others. |
| 6193 | */ |
| 6194 | pkt->pn_node.key = (uint64_t)-1; |
| 6195 | if (end <= buf) { |
| 6196 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6197 | goto drop; |
| 6198 | } |
| 6199 | |
| 6200 | /* Fixed bit */ |
| 6201 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6202 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
| 6203 | quic_padding_check(buf, end)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6204 | /* Some browsers may pad the remaining datagram space with null bytes. |
| 6205 | * That is what we called add padding out of QUIC packets. Such |
| 6206 | * datagrams must be considered as valid. But we can only consume |
| 6207 | * the remaining space. |
| 6208 | */ |
| 6209 | pkt->len = end - buf; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6210 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6211 | } |
| 6212 | |
| 6213 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6214 | goto drop; |
| 6215 | } |
| 6216 | |
| 6217 | /* Header form */ |
| 6218 | if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) { |
| 6219 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6220 | goto drop; |
| 6221 | } |
| 6222 | |
| 6223 | if (long_header) { |
| 6224 | uint64_t len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6225 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6226 | TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6227 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
| 6228 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6229 | goto drop; |
| 6230 | } |
| 6231 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6232 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 6233 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6234 | 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] | 6235 | HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram); |
| 6236 | goto drop; |
| 6237 | } |
| 6238 | |
| 6239 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6240 | * they must have the same DCID. |
| 6241 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6242 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6243 | (pkt->dcid.len != dgram->dcid_len || |
| 6244 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6245 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6246 | goto drop; |
| 6247 | } |
| 6248 | |
| 6249 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 6250 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) { |
| 6251 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6252 | goto drop; |
| 6253 | } |
| 6254 | |
| 6255 | /* RFC9000 6. Version Negotiation */ |
| 6256 | qv = qc_supported_version(version); |
| 6257 | if (!qv) { |
| 6258 | /* unsupported version, send Negotiation packet */ |
| 6259 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
| 6260 | TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6261 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6262 | } |
| 6263 | |
| 6264 | TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6265 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6266 | } |
Amaury Denoyelle | 0eae572 | 2022-10-17 18:05:18 +0200 | [diff] [blame] | 6267 | pkt->version = qv; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6268 | |
| 6269 | /* For Initial packets, and for servers (QUIC clients connections), |
| 6270 | * there is no Initial connection IDs storage. |
| 6271 | */ |
| 6272 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 6273 | uint64_t token_len; |
| 6274 | |
| 6275 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 6276 | end - buf < token_len) { |
| 6277 | TRACE_PROTO("Packet dropped", |
| 6278 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6279 | goto drop; |
| 6280 | } |
| 6281 | |
| 6282 | /* TODO Retry should be automatically activated if |
| 6283 | * suspect network usage is detected. |
| 6284 | */ |
| 6285 | if (global.cluster_secret && !token_len) { |
| 6286 | if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) { |
| 6287 | TRACE_PROTO("Initial without token, sending retry", |
Amaury Denoyelle | 90121b3 | 2022-09-27 10:35:29 +0200 | [diff] [blame] | 6288 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6289 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) { |
| 6290 | TRACE_PROTO("Error during Retry generation", |
Amaury Denoyelle | 90121b3 | 2022-09-27 10:35:29 +0200 | [diff] [blame] | 6291 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6292 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6293 | } |
| 6294 | |
| 6295 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6296 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6297 | } |
| 6298 | } |
| 6299 | else if (!global.cluster_secret && token_len) { |
| 6300 | /* Impossible case: a token was received without configured |
| 6301 | * cluster secret. |
| 6302 | */ |
| 6303 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, |
| 6304 | NULL, NULL, NULL, qv); |
| 6305 | goto drop; |
| 6306 | } |
| 6307 | |
| 6308 | pkt->token = buf; |
| 6309 | pkt->token_len = token_len; |
| 6310 | buf += pkt->token_len; |
| 6311 | } |
| 6312 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
| 6313 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
| 6314 | TRACE_PROTO("Packet dropped", |
| 6315 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6316 | goto drop; |
| 6317 | } |
| 6318 | } |
| 6319 | |
| 6320 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 6321 | end - buf < len) { |
| 6322 | TRACE_PROTO("Packet dropped", |
| 6323 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6324 | goto drop; |
| 6325 | } |
| 6326 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6327 | /* Packet Number is stored here. Packet Length totalizes the |
| 6328 | * rest of the content. |
| 6329 | */ |
| 6330 | pkt->pn_offset = buf - beg; |
| 6331 | pkt->len = pkt->pn_offset + len; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6332 | |
| 6333 | /* Interrupt parsing after packet length retrieval : this |
| 6334 | * ensures that only the packet is dropped but not the whole |
| 6335 | * datagram. |
| 6336 | */ |
| 6337 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
| 6338 | TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT); |
| 6339 | goto drop; |
| 6340 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6341 | } |
| 6342 | else { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6343 | TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6344 | if (end - buf < QUIC_HAP_CID_LEN) { |
| 6345 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6346 | goto drop; |
| 6347 | } |
| 6348 | |
| 6349 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
| 6350 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
| 6351 | |
| 6352 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6353 | * they must have the same DCID. |
| 6354 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6355 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6356 | (pkt->dcid.len != dgram->dcid_len || |
| 6357 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6358 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6359 | goto drop; |
| 6360 | } |
| 6361 | |
| 6362 | buf += QUIC_HAP_CID_LEN; |
| 6363 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6364 | pkt->pn_offset = buf - beg; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6365 | /* A short packet is the last one of a UDP datagram. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6366 | pkt->len = end - beg; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6367 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6368 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6369 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv); |
| 6370 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6371 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6372 | drop: |
| 6373 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6374 | drop_silent: |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6375 | if (!pkt->len) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6376 | pkt->len = end - beg; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6377 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv); |
| 6378 | return -1; |
| 6379 | } |
| 6380 | |
| 6381 | /* Check if received packet <pkt> should be drop due to <qc> already in closing |
| 6382 | * state. This can be true if a CONNECTION_CLOSE has already been emitted for |
| 6383 | * this connection. |
| 6384 | * |
| 6385 | * Returns false if connection is not in closing state else true. The caller |
| 6386 | * should drop the whole datagram in the last case to not mess up <qc> |
| 6387 | * CONNECTION_CLOSE rate limit counter. |
| 6388 | */ |
| 6389 | static int qc_rx_check_closing(struct quic_conn *qc, |
| 6390 | struct quic_rx_packet *pkt) |
| 6391 | { |
| 6392 | if (!(qc->flags & QUIC_FL_CONN_CLOSING)) |
| 6393 | return 0; |
| 6394 | |
| 6395 | TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 6396 | |
| 6397 | /* Check if CONNECTION_CLOSE rate reemission is reached. */ |
| 6398 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 6399 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 6400 | qc->nb_pkt_for_cc++; |
| 6401 | qc->nb_pkt_since_cc = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6402 | } |
| 6403 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6404 | return 1; |
| 6405 | } |
| 6406 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6407 | /* React to a connection migration initiated on <qc> by a client with the new |
| 6408 | * path addresses <peer_addr>/<local_addr>. |
| 6409 | * |
| 6410 | * Returns 0 on success else non-zero. |
| 6411 | */ |
| 6412 | static int qc_handle_conn_migration(struct quic_conn *qc, |
| 6413 | const struct sockaddr_storage *peer_addr, |
| 6414 | const struct sockaddr_storage *local_addr) |
| 6415 | { |
| 6416 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6417 | |
Frédéric Lécaille | 6fc8697 | 2023-01-12 08:29:23 +0100 | [diff] [blame] | 6418 | /* RFC 9000. Connection Migration |
| 6419 | * |
| 6420 | * If the peer sent the disable_active_migration transport parameter, |
| 6421 | * an endpoint also MUST NOT send packets (including probing packets; |
| 6422 | * see Section 9.1) from a different local address to the address the peer |
| 6423 | * used during the handshake, unless the endpoint has acted on a |
| 6424 | * preferred_address transport parameter from the peer. |
| 6425 | */ |
| 6426 | if (qc->li->bind_conf->quic_params.disable_active_migration) { |
| 6427 | TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc); |
| 6428 | goto err; |
| 6429 | } |
| 6430 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6431 | /* RFC 9000 9. Connection Migration |
| 6432 | * |
Amaury Denoyelle | eb6be98 | 2022-11-21 11:14:45 +0100 | [diff] [blame] | 6433 | * The design of QUIC relies on endpoints retaining a stable address for |
| 6434 | * the duration of the handshake. An endpoint MUST NOT initiate |
| 6435 | * connection migration before the handshake is confirmed, as defined in |
| 6436 | * Section 4.1.2 of [QUIC-TLS]. |
| 6437 | */ |
| 6438 | if (qc->state < QUIC_HS_ST_COMPLETE) { |
| 6439 | TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc); |
| 6440 | goto err; |
| 6441 | } |
| 6442 | |
| 6443 | /* RFC 9000 9. Connection Migration |
| 6444 | * |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6445 | * TODO |
| 6446 | * An endpoint MUST |
| 6447 | * perform path validation (Section 8.2) if it detects any change to a |
| 6448 | * peer's address, unless it has previously validated that address. |
| 6449 | */ |
| 6450 | |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 6451 | /* Update quic-conn owned socket if in used. |
| 6452 | * TODO try to reuse it instead of closing and opening a new one. |
| 6453 | */ |
| 6454 | if (qc_test_fd(qc)) { |
| 6455 | /* TODO try to reuse socket instead of closing it and opening a new one. */ |
| 6456 | TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc); |
| 6457 | qc_release_fd(qc, 1); |
| 6458 | qc_alloc_fd(qc, local_addr, peer_addr); |
| 6459 | } |
| 6460 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6461 | qc->local_addr = *local_addr; |
| 6462 | qc->peer_addr = *peer_addr; |
| 6463 | HA_ATOMIC_INC(&qc->prx_counters->conn_migration_done); |
| 6464 | |
| 6465 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6466 | return 0; |
| 6467 | |
| 6468 | err: |
| 6469 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6470 | return 1; |
| 6471 | } |
| 6472 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6473 | /* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied |
| 6474 | * into <qc> receive buffer after header protection removal procedure. |
| 6475 | * |
| 6476 | * <dgram> must be set to the datagram which contains the QUIC packet. <beg> |
| 6477 | * must point to packet buffer first byte. |
| 6478 | * |
| 6479 | * <tasklist_head> may be non-NULL when the caller treat several datagrams for |
| 6480 | * different quic-conn. In this case, each quic-conn tasklet will be appended |
| 6481 | * to it in order to be woken up after the current task. |
| 6482 | * |
| 6483 | * The caller can safely removed the packet data. If packet refcount was not |
| 6484 | * incremented by this function, it means that the connection did not handled |
| 6485 | * it and it should be freed by the caller. |
| 6486 | */ |
| 6487 | static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt, |
| 6488 | struct quic_dgram *dgram, unsigned char *beg, |
| 6489 | struct list **tasklist_head) |
| 6490 | { |
| 6491 | const struct quic_version *qv = pkt->version; |
| 6492 | struct quic_enc_level *qel = NULL; |
| 6493 | size_t b_cspace; |
| 6494 | int io_cb_wakeup = 1; |
| 6495 | |
Amaury Denoyelle | 3f474e6 | 2022-11-24 17:15:08 +0100 | [diff] [blame] | 6496 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 6497 | |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6498 | if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST && |
| 6499 | !quic_peer_validated_addr(qc) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6500 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
| 6501 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 6502 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6503 | /* Reset the anti-amplification bit. It will be set again |
| 6504 | * when sending the next packet if reached again. |
| 6505 | */ |
| 6506 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 6507 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
| 6508 | io_cb_wakeup = 1; |
| 6509 | } |
| 6510 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6511 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 6512 | TRACE_PROTO("Connection error", |
| 6513 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6514 | goto out; |
| 6515 | } |
| 6516 | |
| 6517 | pkt->raw_len = pkt->len; |
| 6518 | quic_rx_pkts_del(qc); |
| 6519 | b_cspace = b_contig_space(&qc->rx.buf); |
| 6520 | if (b_cspace < pkt->len) { |
| 6521 | /* Do not consume buf if space not at the end. */ |
| 6522 | if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) { |
| 6523 | TRACE_PROTO("Packet dropped", |
| 6524 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6525 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6526 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6527 | } |
| 6528 | |
| 6529 | /* Let us consume the remaining contiguous space. */ |
| 6530 | if (b_cspace) { |
| 6531 | b_putchr(&qc->rx.buf, 0x00); |
| 6532 | b_cspace--; |
| 6533 | } |
| 6534 | b_add(&qc->rx.buf, b_cspace); |
| 6535 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 6536 | TRACE_PROTO("Too big packet", |
| 6537 | QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6538 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6539 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6540 | } |
| 6541 | } |
| 6542 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6543 | if (!qc_try_rm_hp(qc, pkt, beg, &qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6544 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6545 | goto drop; |
| 6546 | } |
| 6547 | |
| 6548 | TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 6549 | if (pkt->aad_len) |
| 6550 | qc_pkt_insert(qc, pkt, qel); |
| 6551 | out: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 6552 | *tasklist_head = tasklet_wakeup_after(*tasklist_head, |
| 6553 | qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6554 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6555 | drop_silent: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6556 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6557 | return; |
| 6558 | |
| 6559 | drop: |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6560 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6561 | err: |
| 6562 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 6563 | * This cannot be done by this thread. |
| 6564 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 6565 | if (io_cb_wakeup) |
| 6566 | tasklet_wakeup(qc->wait_event.tasklet); |
| 6567 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6568 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
| 6569 | } |
| 6570 | |
| 6571 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 6572 | * Return 1 if enough room to build this header, 0 if not. |
| 6573 | */ |
| 6574 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 6575 | int type, size_t pn_len, |
| 6576 | struct quic_conn *qc, const struct quic_version *ver) |
| 6577 | { |
| 6578 | int ret = 0; |
| 6579 | |
| 6580 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6581 | |
| 6582 | if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) { |
| 6583 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6584 | goto leave; |
| 6585 | } |
| 6586 | |
| 6587 | type = quic_pkt_type(type, ver->num); |
| 6588 | /* #0 byte flags */ |
| 6589 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 6590 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 6591 | /* Version */ |
| 6592 | quic_write_uint32(buf, end, ver->num); |
| 6593 | *(*buf)++ = qc->dcid.len; |
| 6594 | /* Destination connection ID */ |
| 6595 | if (qc->dcid.len) { |
| 6596 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6597 | *buf += qc->dcid.len; |
| 6598 | } |
| 6599 | /* Source connection ID */ |
| 6600 | *(*buf)++ = qc->scid.len; |
| 6601 | if (qc->scid.len) { |
| 6602 | memcpy(*buf, qc->scid.data, qc->scid.len); |
| 6603 | *buf += qc->scid.len; |
| 6604 | } |
| 6605 | |
| 6606 | ret = 1; |
| 6607 | leave: |
| 6608 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6609 | return ret; |
| 6610 | } |
| 6611 | |
| 6612 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 6613 | * Return 1 if enough room to build this header, 0 if not. |
| 6614 | */ |
| 6615 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 6616 | size_t pn_len, struct quic_conn *qc, |
| 6617 | unsigned char tls_flags) |
| 6618 | { |
| 6619 | int ret = 0; |
| 6620 | |
| 6621 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6622 | |
| 6623 | if (end - *buf < 1 + qc->dcid.len) { |
| 6624 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6625 | goto leave; |
| 6626 | } |
| 6627 | |
| 6628 | /* #0 byte flags */ |
| 6629 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 6630 | ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1); |
| 6631 | /* Destination connection ID */ |
| 6632 | if (qc->dcid.len) { |
| 6633 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6634 | *buf += qc->dcid.len; |
| 6635 | } |
| 6636 | |
| 6637 | ret = 1; |
| 6638 | leave: |
| 6639 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6640 | return ret; |
| 6641 | } |
| 6642 | |
| 6643 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 6644 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 6645 | * with <aead> as AEAD cipher and <key> as secret key. |
| 6646 | * Returns 1 if succeeded or 0 if failed. |
| 6647 | */ |
| 6648 | static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf, |
| 6649 | unsigned char *pn, size_t pnlen, |
| 6650 | struct quic_tls_ctx *tls_ctx) |
| 6651 | |
| 6652 | { |
| 6653 | int i, ret = 0; |
| 6654 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 6655 | * and at most 4 bytes for the packet number |
| 6656 | */ |
| 6657 | unsigned char mask[5] = {0}; |
| 6658 | EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx; |
| 6659 | |
| 6660 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6661 | |
| 6662 | if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) { |
| 6663 | TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc); |
| 6664 | goto out; |
| 6665 | } |
| 6666 | |
| 6667 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 6668 | for (i = 0; i < pnlen; i++) |
| 6669 | pn[i] ^= mask[i + 1]; |
| 6670 | |
| 6671 | ret = 1; |
| 6672 | out: |
| 6673 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6674 | return ret; |
| 6675 | } |
| 6676 | |
| 6677 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 6678 | * ACK ranges if needed to a value below <limit> in bytes. |
| 6679 | * Return 1 if succeeded, 0 if not. |
| 6680 | */ |
| 6681 | static int quic_ack_frm_reduce_sz(struct quic_conn *qc, |
| 6682 | struct quic_frame *ack_frm, size_t limit) |
| 6683 | { |
| 6684 | size_t room, ack_delay_sz; |
| 6685 | int ret = 0; |
| 6686 | |
| 6687 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6688 | |
| 6689 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 6690 | /* A frame is made of 1 byte for the frame type. */ |
| 6691 | room = limit - ack_delay_sz - 1; |
| 6692 | if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room)) |
| 6693 | goto leave; |
| 6694 | |
| 6695 | ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz; |
| 6696 | leave: |
| 6697 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6698 | return ret; |
| 6699 | } |
| 6700 | |
| 6701 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 6702 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 6703 | * with <room> as available room, and <*len> the packet Length field initialized |
| 6704 | * with the number of bytes already present in this buffer which must be taken |
| 6705 | * into an account for the Length packet field value. <headlen> is the number of |
| 6706 | * bytes already present in this packet before building frames. |
| 6707 | * |
| 6708 | * Update consequently <*len> to reflect the size of these frames built |
| 6709 | * by this function. Also attach these frames to <l> frame list. |
| 6710 | * Return 1 if at least one ack-eleciting frame could be built, 0 if not. |
| 6711 | */ |
| 6712 | static inline int qc_build_frms(struct list *outlist, struct list *inlist, |
| 6713 | size_t room, size_t *len, size_t headlen, |
| 6714 | struct quic_enc_level *qel, |
| 6715 | struct quic_conn *qc) |
| 6716 | { |
| 6717 | int ret; |
| 6718 | struct quic_frame *cf, *cfbak; |
| 6719 | |
| 6720 | TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc); |
| 6721 | |
| 6722 | ret = 0; |
| 6723 | if (*len > room) |
| 6724 | goto leave; |
| 6725 | |
| 6726 | /* If we are not probing we must take into an account the congestion |
| 6727 | * control window. |
| 6728 | */ |
| 6729 | if (!qel->pktns->tx.pto_probe) { |
| 6730 | size_t remain = quic_path_prep_data(qc->path); |
| 6731 | |
| 6732 | if (headlen > remain) |
| 6733 | goto leave; |
| 6734 | |
| 6735 | room = QUIC_MIN(room, remain - headlen); |
| 6736 | } |
| 6737 | |
| 6738 | TRACE_PROTO("************** frames build (headlen)", |
| 6739 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
| 6740 | |
| 6741 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 6742 | * returned by this function only if at least one frame could be built |
| 6743 | * in the switch/case block. |
| 6744 | */ |
| 6745 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
| 6746 | /* header length, data length, frame length. */ |
| 6747 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
| 6748 | |
| 6749 | if (!room) |
| 6750 | break; |
| 6751 | |
| 6752 | switch (cf->type) { |
| 6753 | case QUIC_FT_CRYPTO: |
| 6754 | TRACE_DEVEL(" New CRYPTO frame build (room, len)", |
| 6755 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
| 6756 | /* Compute the length of this CRYPTO frame header */ |
| 6757 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 6758 | /* Compute the data length of this CRyPTO frame. */ |
| 6759 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 6760 | TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)", |
| 6761 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen); |
| 6762 | if (!dlen) |
| 6763 | continue; |
| 6764 | |
| 6765 | /* CRYPTO frame length. */ |
| 6766 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 6767 | TRACE_DEVEL(" CRYPTO frame length (flen)", |
| 6768 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 6769 | /* Add the CRYPTO data length and its encoded length to the packet |
| 6770 | * length and the length of this length. |
| 6771 | */ |
| 6772 | *len += flen; |
| 6773 | room -= flen; |
| 6774 | if (dlen == cf->crypto.len) { |
| 6775 | /* <cf> CRYPTO data have been consumed. */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 6776 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6777 | LIST_APPEND(outlist, &cf->list); |
| 6778 | } |
| 6779 | else { |
| 6780 | struct quic_frame *new_cf; |
| 6781 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 6782 | new_cf = qc_frm_alloc(QUIC_FT_CRYPTO); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6783 | if (!new_cf) { |
| 6784 | TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
| 6785 | continue; |
| 6786 | } |
| 6787 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6788 | new_cf->crypto.len = dlen; |
| 6789 | new_cf->crypto.offset = cf->crypto.offset; |
| 6790 | new_cf->crypto.qel = qel; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 6791 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6792 | if (cf->origin) { |
| 6793 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 6794 | /* This <cf> frame was duplicated */ |
| 6795 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 6796 | new_cf->origin = cf->origin; |
Frédéric Lécaille | dd41946 | 2023-01-26 15:07:39 +0100 | [diff] [blame] | 6797 | /* Detach the remaining CRYPTO frame from its original frame */ |
| 6798 | LIST_DEL_INIT(&cf->ref); |
| 6799 | cf->origin = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6800 | } |
| 6801 | LIST_APPEND(outlist, &new_cf->list); |
| 6802 | /* Consume <dlen> bytes of the current frame. */ |
| 6803 | cf->crypto.len -= dlen; |
| 6804 | cf->crypto.offset += dlen; |
| 6805 | } |
| 6806 | break; |
| 6807 | |
| 6808 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 6809 | if (cf->flags & QUIC_FL_TX_FRAME_LOST) { |
| 6810 | struct eb64_node *node = NULL; |
| 6811 | struct qc_stream_desc *stream_desc = NULL; |
| 6812 | struct quic_stream *strm = &cf->stream; |
| 6813 | |
| 6814 | /* As this frame has been already lost, ensure the stream is always |
| 6815 | * available or the range of this frame is not consumed before |
| 6816 | * resending it. |
| 6817 | */ |
| 6818 | node = eb64_lookup(&qc->streams_by_id, strm->id); |
| 6819 | if (!node) { |
| 6820 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 6821 | qc_frm_free(&cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6822 | continue; |
| 6823 | } |
| 6824 | |
| 6825 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 6826 | if (strm->offset.key + strm->len <= stream_desc->ack_offset) { |
| 6827 | TRACE_DEVEL("ignored frame frame in already acked range", |
| 6828 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 6829 | qc_frm_free(&cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6830 | continue; |
| 6831 | } |
| 6832 | else if (strm->offset.key < stream_desc->ack_offset) { |
| 6833 | strm->offset.key = stream_desc->ack_offset; |
| 6834 | TRACE_DEVEL("updated partially acked frame", |
| 6835 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
| 6836 | } |
| 6837 | } |
| 6838 | /* Note that these frames are accepted in short packets only without |
| 6839 | * "Length" packet field. Here, <*len> is used only to compute the |
| 6840 | * sum of the lengths of the already built frames for this packet. |
| 6841 | * |
| 6842 | * Compute the length of this STREAM frame "header" made a all the field |
| 6843 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 6844 | */ |
| 6845 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
| 6846 | ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0); |
| 6847 | /* Compute the data length of this STREAM frame. */ |
| 6848 | avail_room = room - hlen - *len; |
| 6849 | if ((ssize_t)avail_room <= 0) |
| 6850 | continue; |
| 6851 | |
| 6852 | TRACE_DEVEL(" New STREAM frame build (room, len)", |
| 6853 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 6854 | |
| 6855 | /* hlen contains STREAM id and offset. Ensure there is |
| 6856 | * enough room for length field. |
| 6857 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6858 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 6859 | dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz), |
| 6860 | cf->stream.len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6861 | dlen_sz = quic_int_getsize(dlen); |
| 6862 | flen = hlen + dlen_sz + dlen; |
| 6863 | } |
| 6864 | else { |
| 6865 | dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len); |
| 6866 | flen = hlen + dlen; |
| 6867 | } |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 6868 | |
| 6869 | if (cf->stream.len && !dlen) { |
| 6870 | /* Only a small gap is left on buffer, not |
| 6871 | * enough to encode the STREAM data length. |
| 6872 | */ |
| 6873 | continue; |
| 6874 | } |
| 6875 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6876 | TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)", |
| 6877 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen); |
| 6878 | TRACE_DEVEL(" STREAM frame length (flen)", |
| 6879 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 6880 | /* Add the STREAM data length and its encoded length to the packet |
| 6881 | * length and the length of this length. |
| 6882 | */ |
| 6883 | *len += flen; |
| 6884 | room -= flen; |
| 6885 | if (dlen == cf->stream.len) { |
| 6886 | /* <cf> STREAM data have been consumed. */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 6887 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6888 | LIST_APPEND(outlist, &cf->list); |
| 6889 | |
| 6890 | /* Do not notify MUX on retransmission. */ |
| 6891 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 6892 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 6893 | cf->stream.len, |
| 6894 | cf->stream.offset.key); |
| 6895 | } |
| 6896 | } |
| 6897 | else { |
| 6898 | struct quic_frame *new_cf; |
| 6899 | struct buffer cf_buf; |
| 6900 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 6901 | new_cf = qc_frm_alloc(cf->type); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6902 | if (!new_cf) { |
| 6903 | TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
| 6904 | continue; |
| 6905 | } |
| 6906 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6907 | new_cf->stream.stream = cf->stream.stream; |
| 6908 | new_cf->stream.buf = cf->stream.buf; |
| 6909 | new_cf->stream.id = cf->stream.id; |
Amaury Denoyelle | 1dac018 | 2023-02-02 16:45:07 +0100 | [diff] [blame] | 6910 | new_cf->stream.offset = cf->stream.offset; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6911 | new_cf->stream.len = dlen; |
| 6912 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 6913 | /* FIN bit reset */ |
| 6914 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 6915 | new_cf->stream.data = cf->stream.data; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 6916 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6917 | if (cf->origin) { |
| 6918 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 6919 | /* This <cf> frame was duplicated */ |
| 6920 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 6921 | new_cf->origin = cf->origin; |
Frédéric Lécaille | dd41946 | 2023-01-26 15:07:39 +0100 | [diff] [blame] | 6922 | /* Detach this STREAM frame from its origin */ |
| 6923 | LIST_DEL_INIT(&cf->ref); |
| 6924 | cf->origin = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6925 | } |
| 6926 | LIST_APPEND(outlist, &new_cf->list); |
| 6927 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 6928 | /* Consume <dlen> bytes of the current frame. */ |
| 6929 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 6930 | b_size(cf->stream.buf), |
| 6931 | (char *)cf->stream.data - b_orig(cf->stream.buf), 0); |
| 6932 | cf->stream.len -= dlen; |
| 6933 | cf->stream.offset.key += dlen; |
| 6934 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
| 6935 | |
| 6936 | /* Do not notify MUX on retransmission. */ |
| 6937 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 6938 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 6939 | new_cf->stream.len, |
| 6940 | new_cf->stream.offset.key); |
| 6941 | } |
| 6942 | } |
| 6943 | |
| 6944 | /* TODO the MUX is notified about the frame sending via |
| 6945 | * previous qcc_streams_sent_done call. However, the |
| 6946 | * sending can fail later, for example if the sendto |
| 6947 | * system call returns an error. As the MUX has been |
| 6948 | * notified, the transport layer is responsible to |
| 6949 | * bufferize and resent the announced data later. |
| 6950 | */ |
| 6951 | |
| 6952 | break; |
| 6953 | |
| 6954 | default: |
| 6955 | flen = qc_frm_len(cf); |
| 6956 | BUG_ON(!flen); |
| 6957 | if (flen > room) |
| 6958 | continue; |
| 6959 | |
| 6960 | *len += flen; |
| 6961 | room -= flen; |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 6962 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6963 | LIST_APPEND(outlist, &cf->list); |
| 6964 | break; |
| 6965 | } |
| 6966 | |
| 6967 | /* Successful status as soon as a frame could be built */ |
| 6968 | ret = 1; |
| 6969 | } |
| 6970 | |
| 6971 | leave: |
| 6972 | TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc); |
| 6973 | return ret; |
| 6974 | } |
| 6975 | |
| 6976 | /* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out> |
| 6977 | * is used as return parameter and should be zero'ed by the caller. |
| 6978 | */ |
| 6979 | static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel, |
| 6980 | struct quic_frame *out) |
| 6981 | { |
| 6982 | /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels |
| 6983 | * |
| 6984 | * A CONNECTION_CLOSE frame should be sent in several packets with |
| 6985 | * different encryption levels depending on the client context. This is |
| 6986 | * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for |
| 6987 | * more details on how to implement it. |
| 6988 | */ |
| 6989 | TRACE_ENTER(QUIC_EV_CONN_BFRM, qc); |
| 6990 | |
| 6991 | |
| 6992 | if (qc->err.app) { |
| 6993 | if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 6994 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 6995 | /* RFC 9000 10.2.3. Immediate Close during the Handshake |
| 6996 | * |
| 6997 | * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake |
| 6998 | * packet could expose application state or be used to alter application |
| 6999 | * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a |
| 7000 | * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or |
| 7001 | * Handshake packets. Otherwise, information about the application |
| 7002 | * state might be revealed. Endpoints MUST clear the value of the |
| 7003 | * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when |
| 7004 | * converting to a CONNECTION_CLOSE of type 0x1c. |
| 7005 | */ |
| 7006 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 7007 | out->connection_close.error_code = QC_ERR_APPLICATION_ERROR; |
| 7008 | out->connection_close.reason_phrase_len = 0; |
| 7009 | } |
| 7010 | else { |
| 7011 | out->type = QUIC_FT_CONNECTION_CLOSE_APP; |
| 7012 | out->connection_close.error_code = qc->err.code; |
| 7013 | } |
| 7014 | } |
| 7015 | else { |
| 7016 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 7017 | out->connection_close.error_code = qc->err.code; |
| 7018 | } |
| 7019 | TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc); |
| 7020 | |
| 7021 | } |
| 7022 | |
| 7023 | /* This function builds a clear packet from <pkt> information (its type) |
| 7024 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 7025 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 7026 | * filling the buffer with as much frames as possible from <frms> list of |
| 7027 | * prebuilt frames. |
| 7028 | * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are |
| 7029 | * reserved so that to ensure there is enough room to build this AEAD TAG after |
| 7030 | * having returned from this function. |
| 7031 | * This function also updates the value of <buf_pn> pointer to point to the packet |
| 7032 | * number field in this packet. <pn_len> will also have the packet number |
| 7033 | * length as value. |
| 7034 | * |
| 7035 | * Return 1 if succeeded (enough room to buile this packet), O if not. |
| 7036 | */ |
| 7037 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 7038 | size_t dglen, struct quic_tx_packet *pkt, |
| 7039 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
| 7040 | int force_ack, int padding, int cc, int probe, |
| 7041 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 7042 | const struct quic_version *ver, struct list *frms) |
| 7043 | { |
| 7044 | unsigned char *beg, *payload; |
| 7045 | size_t len, len_sz, len_frms, padding_len; |
| 7046 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 7047 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
| 7048 | struct quic_frame cc_frm = { }; |
| 7049 | size_t ack_frm_len, head_len; |
| 7050 | int64_t rx_largest_acked_pn; |
| 7051 | int add_ping_frm; |
| 7052 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 7053 | struct quic_frame *cf; |
| 7054 | int must_ack, ret = 0; |
| 7055 | int nb_aepkts_since_last_ack; |
| 7056 | |
| 7057 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 7058 | |
| 7059 | /* Length field value with CRYPTO frames if present. */ |
| 7060 | len_frms = 0; |
| 7061 | beg = pos; |
| 7062 | /* When not probing, and no immediate close is required, reduce the size of this |
| 7063 | * buffer to respect the congestion controller window. |
| 7064 | * This size will be limited if we have ack-eliciting frames to send from <frms>. |
| 7065 | */ |
| 7066 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
| 7067 | size_t path_room; |
| 7068 | |
| 7069 | path_room = quic_path_prep_data(qc->path); |
| 7070 | if (end - beg > path_room) |
| 7071 | end = beg + path_room; |
| 7072 | } |
| 7073 | |
| 7074 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 7075 | * length field if any. |
| 7076 | */ |
| 7077 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 7078 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 7079 | goto no_room; |
| 7080 | |
| 7081 | end -= QUIC_TLS_TAG_LEN; |
| 7082 | rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn; |
| 7083 | /* packet number length */ |
| 7084 | *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn); |
| 7085 | /* Build the header */ |
| 7086 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
| 7087 | !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) || |
| 7088 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
| 7089 | !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver))) |
| 7090 | goto no_room; |
| 7091 | |
| 7092 | /* Encode the token length (0) for an Initial packet. */ |
| 7093 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
| 7094 | *pos++ = 0; |
| 7095 | head_len = pos - beg; |
| 7096 | /* Build an ACK frame if required. */ |
| 7097 | ack_frm_len = 0; |
| 7098 | nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack; |
| 7099 | must_ack = !qel->pktns->tx.pto_probe && |
| 7100 | (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 7101 | (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK))); |
| 7102 | if (must_ack) { |
| 7103 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
| 7104 | BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root)); |
| 7105 | ack_frm.tx_ack.arngs = arngs; |
| 7106 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 7107 | qel->pktns->tx.ack_delay = |
| 7108 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 7109 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 7110 | } |
| 7111 | ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay; |
| 7112 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 7113 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 7114 | * that from here, we do not know if we will have to send a PING frame. |
| 7115 | * This will be decided after having computed the ack-eliciting frames |
| 7116 | * to be added to this packet. |
| 7117 | */ |
| 7118 | ack_frm_len = quic_ack_frm_reduce_sz(qc, &ack_frm, end - 1 - *pn_len - pos); |
| 7119 | if (!ack_frm_len) |
| 7120 | goto no_room; |
| 7121 | } |
| 7122 | |
| 7123 | /* Length field value without the ack-eliciting frames. */ |
| 7124 | len = ack_frm_len + *pn_len; |
| 7125 | len_frms = 0; |
| 7126 | if (!cc && !LIST_ISEMPTY(frms)) { |
| 7127 | ssize_t room = end - pos; |
| 7128 | |
| 7129 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 7130 | /* Initialize the length of the frames built below to <len>. |
| 7131 | * If any frame could be successfully built by qc_build_frms(), |
| 7132 | * we will have len_frms > len. |
| 7133 | */ |
| 7134 | len_frms = len; |
| 7135 | if (!qc_build_frms(&frm_list, frms, |
| 7136 | end - pos, &len_frms, pos - beg, qel, qc)) { |
| 7137 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
| 7138 | qc, NULL, NULL, &room); |
| 7139 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 7140 | goto no_room; |
| 7141 | } |
| 7142 | } |
| 7143 | |
| 7144 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 7145 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 7146 | * for the encryption tag. It must be taken into an account for the length |
| 7147 | * of this packet. |
| 7148 | */ |
| 7149 | if (len_frms) |
| 7150 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 7151 | else |
| 7152 | len += QUIC_TLS_TAG_LEN; |
| 7153 | /* CONNECTION_CLOSE frame */ |
| 7154 | if (cc) { |
| 7155 | qc_build_cc_frm(qc, qel, &cc_frm); |
| 7156 | len += qc_frm_len(&cc_frm); |
| 7157 | } |
| 7158 | add_ping_frm = 0; |
| 7159 | padding_len = 0; |
| 7160 | len_sz = quic_int_getsize(len); |
| 7161 | /* Add this packet size to <dglen> */ |
| 7162 | dglen += head_len + len_sz + len; |
| 7163 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 7164 | /* This is a maximum padding size */ |
| 7165 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 7166 | /* The length field value is of this packet is <len> + <padding_len> |
| 7167 | * the size of which may be greater than the initial computed size |
| 7168 | * <len_sz>. So, let's deduce the difference between these to packet |
| 7169 | * sizes from <padding_len>. |
| 7170 | */ |
| 7171 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 7172 | len += padding_len; |
| 7173 | } |
| 7174 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
| 7175 | if (qel->pktns->tx.pto_probe) { |
| 7176 | /* If we cannot send a frame, we send a PING frame. */ |
| 7177 | add_ping_frm = 1; |
| 7178 | len += 1; |
| 7179 | } |
| 7180 | /* If there is no frame at all to follow, add at least a PADDING frame. */ |
| 7181 | if (!ack_frm_len && !cc) |
| 7182 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 7183 | } |
| 7184 | |
| 7185 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 7186 | goto no_room; |
| 7187 | |
| 7188 | /* Packet number field address. */ |
| 7189 | *buf_pn = pos; |
| 7190 | |
| 7191 | /* Packet number encoding. */ |
| 7192 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 7193 | goto no_room; |
| 7194 | |
| 7195 | /* payload building (ack-eliciting or not frames) */ |
| 7196 | payload = pos; |
| 7197 | if (ack_frm_len) { |
| 7198 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 7199 | goto no_room; |
| 7200 | |
| 7201 | pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns); |
| 7202 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
| 7203 | } |
| 7204 | |
| 7205 | /* Ack-eliciting frames */ |
| 7206 | if (!LIST_ISEMPTY(&frm_list)) { |
| 7207 | struct quic_frame *tmp_cf; |
| 7208 | list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) { |
| 7209 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
| 7210 | ssize_t room = end - pos; |
| 7211 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
| 7212 | qc, NULL, NULL, &room); |
| 7213 | /* Note that <cf> was added from <frms> to <frm_list> list by |
| 7214 | * qc_build_frms(). |
| 7215 | */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7216 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7217 | LIST_INSERT(frms, &cf->list); |
| 7218 | continue; |
| 7219 | } |
| 7220 | |
| 7221 | quic_tx_packet_refinc(pkt); |
| 7222 | cf->pkt = pkt; |
| 7223 | } |
| 7224 | } |
| 7225 | |
| 7226 | /* Build a PING frame if needed. */ |
| 7227 | if (add_ping_frm) { |
| 7228 | frm.type = QUIC_FT_PING; |
| 7229 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 7230 | goto no_room; |
| 7231 | } |
| 7232 | |
| 7233 | /* Build a CONNECTION_CLOSE frame if needed. */ |
| 7234 | if (cc) { |
| 7235 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 7236 | goto no_room; |
| 7237 | |
| 7238 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 7239 | } |
| 7240 | |
| 7241 | /* Build a PADDING frame if needed. */ |
| 7242 | if (padding_len) { |
| 7243 | frm.type = QUIC_FT_PADDING; |
| 7244 | frm.padding.len = padding_len; |
| 7245 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 7246 | goto no_room; |
| 7247 | } |
| 7248 | |
| 7249 | if (pos == payload) { |
| 7250 | /* No payload was built because of congestion control */ |
| 7251 | TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc); |
| 7252 | goto no_room; |
| 7253 | } |
| 7254 | |
| 7255 | /* If this packet is ack-eliciting and we are probing let's |
| 7256 | * decrement the PTO probe counter. |
| 7257 | */ |
| 7258 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 7259 | qel->pktns->tx.pto_probe) |
| 7260 | qel->pktns->tx.pto_probe--; |
| 7261 | |
| 7262 | pkt->len = pos - beg; |
| 7263 | LIST_SPLICE(&pkt->frms, &frm_list); |
| 7264 | |
| 7265 | ret = 1; |
| 7266 | TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt); |
| 7267 | leave: |
| 7268 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 7269 | return ret; |
| 7270 | |
| 7271 | no_room: |
| 7272 | /* Replace the pre-built frames which could not be add to this packet */ |
| 7273 | LIST_SPLICE(frms, &frm_list); |
| 7274 | TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 7275 | goto leave; |
| 7276 | } |
| 7277 | |
| 7278 | static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type) |
| 7279 | { |
| 7280 | pkt->type = type; |
| 7281 | pkt->len = 0; |
| 7282 | pkt->in_flight_len = 0; |
| 7283 | pkt->pn_node.key = (uint64_t)-1; |
| 7284 | LIST_INIT(&pkt->frms); |
| 7285 | pkt->time_sent = TICK_ETERNITY; |
| 7286 | pkt->next = NULL; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 7287 | pkt->prev = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7288 | pkt->largest_acked_pn = -1; |
| 7289 | pkt->flags = 0; |
| 7290 | pkt->refcnt = 0; |
| 7291 | } |
| 7292 | |
| 7293 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 7294 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 7295 | * of prebuilt frames. |
| 7296 | * |
| 7297 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 7298 | * -1 if there was not enough room to build a packet. |
| 7299 | * XXX NOTE XXX |
| 7300 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 7301 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 7302 | * control window limitation. |
| 7303 | */ |
| 7304 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 7305 | const unsigned char *buf_end, |
| 7306 | struct quic_enc_level *qel, |
| 7307 | struct quic_tls_ctx *tls_ctx, struct list *frms, |
| 7308 | struct quic_conn *qc, const struct quic_version *ver, |
| 7309 | size_t dglen, int pkt_type, int force_ack, |
| 7310 | int padding, int probe, int cc, int *err) |
| 7311 | { |
| 7312 | struct quic_tx_packet *ret_pkt = NULL; |
| 7313 | /* The pointer to the packet number field. */ |
| 7314 | unsigned char *buf_pn; |
| 7315 | unsigned char *beg, *end, *payload; |
| 7316 | int64_t pn; |
| 7317 | size_t pn_len, payload_len, aad_len; |
| 7318 | struct quic_tx_packet *pkt; |
| 7319 | |
| 7320 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel); |
| 7321 | *err = 0; |
| 7322 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 7323 | if (!pkt) { |
| 7324 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc); |
| 7325 | *err = -2; |
| 7326 | goto err; |
| 7327 | } |
| 7328 | |
| 7329 | quic_tx_packet_init(pkt, pkt_type); |
| 7330 | beg = *pos; |
| 7331 | pn_len = 0; |
| 7332 | buf_pn = NULL; |
| 7333 | |
| 7334 | pn = qel->pktns->tx.next_pn + 1; |
| 7335 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
| 7336 | force_ack, padding, cc, probe, qel, qc, ver, frms)) { |
| 7337 | // trace already emitted by function above |
| 7338 | *err = -1; |
| 7339 | goto err; |
| 7340 | } |
| 7341 | |
| 7342 | end = beg + pkt->len; |
| 7343 | payload = buf_pn + pn_len; |
| 7344 | payload_len = end - payload; |
| 7345 | aad_len = payload - beg; |
| 7346 | |
| 7347 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) { |
| 7348 | // trace already emitted by function above |
| 7349 | *err = -2; |
| 7350 | goto err; |
| 7351 | } |
| 7352 | |
| 7353 | end += QUIC_TLS_TAG_LEN; |
| 7354 | pkt->len += QUIC_TLS_TAG_LEN; |
| 7355 | if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) { |
| 7356 | // trace already emitted by function above |
| 7357 | *err = -2; |
| 7358 | goto err; |
| 7359 | } |
| 7360 | |
| 7361 | /* Consume a packet number */ |
| 7362 | qel->pktns->tx.next_pn++; |
| 7363 | qc->tx.prep_bytes += pkt->len; |
| 7364 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) { |
| 7365 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 7366 | TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc); |
| 7367 | } |
| 7368 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 7369 | *pos = end; |
| 7370 | /* Attach the built packet to its tree. */ |
| 7371 | pkt->pn_node.key = pn; |
| 7372 | /* Set the packet in fligth length for in flight packet only. */ |
| 7373 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
| 7374 | pkt->in_flight_len = pkt->len; |
| 7375 | qc->path->prep_in_flight += pkt->len; |
| 7376 | } |
| 7377 | /* Always reset this flags */ |
| 7378 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 7379 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 7380 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 7381 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 7382 | } |
| 7383 | |
| 7384 | pkt->pktns = qel->pktns; |
| 7385 | |
| 7386 | ret_pkt = pkt; |
| 7387 | leave: |
| 7388 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt); |
| 7389 | return ret_pkt; |
| 7390 | |
| 7391 | err: |
| 7392 | /* TODO: what about the frames which have been built |
| 7393 | * for this packet. |
| 7394 | */ |
| 7395 | free_quic_tx_packet(qc, pkt); |
| 7396 | goto leave; |
| 7397 | } |
| 7398 | |
| 7399 | |
| 7400 | static void __quic_conn_init(void) |
| 7401 | { |
| 7402 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 7403 | } |
| 7404 | INITCALL0(STG_REGISTER, __quic_conn_init); |
| 7405 | |
| 7406 | static void __quic_conn_deinit(void) |
| 7407 | { |
| 7408 | BIO_meth_free(ha_quic_meth); |
| 7409 | } |
| 7410 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
| 7411 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7412 | /* Handle a new <dgram> received. Parse each QUIC packets and copied their |
| 7413 | * content to a quic-conn instance. The datagram content can be released after |
| 7414 | * this function. |
| 7415 | * |
| 7416 | * If datagram has been received on a quic-conn owned FD, <from_qc> must be set |
| 7417 | * to the connection instance. <li> is the attached listener. The caller is |
| 7418 | * responsible to ensure that the first packet is destined to this connection |
| 7419 | * by comparing CIDs. |
| 7420 | * |
| 7421 | * If datagram has been received on a receiver FD, <from_qc> will be NULL. This |
| 7422 | * function will thus retrieve the connection from the CID tree or allocate a |
| 7423 | * new one if possible. <li> is the listener attached to the receiver. |
| 7424 | * |
| 7425 | * Returns 0 on success else non-zero. If an error happens, some packets from |
| 7426 | * the datagram may not have been parsed. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7427 | */ |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7428 | int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc, |
| 7429 | struct listener *li) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7430 | { |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7431 | struct quic_rx_packet *pkt; |
| 7432 | struct quic_conn *qc = NULL; |
| 7433 | unsigned char *pos, *end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7434 | struct list *tasklist_head = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7435 | |
| 7436 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 7437 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7438 | pos = dgram->buf; |
| 7439 | end = pos + dgram->len; |
| 7440 | do { |
| 7441 | /* TODO replace zalloc -> alloc. */ |
| 7442 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 7443 | if (!pkt) { |
| 7444 | TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT); |
| 7445 | goto err; |
| 7446 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7447 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7448 | pkt->version = NULL; |
| 7449 | pkt->pn_offset = 0; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7450 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7451 | /* Set flag if pkt is the first one in dgram. */ |
| 7452 | if (pos == dgram->buf) |
| 7453 | pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7454 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7455 | LIST_INIT(&pkt->qc_rx_pkt_list); |
| 7456 | pkt->time_received = now_ms; |
| 7457 | quic_rx_packet_refinc(pkt); |
| 7458 | if (quic_rx_pkt_parse(pkt, pos, end, dgram, li)) |
| 7459 | goto next; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7460 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7461 | /* Search quic-conn instance for first packet of the datagram. |
| 7462 | * quic_rx_packet_parse() is responsible to discard packets |
| 7463 | * with different DCID as the first one in the same datagram. |
| 7464 | */ |
| 7465 | if (!qc) { |
| 7466 | qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li); |
| 7467 | /* qc is NULL if receiving a non Initial packet for an |
| 7468 | * unknown connection. |
| 7469 | */ |
| 7470 | if (!qc) { |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7471 | /* Skip the entire datagram. */ |
| 7472 | pkt->len = end - pos; |
| 7473 | goto next; |
| 7474 | } |
| 7475 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7476 | dgram->qc = qc; |
| 7477 | } |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7478 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7479 | if (qc_rx_check_closing(qc, pkt)) { |
| 7480 | /* Skip the entire datagram. */ |
| 7481 | pkt->len = end - pos; |
| 7482 | goto next; |
| 7483 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7484 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7485 | /* Detect QUIC connection migration. */ |
Frédéric Lécaille | f676954 | 2023-01-12 10:36:26 +0100 | [diff] [blame] | 7486 | if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) { |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7487 | if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) { |
| 7488 | /* Skip the entire datagram. */ |
| 7489 | TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc); |
| 7490 | pkt->len = end - pos; |
| 7491 | goto next; |
| 7492 | } |
| 7493 | } |
| 7494 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7495 | qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7496 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7497 | next: |
| 7498 | pos += pkt->len; |
| 7499 | quic_rx_packet_refdec(pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7500 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7501 | /* Free rejected packets */ |
| 7502 | if (!pkt->refcnt) { |
| 7503 | BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list)); |
| 7504 | pool_free(pool_head_quic_rx_packet, pkt); |
| 7505 | } |
| 7506 | } while (pos < end); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7507 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7508 | /* Increasing the received bytes counter by the UDP datagram length |
| 7509 | * if this datagram could be associated to a connection. |
| 7510 | */ |
| 7511 | if (dgram->qc) |
| 7512 | dgram->qc->rx.bytes += dgram->len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7513 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7514 | /* This must never happen. */ |
| 7515 | BUG_ON(pos > end); |
| 7516 | BUG_ON(pos < end || pos > dgram->buf + dgram->len); |
| 7517 | /* Mark this datagram as consumed */ |
| 7518 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7519 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7520 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 7521 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7522 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7523 | err: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7524 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7525 | return -1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7526 | } |
| 7527 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 7528 | /* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local |
| 7529 | * CIDs. This can be used to determine if a datagram is addressed to the right |
| 7530 | * connection instance. |
| 7531 | * |
| 7532 | * Returns a boolean value. |
| 7533 | */ |
| 7534 | int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len) |
| 7535 | { |
| 7536 | struct ebmb_node *node; |
| 7537 | struct quic_connection_id *id; |
| 7538 | |
| 7539 | /* For ODCID, address is concatenated to it after qc.odcid.len so this |
| 7540 | * comparison is safe. |
| 7541 | */ |
| 7542 | if ((qc->scid.len == dcid_len && |
| 7543 | memcmp(qc->scid.data, dcid, dcid_len) == 0) || |
| 7544 | (qc->odcid.len == dcid_len && |
| 7545 | memcmp(qc->odcid.data, dcid, dcid_len)) == 0) { |
| 7546 | return 1; |
| 7547 | } |
| 7548 | |
| 7549 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, dcid, dcid_len); |
| 7550 | if (node) { |
| 7551 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 7552 | if (qc == id->qc) |
| 7553 | return 1; |
| 7554 | } |
| 7555 | |
| 7556 | return 0; |
| 7557 | } |
| 7558 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7559 | /* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 7560 | * Returns 1 if succeeded, 0 if not. |
| 7561 | */ |
| 7562 | int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 7563 | unsigned char **dcid, size_t *dcid_len) |
| 7564 | { |
| 7565 | int ret = 0, long_header; |
| 7566 | size_t minlen, skip; |
| 7567 | |
| 7568 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 7569 | |
| 7570 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
| 7571 | TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT); |
| 7572 | goto err; |
| 7573 | } |
| 7574 | |
| 7575 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
| 7576 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 7577 | QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN; |
| 7578 | skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF; |
| 7579 | if (end - buf < minlen) |
| 7580 | goto err; |
| 7581 | |
| 7582 | buf += skip; |
| 7583 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 7584 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 7585 | goto err; |
| 7586 | |
| 7587 | *dcid = buf; |
| 7588 | |
| 7589 | ret = 1; |
| 7590 | leave: |
| 7591 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 7592 | return ret; |
| 7593 | |
| 7594 | err: |
| 7595 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT); |
| 7596 | goto leave; |
| 7597 | } |
| 7598 | |
| 7599 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 7600 | void qc_notify_close(struct quic_conn *qc) |
| 7601 | { |
| 7602 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 7603 | |
| 7604 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 7605 | goto leave; |
| 7606 | |
| 7607 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 7608 | /* wake up the MUX */ |
| 7609 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) { |
| 7610 | TRACE_STATE("connection closure notidfied to mux", |
| 7611 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7612 | qc->conn->mux->wake(qc->conn); |
| 7613 | } |
| 7614 | else |
| 7615 | TRACE_STATE("connection closure not notidfied to mux", |
| 7616 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7617 | leave: |
| 7618 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 7619 | } |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7620 | |
| 7621 | |
| 7622 | /* appctx context used by "show quic" command */ |
| 7623 | struct show_quic_ctx { |
| 7624 | unsigned int epoch; |
| 7625 | struct bref bref; /* back-reference to the quic-conn being dumped */ |
| 7626 | unsigned int thr; |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 7627 | int flags; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7628 | }; |
| 7629 | |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 7630 | #define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */ |
| 7631 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7632 | static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private) |
| 7633 | { |
| 7634 | struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 7635 | |
| 7636 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 7637 | return 1; |
| 7638 | |
| 7639 | ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1); |
| 7640 | ctx->thr = 0; |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 7641 | ctx->flags = 0; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7642 | |
Amaury Denoyelle | 10a46de | 2023-02-09 18:18:45 +0100 | [diff] [blame^] | 7643 | if (*args[2] && strcmp(args[2], "all") == 0) |
| 7644 | ctx->flags |= QC_CLI_FL_SHOW_ALL; |
| 7645 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7646 | LIST_INIT(&ctx->bref.users); |
| 7647 | |
| 7648 | return 0; |
| 7649 | } |
| 7650 | |
| 7651 | static int cli_io_handler_dump_quic(struct appctx *appctx) |
| 7652 | { |
| 7653 | struct show_quic_ctx *ctx = appctx->svcctx; |
| 7654 | struct stconn *sc = appctx_sc(appctx); |
| 7655 | struct quic_conn *qc; |
Amaury Denoyelle | 1b0fc43 | 2023-02-01 17:05:10 +0100 | [diff] [blame] | 7656 | struct quic_enc_level *qel; |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 7657 | struct eb64_node *node; |
| 7658 | struct qc_stream_desc *stream; |
Amaury Denoyelle | b89c0e2 | 2023-02-01 17:04:26 +0100 | [diff] [blame] | 7659 | char bufaddr[INET6_ADDRSTRLEN], bufport[6]; |
Amaury Denoyelle | 58d9d5d | 2023-02-01 11:54:43 +0100 | [diff] [blame] | 7660 | int expire; |
| 7661 | unsigned char cid_len; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7662 | |
| 7663 | thread_isolate(); |
| 7664 | |
| 7665 | if (ctx->thr >= global.nbthread) |
| 7666 | goto done; |
| 7667 | |
| 7668 | if (unlikely(sc_ic(sc)->flags & CF_SHUTW)) { |
| 7669 | /* If we're forced to shut down, we might have to remove our |
| 7670 | * reference to the last stream being dumped. |
| 7671 | */ |
| 7672 | if (!LIST_ISEMPTY(&ctx->bref.users)) |
| 7673 | LIST_DEL_INIT(&ctx->bref.users); |
| 7674 | goto done; |
| 7675 | } |
| 7676 | |
| 7677 | chunk_reset(&trash); |
| 7678 | |
| 7679 | if (!LIST_ISEMPTY(&ctx->bref.users)) { |
| 7680 | /* Remove show_quic_ctx from previous quic_conn instance. */ |
| 7681 | LIST_DEL_INIT(&ctx->bref.users); |
| 7682 | } |
| 7683 | else if (!ctx->bref.ref) { |
| 7684 | /* First invocation. */ |
| 7685 | ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n; |
| 7686 | } |
| 7687 | |
| 7688 | while (1) { |
| 7689 | int done = 0; |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 7690 | int i; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7691 | |
| 7692 | if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) { |
| 7693 | done = 1; |
| 7694 | } |
| 7695 | else { |
| 7696 | qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx); |
| 7697 | if ((int)(qc->qc_epoch - ctx->epoch) > 0) |
| 7698 | done = 1; |
| 7699 | } |
| 7700 | |
| 7701 | if (done) { |
| 7702 | ++ctx->thr; |
| 7703 | if (ctx->thr >= global.nbthread) |
| 7704 | break; |
| 7705 | ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n; |
| 7706 | continue; |
| 7707 | } |
| 7708 | |
Amaury Denoyelle | 10a46de | 2023-02-09 18:18:45 +0100 | [diff] [blame^] | 7709 | if (!(ctx->flags & QC_CLI_FL_SHOW_ALL) && |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 7710 | qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) { |
| 7711 | ctx->bref.ref = qc->el_th_ctx.n; |
| 7712 | continue; |
| 7713 | } |
| 7714 | |
Amaury Denoyelle | 58d9d5d | 2023-02-01 11:54:43 +0100 | [diff] [blame] | 7715 | /* CIDs */ |
| 7716 | chunk_appendf(&trash, "* %p[%02u]: scid=", qc, qc->tid); |
| 7717 | for (cid_len = 0; cid_len < qc->scid.len; ++cid_len) |
| 7718 | chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]); |
| 7719 | while (cid_len++ < 20) |
| 7720 | chunk_appendf(&trash, ".."); |
| 7721 | |
| 7722 | chunk_appendf(&trash, " dcid="); |
| 7723 | for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len) |
| 7724 | chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]); |
| 7725 | while (cid_len++ < 20) |
| 7726 | chunk_appendf(&trash, ".."); |
| 7727 | |
| 7728 | chunk_appendf(&trash, "\n"); |
| 7729 | |
| 7730 | /* Connection state */ |
| 7731 | if (qc->flags & QUIC_FL_CONN_CLOSING) |
| 7732 | chunk_appendf(&trash, " st=closing "); |
| 7733 | else if (qc->flags & QUIC_FL_CONN_DRAINING) |
| 7734 | chunk_appendf(&trash, " st=draining "); |
| 7735 | else if (qc->state < QUIC_HS_ST_CONFIRMED) |
| 7736 | chunk_appendf(&trash, " st=handshake "); |
| 7737 | else |
| 7738 | chunk_appendf(&trash, " st=opened "); |
| 7739 | |
| 7740 | if (qc->mux_state == QC_MUX_NULL) |
| 7741 | chunk_appendf(&trash, "mux=null "); |
| 7742 | else if (qc->mux_state == QC_MUX_READY) |
| 7743 | chunk_appendf(&trash, "mux=ready "); |
| 7744 | else |
| 7745 | chunk_appendf(&trash, "mux=released "); |
| 7746 | |
| 7747 | expire = qc->idle_timer_task->expire; |
| 7748 | chunk_appendf(&trash, "expire=%02ds ", |
| 7749 | expire > now_ms ? (expire - now_ms) / 1000 : 0); |
| 7750 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7751 | chunk_appendf(&trash, "\n"); |
| 7752 | |
Amaury Denoyelle | b89c0e2 | 2023-02-01 17:04:26 +0100 | [diff] [blame] | 7753 | /* Socket */ |
| 7754 | chunk_appendf(&trash, " fd=%d", qc->fd); |
| 7755 | if (qc->local_addr.ss_family == AF_INET || |
| 7756 | qc->local_addr.ss_family == AF_INET6) { |
| 7757 | addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr)); |
| 7758 | port_to_str(&qc->local_addr, bufport, sizeof(bufport)); |
| 7759 | chunk_appendf(&trash, " from=%s:%s", bufaddr, bufport); |
| 7760 | |
| 7761 | addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr)); |
| 7762 | port_to_str(&qc->peer_addr, bufport, sizeof(bufport)); |
| 7763 | chunk_appendf(&trash, " to=%s:%s", bufaddr, bufport); |
| 7764 | } |
| 7765 | |
| 7766 | chunk_appendf(&trash, "\n"); |
| 7767 | |
Amaury Denoyelle | 1b0fc43 | 2023-02-01 17:05:10 +0100 | [diff] [blame] | 7768 | /* Encryption levels */ |
| 7769 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 7770 | chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu", |
| 7771 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 7772 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 7773 | chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n", |
| 7774 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 7775 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
| 7776 | chunk_appendf(&trash, " [0-rtt] rx.ackrng=%-6zu tx.inflight=%-6zu", |
| 7777 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 7778 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 7779 | chunk_appendf(&trash, " [1-rtt] rx.ackrng=%-6zu tx.inflight=%-6zu", |
| 7780 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 7781 | |
| 7782 | chunk_appendf(&trash, "\n"); |
| 7783 | |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 7784 | /* Streams */ |
| 7785 | node = eb64_first(&qc->streams_by_id); |
| 7786 | i = 0; |
| 7787 | while (node) { |
| 7788 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 7789 | node = eb64_next(node); |
| 7790 | |
| 7791 | chunk_appendf(&trash, " | stream=%-8llu", (long long unsigned int)stream->by_id.key); |
| 7792 | chunk_appendf(&trash, " off=%-8lu ack=%-8lu", stream->buf_offset, stream->ack_offset); |
| 7793 | |
| 7794 | if (!(++i % 3)) { |
| 7795 | chunk_appendf(&trash, "\n"); |
| 7796 | i = 0; |
| 7797 | } |
| 7798 | } |
| 7799 | |
| 7800 | chunk_appendf(&trash, "\n"); |
| 7801 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7802 | if (applet_putchk(appctx, &trash) == -1) { |
| 7803 | /* Register show_quic_ctx to quic_conn instance. */ |
| 7804 | LIST_APPEND(&qc->back_refs, &ctx->bref.users); |
| 7805 | goto full; |
| 7806 | } |
| 7807 | |
| 7808 | ctx->bref.ref = qc->el_th_ctx.n; |
| 7809 | } |
| 7810 | |
| 7811 | done: |
| 7812 | thread_release(); |
| 7813 | return 1; |
| 7814 | |
| 7815 | full: |
| 7816 | thread_release(); |
| 7817 | return 0; |
| 7818 | } |
| 7819 | |
| 7820 | static void cli_release_show_quic(struct appctx *appctx) |
| 7821 | { |
| 7822 | struct show_quic_ctx *ctx = appctx->svcctx; |
| 7823 | |
| 7824 | if (ctx->thr < global.nbthread) { |
| 7825 | thread_isolate(); |
| 7826 | if (!LIST_ISEMPTY(&ctx->bref.users)) |
| 7827 | LIST_DEL_INIT(&ctx->bref.users); |
| 7828 | thread_release(); |
| 7829 | } |
| 7830 | } |
| 7831 | |
| 7832 | static struct cli_kw_list cli_kws = {{ }, { |
| 7833 | { { "show", "quic", NULL }, "show quic : display quic connections status", cli_parse_show_quic, cli_io_handler_dump_quic, cli_release_show_quic }, |
| 7834 | }}; |
| 7835 | |
| 7836 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 7837 | |
| 7838 | static void init_quic() |
| 7839 | { |
| 7840 | int thr; |
| 7841 | |
| 7842 | for (thr = 0; thr < MAX_THREADS; ++thr) |
| 7843 | LIST_INIT(&ha_thread_ctx[thr].quic_conns); |
| 7844 | } |
| 7845 | INITCALL0(STG_INIT, init_quic); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7846 | |
| 7847 | /* |
| 7848 | * Local variables: |
| 7849 | * c-indent-level: 8 |
| 7850 | * c-basic-offset: 8 |
| 7851 | * End: |
| 7852 | */ |