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 |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include <sys/socket.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | #include <netinet/tcp.h> |
| 26 | |
| 27 | #include <import/ebmbtree.h> |
| 28 | |
| 29 | #include <haproxy/buf-t.h> |
| 30 | #include <haproxy/compat.h> |
| 31 | #include <haproxy/api.h> |
| 32 | #include <haproxy/debug.h> |
| 33 | #include <haproxy/tools.h> |
| 34 | #include <haproxy/ticks.h> |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 35 | #include <haproxy/xxhash.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 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> |
Amaury Denoyelle | 331b8b1 | 2023-10-25 10:52:23 +0200 | [diff] [blame] | 42 | #include <haproxy/frontend.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 43 | #include <haproxy/global.h> |
| 44 | #include <haproxy/h3.h> |
| 45 | #include <haproxy/hq_interop.h> |
| 46 | #include <haproxy/log.h> |
| 47 | #include <haproxy/mux_quic.h> |
| 48 | #include <haproxy/ncbuf.h> |
| 49 | #include <haproxy/pipe.h> |
| 50 | #include <haproxy/proxy.h> |
| 51 | #include <haproxy/quic_cc.h> |
| 52 | #include <haproxy/quic_frame.h> |
| 53 | #include <haproxy/quic_enc.h> |
| 54 | #include <haproxy/quic_loss.h> |
| 55 | #include <haproxy/quic_sock.h> |
| 56 | #include <haproxy/quic_stats.h> |
| 57 | #include <haproxy/quic_stream.h> |
| 58 | #include <haproxy/quic_tp.h> |
| 59 | #include <haproxy/cbuf.h> |
| 60 | #include <haproxy/proto_quic.h> |
| 61 | #include <haproxy/quic_tls.h> |
| 62 | #include <haproxy/ssl_sock.h> |
| 63 | #include <haproxy/task.h> |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 64 | #include <haproxy/thread.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 65 | #include <haproxy/trace.h> |
| 66 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 67 | /* incremented by each "show quic". */ |
| 68 | static unsigned int qc_epoch = 0; |
| 69 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 70 | /* list of supported QUIC versions by this implementation */ |
| 71 | const struct quic_version quic_versions[] = { |
| 72 | { |
| 73 | .num = QUIC_PROTOCOL_VERSION_DRAFT_29, |
| 74 | .initial_salt = initial_salt_draft_29, |
| 75 | .initial_salt_len = sizeof initial_salt_draft_29, |
| 76 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
| 77 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
| 78 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
| 79 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
| 80 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
| 81 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
| 82 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
| 83 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
| 84 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT, |
| 85 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT, |
| 86 | }, |
| 87 | { |
| 88 | .num = QUIC_PROTOCOL_VERSION_1, |
| 89 | .initial_salt = initial_salt_v1, |
| 90 | .initial_salt_len = sizeof initial_salt_v1, |
| 91 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
| 92 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
| 93 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
| 94 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
| 95 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
| 96 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
| 97 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
| 98 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
| 99 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1, |
| 100 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1, |
| 101 | }, |
| 102 | { |
Frédéric Lécaille | 21c4c9b | 2023-01-13 16:37:02 +0100 | [diff] [blame] | 103 | .num = QUIC_PROTOCOL_VERSION_2, |
| 104 | .initial_salt = initial_salt_v2, |
| 105 | .initial_salt_len = sizeof initial_salt_v2, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 106 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2, |
| 107 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1, |
| 108 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2, |
| 109 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1, |
| 110 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2, |
| 111 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1, |
| 112 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2, |
| 113 | .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] | 114 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2, |
| 115 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 116 | }, |
| 117 | }; |
| 118 | |
| 119 | /* The total number of supported versions */ |
| 120 | const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions; |
| 121 | /* Listener only preferred version */ |
| 122 | const struct quic_version *preferred_version; |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 123 | /* RFC 8999 5.4. Version |
| 124 | * A Version field with a |
| 125 | * value of 0x00000000 is reserved for version negotiation |
| 126 | */ |
| 127 | const struct quic_version quic_version_VN_reserved = { .num = 0, }; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 128 | |
| 129 | /* trace source and events */ |
| 130 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 131 | const struct trace_source *src, |
| 132 | const struct ist where, const struct ist func, |
| 133 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 134 | |
| 135 | static const struct trace_event quic_trace_events[] = { |
| 136 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 137 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 138 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 139 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 140 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 141 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 142 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
| 143 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
| 144 | { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" }, |
| 145 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 146 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 147 | { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" }, |
| 148 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 149 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 150 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 151 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 152 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 153 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 154 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 155 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 156 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 157 | { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" }, |
| 158 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 159 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 160 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 161 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 162 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
| 163 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 164 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 165 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 166 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 167 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 168 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
| 169 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
| 170 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 171 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
| 172 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 173 | { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." }, |
| 174 | { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."}, |
| 175 | { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"}, |
| 176 | { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"}, |
| 177 | { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"}, |
| 178 | { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"}, |
| 179 | { .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] | 180 | { .mask = QUIC_EV_CONN_RCV, .name = "conn_recv", .desc = "RX on connection" }, |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 181 | { .mask = QUIC_EV_CONN_SET_AFFINITY, .name = "conn_set_affinity", .desc = "set connection thread affinity" }, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 182 | { /* end */ } |
| 183 | }; |
| 184 | |
| 185 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 186 | /* arg1 */ { /* already used by the connection */ }, |
| 187 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 188 | /* arg3 */ { }, |
| 189 | /* arg4 */ { } |
| 190 | }; |
| 191 | |
| 192 | static const struct name_desc quic_trace_decoding[] = { |
| 193 | #define QUIC_VERB_CLEAN 1 |
| 194 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 195 | { /* end */ } |
| 196 | }; |
| 197 | |
| 198 | |
| 199 | struct trace_source trace_quic = { |
| 200 | .name = IST("quic"), |
| 201 | .desc = "QUIC xprt", |
| 202 | .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */ |
| 203 | .default_cb = quic_trace, |
| 204 | .known_events = quic_trace_events, |
| 205 | .lockon_args = quic_trace_lockon_args, |
| 206 | .decoding = quic_trace_decoding, |
| 207 | .report_events = ~0, /* report everything by default */ |
| 208 | }; |
| 209 | |
| 210 | #define TRACE_SOURCE &trace_quic |
| 211 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 212 | |
| 213 | static BIO_METHOD *ha_quic_meth; |
| 214 | |
| 215 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ); |
| 216 | DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ); |
| 217 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
| 218 | "quic_conn_ctx", sizeof(struct ssl_sock_ctx)); |
| 219 | DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn)); |
| 220 | DECLARE_POOL(pool_head_quic_connection_id, |
Frédéric Lécaille | a946125 | 2023-04-24 18:20:44 +0200 | [diff] [blame] | 221 | "quic_connection_id", sizeof(struct quic_connection_id)); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 222 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
| 223 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet)); |
| 224 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet)); |
| 225 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm)); |
| 226 | 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] | 227 | 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] | 228 | DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame)); |
| 229 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node)); |
| 230 | |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 231 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 232 | struct quic_conn *qc, |
| 233 | const struct quic_cid *odcid, |
| 234 | const struct sockaddr_storage *saddr); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 235 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
| 236 | struct quic_enc_level *qel, struct quic_tls_ctx *ctx, |
| 237 | struct list *frms, struct quic_conn *qc, |
| 238 | const struct quic_version *ver, size_t dglen, int pkt_type, |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 239 | int must_ack, int padding, int probe, int cc, int *err); |
Frédéric Lécaille | 2101700 | 2023-11-08 11:31:21 +0100 | [diff] [blame] | 240 | static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf); |
| 241 | static void qc_purge_tx_buf(struct buffer *buf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 242 | struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state); |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 243 | static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack); |
| 244 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 245 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc); |
| 246 | static int quic_conn_init_timer(struct quic_conn *qc); |
| 247 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc); |
| 248 | |
| 249 | /* Only for debug purpose */ |
| 250 | struct enc_debug_info { |
| 251 | unsigned char *payload; |
| 252 | size_t payload_len; |
| 253 | unsigned char *aad; |
| 254 | size_t aad_len; |
| 255 | uint64_t pn; |
| 256 | }; |
| 257 | |
| 258 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 259 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 260 | unsigned char *payload, size_t payload_len, |
| 261 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 262 | { |
| 263 | edi->payload = payload; |
| 264 | edi->payload_len = payload_len; |
| 265 | edi->aad = aad; |
| 266 | edi->aad_len = aad_len; |
| 267 | edi->pn = pn; |
| 268 | } |
| 269 | |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 270 | /* Used only for QUIC TLS key phase traces */ |
| 271 | struct quic_kp_trace { |
| 272 | const unsigned char *rx_sec; |
| 273 | size_t rx_seclen; |
| 274 | const struct quic_tls_kp *rx; |
| 275 | const unsigned char *tx_sec; |
| 276 | size_t tx_seclen; |
| 277 | const struct quic_tls_kp *tx; |
| 278 | }; |
| 279 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 280 | /* Trace callback for QUIC. |
| 281 | * These traces always expect that arg1, if non-null, is of type connection. |
| 282 | */ |
| 283 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 284 | const struct ist where, const struct ist func, |
| 285 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 286 | { |
| 287 | const struct quic_conn *qc = a1; |
| 288 | |
| 289 | if (qc) { |
| 290 | const struct quic_tls_ctx *tls_ctx; |
| 291 | |
Frédéric Lécaille | eb3e517 | 2023-04-12 13:41:54 +0200 | [diff] [blame] | 292 | chunk_appendf(&trace_buf, " : qc@%p flags=0x%x", qc, qc->flags); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 293 | if (mask & QUIC_EV_CONN_INIT) { |
| 294 | chunk_appendf(&trace_buf, "\n odcid"); |
| 295 | quic_cid_dump(&trace_buf, &qc->odcid); |
| 296 | chunk_appendf(&trace_buf, "\n dcid"); |
| 297 | quic_cid_dump(&trace_buf, &qc->dcid); |
| 298 | chunk_appendf(&trace_buf, "\n scid"); |
| 299 | quic_cid_dump(&trace_buf, &qc->scid); |
| 300 | } |
| 301 | |
| 302 | if (mask & QUIC_EV_TRANSP_PARAMS) { |
| 303 | const struct quic_transport_params *p = a2; |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 304 | |
| 305 | if (p) |
| 306 | quic_transport_params_dump(&trace_buf, qc, p); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 310 | const enum ssl_encryption_level_t *level = a2; |
| 311 | const size_t *len = a3; |
| 312 | |
| 313 | if (level) { |
| 314 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 315 | |
| 316 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 317 | } |
| 318 | if (len) |
| 319 | chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len); |
| 320 | } |
| 321 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 322 | /* Initial read & write secrets. */ |
| 323 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 324 | const unsigned char *rx_sec = a2; |
| 325 | const unsigned char *tx_sec = a3; |
| 326 | |
| 327 | tls_ctx = &qc->els[level].tls_ctx; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 328 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 329 | if (rx_sec) |
| 330 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 331 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
| 332 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 333 | if (tx_sec) |
| 334 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 335 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 336 | } |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 337 | |
| 338 | if ((mask & QUIC_EV_CONN_KP) && qc) { |
| 339 | /* Initial read & write secrets. */ |
| 340 | const struct quic_kp_trace *kp = a2; |
| 341 | |
| 342 | if (kp) { |
| 343 | if (kp->rx) { |
| 344 | chunk_appendf(&trace_buf, "\n RX kp"); |
| 345 | if (kp->rx_sec) |
| 346 | quic_tls_secret_hexdump(&trace_buf, kp->rx_sec, kp->rx_seclen); |
| 347 | quic_tls_kp_keys_hexdump(&trace_buf, kp->rx); |
| 348 | } |
| 349 | if (kp->tx) { |
| 350 | chunk_appendf(&trace_buf, "\n TX kp"); |
| 351 | if (kp->tx_sec) |
| 352 | quic_tls_secret_hexdump(&trace_buf, kp->tx_sec, kp->tx_seclen); |
| 353 | quic_tls_kp_keys_hexdump(&trace_buf, kp->tx); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 358 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 359 | const enum ssl_encryption_level_t *level = a2; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 360 | |
| 361 | if (level) { |
| 362 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 363 | |
| 364 | 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] | 365 | if (quic_tls_has_rx_sec(&qc->els[lvl])) { |
| 366 | tls_ctx = &qc->els[lvl].tls_ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 367 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 368 | } |
| 369 | else |
| 370 | chunk_appendf(&trace_buf, " (none)"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 375 | const enum ssl_encryption_level_t *level = a2; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 376 | |
| 377 | if (level) { |
| 378 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 379 | |
| 380 | 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] | 381 | if (quic_tls_has_tx_sec(&qc->els[lvl])) { |
| 382 | tls_ctx = &qc->els[lvl].tls_ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 383 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 384 | } |
| 385 | else |
| 386 | chunk_appendf(&trace_buf, " (none)"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | } |
| 390 | |
| 391 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 392 | const struct list *l = a2; |
| 393 | |
| 394 | if (l) { |
| 395 | const struct quic_frame *frm; |
| 396 | list_for_each_entry(frm, l, list) { |
| 397 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 398 | chunk_frm_appendf(&trace_buf, frm); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) { |
| 404 | const struct quic_tx_packet *pkt = a2; |
| 405 | const struct quic_enc_level *qel = a3; |
| 406 | const ssize_t *room = a4; |
| 407 | |
| 408 | if (qel) { |
| 409 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | 91369cf | 2023-04-13 15:55:49 +0200 | [diff] [blame] | 410 | chunk_appendf(&trace_buf, " qel=%c flags=0x%x pto_count=%d cwnd=%llu ppif=%lld pif=%llu " |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 411 | "if=%llu pp=%u", |
| 412 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 91369cf | 2023-04-13 15:55:49 +0200 | [diff] [blame] | 413 | qel->pktns->flags, |
Frédéric Lécaille | 4540053 | 2023-02-13 18:39:19 +0100 | [diff] [blame] | 414 | qc->path->loss.pto_count, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 415 | (unsigned long long)qc->path->cwnd, |
| 416 | (unsigned long long)qc->path->prep_in_flight, |
| 417 | (unsigned long long)qc->path->in_flight, |
| 418 | (unsigned long long)pktns->tx.in_flight, |
| 419 | pktns->tx.pto_probe); |
| 420 | } |
| 421 | if (pkt) { |
| 422 | const struct quic_frame *frm; |
| 423 | if (pkt->pn_node.key != (uint64_t)-1) |
| 424 | chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key); |
| 425 | list_for_each_entry(frm, &pkt->frms, list) { |
| 426 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 427 | chunk_frm_appendf(&trace_buf, frm); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | if (room) { |
| 432 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 433 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 434 | (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | if (mask & QUIC_EV_CONN_IO_CB) { |
| 439 | const enum quic_handshake_state *state = a2; |
| 440 | const int *err = a3; |
| 441 | |
| 442 | if (state) |
| 443 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 444 | if (err) |
| 445 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 446 | } |
| 447 | |
| 448 | if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) { |
| 449 | const struct quic_rx_packet *pkt = a2; |
| 450 | const unsigned long *pktlen = a3; |
| 451 | const SSL *ssl = a4; |
| 452 | |
| 453 | if (pkt) { |
| 454 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 455 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 456 | chunk_appendf(&trace_buf, " kp=%d", |
| 457 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 458 | chunk_appendf(&trace_buf, " el=%c", |
| 459 | quic_packet_type_enc_level_char(pkt->type)); |
| 460 | if (pkt->pnl) |
| 461 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 462 | (unsigned long long)pkt->pn); |
| 463 | if (pkt->token_len) |
| 464 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 465 | (unsigned long long)pkt->token_len); |
| 466 | if (pkt->aad_len) |
| 467 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 468 | (unsigned long long)pkt->aad_len); |
| 469 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 470 | pkt->flags, (unsigned long long)pkt->len); |
| 471 | } |
| 472 | if (pktlen) |
| 473 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 474 | if (ssl) { |
| 475 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 476 | chunk_appendf(&trace_buf, " el=%c", |
| 477 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 482 | const struct quic_rx_packet *pkt = a2; |
| 483 | const struct quic_rx_crypto_frm *cf = a3; |
| 484 | const SSL *ssl = a4; |
| 485 | |
| 486 | if (pkt) |
| 487 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 488 | quic_packet_type_enc_level_char(pkt->type), |
| 489 | (unsigned long long)pkt->pn); |
| 490 | if (cf) |
| 491 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 492 | (unsigned long long)cf->offset_node.key, |
| 493 | (unsigned long long)cf->len); |
| 494 | if (ssl) { |
| 495 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 496 | chunk_appendf(&trace_buf, " rel=%c", |
| 497 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 498 | } |
| 499 | |
| 500 | if (qc->err.code) |
| 501 | chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code); |
| 502 | } |
| 503 | |
| 504 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 505 | const struct quic_frame *frm = a2; |
| 506 | |
| 507 | if (frm) |
| 508 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 509 | } |
| 510 | |
| 511 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 512 | const struct quic_enc_level *qel = a2; |
Frédéric Lécaille | e95e00e | 2023-04-24 10:59:33 +0200 | [diff] [blame] | 513 | const struct list *l = a3; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 514 | |
| 515 | if (qel) { |
| 516 | const struct quic_pktns *pktns = qel->pktns; |
| 517 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 91369cf | 2023-04-13 15:55:49 +0200 | [diff] [blame] | 518 | " qel=%c flags=0x%x state=%s ack?%d pto_count=%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] | 519 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 91369cf | 2023-04-13 15:55:49 +0200 | [diff] [blame] | 520 | qel->pktns->flags, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 521 | quic_hdshk_state_str(qc->state), |
| 522 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 4540053 | 2023-02-13 18:39:19 +0100 | [diff] [blame] | 523 | qc->path->loss.pto_count, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 524 | (unsigned long long)qc->path->cwnd, |
| 525 | (unsigned long long)qc->path->prep_in_flight, |
| 526 | (unsigned long long)qc->path->in_flight, |
| 527 | (unsigned long long)pktns->tx.in_flight, |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 528 | pktns->tx.pto_probe, |
| 529 | qel->cstream ? (unsigned long long)qel->cstream->rx.offset : 0); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 530 | } |
Frédéric Lécaille | e95e00e | 2023-04-24 10:59:33 +0200 | [diff] [blame] | 531 | |
| 532 | if (l) { |
| 533 | const struct quic_frame *frm; |
| 534 | list_for_each_entry(frm, l, list) { |
| 535 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 536 | chunk_frm_appendf(&trace_buf, frm); |
| 537 | } |
| 538 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 542 | const struct enc_debug_info *edi = a2; |
| 543 | |
| 544 | if (edi) |
| 545 | chunk_appendf(&trace_buf, |
| 546 | " payload=@%p payload_len=%llu" |
| 547 | " aad=@%p aad_len=%llu pn=%llu", |
| 548 | edi->payload, (unsigned long long)edi->payload_len, |
| 549 | edi->aad, (unsigned long long)edi->aad_len, |
| 550 | (unsigned long long)edi->pn); |
| 551 | } |
| 552 | |
| 553 | if (mask & QUIC_EV_CONN_RMHP) { |
| 554 | const struct quic_rx_packet *pkt = a2; |
| 555 | |
| 556 | if (pkt) { |
| 557 | const int *ret = a3; |
| 558 | |
| 559 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 560 | if (ret && *ret) |
| 561 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 562 | pkt->pnl, (unsigned long long)pkt->pn); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
| 567 | const struct quic_frame *frm = a2; |
| 568 | const unsigned long *val1 = a3; |
| 569 | const unsigned long *val2 = a4; |
| 570 | |
| 571 | if (frm) { |
| 572 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 573 | chunk_frm_appendf(&trace_buf, frm); |
| 574 | } |
| 575 | if (val1) |
| 576 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 577 | if (val2) |
| 578 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 579 | } |
| 580 | |
| 581 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 582 | const struct qf_stream *strm_frm = a2; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 583 | const struct qc_stream_desc *stream = a3; |
| 584 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 585 | if (strm_frm) |
| 586 | chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)strm_frm->offset.key, (ull)strm_frm->len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 587 | if (stream) |
| 588 | chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset); |
| 589 | } |
| 590 | |
| 591 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 592 | const unsigned int *rtt_sample = a2; |
| 593 | const unsigned int *ack_delay = a3; |
| 594 | const struct quic_loss *ql = a4; |
| 595 | |
| 596 | if (rtt_sample) |
| 597 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 598 | if (ack_delay) |
| 599 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 600 | if (ql) |
| 601 | chunk_appendf(&trace_buf, |
| 602 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
Frédéric Lécaille | 35fb593 | 2023-09-05 15:24:11 +0200 | [diff] [blame] | 603 | ql->srtt, ql->rtt_var, ql->rtt_min); |
| 604 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 605 | } |
| 606 | if (mask & QUIC_EV_CONN_CC) { |
| 607 | const struct quic_cc_event *ev = a2; |
| 608 | const struct quic_cc *cc = a3; |
| 609 | |
| 610 | if (a2) |
| 611 | quic_cc_event_trace(&trace_buf, ev); |
| 612 | if (a3) |
| 613 | quic_cc_state_trace(&trace_buf, cc); |
| 614 | } |
| 615 | |
| 616 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 617 | const struct quic_pktns *pktns = a2; |
| 618 | const struct list *lost_pkts = a3; |
| 619 | |
| 620 | if (pktns) { |
| 621 | chunk_appendf(&trace_buf, " pktns=%s", |
| 622 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 623 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 624 | if (pktns->tx.loss_time) |
| 625 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 626 | TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time))); |
| 627 | } |
| 628 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 629 | struct quic_tx_packet *pkt; |
| 630 | |
| 631 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 632 | list_for_each_entry(pkt, lost_pkts, list) |
| 633 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) { |
| 638 | const struct quic_pktns *pktns = a2; |
| 639 | const int *duration = a3; |
| 640 | const uint64_t *ifae_pkts = a4; |
| 641 | |
| 642 | if (ifae_pkts) |
| 643 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 644 | (unsigned long long)*ifae_pkts); |
| 645 | if (pktns) { |
| 646 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
| 647 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 648 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 649 | pktns->tx.pto_probe); |
| 650 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 651 | if (pktns->tx.in_flight) |
| 652 | chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight); |
| 653 | if (pktns->tx.loss_time) |
| 654 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 655 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 656 | } |
| 657 | if (mask & QUIC_EV_CONN_SPTO) { |
| 658 | if (pktns->tx.time_of_last_eliciting) |
| 659 | chunk_appendf(&trace_buf, " tole=%dms", |
| 660 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 661 | if (duration) |
| 662 | chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration)); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) { |
| 667 | chunk_appendf(&trace_buf, |
| 668 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 673 | const struct quic_tx_packet *pkt = a2; |
| 674 | |
Frédéric Lécaille | 4540053 | 2023-02-13 18:39:19 +0100 | [diff] [blame] | 675 | chunk_appendf(&trace_buf, " pto_count=%d cwnd=%llu ppif=%llu pif=%llu", |
| 676 | qc->path->loss.pto_count, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 677 | (unsigned long long)qc->path->cwnd, |
| 678 | (unsigned long long)qc->path->prep_in_flight, |
| 679 | (unsigned long long)qc->path->in_flight); |
| 680 | if (pkt) { |
| 681 | const struct quic_frame *frm; |
Frédéric Lécaille | 6fd2576 | 2023-04-07 19:01:33 +0200 | [diff] [blame] | 682 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) |
| 683 | chunk_appendf(&trace_buf, " ack"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 684 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
| 685 | (unsigned long)pkt->pn_node.key, |
| 686 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 687 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 688 | (unsigned long long)pkt->in_flight_len); |
| 689 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 690 | (unsigned long long)qc->rx.bytes, |
| 691 | (unsigned long long)qc->tx.bytes); |
| 692 | list_for_each_entry(frm, &pkt->frms, list) { |
| 693 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 694 | chunk_frm_appendf(&trace_buf, frm); |
| 695 | } |
Frédéric Lécaille | bc09f74 | 2023-02-13 17:45:36 +0100 | [diff] [blame] | 696 | |
| 697 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 698 | chunk_appendf(&trace_buf, " with scid"); |
| 699 | quic_cid_dump(&trace_buf, &qc->scid); |
| 700 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
| 704 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 705 | const uint8_t *alert = a2; |
| 706 | const enum ssl_encryption_level_t *level = a3; |
| 707 | |
| 708 | if (alert) |
| 709 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 710 | if (level) |
| 711 | chunk_appendf(&trace_buf, " el=%c", |
| 712 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 713 | } |
| 714 | |
| 715 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 716 | const size_t *sz1 = a2; |
| 717 | const size_t *sz2 = a3; |
| 718 | const size_t *sz3 = a4; |
| 719 | |
| 720 | if (sz1) |
| 721 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 722 | if (sz2) |
| 723 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 724 | if (sz3) |
| 725 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 726 | } |
| 727 | |
| 728 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 729 | const struct quic_frame *frm = a2; |
| 730 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 731 | if (frm) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 732 | chunk_frm_appendf(&trace_buf, frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 733 | } |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 734 | |
| 735 | if (mask & QUIC_EV_CONN_ELEVELSEL) { |
| 736 | const enum quic_handshake_state *state = a2; |
| 737 | const enum quic_tls_enc_level *level = a3; |
| 738 | const enum quic_tls_enc_level *next_level = a4; |
| 739 | |
| 740 | if (state) |
| 741 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(qc->state)); |
| 742 | if (level) |
| 743 | chunk_appendf(&trace_buf, " level=%c", quic_enc_level_char(*level)); |
| 744 | if (next_level) |
| 745 | chunk_appendf(&trace_buf, " next_level=%c", quic_enc_level_char(*next_level)); |
| 746 | |
| 747 | } |
Amaury Denoyelle | 5b41486 | 2022-10-24 17:40:37 +0200 | [diff] [blame] | 748 | |
Frédéric Lécaille | 495968e | 2023-04-03 17:42:05 +0200 | [diff] [blame] | 749 | if (mask & QUIC_EV_CONN_IDLE_TIMER) { |
| 750 | if (tick_isset(qc->ack_expire)) |
| 751 | chunk_appendf(&trace_buf, " ack_expire=%ums", |
| 752 | TICKS_TO_MS(tick_remain(now_ms, qc->ack_expire))); |
| 753 | if (tick_isset(qc->idle_expire)) |
| 754 | chunk_appendf(&trace_buf, " idle_expire=%ums", |
| 755 | TICKS_TO_MS(tick_remain(now_ms, qc->idle_expire))); |
Frédéric Lécaille | ce5c145 | 2023-04-05 09:44:21 +0200 | [diff] [blame] | 756 | if (qc->idle_timer_task && tick_isset(qc->idle_timer_task->expire)) |
Frédéric Lécaille | 495968e | 2023-04-03 17:42:05 +0200 | [diff] [blame] | 757 | chunk_appendf(&trace_buf, " expire=%ums", |
| 758 | TICKS_TO_MS(tick_remain(now_ms, qc->idle_timer_task->expire))); |
| 759 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 760 | } |
Frédéric Lécaille | aaf32f0 | 2023-05-12 17:37:29 +0200 | [diff] [blame] | 761 | |
| 762 | if (mask & QUIC_EV_CONN_RCV) { |
| 763 | int i; |
| 764 | const struct quic_dgram *dgram = a2; |
| 765 | char bufaddr[INET6_ADDRSTRLEN], bufport[6]; |
| 766 | |
| 767 | if (qc) { |
| 768 | addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr)); |
| 769 | port_to_str(&qc->peer_addr, bufport, sizeof(bufport)); |
| 770 | chunk_appendf(&trace_buf, " peer_addr=%s:%s ", bufaddr, bufport); |
| 771 | } |
| 772 | |
| 773 | if (dgram) { |
| 774 | chunk_appendf(&trace_buf, " dgram.len=%zu", dgram->len); |
| 775 | /* Socket */ |
| 776 | if (dgram->saddr.ss_family == AF_INET || |
| 777 | dgram->saddr.ss_family == AF_INET6) { |
| 778 | addr_to_str(&dgram->saddr, bufaddr, sizeof(bufaddr)); |
| 779 | port_to_str(&dgram->saddr, bufport, sizeof(bufport)); |
| 780 | chunk_appendf(&trace_buf, "saddr=%s:%s ", bufaddr, bufport); |
| 781 | |
| 782 | addr_to_str(&dgram->daddr, bufaddr, sizeof(bufaddr)); |
| 783 | port_to_str(&dgram->daddr, bufport, sizeof(bufport)); |
| 784 | chunk_appendf(&trace_buf, "daddr=%s:%s ", bufaddr, bufport); |
| 785 | } |
| 786 | /* DCID */ |
| 787 | for (i = 0; i < dgram->dcid_len; ++i) |
| 788 | chunk_appendf(&trace_buf, "%02x", dgram->dcid[i]); |
| 789 | |
| 790 | } |
| 791 | } |
| 792 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 793 | if (mask & QUIC_EV_CONN_LPKT) { |
| 794 | const struct quic_rx_packet *pkt = a2; |
| 795 | const uint64_t *len = a3; |
| 796 | const struct quic_version *ver = a4; |
| 797 | |
| 798 | if (pkt) { |
| 799 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 800 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 801 | if (pkt->pn_node.key != (uint64_t)-1) |
| 802 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 803 | } |
| 804 | |
| 805 | if (len) |
| 806 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
| 807 | |
| 808 | if (ver) |
| 809 | chunk_appendf(&trace_buf, " ver=0x%08x", ver->num); |
| 810 | } |
| 811 | |
| 812 | if (mask & QUIC_EV_STATELESS_RST) { |
| 813 | const struct quic_cid *cid = a2; |
| 814 | |
| 815 | if (cid) |
| 816 | quic_cid_dump(&trace_buf, cid); |
| 817 | } |
| 818 | |
| 819 | } |
| 820 | |
| 821 | /* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */ |
| 822 | static inline int quic_peer_validated_addr(struct quic_conn *qc) |
| 823 | { |
| 824 | struct quic_pktns *hdshk_pktns, *app_pktns; |
| 825 | |
| 826 | if (!qc_is_listener(qc)) |
| 827 | return 1; |
| 828 | |
| 829 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 830 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 831 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 832 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 833 | qc->state >= QUIC_HS_ST_COMPLETE) |
| 834 | return 1; |
| 835 | |
| 836 | return 0; |
| 837 | } |
| 838 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 839 | /* To be called to kill a connection as soon as possible (without sending any packet). */ |
| 840 | void qc_kill_conn(struct quic_conn *qc) |
| 841 | { |
Frédéric Lécaille | 2f53111 | 2023-02-10 14:44:51 +0100 | [diff] [blame] | 842 | TRACE_ENTER(QUIC_EV_CONN_KILL, qc); |
Frédéric Lécaille | 495968e | 2023-04-03 17:42:05 +0200 | [diff] [blame] | 843 | TRACE_PROTO("killing the connection", QUIC_EV_CONN_KILL, qc); |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 844 | qc->flags |= QUIC_FL_CONN_TO_KILL; |
Frédéric Lécaille | f4bec87 | 2023-11-20 16:54:57 +0100 | [diff] [blame] | 845 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
Amaury Denoyelle | eaa32c6 | 2024-06-04 11:56:09 +0200 | [diff] [blame] | 846 | |
| 847 | if (!(qc->flags & QUIC_FL_CONN_EXP_TIMER)) |
| 848 | task_wakeup(qc->idle_timer_task, TASK_WOKEN_OTHER); |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 849 | |
| 850 | qc_notify_err(qc); |
| 851 | |
Frédéric Lécaille | 2f53111 | 2023-02-10 14:44:51 +0100 | [diff] [blame] | 852 | TRACE_LEAVE(QUIC_EV_CONN_KILL, qc); |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 853 | } |
| 854 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 855 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 856 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 857 | */ |
| 858 | static inline void qc_set_timer(struct quic_conn *qc) |
| 859 | { |
| 860 | struct quic_pktns *pktns; |
| 861 | unsigned int pto; |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 862 | int handshake_confirmed; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 863 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 864 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc); |
| 865 | TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 866 | |
Frédéric Lécaille | dd41a45 | 2023-02-09 07:48:33 +0100 | [diff] [blame] | 867 | pktns = NULL; |
| 868 | if (!qc->timer_task) { |
| 869 | TRACE_PROTO("already released timer task", QUIC_EV_CONN_STIMER, qc); |
| 870 | goto leave; |
| 871 | } |
| 872 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 873 | pktns = quic_loss_pktns(qc); |
| 874 | if (tick_isset(pktns->tx.loss_time)) { |
| 875 | qc->timer = pktns->tx.loss_time; |
| 876 | goto out; |
| 877 | } |
| 878 | |
| 879 | /* anti-amplification: the timer must be |
| 880 | * cancelled for a server which reached the anti-amplification limit. |
| 881 | */ |
| 882 | if (!quic_peer_validated_addr(qc) && |
| 883 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
| 884 | TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc); |
| 885 | qc->timer = TICK_ETERNITY; |
| 886 | goto out; |
| 887 | } |
| 888 | |
| 889 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
| 890 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
| 891 | /* Timer cancellation. */ |
| 892 | qc->timer = TICK_ETERNITY; |
| 893 | goto out; |
| 894 | } |
| 895 | |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 896 | handshake_confirmed = qc->state >= QUIC_HS_ST_CONFIRMED; |
| 897 | pktns = quic_pto_pktns(qc, handshake_confirmed, &pto); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 898 | if (tick_isset(pto)) |
| 899 | qc->timer = pto; |
| 900 | out: |
Frédéric Lécaille | dd41a45 | 2023-02-09 07:48:33 +0100 | [diff] [blame] | 901 | if (qc->timer == TICK_ETERNITY) { |
| 902 | qc->timer_task->expire = TICK_ETERNITY; |
| 903 | } |
| 904 | else if (tick_is_expired(qc->timer, now_ms)) { |
| 905 | TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc); |
| 906 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 907 | } |
| 908 | else { |
| 909 | TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc); |
| 910 | task_schedule(qc->timer_task, qc->timer); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 911 | } |
Frédéric Lécaille | dd41a45 | 2023-02-09 07:48:33 +0100 | [diff] [blame] | 912 | leave: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 913 | TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, pktns); |
| 914 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 918 | * connection. |
| 919 | * Return 1 if succeeded, 0 if not. |
| 920 | */ |
| 921 | static int quic_tls_key_update(struct quic_conn *qc) |
| 922 | { |
| 923 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 924 | struct quic_tls_secrets *rx = &tls_ctx->rx; |
| 925 | struct quic_tls_secrets *tx = &tls_ctx->tx; |
| 926 | /* Used only for the traces */ |
| 927 | struct quic_kp_trace kp_trace = { |
| 928 | .rx_sec = rx->secret, |
| 929 | .rx_seclen = rx->secretlen, |
| 930 | .tx_sec = tx->secret, |
| 931 | .tx_seclen = tx->secretlen, |
| 932 | }; |
| 933 | /* The next key phase secrets to be derived */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 934 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 935 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 936 | const struct quic_version *ver = |
| 937 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
| 938 | int ret = 0; |
| 939 | |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 940 | TRACE_ENTER(QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 941 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 942 | nxt_rx = &qc->ku.nxt_rx; |
| 943 | nxt_tx = &qc->ku.nxt_tx; |
| 944 | |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 945 | TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0, |
| 946 | "nxt_rx->secretlen=%llu rx->secretlen=%llu", |
| 947 | (ull)nxt_rx->secretlen, (ull)rx->secretlen); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 948 | /* Prepare new RX secrets */ |
| 949 | if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen, |
| 950 | rx->secret, rx->secretlen)) { |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 951 | TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 952 | goto leave; |
| 953 | } |
| 954 | |
| 955 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver, |
| 956 | nxt_rx->key, nxt_rx->keylen, |
| 957 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 958 | nxt_rx->secret, nxt_rx->secretlen)) { |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 959 | TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 960 | goto leave; |
| 961 | } |
| 962 | |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 963 | kp_trace.rx = nxt_rx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 964 | /* Prepare new TX secrets */ |
| 965 | if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen, |
| 966 | tx->secret, tx->secretlen)) { |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 967 | TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 968 | goto leave; |
| 969 | } |
| 970 | |
| 971 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver, |
| 972 | nxt_tx->key, nxt_tx->keylen, |
| 973 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 974 | nxt_tx->secret, nxt_tx->secretlen)) { |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 975 | TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 976 | goto leave; |
| 977 | } |
| 978 | |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 979 | kp_trace.tx = nxt_tx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 980 | if (nxt_rx->ctx) { |
| 981 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 982 | nxt_rx->ctx = NULL; |
| 983 | } |
| 984 | |
| 985 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
Frédéric Lécaille | 7a01ff7 | 2023-05-02 20:03:19 +0200 | [diff] [blame] | 986 | TRACE_ERROR("could not initialize RX TLS cipher context", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 987 | goto leave; |
| 988 | } |
| 989 | |
| 990 | if (nxt_tx->ctx) { |
| 991 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 992 | nxt_tx->ctx = NULL; |
| 993 | } |
| 994 | |
Frédéric Lécaille | 7a01ff7 | 2023-05-02 20:03:19 +0200 | [diff] [blame] | 995 | if (!quic_tls_tx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
| 996 | TRACE_ERROR("could not initialize TX TLS cipher context", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 997 | goto leave; |
| 998 | } |
| 999 | |
| 1000 | ret = 1; |
| 1001 | leave: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 1002 | TRACE_PROTO("key update", QUIC_EV_CONN_KP, qc, &kp_trace); |
| 1003 | TRACE_LEAVE(QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1004 | return ret; |
| 1005 | } |
| 1006 | |
| 1007 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 1008 | * Must be used after having updated them. |
| 1009 | * Always succeeds. |
| 1010 | */ |
| 1011 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 1012 | { |
| 1013 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 1014 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 1015 | EVP_CIPHER_CTX *curr_ctx; |
| 1016 | |
| 1017 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 1018 | |
| 1019 | /* Rotate the RX secrets */ |
| 1020 | curr_ctx = tls_ctx->rx.ctx; |
| 1021 | curr_secret = tls_ctx->rx.secret; |
| 1022 | curr_iv = tls_ctx->rx.iv; |
| 1023 | curr_key = tls_ctx->rx.key; |
| 1024 | |
| 1025 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
| 1026 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 1027 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 1028 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 1029 | |
| 1030 | qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx; |
| 1031 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 1032 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 1033 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 1034 | |
| 1035 | qc->ku.prv_rx.ctx = curr_ctx; |
| 1036 | qc->ku.prv_rx.secret = curr_secret; |
| 1037 | qc->ku.prv_rx.iv = curr_iv; |
| 1038 | qc->ku.prv_rx.key = curr_key; |
| 1039 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 1040 | |
| 1041 | /* Update the TX secrets */ |
| 1042 | curr_ctx = tls_ctx->tx.ctx; |
| 1043 | curr_secret = tls_ctx->tx.secret; |
| 1044 | curr_iv = tls_ctx->tx.iv; |
| 1045 | curr_key = tls_ctx->tx.key; |
| 1046 | |
| 1047 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
| 1048 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 1049 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 1050 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 1051 | |
| 1052 | qc->ku.nxt_tx.ctx = curr_ctx; |
| 1053 | qc->ku.nxt_tx.secret = curr_secret; |
| 1054 | qc->ku.nxt_tx.iv = curr_iv; |
| 1055 | qc->ku.nxt_tx.key = curr_key; |
| 1056 | |
| 1057 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 1058 | } |
| 1059 | |
| 1060 | /* returns 0 on error, 1 on success */ |
| 1061 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 1062 | const uint8_t *read_secret, |
| 1063 | const uint8_t *write_secret, size_t secret_len) |
| 1064 | { |
| 1065 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1066 | struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 1067 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1068 | struct quic_tls_secrets *rx = NULL, *tx = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1069 | const struct quic_version *ver = |
| 1070 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
| 1071 | int ret = 0; |
| 1072 | |
| 1073 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
| 1074 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 1075 | |
| 1076 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 1077 | TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc); |
| 1078 | goto out; |
| 1079 | } |
| 1080 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1081 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 1082 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1083 | goto out; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1086 | if (!read_secret) |
| 1087 | goto write; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1088 | |
| 1089 | rx = &tls_ctx->rx; |
Frédéric Lécaille | 8fbabea | 2023-10-11 09:28:36 +0200 | [diff] [blame] | 1090 | rx->aead = tls_aead(cipher); |
| 1091 | rx->md = tls_md(cipher); |
| 1092 | rx->hp = tls_hp(cipher); |
| 1093 | if (!rx->aead || !rx->md || !rx->hp) |
| 1094 | goto leave; |
| 1095 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1096 | if (!quic_tls_secrets_keys_alloc(rx)) { |
| 1097 | TRACE_ERROR("RX keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 1098 | goto leave; |
| 1099 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1100 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1101 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen, |
| 1102 | rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key, |
| 1103 | read_secret, secret_len)) { |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1104 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1105 | goto leave; |
| 1106 | } |
| 1107 | |
| 1108 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 1109 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 1110 | goto leave; |
| 1111 | } |
| 1112 | |
| 1113 | if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) { |
| 1114 | TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 1115 | goto leave; |
| 1116 | } |
| 1117 | |
| 1118 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 1119 | * listener. Note that a listener derives only RX secrets for this |
| 1120 | * level. |
| 1121 | */ |
| 1122 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) { |
| 1123 | TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc); |
| 1124 | quic_accept_push_qc(qc); |
| 1125 | } |
| 1126 | |
| 1127 | write: |
| 1128 | |
| 1129 | if (!write_secret) |
Frédéric Lécaille | aada881 | 2023-06-06 17:40:41 +0200 | [diff] [blame] | 1130 | goto keyupdate_init; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1131 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1132 | tx = &tls_ctx->tx; |
Frédéric Lécaille | 8fbabea | 2023-10-11 09:28:36 +0200 | [diff] [blame] | 1133 | tx->aead = tls_aead(cipher); |
| 1134 | tx->md = tls_md(cipher); |
| 1135 | tx->hp = tls_hp(cipher); |
| 1136 | if (!tx->aead || !tx->md || !tx->hp) |
| 1137 | goto leave; |
| 1138 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1139 | if (!quic_tls_secrets_keys_alloc(tx)) { |
| 1140 | TRACE_ERROR("TX keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 1141 | goto leave; |
| 1142 | } |
| 1143 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1144 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen, |
| 1145 | tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key, |
| 1146 | write_secret, secret_len)) { |
| 1147 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 1148 | goto leave; |
| 1149 | } |
| 1150 | |
| 1151 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 1152 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 1153 | goto leave; |
| 1154 | } |
| 1155 | |
| 1156 | if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) { |
| 1157 | TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 1158 | goto leave; |
| 1159 | } |
| 1160 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 1161 | if (level == ssl_encryption_handshake && qc_is_listener(qc)) { |
| 1162 | qc->enc_params_len = |
| 1163 | quic_transport_params_encode(qc->enc_params, |
| 1164 | qc->enc_params + sizeof qc->enc_params, |
| 1165 | &qc->rx.params, ver, 1); |
| 1166 | if (!qc->enc_params_len) { |
| 1167 | TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_RWSEC); |
| 1168 | goto leave; |
| 1169 | } |
| 1170 | |
| 1171 | if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) { |
| 1172 | TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_RWSEC); |
| 1173 | goto leave; |
| 1174 | } |
| 1175 | } |
| 1176 | |
Frédéric Lécaille | aada881 | 2023-06-06 17:40:41 +0200 | [diff] [blame] | 1177 | keyupdate_init: |
| 1178 | /* Store the secret provided by the TLS stack, required for keyupdate. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1179 | if (level == ssl_encryption_application) { |
| 1180 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 1181 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 1182 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 1183 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1184 | if (rx) { |
| 1185 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
| 1186 | TRACE_ERROR("Could not allocate RX Application secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 1187 | goto leave; |
| 1188 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1189 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1190 | memcpy(rx->secret, read_secret, secret_len); |
| 1191 | rx->secretlen = secret_len; |
| 1192 | } |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1193 | |
| 1194 | if (tx) { |
| 1195 | if (!(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
| 1196 | TRACE_ERROR("Could not allocate TX Application secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 1197 | goto leave; |
| 1198 | } |
| 1199 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1200 | memcpy(tx->secret, write_secret, secret_len); |
| 1201 | tx->secretlen = secret_len; |
| 1202 | } |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1203 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1204 | /* Initialize all the secret keys lengths */ |
| 1205 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | out: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1209 | ret = 1; |
| 1210 | leave: |
Frédéric Lécaille | 8fbabea | 2023-10-11 09:28:36 +0200 | [diff] [blame] | 1211 | if (!ret) { |
| 1212 | /* Release the CRYPTO frames which have been provided by the TLS stack |
| 1213 | * to prevent the transmission of ack-eliciting packets. |
| 1214 | */ |
| 1215 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 1216 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
| 1217 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns); |
| 1218 | quic_set_tls_alert(qc, SSL_AD_HANDSHAKE_FAILURE); |
| 1219 | } |
| 1220 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1221 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |
| 1225 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 1226 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 1227 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 1228 | * It fails (returns 0) only if it could not managed to allocate enough CRYPTO |
| 1229 | * buffers to store all the data. |
| 1230 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 1231 | */ |
| 1232 | static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel, |
| 1233 | const unsigned char *data, size_t len) |
| 1234 | { |
| 1235 | struct quic_crypto_buf **qcb; |
| 1236 | /* The remaining byte to store in CRYPTO buffers. */ |
| 1237 | size_t cf_offset, cf_len, *nb_buf; |
| 1238 | unsigned char *pos; |
| 1239 | int ret = 0; |
| 1240 | |
| 1241 | nb_buf = &qel->tx.crypto.nb_buf; |
| 1242 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 1243 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 1244 | cf_len = len; |
| 1245 | |
| 1246 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1247 | |
| 1248 | while (len) { |
| 1249 | size_t to_copy, room; |
| 1250 | |
| 1251 | pos = (*qcb)->data + (*qcb)->sz; |
| 1252 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1253 | to_copy = len > room ? room : len; |
| 1254 | if (to_copy) { |
| 1255 | memcpy(pos, data, to_copy); |
| 1256 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1257 | qel->tx.crypto.sz += to_copy; |
| 1258 | (*qcb)->sz += to_copy; |
| 1259 | len -= to_copy; |
| 1260 | data += to_copy; |
| 1261 | } |
| 1262 | else { |
| 1263 | struct quic_crypto_buf **tmp; |
| 1264 | |
| 1265 | // FIXME: realloc! |
| 1266 | tmp = realloc(qel->tx.crypto.bufs, |
| 1267 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1268 | if (tmp) { |
| 1269 | qel->tx.crypto.bufs = tmp; |
| 1270 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1271 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1272 | if (!*qcb) { |
| 1273 | TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc); |
| 1274 | goto leave; |
| 1275 | } |
| 1276 | |
| 1277 | (*qcb)->sz = 0; |
| 1278 | ++*nb_buf; |
| 1279 | } |
| 1280 | else { |
| 1281 | break; |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1287 | * have been buffered. |
| 1288 | */ |
| 1289 | if (!len) { |
| 1290 | struct quic_frame *frm; |
| 1291 | struct quic_frame *found = NULL; |
| 1292 | |
| 1293 | /* There is at most one CRYPTO frame in this packet number |
| 1294 | * space. Let's look for it. |
| 1295 | */ |
| 1296 | list_for_each_entry(frm, &qel->pktns->tx.frms, list) { |
| 1297 | if (frm->type != QUIC_FT_CRYPTO) |
| 1298 | continue; |
| 1299 | |
| 1300 | /* Found */ |
| 1301 | found = frm; |
| 1302 | break; |
| 1303 | } |
| 1304 | |
| 1305 | if (found) { |
| 1306 | found->crypto.len += cf_len; |
| 1307 | } |
| 1308 | else { |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 1309 | frm = qc_frm_alloc(QUIC_FT_CRYPTO); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1310 | if (!frm) { |
| 1311 | TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc); |
| 1312 | goto leave; |
| 1313 | } |
| 1314 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1315 | frm->crypto.offset = cf_offset; |
| 1316 | frm->crypto.len = cf_len; |
| 1317 | frm->crypto.qel = qel; |
| 1318 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
| 1319 | } |
| 1320 | } |
| 1321 | ret = len == 0; |
| 1322 | leave: |
| 1323 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1324 | return ret; |
| 1325 | } |
| 1326 | |
| 1327 | /* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive |
| 1328 | * activity for <qc> will be interrupted. |
| 1329 | */ |
| 1330 | void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err) |
| 1331 | { |
| 1332 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1333 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) |
| 1334 | goto leave; |
| 1335 | |
| 1336 | TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc); |
| 1337 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 1338 | qc->err.code = err.code; |
| 1339 | qc->err.app = err.app; |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 1340 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1341 | leave: |
| 1342 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 1343 | } |
| 1344 | |
| 1345 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1346 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1347 | { |
| 1348 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1349 | |
| 1350 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 1351 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 1352 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
| 1353 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 1354 | } |
| 1355 | quic_set_connection_close(qc, quic_err_tls(alert)); |
| 1356 | qc->flags |= QUIC_FL_CONN_TLS_ALERT; |
| 1357 | TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc); |
| 1358 | |
| 1359 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
| 1360 | } |
| 1361 | |
| 1362 | /* Set the application for <qc> QUIC connection. |
| 1363 | * Return 1 if succeeded, 0 if not. |
| 1364 | */ |
| 1365 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1366 | { |
| 1367 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1368 | qc->app_ops = &h3_ops; |
| 1369 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1370 | qc->app_ops = &hq_interop_ops; |
| 1371 | else |
| 1372 | return 0; |
| 1373 | |
| 1374 | return 1; |
| 1375 | } |
| 1376 | |
| 1377 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1378 | * wants to provide the QUIC layer with CRYPTO data. |
| 1379 | * Returns 1 if succeeded, 0 if not. |
| 1380 | */ |
| 1381 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1382 | const uint8_t *data, size_t len) |
| 1383 | { |
| 1384 | struct quic_conn *qc; |
| 1385 | enum quic_tls_enc_level tel; |
| 1386 | struct quic_enc_level *qel; |
| 1387 | int ret = 0; |
| 1388 | |
| 1389 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1390 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1391 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 1392 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 1393 | TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc); |
| 1394 | goto out; |
| 1395 | } |
| 1396 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1397 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 1398 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
| 1399 | goto out; |
| 1400 | } |
| 1401 | |
| 1402 | tel = ssl_to_quic_enc_level(level); |
| 1403 | if (tel == -1) { |
| 1404 | TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc); |
| 1405 | goto leave; |
| 1406 | } |
| 1407 | |
| 1408 | qel = &qc->els[tel]; |
| 1409 | if (!quic_crypto_data_cpy(qc, qel, data, len)) { |
| 1410 | TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc); |
| 1411 | goto leave; |
| 1412 | } |
| 1413 | |
| 1414 | TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
| 1415 | qc, &level, &len); |
| 1416 | out: |
| 1417 | ret = 1; |
| 1418 | leave: |
| 1419 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1420 | return ret; |
| 1421 | } |
| 1422 | |
| 1423 | int ha_quic_flush_flight(SSL *ssl) |
| 1424 | { |
| 1425 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1426 | |
| 1427 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1428 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
| 1429 | |
| 1430 | return 1; |
| 1431 | } |
| 1432 | |
| 1433 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1434 | { |
| 1435 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1436 | |
| 1437 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1438 | |
| 1439 | TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1440 | |
| 1441 | quic_set_tls_alert(qc, alert); |
| 1442 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
| 1443 | return 1; |
| 1444 | } |
| 1445 | |
| 1446 | /* QUIC TLS methods */ |
| 1447 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1448 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1449 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1450 | .flush_flight = ha_quic_flush_flight, |
| 1451 | .send_alert = ha_quic_send_alert, |
| 1452 | }; |
| 1453 | |
| 1454 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1455 | * Returns an error count. |
| 1456 | */ |
| 1457 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1458 | { |
| 1459 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1460 | int cfgerr = 0; |
| 1461 | |
| 1462 | long options = |
| 1463 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1464 | SSL_OP_SINGLE_ECDH_USE | |
| 1465 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1466 | SSL_CTX *ctx; |
| 1467 | |
| 1468 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1469 | bind_conf->initial_ctx = ctx; |
| 1470 | |
| 1471 | SSL_CTX_set_options(ctx, options); |
| 1472 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1473 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1474 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1475 | |
| 1476 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1477 | # if defined(HAVE_SSL_CLIENT_HELLO_CB) |
| 1478 | # if defined(SSL_OP_NO_ANTI_REPLAY) |
| 1479 | if (bind_conf->ssl_conf.early_data) { |
| 1480 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
William Lallemand | 3ccfc10 | 2023-08-21 13:51:56 +0200 | [diff] [blame] | 1481 | # ifdef USE_QUIC_OPENSSL_COMPAT |
| 1482 | ha_warning("Binding [%s:%d] for %s %s: 0-RTT is not supported in limited QUIC compatibility mode, ignored.\n", |
| 1483 | bind_conf->file, bind_conf->line, proxy_type_str(bind_conf->frontend), bind_conf->frontend->id); |
| 1484 | # else |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1485 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
William Lallemand | 3ccfc10 | 2023-08-21 13:51:56 +0200 | [diff] [blame] | 1486 | # endif /* ! USE_QUIC_OPENSSL_COMPAT */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1487 | } |
| 1488 | # endif /* !SSL_OP_NO_ANTI_REPLAY */ |
| 1489 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1490 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1491 | # else /* ! HAVE_SSL_CLIENT_HELLO_CB */ |
| 1492 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1493 | # endif |
| 1494 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1495 | #endif |
Frédéric Lécaille | 29d2ce3 | 2023-06-08 09:28:31 +0200 | [diff] [blame] | 1496 | #ifdef USE_QUIC_OPENSSL_COMPAT |
| 1497 | if (!quic_tls_compat_init(bind_conf, ctx)) |
William Lallemand | 48d3c30 | 2023-08-21 15:22:57 +0200 | [diff] [blame] | 1498 | cfgerr++; |
Frédéric Lécaille | 29d2ce3 | 2023-06-08 09:28:31 +0200 | [diff] [blame] | 1499 | #endif |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1500 | |
| 1501 | return cfgerr; |
| 1502 | } |
| 1503 | |
| 1504 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1505 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1506 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1507 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1508 | */ |
| 1509 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1510 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1511 | { |
| 1512 | uint64_t expected_pn = largest_pn + 1; |
| 1513 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1514 | uint64_t pn_hwin = pn_win / 2; |
| 1515 | uint64_t pn_mask = pn_win - 1; |
| 1516 | uint64_t candidate_pn; |
| 1517 | |
| 1518 | |
| 1519 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1520 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1521 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1522 | candidate_pn + pn_hwin <= expected_pn) |
| 1523 | return candidate_pn + pn_win; |
| 1524 | |
| 1525 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1526 | return candidate_pn - pn_win; |
| 1527 | |
| 1528 | return candidate_pn; |
| 1529 | } |
| 1530 | |
| 1531 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1532 | * cryptographic context. |
| 1533 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1534 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1535 | * <end> points to one byte past the end of this packet. |
| 1536 | * Returns 1 if succeeded, 0 if not. |
| 1537 | */ |
| 1538 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1539 | struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
| 1540 | int64_t largest_pn, unsigned char *pn, unsigned char *byte0) |
| 1541 | { |
| 1542 | int ret, i, pnlen; |
| 1543 | uint64_t packet_number; |
| 1544 | uint32_t truncated_pn = 0; |
| 1545 | unsigned char mask[5] = {0}; |
| 1546 | unsigned char *sample; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1547 | |
| 1548 | TRACE_ENTER(QUIC_EV_CONN_RMHP, qc); |
| 1549 | |
| 1550 | ret = 0; |
| 1551 | |
| 1552 | /* Check there is enough data in this packet. */ |
| 1553 | if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
| 1554 | TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1555 | goto leave; |
| 1556 | } |
| 1557 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1558 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1559 | |
| 1560 | if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) { |
| 1561 | TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1562 | goto leave; |
| 1563 | } |
| 1564 | |
| 1565 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1566 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1567 | for (i = 0; i < pnlen; i++) { |
| 1568 | pn[i] ^= mask[i + 1]; |
| 1569 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1570 | } |
| 1571 | |
| 1572 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1573 | /* Store remaining information for this unprotected header */ |
| 1574 | pkt->pn = packet_number; |
| 1575 | pkt->pnl = pnlen; |
| 1576 | |
| 1577 | ret = 1; |
| 1578 | leave: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1579 | TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc); |
| 1580 | return ret; |
| 1581 | } |
| 1582 | |
| 1583 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1584 | * address, with <payload_len> as payload length, <aad> as address of |
| 1585 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1586 | * context. |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 1587 | * |
| 1588 | * TODO no error is expected as encryption is done in place but encryption |
| 1589 | * manual is unclear. <fail> will be set to true if an error is detected. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1590 | */ |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 1591 | static void quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1592 | unsigned char *aad, size_t aad_len, uint64_t pn, |
| 1593 | struct quic_tls_ctx *tls_ctx, struct quic_conn *qc, |
| 1594 | int *fail) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1595 | { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1596 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 1597 | unsigned char *tx_iv = tls_ctx->tx.iv; |
| 1598 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
| 1599 | struct enc_debug_info edi; |
| 1600 | |
| 1601 | TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc); |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 1602 | *fail = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1603 | |
Amaury Denoyelle | 5eadc27 | 2023-05-16 18:11:01 +0200 | [diff] [blame] | 1604 | quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1605 | |
| 1606 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
Emeric Brun | e0190c6 | 2023-07-11 14:53:41 +0200 | [diff] [blame] | 1607 | tls_ctx->tx.ctx, tls_ctx->tx.aead, iv)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1608 | TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc); |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 1609 | *fail = 1; |
| 1610 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1611 | } |
| 1612 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1613 | TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1614 | } |
| 1615 | |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 1616 | /* Select the correct TLS cipher context to used to decipher <pkt> packet |
| 1617 | * attached to <qc> connection from <qel> encryption level. |
| 1618 | */ |
| 1619 | static inline struct quic_tls_ctx *qc_select_tls_ctx(struct quic_conn *qc, |
| 1620 | struct quic_enc_level *qel, |
| 1621 | struct quic_rx_packet *pkt) |
| 1622 | { |
| 1623 | return pkt->type != QUIC_PACKET_TYPE_INITIAL ? &qel->tls_ctx : |
| 1624 | pkt->version == qc->negotiated_version ? &qc->negotiated_ictx : &qel->tls_ctx; |
| 1625 | } |
| 1626 | |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1627 | /* Decrypt <pkt> packet using encryption level <qel> for <qc> connection. |
| 1628 | * Decryption is done in place in packet buffer. |
| 1629 | * |
Ilya Shipitsin | 5fa29b8 | 2022-12-07 09:46:19 +0500 | [diff] [blame] | 1630 | * Returns 1 on success else 0. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1631 | */ |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1632 | static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel, |
| 1633 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1634 | { |
| 1635 | int ret, kp_changed; |
| 1636 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 1637 | struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1638 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
| 1639 | unsigned char *rx_iv = tls_ctx->rx.iv; |
| 1640 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1641 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1642 | |
| 1643 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 1644 | |
| 1645 | ret = 0; |
| 1646 | kp_changed = 0; |
| 1647 | |
| 1648 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1649 | /* The two tested bits are not at the same position, |
| 1650 | * this is why they are first both inversed. |
| 1651 | */ |
| 1652 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1653 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1654 | /* The lowest packet number of a previous key phase |
| 1655 | * cannot be null if it really stores previous key phase |
| 1656 | * secrets. |
| 1657 | */ |
| 1658 | // TODO: check if BUG_ON() more suitable |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1659 | if (!qc->ku.prv_rx.pn) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1660 | TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc); |
| 1661 | goto leave; |
| 1662 | } |
| 1663 | |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1664 | rx_ctx = qc->ku.prv_rx.ctx; |
| 1665 | rx_iv = qc->ku.prv_rx.iv; |
| 1666 | rx_key = qc->ku.prv_rx.key; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1667 | } |
| 1668 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1669 | /* Next key phase */ |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 1670 | TRACE_PROTO("Key phase changed", QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1671 | kp_changed = 1; |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1672 | rx_ctx = qc->ku.nxt_rx.ctx; |
| 1673 | rx_iv = qc->ku.nxt_rx.iv; |
| 1674 | rx_key = qc->ku.nxt_rx.key; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1675 | } |
| 1676 | } |
| 1677 | } |
| 1678 | |
Amaury Denoyelle | 5eadc27 | 2023-05-16 18:11:01 +0200 | [diff] [blame] | 1679 | quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1680 | |
| 1681 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1682 | pkt->data, pkt->aad_len, |
| 1683 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
| 1684 | if (!ret) { |
| 1685 | TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1686 | goto leave; |
| 1687 | } |
| 1688 | |
| 1689 | /* Update the keys only if the packet decryption succeeded. */ |
| 1690 | if (kp_changed) { |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1691 | quic_tls_rotate_keys(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1692 | /* Toggle the Key Phase bit */ |
| 1693 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1694 | /* Store the lowest packet number received for the current key phase */ |
| 1695 | tls_ctx->rx.pn = pkt->pn; |
| 1696 | /* Prepare the next key update */ |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1697 | if (!quic_tls_key_update(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1698 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1699 | goto leave; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | /* Update the packet length (required to parse the frames). */ |
| 1704 | pkt->len -= QUIC_TLS_TAG_LEN; |
| 1705 | ret = 1; |
| 1706 | leave: |
| 1707 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 1708 | return ret; |
| 1709 | } |
| 1710 | |
| 1711 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1712 | /* Release <frm> frame and mark its copies as acknowledged */ |
| 1713 | void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm) |
| 1714 | { |
| 1715 | uint64_t pn; |
| 1716 | struct quic_frame *origin, *f, *tmp; |
| 1717 | |
| 1718 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1719 | |
| 1720 | /* Identify this frame: a frame copy or one of its copies */ |
| 1721 | origin = frm->origin ? frm->origin : frm; |
| 1722 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1723 | * possibly a copy of <origin> |
| 1724 | */ |
| 1725 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1726 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1727 | * not release the packets (releasing the frames) at this time as |
| 1728 | * they are possibly also to be acknowledged alongside the |
| 1729 | * the current one. |
| 1730 | */ |
| 1731 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
| 1732 | if (f->pkt) { |
| 1733 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1734 | f->origin = NULL; |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1735 | LIST_DEL_INIT(&f->ref); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1736 | pn = f->pkt->pn_node.key; |
| 1737 | TRACE_DEVEL("mark frame as acked from packet", |
| 1738 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1739 | } |
| 1740 | else { |
| 1741 | TRACE_DEVEL("freeing unsent frame", |
| 1742 | QUIC_EV_CONN_PRSAFRM, qc, f); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1743 | LIST_DEL_INIT(&f->ref); |
| 1744 | qc_frm_free(&f); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1745 | } |
| 1746 | } |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1747 | LIST_DEL_INIT(&frm->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1748 | pn = frm->pkt->pn_node.key; |
| 1749 | quic_tx_packet_refdec(frm->pkt); |
| 1750 | TRACE_DEVEL("freeing frame from packet", |
| 1751 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1752 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1753 | |
| 1754 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1755 | } |
| 1756 | |
| 1757 | /* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released |
| 1758 | * and all STREAM data are acknowledged. The MUX is responsible to have set |
| 1759 | * <qc.err> before as it is reused for the CONNECTION_CLOSE frame. |
| 1760 | * |
| 1761 | * TODO this should also be called on lost packet detection |
| 1762 | */ |
| 1763 | void qc_check_close_on_released_mux(struct quic_conn *qc) |
| 1764 | { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1765 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1766 | |
| 1767 | if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) { |
| 1768 | /* Reuse errcode which should have been previously set by the MUX on release. */ |
| 1769 | quic_set_connection_close(qc, qc->err); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 1770 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 1774 | } |
| 1775 | |
| 1776 | /* Remove from <stream> the acknowledged frames. |
| 1777 | * |
| 1778 | * Returns 1 if at least one frame was removed else 0. |
| 1779 | */ |
| 1780 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1781 | struct qc_stream_desc *stream) |
| 1782 | { |
| 1783 | int ret; |
| 1784 | struct eb64_node *frm_node; |
| 1785 | |
| 1786 | TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc); |
| 1787 | |
| 1788 | ret = 0; |
| 1789 | frm_node = eb64_first(&stream->acked_frms); |
| 1790 | while (frm_node) { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 1791 | struct qf_stream *strm_frm; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1792 | struct quic_frame *frm; |
| 1793 | size_t offset, len; |
| 1794 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 1795 | strm_frm = eb64_entry(frm_node, struct qf_stream, offset); |
| 1796 | offset = strm_frm->offset.key; |
| 1797 | len = strm_frm->len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1798 | |
| 1799 | if (offset > stream->ack_offset) |
| 1800 | break; |
| 1801 | |
| 1802 | if (qc_stream_desc_ack(&stream, offset, len)) { |
| 1803 | /* cf. next comment : frame may be freed at this stage. */ |
| 1804 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 1805 | qc, stream ? strm_frm : NULL, stream); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1806 | ret = 1; |
| 1807 | } |
| 1808 | |
| 1809 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1810 | * has been freed. with the stream frames tree. Nothing to do |
| 1811 | * anymore in here. |
| 1812 | */ |
| 1813 | if (!stream) { |
| 1814 | qc_check_close_on_released_mux(qc); |
| 1815 | ret = 1; |
| 1816 | goto leave; |
| 1817 | } |
| 1818 | |
| 1819 | frm_node = eb64_next(frm_node); |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 1820 | eb64_delete(&strm_frm->offset); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1821 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 1822 | frm = container_of(strm_frm, struct quic_frame, stream); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1823 | qc_release_frm(qc, frm); |
| 1824 | } |
| 1825 | |
| 1826 | leave: |
| 1827 | TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc); |
| 1828 | return ret; |
| 1829 | } |
| 1830 | |
| 1831 | /* Treat <frm> frame whose packet it is attached to has just been acknowledged. */ |
| 1832 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1833 | struct quic_frame *frm) |
| 1834 | { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 1835 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1836 | TRACE_PROTO("RX ack TX frm", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1837 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1838 | switch (frm->type) { |
| 1839 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1840 | { |
Amaury Denoyelle | 888c5f2 | 2023-04-24 14:26:30 +0200 | [diff] [blame] | 1841 | struct qf_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1842 | struct eb64_node *node = NULL; |
| 1843 | struct qc_stream_desc *stream = NULL; |
| 1844 | const size_t offset = strm_frm->offset.key; |
| 1845 | const size_t len = strm_frm->len; |
| 1846 | |
| 1847 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1848 | * might be freed at this stage. Use the id to do a proper |
| 1849 | * lookup. |
| 1850 | * |
| 1851 | * TODO if lookup operation impact on the perf is noticeable, |
| 1852 | * implement a refcount on qc_stream_desc instances. |
| 1853 | */ |
| 1854 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1855 | if (!node) { |
| 1856 | TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm); |
| 1857 | qc_release_frm(qc, frm); |
| 1858 | /* early return */ |
| 1859 | goto leave; |
| 1860 | } |
| 1861 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1862 | |
| 1863 | TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
| 1864 | if (offset <= stream->ack_offset) { |
| 1865 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1866 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1867 | qc, strm_frm, stream); |
| 1868 | } |
| 1869 | |
| 1870 | if (!stream) { |
| 1871 | /* no need to continue if stream freed. */ |
| 1872 | TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc); |
| 1873 | qc_release_frm(qc, frm); |
| 1874 | qc_check_close_on_released_mux(qc); |
| 1875 | break; |
| 1876 | } |
| 1877 | |
| 1878 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1879 | qc, strm_frm, stream); |
| 1880 | qc_release_frm(qc, frm); |
| 1881 | } |
| 1882 | else { |
| 1883 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
| 1884 | } |
| 1885 | |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 1886 | quic_stream_try_to_consume(qc, stream); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1887 | } |
| 1888 | break; |
| 1889 | default: |
| 1890 | qc_release_frm(qc, frm); |
| 1891 | } |
| 1892 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1893 | leave: |
| 1894 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1895 | } |
| 1896 | |
Frédéric Lécaille | 39bde28 | 2023-08-29 14:48:54 +0200 | [diff] [blame] | 1897 | /* Collect newly acknowledged TX packets from <pkts> ebtree into <newly_acked_pkts> |
| 1898 | * list depending on <largest> and <smallest> packet number of a range of acknowledged |
| 1899 | * packets announced in an ACK frame. <largest_node> may be provided to start |
| 1900 | * looking from this packet node. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1901 | */ |
Frédéric Lécaille | 39bde28 | 2023-08-29 14:48:54 +0200 | [diff] [blame] | 1902 | static void qc_newly_acked_pkts(struct quic_conn *qc, struct eb_root *pkts, |
| 1903 | struct list *newly_acked_pkts, |
| 1904 | struct eb64_node *largest_node, |
| 1905 | uint64_t largest, uint64_t smallest) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1906 | { |
| 1907 | struct eb64_node *node; |
| 1908 | struct quic_tx_packet *pkt; |
| 1909 | |
| 1910 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1911 | |
Frédéric Lécaille | c664e64 | 2023-03-15 17:21:13 +0100 | [diff] [blame] | 1912 | node = eb64_lookup_ge(pkts, smallest); |
| 1913 | if (!node) |
| 1914 | goto leave; |
| 1915 | |
| 1916 | largest_node = largest_node ? largest_node : eb64_lookup_le(pkts, largest); |
| 1917 | if (!largest_node) |
| 1918 | goto leave; |
| 1919 | |
| 1920 | while (node && node->key <= largest_node->key) { |
Frédéric Lécaille | 39bde28 | 2023-08-29 14:48:54 +0200 | [diff] [blame] | 1921 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 1922 | LIST_APPEND(newly_acked_pkts, &pkt->list); |
| 1923 | node = eb64_next(node); |
| 1924 | eb64_delete(&pkt->pn_node); |
| 1925 | } |
| 1926 | |
| 1927 | leave: |
| 1928 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1929 | } |
| 1930 | |
| 1931 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1932 | * deallocating them, and their TX frames. |
| 1933 | * May be NULL if <largest> node could not be found. |
| 1934 | */ |
| 1935 | static void qc_ackrng_pkts(struct quic_conn *qc, |
| 1936 | unsigned int *pkt_flags, struct list *newly_acked_pkts) |
| 1937 | { |
| 1938 | struct quic_tx_packet *pkt, *tmp; |
| 1939 | |
| 1940 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1941 | |
| 1942 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1943 | struct quic_frame *frm, *frmbak; |
| 1944 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1945 | *pkt_flags |= pkt->flags; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1946 | TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 1947 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1948 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 1949 | /* If there are others packet in the same datagram <pkt> is attached to, |
| 1950 | * detach the previous one and the next one from <pkt>. |
| 1951 | */ |
Frédéric Lécaille | 74b5f7b | 2022-11-20 18:35:35 +0100 | [diff] [blame] | 1952 | quic_tx_packet_dgram_detach(pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1953 | eb64_delete(&pkt->pn_node); |
| 1954 | } |
| 1955 | |
Frédéric Lécaille | c664e64 | 2023-03-15 17:21:13 +0100 | [diff] [blame] | 1956 | leave: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1957 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1958 | } |
| 1959 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1960 | /* Remove all frames from <pkt_frm_list> and reinsert them in the same order |
| 1961 | * they have been sent into <pktns_frm_list>. The loss counter of each frame is |
| 1962 | * incremented and checked if it does not exceed retransmission limit. |
| 1963 | * |
| 1964 | * Returns 1 on success, 0 if a frame loss limit is exceeded. A |
| 1965 | * CONNECTION_CLOSE is scheduled in this case. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1966 | */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1967 | static inline int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1968 | struct quic_tx_packet *pkt, |
| 1969 | struct list *pktns_frm_list) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1970 | { |
| 1971 | struct quic_frame *frm, *frmbak; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1972 | struct list *pkt_frm_list = &pkt->frms; |
| 1973 | uint64_t pn = pkt->pn_node.key; |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1974 | int close = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1975 | |
| 1976 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1977 | |
| 1978 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1979 | /* First remove this frame from the packet it was attached to */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1980 | LIST_DEL_INIT(&frm->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1981 | quic_tx_packet_refdec(pkt); |
| 1982 | /* At this time, this frame is not freed but removed from its packet */ |
| 1983 | frm->pkt = NULL; |
| 1984 | /* Remove any reference to this frame */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1985 | qc_frm_unref(frm, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1986 | switch (frm->type) { |
| 1987 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1988 | { |
Amaury Denoyelle | 888c5f2 | 2023-04-24 14:26:30 +0200 | [diff] [blame] | 1989 | struct qf_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1990 | struct eb64_node *node = NULL; |
| 1991 | struct qc_stream_desc *stream_desc; |
| 1992 | |
| 1993 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1994 | if (!node) { |
| 1995 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1996 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1997 | qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1998 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1999 | continue; |
| 2000 | } |
| 2001 | |
| 2002 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2003 | /* Do not resend this frame if in the "already acked range" */ |
| 2004 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 2005 | TRACE_DEVEL("ignored frame in already acked range", |
| 2006 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 2007 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2008 | continue; |
| 2009 | } |
| 2010 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
Frédéric Lécaille | ca07979 | 2023-03-17 08:56:50 +0100 | [diff] [blame] | 2011 | uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key; |
| 2012 | |
Frédéric Lécaille | c425e03 | 2023-03-20 14:32:59 +0100 | [diff] [blame] | 2013 | qc_stream_frm_mv_fwd(frm, diff); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2014 | TRACE_DEVEL("updated partially acked frame", |
| 2015 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2016 | } |
| 2017 | break; |
| 2018 | } |
| 2019 | |
| 2020 | default: |
| 2021 | break; |
| 2022 | } |
| 2023 | |
| 2024 | /* Do not resend probing packet with old data */ |
| 2025 | if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) { |
| 2026 | TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 2027 | qc, frm, &pn); |
| 2028 | if (frm->origin) |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 2029 | LIST_DEL_INIT(&frm->ref); |
| 2030 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2031 | continue; |
| 2032 | } |
| 2033 | |
| 2034 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 2035 | TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2036 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 2037 | qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 2038 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2039 | } |
| 2040 | else { |
Amaury Denoyelle | 24d5b72 | 2023-01-31 11:44:50 +0100 | [diff] [blame] | 2041 | if (++frm->loss_count >= global.tune.quic_max_frame_loss) { |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2042 | TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc); |
| 2043 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR)); |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 2044 | qc_notify_err(qc); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2045 | close = 1; |
| 2046 | } |
| 2047 | |
Frédéric Lécaille | be795ce | 2023-03-08 18:23:13 +0100 | [diff] [blame] | 2048 | LIST_APPEND(pktns_frm_list, &frm->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2049 | TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2050 | } |
| 2051 | } |
| 2052 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2053 | end: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2054 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2055 | return !close; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | /* Free <pkt> TX packet and its attached frames. |
| 2059 | * This is the responsibility of the caller to remove this packet of |
| 2060 | * any data structure it was possibly attached to. |
| 2061 | */ |
| 2062 | static inline void free_quic_tx_packet(struct quic_conn *qc, |
| 2063 | struct quic_tx_packet *pkt) |
| 2064 | { |
| 2065 | struct quic_frame *frm, *frmbak; |
| 2066 | |
| 2067 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 2068 | |
| 2069 | if (!pkt) |
| 2070 | goto leave; |
| 2071 | |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 2072 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 2073 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2074 | pool_free(pool_head_quic_tx_packet, pkt); |
| 2075 | |
| 2076 | leave: |
| 2077 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 2078 | } |
| 2079 | |
| 2080 | /* Free the TX packets of <pkts> list */ |
Frédéric Lécaille | 39bde28 | 2023-08-29 14:48:54 +0200 | [diff] [blame] | 2081 | static inline __maybe_unused void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2082 | { |
| 2083 | struct quic_tx_packet *pkt, *tmp; |
| 2084 | |
| 2085 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 2086 | |
| 2087 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 2088 | LIST_DELETE(&pkt->list); |
| 2089 | eb64_delete(&pkt->pn_node); |
| 2090 | free_quic_tx_packet(qc, pkt); |
| 2091 | } |
| 2092 | |
| 2093 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 2094 | } |
| 2095 | |
| 2096 | /* Remove already sent ranges of acknowledged packet numbers from |
| 2097 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 2098 | * updating the range which contains <largest_acked_pn>. |
| 2099 | * Never fails. |
| 2100 | */ |
| 2101 | static void qc_treat_ack_of_ack(struct quic_conn *qc, |
| 2102 | struct quic_pktns *pktns, |
| 2103 | int64_t largest_acked_pn) |
| 2104 | { |
| 2105 | struct eb64_node *ar, *next_ar; |
| 2106 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 2107 | |
| 2108 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2109 | |
| 2110 | ar = eb64_first(&arngs->root); |
| 2111 | while (ar) { |
| 2112 | struct quic_arng_node *ar_node; |
| 2113 | |
| 2114 | next_ar = eb64_next(ar); |
| 2115 | ar_node = eb64_entry(ar, struct quic_arng_node, first); |
| 2116 | |
| 2117 | if ((int64_t)ar_node->first.key > largest_acked_pn) { |
| 2118 | TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc); |
| 2119 | break; |
| 2120 | } |
| 2121 | |
| 2122 | if (largest_acked_pn < ar_node->last) { |
| 2123 | eb64_delete(ar); |
| 2124 | ar_node->first.key = largest_acked_pn + 1; |
| 2125 | eb64_insert(&arngs->root, ar); |
| 2126 | break; |
| 2127 | } |
| 2128 | |
Frédéric Lécaille | 0dd4fa5 | 2023-05-02 08:57:37 +0200 | [diff] [blame] | 2129 | /* Do not empty the tree: the first ACK range contains the |
| 2130 | * largest acknowledged packet number. |
| 2131 | */ |
| 2132 | if (arngs->sz == 1) |
| 2133 | break; |
| 2134 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2135 | eb64_delete(ar); |
| 2136 | pool_free(pool_head_quic_arng, ar_node); |
| 2137 | arngs->sz--; |
| 2138 | ar = next_ar; |
| 2139 | } |
| 2140 | |
| 2141 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2142 | } |
| 2143 | |
| 2144 | /* Send a packet ack event nofication for each newly acked packet of |
| 2145 | * <newly_acked_pkts> list and free them. |
| 2146 | * Always succeeds. |
| 2147 | */ |
| 2148 | static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc, |
| 2149 | struct list *newly_acked_pkts) |
| 2150 | { |
| 2151 | struct quic_tx_packet *pkt, *tmp; |
| 2152 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 2153 | |
| 2154 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2155 | |
| 2156 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 2157 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
| 2158 | qc->path->prep_in_flight -= pkt->in_flight_len; |
| 2159 | qc->path->in_flight -= pkt->in_flight_len; |
| 2160 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
| 2161 | qc->path->ifae_pkts--; |
| 2162 | /* If this packet contained an ACK frame, proceed to the |
| 2163 | * acknowledging of range of acks from the largest acknowledged |
| 2164 | * packet number which was sent in an ACK frame by this packet. |
| 2165 | */ |
| 2166 | if (pkt->largest_acked_pn != -1) |
| 2167 | qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn); |
| 2168 | ev.ack.acked = pkt->in_flight_len; |
| 2169 | ev.ack.time_sent = pkt->time_sent; |
| 2170 | quic_cc_event(&qc->path->cc, &ev); |
Frédéric Lécaille | 39bde28 | 2023-08-29 14:48:54 +0200 | [diff] [blame] | 2171 | LIST_DEL_INIT(&pkt->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2172 | quic_tx_packet_refdec(pkt); |
| 2173 | } |
| 2174 | |
| 2175 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2176 | |
| 2177 | } |
| 2178 | |
| 2179 | /* Release all the frames attached to <pktns> packet number space */ |
Frédéric Lécaille | 8fbabea | 2023-10-11 09:28:36 +0200 | [diff] [blame] | 2180 | void qc_release_pktns_frms(struct quic_conn *qc, struct quic_pktns *pktns) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2181 | { |
| 2182 | struct quic_frame *frm, *frmbak; |
| 2183 | |
| 2184 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2185 | |
Frédéric Lécaille | 8fbabea | 2023-10-11 09:28:36 +0200 | [diff] [blame] | 2186 | if (!pktns) |
| 2187 | goto leave; |
| 2188 | |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 2189 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) |
| 2190 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2191 | |
Frédéric Lécaille | 8fbabea | 2023-10-11 09:28:36 +0200 | [diff] [blame] | 2192 | leave: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2193 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2194 | } |
| 2195 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2196 | /* Handle <pkts> list of lost packets detected at <now_us> handling their TX |
| 2197 | * frames. Send a packet loss event to the congestion controller if in flight |
| 2198 | * packet have been lost. Also frees the packet in <pkts> list. |
| 2199 | * |
| 2200 | * Returns 1 on success else 0 if loss limit has been exceeded. A |
| 2201 | * CONNECTION_CLOSE was prepared to close the connection ASAP. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2202 | */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2203 | static inline int qc_release_lost_pkts(struct quic_conn *qc, |
| 2204 | struct quic_pktns *pktns, |
| 2205 | struct list *pkts, |
| 2206 | uint64_t now_us) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2207 | { |
| 2208 | struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost; |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2209 | int close = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2210 | |
| 2211 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2212 | |
| 2213 | if (LIST_ISEMPTY(pkts)) |
| 2214 | goto leave; |
| 2215 | |
| 2216 | oldest_lost = newest_lost = NULL; |
| 2217 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 2218 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2219 | |
| 2220 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
| 2221 | qc->path->prep_in_flight -= pkt->in_flight_len; |
| 2222 | qc->path->in_flight -= pkt->in_flight_len; |
| 2223 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
| 2224 | qc->path->ifae_pkts--; |
| 2225 | /* Treat the frames of this lost packet. */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2226 | if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms)) |
| 2227 | close = 1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2228 | LIST_DELETE(&pkt->list); |
| 2229 | if (!oldest_lost) { |
| 2230 | oldest_lost = newest_lost = pkt; |
| 2231 | } |
| 2232 | else { |
| 2233 | if (newest_lost != oldest_lost) |
| 2234 | quic_tx_packet_refdec(newest_lost); |
| 2235 | newest_lost = pkt; |
| 2236 | } |
| 2237 | } |
| 2238 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2239 | if (!close) { |
| 2240 | if (newest_lost) { |
| 2241 | /* Sent a congestion event to the controller */ |
| 2242 | struct quic_cc_event ev = { }; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2243 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2244 | ev.type = QUIC_CC_EVT_LOSS; |
| 2245 | ev.loss.time_sent = newest_lost->time_sent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2246 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2247 | quic_cc_event(&qc->path->cc, &ev); |
| 2248 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2249 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2250 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 2251 | * We must check if we are experiencing a persistent congestion. |
| 2252 | * If this is the case, the congestion controller must re-enter |
| 2253 | * slow start state. |
| 2254 | */ |
| 2255 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 2256 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2257 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2258 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 2259 | now_ms, qc->max_ack_delay)) |
| 2260 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 2261 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2262 | } |
| 2263 | |
Amaury Denoyelle | 3a72ba2 | 2022-11-14 11:41:34 +0100 | [diff] [blame] | 2264 | /* <oldest_lost> cannot be NULL at this stage because we have ensured |
| 2265 | * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a |
| 2266 | * possible overflow on a 0 byte region with O2 optimization. |
| 2267 | */ |
| 2268 | ALREADY_CHECKED(oldest_lost); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2269 | quic_tx_packet_refdec(oldest_lost); |
| 2270 | if (newest_lost != oldest_lost) |
| 2271 | quic_tx_packet_refdec(newest_lost); |
| 2272 | |
| 2273 | leave: |
| 2274 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2275 | return !close; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 2279 | * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e. |
| 2280 | * if the largest acked packet was newly acked and if there was at least one newly |
| 2281 | * acked ack-eliciting packet. |
| 2282 | * Return 1, if succeeded, 0 if not. |
| 2283 | */ |
| 2284 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 2285 | struct quic_frame *frm, |
| 2286 | struct quic_enc_level *qel, |
| 2287 | unsigned int *rtt_sample, |
| 2288 | const unsigned char **pos, const unsigned char *end) |
| 2289 | { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 2290 | struct qf_ack *ack_frm = &frm->ack; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2291 | uint64_t smallest, largest; |
| 2292 | struct eb_root *pkts; |
| 2293 | struct eb64_node *largest_node; |
| 2294 | unsigned int time_sent, pkt_flags; |
| 2295 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 2296 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
Frédéric Lécaille | f078d78 | 2023-08-29 11:01:19 +0200 | [diff] [blame] | 2297 | int ret = 0, new_largest_acked_pn = 0; |
Frédéric Lécaille | 39bde28 | 2023-08-29 14:48:54 +0200 | [diff] [blame] | 2298 | struct quic_tx_packet *pkt, *tmp; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2299 | |
| 2300 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2301 | |
Frédéric Lécaille | 39bde28 | 2023-08-29 14:48:54 +0200 | [diff] [blame] | 2302 | pkts = &qel->pktns->tx.pkts; |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 2303 | if (ack_frm->largest_ack > qel->pktns->tx.next_pn) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2304 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 2305 | qc, NULL, &ack_frm->largest_ack); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2306 | goto err; |
| 2307 | } |
| 2308 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 2309 | if (ack_frm->first_ack_range > ack_frm->largest_ack) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2310 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 2311 | qc, NULL, &ack_frm->first_ack_range); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2312 | goto err; |
| 2313 | } |
| 2314 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 2315 | largest = ack_frm->largest_ack; |
| 2316 | smallest = largest - ack_frm->first_ack_range; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2317 | pkt_flags = 0; |
| 2318 | largest_node = NULL; |
| 2319 | time_sent = 0; |
| 2320 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 2321 | if ((int64_t)ack_frm->largest_ack > qel->pktns->rx.largest_acked_pn) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2322 | largest_node = eb64_lookup(pkts, largest); |
| 2323 | if (!largest_node) { |
| 2324 | TRACE_DEVEL("Largest acked packet not found", |
| 2325 | QUIC_EV_CONN_PRSAFRM, qc); |
| 2326 | } |
| 2327 | else { |
| 2328 | time_sent = eb64_entry(largest_node, |
| 2329 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | f078d78 | 2023-08-29 11:01:19 +0200 | [diff] [blame] | 2330 | new_largest_acked_pn = 1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2331 | } |
| 2332 | } |
| 2333 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 2334 | TRACE_PROTO("RX ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2335 | qc, NULL, &largest, &smallest); |
| 2336 | do { |
| 2337 | uint64_t gap, ack_range; |
| 2338 | |
Frédéric Lécaille | 39bde28 | 2023-08-29 14:48:54 +0200 | [diff] [blame] | 2339 | qc_newly_acked_pkts(qc, pkts, &newly_acked_pkts, |
| 2340 | largest_node, largest, smallest); |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 2341 | if (!ack_frm->ack_range_num--) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2342 | break; |
| 2343 | |
| 2344 | if (!quic_dec_int(&gap, pos, end)) { |
| 2345 | TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc); |
| 2346 | goto err; |
| 2347 | } |
| 2348 | |
| 2349 | if (smallest < gap + 2) { |
| 2350 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
| 2351 | qc, NULL, &gap, &smallest); |
| 2352 | goto err; |
| 2353 | } |
| 2354 | |
| 2355 | largest = smallest - gap - 2; |
| 2356 | if (!quic_dec_int(&ack_range, pos, end)) { |
| 2357 | TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc); |
| 2358 | goto err; |
| 2359 | } |
| 2360 | |
| 2361 | if (largest < ack_range) { |
| 2362 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
| 2363 | qc, NULL, &largest, &ack_range); |
| 2364 | goto err; |
| 2365 | } |
| 2366 | |
| 2367 | /* Do not use this node anymore. */ |
| 2368 | largest_node = NULL; |
| 2369 | /* Next range */ |
| 2370 | smallest = largest - ack_range; |
| 2371 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 2372 | TRACE_PROTO("RX next ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2373 | qc, NULL, &largest, &smallest); |
| 2374 | } while (1); |
| 2375 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2376 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
Frédéric Lécaille | 39bde28 | 2023-08-29 14:48:54 +0200 | [diff] [blame] | 2377 | qc_ackrng_pkts(qc, &pkt_flags, &newly_acked_pkts); |
| 2378 | if (new_largest_acked_pn && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2379 | *rtt_sample = tick_remain(time_sent, now_ms); |
| 2380 | qel->pktns->rx.largest_acked_pn = ack_frm->largest_ack; |
| 2381 | } |
| 2382 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2383 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
| 2384 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2385 | if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms)) |
| 2386 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2387 | } |
| 2388 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 2389 | if (quic_peer_validated_addr(qc)) |
| 2390 | qc->path->loss.pto_count = 0; |
| 2391 | qc_set_timer(qc); |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 2392 | qc_notify_send(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | ret = 1; |
| 2396 | leave: |
| 2397 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2398 | return ret; |
| 2399 | |
| 2400 | err: |
Frédéric Lécaille | 39bde28 | 2023-08-29 14:48:54 +0200 | [diff] [blame] | 2401 | /* Move back these packets into their tree. */ |
| 2402 | list_for_each_entry_safe(pkt, tmp, &newly_acked_pkts, list) { |
| 2403 | LIST_DEL_INIT(&pkt->list); |
| 2404 | eb64_insert(pkts, &pkt->pn_node); |
| 2405 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2406 | goto leave; |
| 2407 | } |
| 2408 | |
| 2409 | /* This function gives the detail of the SSL error. It is used only |
| 2410 | * if the debug mode and the verbose mode are activated. It dump all |
| 2411 | * the SSL error until the stack was empty. |
| 2412 | */ |
| 2413 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2414 | { |
| 2415 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2416 | while (1) { |
| 2417 | const char *func = NULL; |
| 2418 | unsigned long ret; |
| 2419 | |
| 2420 | ERR_peek_error_func(&func); |
| 2421 | ret = ERR_get_error(); |
| 2422 | if (!ret) |
| 2423 | return; |
| 2424 | |
| 2425 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 2426 | func, ERR_reason_error_string(ret)); |
| 2427 | } |
| 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2432 | const char **str, int *len); |
| 2433 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2434 | /* Finalize <qc> QUIC connection: |
| 2435 | * - initialize the Initial QUIC TLS context for negotiated version, |
| 2436 | * - derive the secrets for this context, |
| 2437 | * - set them into the TLS stack, |
| 2438 | * |
| 2439 | * MUST be called after having received the remote transport parameters which |
| 2440 | * are parsed when the TLS callback for the ClientHello message is called upon |
| 2441 | * SSL_do_handshake() calls, not necessarily at the first time as this TLS |
Ilya Shipitsin | 07be66d | 2023-04-01 12:26:42 +0200 | [diff] [blame] | 2442 | * message may be split between packets |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2443 | * Return 1 if succeeded, 0 if not. |
| 2444 | */ |
| 2445 | static int qc_conn_finalize(struct quic_conn *qc, int server) |
| 2446 | { |
| 2447 | int ret = 0; |
| 2448 | |
| 2449 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 2450 | |
| 2451 | if (qc->flags & QUIC_FL_CONN_FINALIZED) |
| 2452 | goto finalized; |
| 2453 | |
| 2454 | if (qc->negotiated_version && |
| 2455 | !qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version, |
| 2456 | qc->odcid.data, qc->odcid.len, server)) |
| 2457 | goto out; |
| 2458 | |
| 2459 | /* This connection is functional (ready to send/receive) */ |
| 2460 | qc->flags |= QUIC_FL_CONN_FINALIZED; |
| 2461 | |
| 2462 | finalized: |
| 2463 | ret = 1; |
| 2464 | out: |
| 2465 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 2466 | return ret; |
| 2467 | } |
| 2468 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2469 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2470 | * from <qel> encryption level with <ctx> as QUIC connection context. |
| 2471 | * Remaining parameter are there for debugging purposes. |
| 2472 | * Return 1 if succeeded, 0 if not. |
| 2473 | */ |
| 2474 | static inline int qc_provide_cdata(struct quic_enc_level *el, |
| 2475 | struct ssl_sock_ctx *ctx, |
| 2476 | const unsigned char *data, size_t len, |
| 2477 | struct quic_rx_packet *pkt, |
| 2478 | struct quic_rx_crypto_frm *cf) |
| 2479 | { |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2480 | #ifdef DEBUG_STRICT |
| 2481 | enum ncb_ret ncb_ret; |
| 2482 | #endif |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2483 | int ssl_err, state; |
| 2484 | struct quic_conn *qc; |
| 2485 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2486 | struct ncbuf *ncbuf = &el->cstream->rx.ncbuf; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2487 | |
| 2488 | ssl_err = SSL_ERROR_NONE; |
| 2489 | qc = ctx->qc; |
| 2490 | |
| 2491 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2492 | |
| 2493 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2494 | TRACE_ERROR("SSL_provide_quic_data() error", |
| 2495 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
| 2496 | goto leave; |
| 2497 | } |
| 2498 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2499 | TRACE_PROTO("in order CRYPTO data", |
| 2500 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
| 2501 | |
| 2502 | state = qc->state; |
| 2503 | if (state < QUIC_HS_ST_COMPLETE) { |
| 2504 | ssl_err = SSL_do_handshake(ctx->ssl); |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2505 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 2506 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 2507 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc); |
| 2508 | goto leave; |
| 2509 | } |
| 2510 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2511 | /* Finalize the connection as soon as possible if the peer transport parameters |
| 2512 | * have been received. This may be useful to send packets even if this |
| 2513 | * handshake fails. |
| 2514 | */ |
| 2515 | if ((qc->flags & QUIC_FL_CONN_TX_TP_RECEIVED) && !qc_conn_finalize(qc, 1)) { |
| 2516 | TRACE_ERROR("connection finalization failed", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2517 | goto leave; |
| 2518 | } |
| 2519 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2520 | if (ssl_err != 1) { |
| 2521 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2522 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2523 | TRACE_PROTO("SSL handshake in progress", |
| 2524 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2525 | goto out; |
| 2526 | } |
| 2527 | |
| 2528 | /* TODO: Should close the connection asap */ |
| 2529 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2530 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2531 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2532 | HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail); |
| 2533 | } |
| 2534 | TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2535 | qc_ssl_dump_errors(ctx->conn); |
| 2536 | ERR_clear_error(); |
| 2537 | goto leave; |
| 2538 | } |
Frederic Lecaille | 314a4c0 | 2024-05-22 10:22:09 +0200 | [diff] [blame] | 2539 | #if defined(LIBRESSL_VERSION_NUMBER) |
| 2540 | else if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 2541 | /* Some libressl versions emit TLS alerts without making the handshake |
| 2542 | * (SSL_do_handshake()) fail. This is at least the case for |
| 2543 | * libressl-3.9.0 when forcing the TLS cipher to TLS_AES_128_CCM_SHA256. |
| 2544 | */ |
| 2545 | TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2546 | HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail); |
| 2547 | goto leave; |
| 2548 | } |
| 2549 | #endif |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2550 | |
| 2551 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2552 | |
| 2553 | /* Check the alpn could be negotiated */ |
| 2554 | if (!qc->app_ops) { |
| 2555 | TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2556 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
| 2557 | goto leave; |
| 2558 | } |
| 2559 | |
| 2560 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2561 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2562 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2563 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2564 | } |
| 2565 | /* I/O callback switch */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2566 | qc->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2567 | if (qc_is_listener(ctx->qc)) { |
Amaury Denoyelle | 1304d19 | 2023-04-11 16:46:03 +0200 | [diff] [blame] | 2568 | qc->flags |= QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2569 | qc->state = QUIC_HS_ST_CONFIRMED; |
Amaury Denoyelle | b75338e | 2024-03-04 18:41:39 +0100 | [diff] [blame] | 2570 | |
| 2571 | if (!(qc->flags & QUIC_FL_CONN_ACCEPT_REGISTERED)) { |
| 2572 | quic_accept_push_qc(qc); |
| 2573 | } |
| 2574 | else { |
| 2575 | /* Connection already accepted if 0-RTT used. |
| 2576 | * In this case, schedule quic-conn to ensure |
| 2577 | * post-handshake frames are emitted. |
| 2578 | */ |
| 2579 | tasklet_wakeup(qc->wait_event.tasklet); |
| 2580 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2581 | } |
| 2582 | else { |
| 2583 | qc->state = QUIC_HS_ST_COMPLETE; |
| 2584 | } |
| 2585 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 2586 | /* Prepare the next key update */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2587 | if (!quic_tls_key_update(qc)) { |
| 2588 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc); |
| 2589 | goto leave; |
| 2590 | } |
| 2591 | } else { |
| 2592 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2593 | if (ssl_err != 1) { |
| 2594 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2595 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2596 | TRACE_PROTO("SSL post handshake in progress", |
| 2597 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2598 | goto out; |
| 2599 | } |
| 2600 | |
| 2601 | TRACE_ERROR("SSL post handshake error", |
| 2602 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2603 | goto leave; |
| 2604 | } |
| 2605 | |
| 2606 | TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2607 | } |
| 2608 | |
| 2609 | out: |
| 2610 | ret = 1; |
| 2611 | leave: |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2612 | /* The CRYPTO data are consumed even in case of an error to release |
| 2613 | * the memory asap. |
| 2614 | */ |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2615 | if (!ncb_is_null(ncbuf)) { |
| 2616 | #ifdef DEBUG_STRICT |
| 2617 | ncb_ret = ncb_advance(ncbuf, len); |
| 2618 | /* ncb_advance() must always succeed. This is guaranteed as |
| 2619 | * this is only done inside a data block. If false, this will |
| 2620 | * lead to handshake failure with quic_enc_level offset shifted |
| 2621 | * from buffer data. |
| 2622 | */ |
| 2623 | BUG_ON(ncb_ret != NCB_RET_OK); |
| 2624 | #else |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2625 | ncb_advance(ncbuf, len); |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2626 | #endif |
| 2627 | } |
| 2628 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2629 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
| 2630 | return ret; |
| 2631 | } |
| 2632 | |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2633 | /* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc> |
| 2634 | * connection. <fin> is true if FIN bit is set on frame type. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2635 | * |
| 2636 | * Return 1 on success. On error, 0 is returned. In this case, the packet |
| 2637 | * containing the frame must not be acknowledged. |
| 2638 | */ |
| 2639 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
Amaury Denoyelle | 888c5f2 | 2023-04-24 14:26:30 +0200 | [diff] [blame] | 2640 | struct qf_stream *strm_frm, |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2641 | struct quic_conn *qc, char fin) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2642 | { |
| 2643 | int ret; |
| 2644 | |
| 2645 | /* RFC9000 13.1. Packet Processing |
| 2646 | * |
| 2647 | * A packet MUST NOT be acknowledged until packet protection has been |
| 2648 | * successfully removed and all frames contained in the packet have |
| 2649 | * been processed. For STREAM frames, this means the data has been |
| 2650 | * enqueued in preparation to be received by the application protocol, |
| 2651 | * but it does not require that data be delivered and consumed. |
| 2652 | */ |
| 2653 | TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc); |
| 2654 | |
| 2655 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2656 | strm_frm->offset.key, fin, (char *)strm_frm->data); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2657 | |
| 2658 | /* frame rejected - packet must not be acknowledeged */ |
| 2659 | TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc); |
| 2660 | return !ret; |
| 2661 | } |
| 2662 | |
| 2663 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2664 | * for <qc> QUIC connection. |
| 2665 | * This is a best effort function which never fails even if no memory could be |
| 2666 | * allocated to duplicate these frames. |
| 2667 | */ |
| 2668 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2669 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2670 | { |
| 2671 | struct quic_frame *frm, *frmbak; |
| 2672 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2673 | |
| 2674 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2675 | |
| 2676 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2677 | struct quic_frame *dup_frm, *origin; |
| 2678 | |
Frédéric Lécaille | e6359b6 | 2023-03-02 14:49:22 +0100 | [diff] [blame] | 2679 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 2680 | TRACE_DEVEL("already acknowledged frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2681 | continue; |
| 2682 | } |
| 2683 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2684 | switch (frm->type) { |
| 2685 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2686 | { |
Amaury Denoyelle | 888c5f2 | 2023-04-24 14:26:30 +0200 | [diff] [blame] | 2687 | struct qf_stream *strm_frm = &frm->stream; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2688 | struct eb64_node *node = NULL; |
| 2689 | struct qc_stream_desc *stream_desc; |
| 2690 | |
| 2691 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2692 | if (!node) { |
| 2693 | TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2694 | continue; |
| 2695 | } |
| 2696 | |
| 2697 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2698 | /* Do not resend this frame if in the "already acked range" */ |
| 2699 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 2700 | TRACE_DEVEL("ignored frame in already acked range", |
| 2701 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2702 | continue; |
| 2703 | } |
| 2704 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
Frédéric Lécaille | ca07979 | 2023-03-17 08:56:50 +0100 | [diff] [blame] | 2705 | uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key; |
| 2706 | |
Frédéric Lécaille | c425e03 | 2023-03-20 14:32:59 +0100 | [diff] [blame] | 2707 | qc_stream_frm_mv_fwd(frm, diff); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2708 | TRACE_DEVEL("updated partially acked frame", |
| 2709 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2710 | } |
Amaury Denoyelle | c8a0efb | 2023-02-22 10:44:27 +0100 | [diff] [blame] | 2711 | |
| 2712 | strm_frm->dup = 1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2713 | break; |
| 2714 | } |
| 2715 | |
| 2716 | default: |
| 2717 | break; |
| 2718 | } |
| 2719 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2720 | /* If <frm> is already a copy of another frame, we must take |
| 2721 | * its original frame as source for the copy. |
| 2722 | */ |
| 2723 | origin = frm->origin ? frm->origin : frm; |
| 2724 | dup_frm = qc_frm_dup(origin); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2725 | if (!dup_frm) { |
| 2726 | TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2727 | break; |
| 2728 | } |
| 2729 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2730 | TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2731 | if (origin->pkt) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2732 | TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM, |
Frédéric Lécaille | fe97c6b | 2023-11-21 20:31:32 +0100 | [diff] [blame] | 2733 | qc, dup_frm, &origin->pkt->pn_node.key); |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2734 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2735 | else { |
| 2736 | /* <origin> is a frame which was sent from a packet detected as lost. */ |
| 2737 | TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc); |
| 2738 | } |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2739 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2740 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2741 | } |
| 2742 | |
| 2743 | LIST_SPLICE(out_frm_list, &tmp); |
| 2744 | |
| 2745 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2746 | } |
| 2747 | |
Frédéric Lécaille | e6359b6 | 2023-03-02 14:49:22 +0100 | [diff] [blame] | 2748 | /* Boolean function which return 1 if <pkt> TX packet is only made of |
| 2749 | * already acknowledged frame. |
| 2750 | */ |
| 2751 | static inline int qc_pkt_with_only_acked_frms(struct quic_tx_packet *pkt) |
| 2752 | { |
| 2753 | struct quic_frame *frm; |
| 2754 | |
| 2755 | list_for_each_entry(frm, &pkt->frms, list) |
| 2756 | if (!(frm->flags & QUIC_FL_TX_FRAME_ACKED)) |
| 2757 | return 0; |
| 2758 | |
| 2759 | return 1; |
| 2760 | } |
| 2761 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2762 | /* Prepare a fast retransmission from <qel> encryption level */ |
| 2763 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2764 | struct quic_enc_level *qel, |
| 2765 | struct list *frms1, struct list *frms2) |
| 2766 | { |
| 2767 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
| 2768 | struct list *frms = frms1; |
| 2769 | struct eb64_node *node; |
| 2770 | struct quic_tx_packet *pkt; |
| 2771 | |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2772 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2773 | |
| 2774 | BUG_ON(frms1 == frms2); |
| 2775 | |
| 2776 | pkt = NULL; |
| 2777 | node = eb64_first(pkts); |
| 2778 | start: |
| 2779 | while (node) { |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2780 | struct quic_tx_packet *p; |
| 2781 | |
| 2782 | p = eb64_entry(node, struct quic_tx_packet, pn_node); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2783 | node = eb64_next(node); |
| 2784 | /* Skip the empty and coalesced packets */ |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 2785 | TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0, |
Frédéric Lécaille | e6359b6 | 2023-03-02 14:49:22 +0100 | [diff] [blame] | 2786 | "--> pn=%llu (%d %d %d)", (ull)p->pn_node.key, |
| 2787 | LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED), |
| 2788 | qc_pkt_with_only_acked_frms(p)); |
| 2789 | if (!LIST_ISEMPTY(&p->frms) && !qc_pkt_with_only_acked_frms(p)) { |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2790 | pkt = p; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2791 | break; |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2792 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | if (!pkt) |
| 2796 | goto leave; |
| 2797 | |
| 2798 | /* When building a packet from another one, the field which may increase the |
| 2799 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2800 | */ |
| 2801 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2802 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
Frédéric Lécaille | a65b71f | 2023-03-03 10:16:32 +0100 | [diff] [blame] | 2803 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2804 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 2805 | goto leave; |
| 2806 | } |
| 2807 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 2808 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2809 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2810 | if (frms == frms1 && frms2) { |
| 2811 | frms = frms2; |
| 2812 | goto start; |
| 2813 | } |
| 2814 | leave: |
| 2815 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 2816 | } |
| 2817 | |
| 2818 | /* Prepare a fast retransmission during a handshake after a client |
| 2819 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2820 | * Initial packets send them coalescing with others (Handshake here). |
| 2821 | * (Listener only function). |
| 2822 | */ |
| 2823 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2824 | struct list *ifrms, struct list *hfrms) |
| 2825 | { |
| 2826 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2827 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2828 | |
| 2829 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2830 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2831 | struct quic_enc_level *qel = iqel; |
| 2832 | struct eb_root *pkts; |
| 2833 | struct eb64_node *node; |
| 2834 | struct quic_tx_packet *pkt; |
| 2835 | struct list *tmp = &itmp; |
| 2836 | |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2837 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2838 | start: |
| 2839 | pkt = NULL; |
| 2840 | pkts = &qel->pktns->tx.pkts; |
| 2841 | node = eb64_first(pkts); |
| 2842 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2843 | while (node) { |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2844 | struct quic_tx_packet *p; |
| 2845 | |
| 2846 | p = eb64_entry(node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 2847 | TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0, |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2848 | "--> pn=%llu (%d %d)", (ull)p->pn_node.key, |
| 2849 | LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED)); |
Frédéric Lécaille | e6359b6 | 2023-03-02 14:49:22 +0100 | [diff] [blame] | 2850 | if (!LIST_ISEMPTY(&p->frms) && !(p->flags & QUIC_FL_TX_PACKET_COALESCED) && |
| 2851 | !qc_pkt_with_only_acked_frms(p)) { |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2852 | pkt = p; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2853 | break; |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2854 | } |
| 2855 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2856 | node = eb64_next(node); |
| 2857 | } |
| 2858 | |
| 2859 | if (!pkt) |
| 2860 | goto end; |
| 2861 | |
| 2862 | /* When building a packet from another one, the field which may increase the |
| 2863 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2864 | */ |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2865 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 2866 | size_t dglen = pkt->len + 4; |
| 2867 | |
| 2868 | dglen += pkt->next ? pkt->next->len + 4 : 0; |
| 2869 | if (dglen > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
Frédéric Lécaille | a65b71f | 2023-03-03 10:16:32 +0100 | [diff] [blame] | 2870 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2871 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 2872 | if (pkt->next) |
| 2873 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt->next); |
| 2874 | goto end; |
| 2875 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | qel->pktns->tx.pto_probe += 1; |
| 2879 | |
| 2880 | /* No risk to loop here, #packet per datagram is bounded */ |
| 2881 | requeue: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 2882 | TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2883 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
| 2884 | if (qel == iqel) { |
| 2885 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2886 | pkt = pkt->next; |
| 2887 | tmp = &htmp; |
| 2888 | hqel->pktns->tx.pto_probe += 1; |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2889 | TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2890 | goto requeue; |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | end: |
| 2895 | LIST_SPLICE(ifrms, &itmp); |
| 2896 | LIST_SPLICE(hfrms, &htmp); |
| 2897 | |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2898 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
| 2901 | static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm) |
| 2902 | { |
| 2903 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 2904 | |
| 2905 | if (frm->type == QUIC_FT_CONNECTION_CLOSE) |
| 2906 | quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code); |
| 2907 | else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) { |
| 2908 | if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt) |
| 2909 | goto out; |
| 2910 | |
| 2911 | qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code); |
| 2912 | } |
| 2913 | |
| 2914 | out: |
| 2915 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 2916 | } |
| 2917 | |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2918 | /* Cancel a request on connection <qc> for stream id <id>. This is useful when |
| 2919 | * the client opens a new stream but the MUX has already been released. A |
Amaury Denoyelle | 7546301 | 2023-02-20 10:31:27 +0100 | [diff] [blame] | 2920 | * STOP_SENDING + RESET_STREAM frames are prepared for emission. |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2921 | * |
| 2922 | * TODO this function is closely related to H3. Its place should be in H3 layer |
| 2923 | * instead of quic-conn but this requires an architecture adjustment. |
| 2924 | * |
Ilya Shipitsin | 07be66d | 2023-04-01 12:26:42 +0200 | [diff] [blame] | 2925 | * Returns 1 on success else 0. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2926 | */ |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2927 | static int qc_h3_request_reject(struct quic_conn *qc, uint64_t id) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2928 | { |
| 2929 | int ret = 0; |
Amaury Denoyelle | 7546301 | 2023-02-20 10:31:27 +0100 | [diff] [blame] | 2930 | struct quic_frame *ss, *rs; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2931 | struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2932 | const uint64_t app_error_code = H3_REQUEST_REJECTED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2933 | |
| 2934 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2935 | |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2936 | /* Do not emit rejection for unknown unidirectional stream as it is |
| 2937 | * forbidden to close some of them (H3 control stream and QPACK |
| 2938 | * encoder/decoder streams). |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2939 | */ |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2940 | if (quic_stream_is_uni(id)) { |
| 2941 | ret = 1; |
| 2942 | goto out; |
| 2943 | } |
| 2944 | |
Amaury Denoyelle | 7546301 | 2023-02-20 10:31:27 +0100 | [diff] [blame] | 2945 | ss = qc_frm_alloc(QUIC_FT_STOP_SENDING); |
| 2946 | if (!ss) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2947 | TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2948 | goto out; |
| 2949 | } |
| 2950 | |
Amaury Denoyelle | 7546301 | 2023-02-20 10:31:27 +0100 | [diff] [blame] | 2951 | ss->stop_sending.id = id; |
| 2952 | ss->stop_sending.app_error_code = app_error_code; |
| 2953 | |
| 2954 | rs = qc_frm_alloc(QUIC_FT_RESET_STREAM); |
| 2955 | if (!rs) { |
| 2956 | TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2957 | qc_frm_free(&ss); |
| 2958 | goto out; |
| 2959 | } |
| 2960 | |
| 2961 | rs->reset_stream.id = id; |
| 2962 | rs->reset_stream.app_error_code = app_error_code; |
| 2963 | rs->reset_stream.final_size = 0; |
| 2964 | |
| 2965 | LIST_APPEND(&qel->pktns->tx.frms, &ss->list); |
| 2966 | LIST_APPEND(&qel->pktns->tx.frms, &rs->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2967 | ret = 1; |
| 2968 | out: |
| 2969 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2970 | return ret; |
| 2971 | } |
| 2972 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2973 | /* Release the underlying memory use by <ncbuf> non-contiguous buffer */ |
| 2974 | static void quic_free_ncbuf(struct ncbuf *ncbuf) |
| 2975 | { |
| 2976 | struct buffer buf; |
| 2977 | |
| 2978 | if (ncb_is_null(ncbuf)) |
| 2979 | return; |
| 2980 | |
| 2981 | buf = b_make(ncbuf->area, ncbuf->size, 0, 0); |
| 2982 | b_free(&buf); |
| 2983 | offer_buffers(NULL, 1); |
| 2984 | |
| 2985 | *ncbuf = NCBUF_NULL; |
| 2986 | } |
| 2987 | |
| 2988 | /* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */ |
| 2989 | static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf) |
| 2990 | { |
| 2991 | struct buffer buf = BUF_NULL; |
| 2992 | |
| 2993 | if (!ncb_is_null(ncbuf)) |
| 2994 | return ncbuf; |
| 2995 | |
| 2996 | b_alloc(&buf); |
| 2997 | BUG_ON(b_is_null(&buf)); |
| 2998 | |
| 2999 | *ncbuf = ncb_make(buf.area, buf.size, 0); |
| 3000 | ncb_init(ncbuf, 0); |
| 3001 | |
| 3002 | return ncbuf; |
| 3003 | } |
| 3004 | |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3005 | /* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn. |
| 3006 | * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the |
| 3007 | * speed up handshake completion may be run after having received duplicated |
| 3008 | * CRYPTO data. |
| 3009 | */ |
| 3010 | static int qc_handle_crypto_frm(struct quic_conn *qc, |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3011 | struct qf_crypto *crypto_frm, struct quic_rx_packet *pkt, |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3012 | struct quic_enc_level *qel, int *fast_retrans) |
| 3013 | { |
| 3014 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3015 | enum ncb_ret ncb_ret; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3016 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 3017 | struct quic_rx_crypto_frm cfdebug = { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3018 | .offset_node.key = crypto_frm->offset, |
| 3019 | .len = crypto_frm->len, |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3020 | }; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3021 | struct quic_cstream *cstream = qel->cstream; |
| 3022 | struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3023 | |
| 3024 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 3025 | if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) { |
| 3026 | TRACE_PROTO("CRYPTO data discarded", |
| 3027 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 3028 | goto done; |
| 3029 | } |
| 3030 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3031 | if (unlikely(crypto_frm->offset < cstream->rx.offset)) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3032 | size_t diff; |
| 3033 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3034 | if (crypto_frm->offset + crypto_frm->len <= cstream->rx.offset) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3035 | /* Nothing to do */ |
| 3036 | TRACE_PROTO("Already received CRYPTO data", |
| 3037 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 3038 | if (qc_is_listener(qc) && qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] && |
| 3039 | !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP)) |
| 3040 | *fast_retrans = 1; |
| 3041 | goto done; |
| 3042 | } |
| 3043 | |
| 3044 | TRACE_PROTO("Partially already received CRYPTO data", |
| 3045 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 3046 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3047 | diff = cstream->rx.offset - crypto_frm->offset; |
| 3048 | crypto_frm->len -= diff; |
| 3049 | crypto_frm->data += diff; |
| 3050 | crypto_frm->offset = cstream->rx.offset; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3051 | } |
| 3052 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3053 | if (crypto_frm->offset == cstream->rx.offset && ncb_is_empty(ncbuf)) { |
| 3054 | if (!qc_provide_cdata(qel, qc->xprt_ctx, crypto_frm->data, crypto_frm->len, |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3055 | pkt, &cfdebug)) { |
| 3056 | // trace already emitted by function above |
| 3057 | goto leave; |
| 3058 | } |
| 3059 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3060 | cstream->rx.offset += crypto_frm->len; |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 3061 | 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] | 3062 | goto done; |
| 3063 | } |
| 3064 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3065 | if (!quic_get_ncbuf(ncbuf) || |
| 3066 | ncb_is_null(ncbuf)) { |
| 3067 | 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] | 3068 | goto leave; |
| 3069 | } |
| 3070 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3071 | /* crypto_frm->offset > cstream-trx.offset */ |
| 3072 | ncb_ret = ncb_add(ncbuf, crypto_frm->offset - cstream->rx.offset, |
| 3073 | (const char *)crypto_frm->data, crypto_frm->len, NCB_ADD_COMPARE); |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3074 | if (ncb_ret != NCB_RET_OK) { |
| 3075 | if (ncb_ret == NCB_RET_DATA_REJ) { |
| 3076 | TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc); |
| 3077 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION)); |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 3078 | qc_notify_err(qc); |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3079 | } |
| 3080 | else if (ncb_ret == NCB_RET_GAP_SIZE) { |
| 3081 | TRACE_ERROR("cannot bufferize frame due to gap size limit", |
| 3082 | QUIC_EV_CONN_PRSHPKT, qc); |
| 3083 | } |
| 3084 | goto leave; |
| 3085 | } |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3086 | |
| 3087 | done: |
| 3088 | ret = 1; |
| 3089 | leave: |
| 3090 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 3091 | return ret; |
| 3092 | } |
| 3093 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3094 | /* Build a NEW_CONNECTION_ID frame for <conn_id> CID of <qc> connection. |
| 3095 | * |
| 3096 | * Returns 1 on success else 0. |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3097 | */ |
| 3098 | static int qc_build_new_connection_id_frm(struct quic_conn *qc, |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3099 | struct quic_connection_id *conn_id) |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3100 | { |
| 3101 | int ret = 0; |
| 3102 | struct quic_frame *frm; |
| 3103 | struct quic_enc_level *qel; |
| 3104 | |
| 3105 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 3106 | |
| 3107 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3108 | frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID); |
| 3109 | if (!frm) { |
| 3110 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3111 | goto leave; |
| 3112 | } |
| 3113 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3114 | quic_connection_id_to_frm_cpy(frm, conn_id); |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3115 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
| 3116 | ret = 1; |
| 3117 | leave: |
| 3118 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 3119 | return ret; |
| 3120 | } |
| 3121 | |
| 3122 | |
| 3123 | /* Handle RETIRE_CONNECTION_ID frame from <frm> frame. |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3124 | * Return 1 if succeeded, 0 if not. If succeeded, also set <to_retire> |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3125 | * to the CID to be retired if not already retired. |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3126 | */ |
| 3127 | static int qc_handle_retire_connection_id_frm(struct quic_conn *qc, |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3128 | struct quic_frame *frm, |
| 3129 | struct quic_cid *dcid, |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3130 | struct quic_connection_id **to_retire) |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3131 | { |
| 3132 | int ret = 0; |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3133 | struct qf_retire_connection_id *rcid_frm = &frm->retire_connection_id; |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3134 | struct eb64_node *node; |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3135 | struct quic_connection_id *conn_id; |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3136 | |
| 3137 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 3138 | |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3139 | /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames: |
| 3140 | * Receipt of a RETIRE_CONNECTION_ID frame containing a sequence number greater |
| 3141 | * than any previously sent to the peer MUST be treated as a connection error |
| 3142 | * of type PROTOCOL_VIOLATION. |
| 3143 | */ |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3144 | if (rcid_frm->seq_num >= qc->next_cid_seq_num) { |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3145 | TRACE_PROTO("CID seq. number too big", QUIC_EV_CONN_PSTRM, qc, frm); |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3146 | goto protocol_violation; |
| 3147 | } |
| 3148 | |
| 3149 | /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames: |
| 3150 | * The sequence number specified in a RETIRE_CONNECTION_ID frame MUST NOT refer to |
| 3151 | * the Destination Connection ID field of the packet in which the frame is contained. |
| 3152 | * The peer MAY treat this as a connection error of type PROTOCOL_VIOLATION. |
| 3153 | */ |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3154 | node = eb64_lookup(&qc->cids, rcid_frm->seq_num); |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3155 | if (!node) { |
| 3156 | TRACE_PROTO("CID already retired", QUIC_EV_CONN_PSTRM, qc, frm); |
| 3157 | goto out; |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3158 | } |
| 3159 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3160 | conn_id = eb64_entry(node, struct quic_connection_id, seq_num); |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3161 | /* Note that the length of <dcid> has already been checked. It must match the |
| 3162 | * length of the CIDs which have been provided to the peer. |
| 3163 | */ |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3164 | if (!memcmp(dcid->data, conn_id->cid.data, QUIC_HAP_CID_LEN)) { |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3165 | TRACE_PROTO("cannot retire the current CID", QUIC_EV_CONN_PSTRM, qc, frm); |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3166 | goto protocol_violation; |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3167 | } |
| 3168 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3169 | *to_retire = conn_id; |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3170 | out: |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3171 | ret = 1; |
| 3172 | leave: |
| 3173 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 3174 | return ret; |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3175 | protocol_violation: |
| 3176 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION)); |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 3177 | qc_notify_err(qc); |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3178 | goto leave; |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3179 | } |
| 3180 | |
Amaury Denoyelle | efed86c | 2023-03-08 09:42:04 +0100 | [diff] [blame] | 3181 | /* Remove a <qc> quic-conn from its ha_thread_ctx list. If <closing> is true, |
| 3182 | * it will immediately be reinserted in the ha_thread_ctx quic_conns_clo list. |
| 3183 | */ |
| 3184 | static void qc_detach_th_ctx_list(struct quic_conn *qc, int closing) |
| 3185 | { |
| 3186 | struct bref *bref, *back; |
| 3187 | |
| 3188 | /* Detach CLI context watchers currently dumping this connection. |
| 3189 | * Reattach them to the next quic_conn instance. |
| 3190 | */ |
| 3191 | list_for_each_entry_safe(bref, back, &qc->back_refs, users) { |
| 3192 | /* Remove watcher from this quic_conn instance. */ |
| 3193 | LIST_DEL_INIT(&bref->users); |
| 3194 | |
| 3195 | /* Attach it to next instance unless it was the last list element. */ |
| 3196 | if (qc->el_th_ctx.n != &th_ctx->quic_conns && |
| 3197 | qc->el_th_ctx.n != &th_ctx->quic_conns_clo) { |
| 3198 | struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx, |
| 3199 | struct quic_conn *, |
| 3200 | el_th_ctx); |
| 3201 | LIST_APPEND(&next->back_refs, &bref->users); |
| 3202 | } |
| 3203 | bref->ref = qc->el_th_ctx.n; |
| 3204 | __ha_barrier_store(); |
| 3205 | } |
| 3206 | |
| 3207 | /* Remove quic_conn from global ha_thread_ctx list. */ |
| 3208 | LIST_DEL_INIT(&qc->el_th_ctx); |
| 3209 | |
| 3210 | if (closing) |
| 3211 | LIST_APPEND(&th_ctx->quic_conns_clo, &qc->el_th_ctx); |
| 3212 | } |
| 3213 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3214 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel> |
| 3215 | * as encryption level. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3216 | * Returns 1 if succeeded, 0 if failed. |
| 3217 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3218 | 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] | 3219 | struct quic_enc_level *qel) |
| 3220 | { |
| 3221 | struct quic_frame frm; |
| 3222 | const unsigned char *pos, *end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3223 | int fast_retrans = 0, ret = 0; |
| 3224 | |
| 3225 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 3226 | /* Skip the AAD */ |
| 3227 | pos = pkt->data + pkt->aad_len; |
| 3228 | end = pkt->data + pkt->len; |
| 3229 | |
Amaury Denoyelle | 3a89348 | 2023-10-09 10:42:35 +0200 | [diff] [blame] | 3230 | /* Packet with no frame. */ |
| 3231 | if (pos == end) { |
| 3232 | /* RFC9000 12.4. Frames and Frame Types |
| 3233 | * |
| 3234 | * The payload of a packet that contains frames MUST contain at least |
| 3235 | * one frame, and MAY contain multiple frames and multiple frame types. |
| 3236 | * An endpoint MUST treat receipt of a packet containing no frames as a |
| 3237 | * connection error of type PROTOCOL_VIOLATION. Frames always fit within |
| 3238 | * a single QUIC packet and cannot span multiple packets. |
| 3239 | */ |
| 3240 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION)); |
| 3241 | goto leave; |
| 3242 | } |
| 3243 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3244 | while (pos < end) { |
| 3245 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) { |
| 3246 | // trace already emitted by function above |
| 3247 | goto leave; |
| 3248 | } |
| 3249 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3250 | switch (frm.type) { |
| 3251 | case QUIC_FT_PADDING: |
| 3252 | break; |
| 3253 | case QUIC_FT_PING: |
| 3254 | break; |
| 3255 | case QUIC_FT_ACK: |
| 3256 | { |
| 3257 | unsigned int rtt_sample; |
| 3258 | |
Frédéric Lécaille | 809bd9f | 2023-04-06 13:13:08 +0200 | [diff] [blame] | 3259 | rtt_sample = UINT_MAX; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3260 | if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) { |
| 3261 | // trace already emitted by function above |
| 3262 | goto leave; |
| 3263 | } |
| 3264 | |
Frédéric Lécaille | 809bd9f | 2023-04-06 13:13:08 +0200 | [diff] [blame] | 3265 | if (rtt_sample != UINT_MAX) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3266 | unsigned int ack_delay; |
| 3267 | |
| 3268 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
| 3269 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
| 3270 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 3271 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
| 3272 | quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc); |
| 3273 | } |
| 3274 | break; |
| 3275 | } |
| 3276 | case QUIC_FT_RESET_STREAM: |
Amaury Denoyelle | 5854fc0 | 2022-12-09 16:25:48 +0100 | [diff] [blame] | 3277 | if (qc->mux_state == QC_MUX_READY) { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3278 | struct qf_reset_stream *rs_frm = &frm.reset_stream; |
| 3279 | qcc_recv_reset_stream(qc->qcc, rs_frm->id, rs_frm->app_error_code, rs_frm->final_size); |
Amaury Denoyelle | 5854fc0 | 2022-12-09 16:25:48 +0100 | [diff] [blame] | 3280 | } |
| 3281 | break; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3282 | case QUIC_FT_STOP_SENDING: |
| 3283 | { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3284 | struct qf_stop_sending *ss_frm = &frm.stop_sending; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3285 | if (qc->mux_state == QC_MUX_READY) { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3286 | if (qcc_recv_stop_sending(qc->qcc, ss_frm->id, |
| 3287 | ss_frm->app_error_code)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3288 | TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 3289 | goto leave; |
| 3290 | } |
| 3291 | } |
| 3292 | break; |
| 3293 | } |
| 3294 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3295 | if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3296 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3297 | break; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3298 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 3299 | { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3300 | struct qf_stream *strm_frm = &frm.stream; |
| 3301 | unsigned nb_streams = qc->rx.strms[qcs_id_type(strm_frm->id)].nb_streams; |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 3302 | const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3303 | |
| 3304 | /* The upper layer may not be allocated. */ |
| 3305 | if (qc->mux_state != QC_MUX_READY) { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3306 | if ((strm_frm->id >> QCS_ID_TYPE_SHIFT) < nb_streams) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3307 | TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3308 | } |
| 3309 | else { |
| 3310 | TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc); |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 3311 | if (qc->app_ops == &h3_ops) { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3312 | if (!qc_h3_request_reject(qc, strm_frm->id)) { |
Amaury Denoyelle | 156a89a | 2023-02-20 10:32:16 +0100 | [diff] [blame] | 3313 | TRACE_ERROR("error on request rejection", QUIC_EV_CONN_PRSHPKT, qc); |
| 3314 | /* This packet will not be acknowledged */ |
| 3315 | goto leave; |
| 3316 | } |
| 3317 | } |
| 3318 | else { |
| 3319 | /* This packet will not be acknowledged */ |
| 3320 | goto leave; |
Frédéric Lécaille | d18025e | 2023-01-20 15:33:50 +0100 | [diff] [blame] | 3321 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3322 | } |
Amaury Denoyelle | 315a4f6 | 2023-03-06 09:10:53 +0100 | [diff] [blame] | 3323 | |
| 3324 | break; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3325 | } |
| 3326 | |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3327 | if (!qc_handle_strm_frm(pkt, strm_frm, qc, fin)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3328 | TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 3329 | goto leave; |
| 3330 | } |
| 3331 | |
| 3332 | break; |
| 3333 | } |
| 3334 | case QUIC_FT_MAX_DATA: |
| 3335 | if (qc->mux_state == QC_MUX_READY) { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3336 | struct qf_max_data *md_frm = &frm.max_data; |
| 3337 | qcc_recv_max_data(qc->qcc, md_frm->max_data); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3338 | } |
| 3339 | break; |
| 3340 | case QUIC_FT_MAX_STREAM_DATA: |
| 3341 | if (qc->mux_state == QC_MUX_READY) { |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 3342 | struct qf_max_stream_data *msd_frm = &frm.max_stream_data; |
| 3343 | if (qcc_recv_max_stream_data(qc->qcc, msd_frm->id, |
| 3344 | msd_frm->max_stream_data)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3345 | TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 3346 | goto leave; |
| 3347 | } |
| 3348 | } |
| 3349 | break; |
| 3350 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 3351 | case QUIC_FT_MAX_STREAMS_UNI: |
| 3352 | break; |
| 3353 | case QUIC_FT_DATA_BLOCKED: |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 3354 | qc->cntrs.data_blocked++; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3355 | break; |
| 3356 | case QUIC_FT_STREAM_DATA_BLOCKED: |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 3357 | qc->cntrs.stream_data_blocked++; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3358 | break; |
| 3359 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
Amaury Denoyelle | 6d6ee0d | 2023-05-25 10:36:04 +0200 | [diff] [blame] | 3360 | qc->cntrs.streams_blocked_bidi++; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3361 | break; |
| 3362 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
Amaury Denoyelle | 6d6ee0d | 2023-05-25 10:36:04 +0200 | [diff] [blame] | 3363 | qc->cntrs.streams_blocked_uni++; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3364 | break; |
| 3365 | case QUIC_FT_NEW_CONNECTION_ID: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3366 | /* XXX TO DO XXX */ |
| 3367 | break; |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3368 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 3369 | { |
Willy Tarreau | 7f9656c | 2024-05-24 11:04:30 +0200 | [diff] [blame] | 3370 | struct quic_cid_tree *tree __maybe_unused; |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3371 | struct quic_connection_id *conn_id = NULL; |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3372 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3373 | if (!qc_handle_retire_connection_id_frm(qc, &frm, &pkt->dcid, &conn_id)) |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3374 | goto leave; |
| 3375 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3376 | if (!conn_id) |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3377 | break; |
| 3378 | |
Frédéric Lécaille | 0b25d70 | 2023-12-13 11:45:43 +0100 | [diff] [blame] | 3379 | tree = &quic_cid_trees[quic_cid_tree_idx(&conn_id->cid)]; |
| 3380 | HA_RWLOCK_WRLOCK(QC_CID_LOCK, &tree->lock); |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3381 | ebmb_delete(&conn_id->node); |
Frédéric Lécaille | 0b25d70 | 2023-12-13 11:45:43 +0100 | [diff] [blame] | 3382 | HA_RWLOCK_WRUNLOCK(QC_CID_LOCK, &tree->lock); |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3383 | eb64_delete(&conn_id->seq_num); |
| 3384 | pool_free(pool_head_quic_connection_id, conn_id); |
Frédéric Lécaille | cc101cd | 2023-03-08 11:01:58 +0100 | [diff] [blame] | 3385 | TRACE_PROTO("CID retired", QUIC_EV_CONN_PSTRM, qc); |
| 3386 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3387 | conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL); |
| 3388 | if (!conn_id) { |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3389 | TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3390 | } |
| 3391 | else { |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 3392 | quic_cid_insert(conn_id); |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 3393 | qc_build_new_connection_id_frm(qc, conn_id); |
Frédéric Lécaille | 8ac8a87 | 2023-03-06 18:16:34 +0100 | [diff] [blame] | 3394 | } |
| 3395 | break; |
| 3396 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3397 | case QUIC_FT_CONNECTION_CLOSE: |
| 3398 | case QUIC_FT_CONNECTION_CLOSE_APP: |
| 3399 | /* Increment the error counters */ |
| 3400 | qc_cc_err_count_inc(qc, &frm); |
| 3401 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
| 3402 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 3403 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 3404 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 3405 | } |
| 3406 | TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 3407 | /* RFC 9000 10.2. Immediate Close: |
| 3408 | * The closing and draining connection states exist to ensure |
| 3409 | * that connections close cleanly and that delayed or reordered |
| 3410 | * packets are properly discarded. These states SHOULD persist |
| 3411 | * for at least three times the current PTO interval... |
| 3412 | * |
| 3413 | * Rearm the idle timeout only one time when entering draining |
| 3414 | * state. |
| 3415 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3416 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | efed86c | 2023-03-08 09:42:04 +0100 | [diff] [blame] | 3417 | qc_detach_th_ctx_list(qc, 1); |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 3418 | qc_idle_timer_do_rearm(qc, 0); |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 3419 | qc_notify_err(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3420 | } |
| 3421 | break; |
| 3422 | case QUIC_FT_HANDSHAKE_DONE: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3423 | if (qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3424 | TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame", |
| 3425 | QUIC_EV_CONN_PRSHPKT, qc); |
Amaury Denoyelle | 2e4d3e4 | 2024-02-14 18:13:08 +0100 | [diff] [blame] | 3426 | |
| 3427 | /* RFC 9000 19.20. HANDSHAKE_DONE Frames |
| 3428 | * |
| 3429 | * A |
| 3430 | * server MUST treat receipt of a HANDSHAKE_DONE frame as a connection |
| 3431 | * error of type PROTOCOL_VIOLATION. |
| 3432 | */ |
| 3433 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION)); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3434 | goto leave; |
| 3435 | } |
| 3436 | |
| 3437 | qc->state = QUIC_HS_ST_CONFIRMED; |
| 3438 | break; |
| 3439 | default: |
| 3440 | TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc); |
| 3441 | goto leave; |
| 3442 | } |
| 3443 | } |
| 3444 | |
| 3445 | /* Flag this packet number space as having received a packet. */ |
| 3446 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
| 3447 | |
| 3448 | if (fast_retrans) { |
| 3449 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3450 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3451 | |
| 3452 | TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc); |
| 3453 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 3454 | qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP; |
| 3455 | } |
| 3456 | |
| 3457 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 3458 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 3459 | * be discarded. |
| 3460 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3461 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3462 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
| 3463 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 3464 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3465 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3466 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 3467 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3468 | qc_set_timer(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3469 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3470 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 3471 | } |
| 3472 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 3473 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3474 | } |
| 3475 | } |
| 3476 | |
| 3477 | ret = 1; |
| 3478 | leave: |
| 3479 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 3480 | return ret; |
| 3481 | } |
| 3482 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3483 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3484 | /* Allocate Tx buffer from <qc> quic-conn if needed. |
| 3485 | * |
| 3486 | * Returns allocated buffer or NULL on error. |
| 3487 | */ |
| 3488 | static struct buffer *qc_txb_alloc(struct quic_conn *qc) |
| 3489 | { |
| 3490 | struct buffer *buf = &qc->tx.buf; |
| 3491 | if (!b_alloc(buf)) |
| 3492 | return NULL; |
| 3493 | |
| 3494 | return buf; |
| 3495 | } |
| 3496 | |
| 3497 | /* Free Tx buffer from <qc> if it is empty. */ |
| 3498 | static void qc_txb_release(struct quic_conn *qc) |
| 3499 | { |
| 3500 | struct buffer *buf = &qc->tx.buf; |
| 3501 | |
| 3502 | /* For the moment sending function is responsible to purge the buffer |
| 3503 | * entirely. It may change in the future but this requires to be able |
| 3504 | * to reuse old data. |
Frédéric Lécaille | bbf86be | 2023-02-20 09:28:58 +0100 | [diff] [blame] | 3505 | * For the momemt we do not care to leave data in the buffer for |
| 3506 | * a connection which is supposed to be killed asap. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3507 | */ |
| 3508 | BUG_ON_HOT(buf && b_data(buf)); |
| 3509 | |
| 3510 | if (!b_data(buf)) { |
| 3511 | b_free(buf); |
| 3512 | offer_buffers(NULL, 1); |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | /* Commit a datagram payload written into <buf> of length <length>. <first_pkt> |
| 3517 | * must contains the address of the first packet stored in the payload. |
| 3518 | * |
| 3519 | * Caller is responsible that there is enough space in the buffer. |
| 3520 | */ |
| 3521 | static void qc_txb_store(struct buffer *buf, uint16_t length, |
| 3522 | struct quic_tx_packet *first_pkt) |
| 3523 | { |
| 3524 | const size_t hdlen = sizeof(uint16_t) + sizeof(void *); |
| 3525 | BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */ |
| 3526 | |
| 3527 | write_u16(b_tail(buf), length); |
| 3528 | write_ptr(b_tail(buf) + sizeof(length), first_pkt); |
| 3529 | b_add(buf, hdlen + length); |
| 3530 | } |
| 3531 | |
| 3532 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 3533 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 3534 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 3535 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 3536 | * Also set <*must_ack> to inform the caller if an acknowledgement should be sent. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3537 | */ |
| 3538 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 3539 | struct quic_enc_level *qel, int cc, int probe, |
| 3540 | int *must_ack) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3541 | { |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 3542 | int force_ack = |
| 3543 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3544 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3545 | int nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack; |
| 3546 | |
| 3547 | /* An acknowledgement must be sent if this has been forced by the caller, |
| 3548 | * typically during the handshake when the packets must be acknowledged as |
| 3549 | * soon as possible. This is also the case when the ack delay timer has been |
| 3550 | * triggered, or at least every QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK packets. |
| 3551 | */ |
| 3552 | *must_ack = (qc->flags & QUIC_FL_CONN_ACK_TIMER_FIRED) || |
| 3553 | ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 3554 | (force_ack || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK)); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3555 | |
| 3556 | /* Do not build any more packet if the TX secrets are not available or |
| 3557 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 3558 | * and if there is no more packets to send upon PTO expiration |
| 3559 | * and if there is no more ack-eliciting frames to send or in flight |
| 3560 | * congestion control limit is reached for prepared data |
| 3561 | */ |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 3562 | if (!quic_tls_has_tx_sec(qel) || |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 3563 | (!cc && !probe && !*must_ack && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3564 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 3565 | return 0; |
| 3566 | } |
| 3567 | |
| 3568 | return 1; |
| 3569 | } |
| 3570 | |
| 3571 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3572 | * <frms>. Each packet is stored in a distinct datagram written to <buf>. |
| 3573 | * |
| 3574 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3575 | * the address of the packet contained in the datagram. |
| 3576 | * |
| 3577 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3578 | * -1 if something wrong happened. |
| 3579 | */ |
| 3580 | static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3581 | struct list *frms) |
| 3582 | { |
| 3583 | int ret = -1; |
| 3584 | struct quic_enc_level *qel; |
| 3585 | unsigned char *end, *pos; |
| 3586 | struct quic_tx_packet *pkt; |
| 3587 | size_t total; |
| 3588 | /* Each datagram is prepended with its length followed by the address |
| 3589 | * of the first packet in the datagram. |
| 3590 | */ |
| 3591 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt); |
| 3592 | |
| 3593 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3594 | |
| 3595 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3596 | total = 0; |
| 3597 | pos = (unsigned char *)b_tail(buf); |
| 3598 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) { |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 3599 | int err, probe, cc, must_ack; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3600 | |
Frédéric Lécaille | e95e00e | 2023-04-24 10:59:33 +0200 | [diff] [blame] | 3601 | TRACE_PROTO("TX prep app pkts", QUIC_EV_CONN_PHPKTS, qc, qel, frms); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3602 | probe = 0; |
| 3603 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3604 | /* We do not probe if an immediate close was asked */ |
| 3605 | if (!cc) |
| 3606 | probe = qel->pktns->tx.pto_probe; |
| 3607 | |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 3608 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3609 | break; |
| 3610 | |
| 3611 | /* Leave room for the datagram header */ |
| 3612 | pos += dg_headlen; |
| 3613 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3614 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3615 | } |
| 3616 | else { |
| 3617 | end = pos + qc->path->mtu; |
| 3618 | } |
| 3619 | |
| 3620 | pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0, |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 3621 | QUIC_PACKET_TYPE_SHORT, must_ack, 0, probe, cc, &err); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3622 | switch (err) { |
Frédéric Lécaille | 2101700 | 2023-11-08 11:31:21 +0100 | [diff] [blame] | 3623 | case -3: |
| 3624 | qc_purge_txbuf(qc, buf); |
| 3625 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3626 | case -2: |
| 3627 | // trace already emitted by function above |
| 3628 | goto leave; |
| 3629 | case -1: |
| 3630 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 3631 | * MTU, we are here because of the congestion control window. There is |
| 3632 | * no need to try to reuse this buffer. |
| 3633 | */ |
Frédéric Lécaille | e47adca | 2023-04-07 18:12:00 +0200 | [diff] [blame] | 3634 | TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3635 | goto out; |
| 3636 | default: |
| 3637 | break; |
| 3638 | } |
| 3639 | |
| 3640 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 3641 | BUG_ON(!pkt); |
| 3642 | |
| 3643 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3644 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3645 | |
| 3646 | total += pkt->len; |
| 3647 | |
| 3648 | /* Write datagram header. */ |
| 3649 | qc_txb_store(buf, pkt->len, pkt); |
| 3650 | } |
| 3651 | |
| 3652 | out: |
| 3653 | ret = total; |
| 3654 | leave: |
| 3655 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3656 | return ret; |
| 3657 | } |
| 3658 | |
| 3659 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3660 | * <frms>. Several packets can be regrouped in a single datagram. The result is |
| 3661 | * written into <buf>. |
| 3662 | * |
| 3663 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3664 | * the address of first packet in the datagram. |
| 3665 | * |
| 3666 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3667 | * -1 if something wrong happened. |
| 3668 | */ |
| 3669 | static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3670 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3671 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3672 | { |
| 3673 | struct quic_enc_level *qel; |
| 3674 | unsigned char *end, *pos; |
| 3675 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 3676 | /* length of datagrams */ |
| 3677 | uint16_t dglen; |
| 3678 | size_t total; |
| 3679 | int ret = -1, padding; |
| 3680 | /* Each datagram is prepended with its length followed by the address |
| 3681 | * of the first packet in the datagram. |
| 3682 | */ |
| 3683 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt); |
| 3684 | struct list *frms; |
| 3685 | |
| 3686 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3687 | |
| 3688 | /* Currently qc_prep_pkts() does not handle buffer wrapping so the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 3689 | * caller must ensure that buf is reset. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3690 | */ |
| 3691 | BUG_ON_HOT(buf->head || buf->data); |
| 3692 | |
| 3693 | total = 0; |
| 3694 | qel = &qc->els[tel]; |
| 3695 | frms = tel_frms; |
| 3696 | dglen = 0; |
| 3697 | padding = 0; |
| 3698 | pos = (unsigned char *)b_head(buf); |
| 3699 | first_pkt = prv_pkt = NULL; |
| 3700 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 3701 | int err, probe, cc, must_ack; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3702 | enum quic_pkt_type pkt_type; |
| 3703 | struct quic_tls_ctx *tls_ctx; |
| 3704 | const struct quic_version *ver; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3705 | |
Frédéric Lécaille | e47adca | 2023-04-07 18:12:00 +0200 | [diff] [blame] | 3706 | TRACE_PROTO("TX prep pkts", QUIC_EV_CONN_PHPKTS, qc, qel); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3707 | probe = 0; |
| 3708 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3709 | /* We do not probe if an immediate close was asked */ |
| 3710 | if (!cc) |
| 3711 | probe = qel->pktns->tx.pto_probe; |
| 3712 | |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 3713 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3714 | if (prv_pkt) |
| 3715 | qc_txb_store(buf, dglen, first_pkt); |
| 3716 | /* Let's select the next encryption level */ |
| 3717 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 3718 | tel = next_tel; |
| 3719 | frms = next_tel_frms; |
| 3720 | qel = &qc->els[tel]; |
| 3721 | /* Build a new datagram */ |
| 3722 | prv_pkt = NULL; |
| 3723 | TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc); |
| 3724 | continue; |
| 3725 | } |
| 3726 | break; |
| 3727 | } |
| 3728 | |
| 3729 | pkt_type = quic_tls_level_pkt_type(tel); |
| 3730 | if (!prv_pkt) { |
| 3731 | /* Leave room for the datagram header */ |
| 3732 | pos += dg_headlen; |
| 3733 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3734 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3735 | } |
| 3736 | else { |
| 3737 | end = pos + qc->path->mtu; |
| 3738 | } |
| 3739 | } |
| 3740 | |
Frédéric Lécaille | 69e7118 | 2023-02-20 14:39:41 +0100 | [diff] [blame] | 3741 | /* RFC 9000 14.1 Initial datagram size |
| 3742 | * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting |
| 3743 | * Initial packets to at least the smallest allowed maximum datagram size of |
| 3744 | * 1200 bytes. |
| 3745 | * |
| 3746 | * Ensure that no ack-eliciting packets are sent into too small datagrams |
| 3747 | */ |
| 3748 | if (pkt_type == QUIC_PACKET_TYPE_INITIAL && !LIST_ISEMPTY(tel_frms)) { |
| 3749 | if (end - pos < QUIC_INITIAL_PACKET_MINLEN) { |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 3750 | TRACE_PROTO("No more enough room to build an Initial packet", |
Frédéric Lécaille | 69e7118 | 2023-02-20 14:39:41 +0100 | [diff] [blame] | 3751 | QUIC_EV_CONN_PHPKTS, qc); |
| 3752 | goto out; |
| 3753 | } |
| 3754 | |
| 3755 | /* Pad this Initial packet if there is no ack-eliciting frames to send from |
| 3756 | * the next packet number space. |
| 3757 | */ |
Frédéric Lécaille | ec93721 | 2023-03-03 17:34:41 +0100 | [diff] [blame] | 3758 | if (!next_tel_frms || LIST_ISEMPTY(next_tel_frms)) |
Frédéric Lécaille | 69e7118 | 2023-02-20 14:39:41 +0100 | [diff] [blame] | 3759 | padding = 1; |
| 3760 | } |
| 3761 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3762 | if (qc->negotiated_version) { |
| 3763 | ver = qc->negotiated_version; |
| 3764 | if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 3765 | tls_ctx = &qc->negotiated_ictx; |
| 3766 | else |
| 3767 | tls_ctx = &qel->tls_ctx; |
| 3768 | } |
| 3769 | else { |
| 3770 | ver = qc->original_version; |
| 3771 | tls_ctx = &qel->tls_ctx; |
| 3772 | } |
| 3773 | |
| 3774 | cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms, |
| 3775 | qc, ver, dglen, pkt_type, |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 3776 | must_ack, padding, probe, cc, &err); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3777 | switch (err) { |
Frédéric Lécaille | 2101700 | 2023-11-08 11:31:21 +0100 | [diff] [blame] | 3778 | case -3: |
| 3779 | qc_purge_tx_buf(buf); |
| 3780 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3781 | case -2: |
| 3782 | // trace already emitted by function above |
| 3783 | goto leave; |
| 3784 | case -1: |
| 3785 | /* If there was already a correct packet present, set the |
| 3786 | * current datagram as prepared into <cbuf>. |
| 3787 | */ |
| 3788 | if (prv_pkt) |
| 3789 | qc_txb_store(buf, dglen, first_pkt); |
Frédéric Lécaille | e47adca | 2023-04-07 18:12:00 +0200 | [diff] [blame] | 3790 | TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3791 | goto out; |
| 3792 | default: |
| 3793 | break; |
| 3794 | } |
| 3795 | |
| 3796 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 3797 | BUG_ON(!cur_pkt); |
| 3798 | |
| 3799 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3800 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3801 | |
| 3802 | total += cur_pkt->len; |
| 3803 | /* keep trace of the first packet in the datagram */ |
| 3804 | if (!first_pkt) |
| 3805 | first_pkt = cur_pkt; |
Frédéric Lécaille | 74b5f7b | 2022-11-20 18:35:35 +0100 | [diff] [blame] | 3806 | /* Attach the current one to the previous one and vice versa */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3807 | if (prv_pkt) { |
| 3808 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 3809 | cur_pkt->prev = prv_pkt; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3810 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 3811 | } |
| 3812 | /* Let's say we have to build a new dgram */ |
| 3813 | prv_pkt = NULL; |
| 3814 | dglen += cur_pkt->len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3815 | /* If the data for the current encryption level have all been sent, |
| 3816 | * select the next level. |
| 3817 | */ |
| 3818 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
Frédéric Lécaille | a576c1b | 2023-04-13 15:59:02 +0200 | [diff] [blame] | 3819 | next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms))) { |
Frédéric Lécaille | b546193 | 2023-06-14 08:54:51 +0200 | [diff] [blame] | 3820 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 3821 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3822 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3823 | tel = next_tel; |
| 3824 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 3825 | frms = &qc->els[tel].pktns->tx.frms; |
| 3826 | else |
| 3827 | frms = next_tel_frms; |
| 3828 | qel = &qc->els[tel]; |
| 3829 | if (!LIST_ISEMPTY(frms)) { |
| 3830 | /* If there is data for the next level, do not |
| 3831 | * consume a datagram. |
| 3832 | */ |
| 3833 | prv_pkt = cur_pkt; |
| 3834 | } |
| 3835 | } |
| 3836 | |
| 3837 | /* If we have to build a new datagram, set the current datagram as |
| 3838 | * prepared into <cbuf>. |
| 3839 | */ |
| 3840 | if (!prv_pkt) { |
| 3841 | qc_txb_store(buf, dglen, first_pkt); |
| 3842 | first_pkt = NULL; |
| 3843 | dglen = 0; |
| 3844 | padding = 0; |
| 3845 | } |
| 3846 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
| 3847 | (!qc_is_listener(qc) || |
| 3848 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3849 | padding = 1; |
| 3850 | } |
| 3851 | } |
| 3852 | |
| 3853 | out: |
| 3854 | ret = total; |
| 3855 | leave: |
| 3856 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3857 | return ret; |
| 3858 | } |
| 3859 | |
Frédéric Lécaille | ce0bb33 | 2023-04-24 11:11:55 +0200 | [diff] [blame] | 3860 | /* Free all frames in <l> list. In addition also remove all these frames |
| 3861 | * from the original ones if they are the results of duplications. |
| 3862 | */ |
Frédéric Lécaille | e66d67a | 2023-04-24 12:05:46 +0200 | [diff] [blame] | 3863 | static inline void qc_free_frm_list(struct list *l) |
Frédéric Lécaille | ce0bb33 | 2023-04-24 11:11:55 +0200 | [diff] [blame] | 3864 | { |
| 3865 | struct quic_frame *frm, *frmbak; |
| 3866 | |
| 3867 | list_for_each_entry_safe(frm, frmbak, l, list) { |
| 3868 | LIST_DEL_INIT(&frm->ref); |
| 3869 | qc_frm_free(&frm); |
| 3870 | } |
| 3871 | } |
| 3872 | |
| 3873 | /* Free <pkt> TX packet and all the packets coalesced to it. */ |
Frédéric Lécaille | e66d67a | 2023-04-24 12:05:46 +0200 | [diff] [blame] | 3874 | static inline void qc_free_tx_coalesced_pkts(struct quic_tx_packet *p) |
Frédéric Lécaille | ce0bb33 | 2023-04-24 11:11:55 +0200 | [diff] [blame] | 3875 | { |
| 3876 | struct quic_tx_packet *pkt, *nxt_pkt; |
| 3877 | |
| 3878 | for (pkt = p; pkt; pkt = nxt_pkt) { |
Frédéric Lécaille | e66d67a | 2023-04-24 12:05:46 +0200 | [diff] [blame] | 3879 | qc_free_frm_list(&pkt->frms); |
Frédéric Lécaille | ce0bb33 | 2023-04-24 11:11:55 +0200 | [diff] [blame] | 3880 | nxt_pkt = pkt->next; |
| 3881 | pool_free(pool_head_quic_tx_packet, pkt); |
| 3882 | } |
| 3883 | } |
| 3884 | |
| 3885 | /* Purge <buf> TX buffer from its prepare packets. */ |
Frédéric Lécaille | e66d67a | 2023-04-24 12:05:46 +0200 | [diff] [blame] | 3886 | static void qc_purge_tx_buf(struct buffer *buf) |
Frédéric Lécaille | ce0bb33 | 2023-04-24 11:11:55 +0200 | [diff] [blame] | 3887 | { |
| 3888 | while (b_contig_data(buf, 0)) { |
| 3889 | uint16_t dglen; |
| 3890 | struct quic_tx_packet *pkt; |
| 3891 | size_t headlen = sizeof dglen + sizeof pkt; |
| 3892 | |
| 3893 | dglen = read_u16(b_head(buf)); |
| 3894 | pkt = read_ptr(b_head(buf) + sizeof dglen); |
Frédéric Lécaille | e66d67a | 2023-04-24 12:05:46 +0200 | [diff] [blame] | 3895 | qc_free_tx_coalesced_pkts(pkt); |
Frédéric Lécaille | ce0bb33 | 2023-04-24 11:11:55 +0200 | [diff] [blame] | 3896 | b_del(buf, dglen + headlen); |
| 3897 | } |
| 3898 | |
| 3899 | BUG_ON(b_data(buf)); |
| 3900 | } |
| 3901 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3902 | /* Send datagrams stored in <buf>. |
| 3903 | * |
Amaury Denoyelle | 1febc2d | 2023-02-23 11:18:38 +0100 | [diff] [blame] | 3904 | * This function returns 1 for success. On error, there is several behavior |
| 3905 | * depending on underlying sendto() error : |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 3906 | * - for an unrecoverable error, 0 is returned and connection is killed. |
| 3907 | * - a transient error is handled differently if connection has its owned |
| 3908 | * socket. If this is the case, 0 is returned and socket is subscribed on the |
| 3909 | * poller. The other case is assimilated to a success case with 1 returned. |
Amaury Denoyelle | 1febc2d | 2023-02-23 11:18:38 +0100 | [diff] [blame] | 3910 | * Remaining data are purged from the buffer and will eventually be detected |
| 3911 | * as lost which gives the opportunity to retry sending. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3912 | */ |
| 3913 | int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx) |
| 3914 | { |
Frédéric Lécaille | a2c62c3 | 2023-02-10 14:13:43 +0100 | [diff] [blame] | 3915 | int ret = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3916 | struct quic_conn *qc; |
| 3917 | char skip_sendto = 0; |
| 3918 | |
| 3919 | qc = ctx->qc; |
| 3920 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
| 3921 | while (b_contig_data(buf, 0)) { |
| 3922 | unsigned char *pos; |
| 3923 | struct buffer tmpbuf = { }; |
| 3924 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3925 | uint16_t dglen; |
| 3926 | size_t headlen = sizeof dglen + sizeof first_pkt; |
| 3927 | unsigned int time_sent; |
| 3928 | |
| 3929 | pos = (unsigned char *)b_head(buf); |
| 3930 | dglen = read_u16(pos); |
| 3931 | BUG_ON_HOT(!dglen); /* this should not happen */ |
| 3932 | |
| 3933 | pos += sizeof dglen; |
| 3934 | first_pkt = read_ptr(pos); |
| 3935 | pos += sizeof first_pkt; |
| 3936 | tmpbuf.area = (char *)pos; |
| 3937 | tmpbuf.size = tmpbuf.data = dglen; |
| 3938 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 3939 | TRACE_PROTO("TX dgram", QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3940 | /* If sendto is on error just skip the call to it for the rest |
| 3941 | * of the loop but continue to purge the buffer. Data will be |
| 3942 | * transmitted when QUIC packets are detected as lost on our |
| 3943 | * side. |
| 3944 | * |
| 3945 | * TODO use fd-monitoring to detect when send operation can be |
| 3946 | * retry. This should improve the bandwidth without relying on |
| 3947 | * retransmission timer. However, it requires a major rework on |
| 3948 | * quic-conn fd management. |
| 3949 | */ |
| 3950 | if (!skip_sendto) { |
Amaury Denoyelle | 1febc2d | 2023-02-23 11:18:38 +0100 | [diff] [blame] | 3951 | int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0); |
| 3952 | if (ret < 0) { |
Frédéric Lécaille | ce0bb33 | 2023-04-24 11:11:55 +0200 | [diff] [blame] | 3953 | TRACE_ERROR("sendto fatal error", QUIC_EV_CONN_SPPKTS, qc, first_pkt); |
Amaury Denoyelle | 1febc2d | 2023-02-23 11:18:38 +0100 | [diff] [blame] | 3954 | qc_kill_conn(qc); |
Frédéric Lécaille | e66d67a | 2023-04-24 12:05:46 +0200 | [diff] [blame] | 3955 | qc_free_tx_coalesced_pkts(first_pkt); |
Frédéric Lécaille | ce0bb33 | 2023-04-24 11:11:55 +0200 | [diff] [blame] | 3956 | b_del(buf, dglen + headlen); |
Frédéric Lécaille | e66d67a | 2023-04-24 12:05:46 +0200 | [diff] [blame] | 3957 | qc_purge_tx_buf(buf); |
Amaury Denoyelle | 1febc2d | 2023-02-23 11:18:38 +0100 | [diff] [blame] | 3958 | goto leave; |
| 3959 | } |
| 3960 | else if (!ret) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 3961 | /* Connection owned socket : poller will wake us up when transient error is cleared. */ |
| 3962 | if (qc_test_fd(qc)) { |
| 3963 | TRACE_ERROR("sendto error, subscribe to poller", QUIC_EV_CONN_SPPKTS, qc); |
| 3964 | goto leave; |
| 3965 | } |
| 3966 | |
| 3967 | /* No connection owned-socket : rely on retransmission to retry sending. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3968 | skip_sendto = 1; |
| 3969 | TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc); |
| 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | b_del(buf, dglen + headlen); |
| 3974 | qc->tx.bytes += tmpbuf.data; |
| 3975 | time_sent = now_ms; |
| 3976 | |
| 3977 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
Frédéric Lécaille | ceb88b8 | 2023-02-20 14:43:55 +0100 | [diff] [blame] | 3978 | /* RFC 9000 14.1 Initial datagram size |
| 3979 | * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting |
| 3980 | * Initial packets to at least the smallest allowed maximum datagram size of |
| 3981 | * 1200 bytes. |
| 3982 | */ |
Frédéric Lécaille | 12a815a | 2023-05-24 15:55:14 +0200 | [diff] [blame] | 3983 | qc->cntrs.sent_pkt++; |
Frédéric Lécaille | ceb88b8 | 2023-02-20 14:43:55 +0100 | [diff] [blame] | 3984 | BUG_ON_HOT(pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 3985 | (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) && |
| 3986 | dglen < QUIC_INITIAL_PACKET_MINLEN); |
| 3987 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3988 | pkt->time_sent = time_sent; |
| 3989 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3990 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
| 3991 | qc->path->ifae_pkts++; |
| 3992 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 3993 | qc_idle_timer_rearm(qc, 0, 0); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3994 | } |
| 3995 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3996 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3997 | qc->flags |= QUIC_FL_CONN_CLOSING; |
Amaury Denoyelle | efed86c | 2023-03-08 09:42:04 +0100 | [diff] [blame] | 3998 | qc_detach_th_ctx_list(qc, 1); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3999 | |
| 4000 | /* RFC 9000 10.2. Immediate Close: |
| 4001 | * The closing and draining connection states exist to ensure |
| 4002 | * that connections close cleanly and that delayed or reordered |
| 4003 | * packets are properly discarded. These states SHOULD persist |
| 4004 | * for at least three times the current PTO interval... |
| 4005 | * |
| 4006 | * Rearm the idle timeout only one time when entering closing |
| 4007 | * state. |
| 4008 | */ |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 4009 | qc_idle_timer_do_rearm(qc, 0); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4010 | if (qc->timer_task) { |
| 4011 | task_destroy(qc->timer_task); |
| 4012 | qc->timer_task = NULL; |
| 4013 | } |
| 4014 | } |
| 4015 | qc->path->in_flight += pkt->in_flight_len; |
| 4016 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 4017 | if (pkt->in_flight_len) |
| 4018 | qc_set_timer(qc); |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 4019 | TRACE_PROTO("TX pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4020 | next_pkt = pkt->next; |
| 4021 | quic_tx_packet_refinc(pkt); |
| 4022 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
| 4023 | } |
| 4024 | } |
| 4025 | |
Frédéric Lécaille | a2c62c3 | 2023-02-10 14:13:43 +0100 | [diff] [blame] | 4026 | ret = 1; |
| 4027 | leave: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4028 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 4029 | |
Frédéric Lécaille | a2c62c3 | 2023-02-10 14:13:43 +0100 | [diff] [blame] | 4030 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4031 | } |
| 4032 | |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4033 | /* Copy at <pos> position a stateless reset token depending on the |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4034 | * <salt> salt input. This is the cluster secret which will be derived |
| 4035 | * as HKDF input secret to generate this token. |
| 4036 | * Return 1 if succeeded, 0 if not. |
| 4037 | */ |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4038 | static int quic_stateless_reset_token_cpy(unsigned char *pos, size_t len, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4039 | const unsigned char *salt, size_t saltlen) |
| 4040 | { |
| 4041 | /* Input secret */ |
Frédéric Lécaille | 0499db4 | 2023-09-07 18:43:52 +0200 | [diff] [blame] | 4042 | const unsigned char *key = global.cluster_secret; |
| 4043 | size_t keylen = sizeof global.cluster_secret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4044 | /* Info */ |
| 4045 | const unsigned char label[] = "stateless token"; |
| 4046 | size_t labellen = sizeof label - 1; |
| 4047 | int ret; |
| 4048 | |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4049 | ret = quic_hkdf_extract_and_expand(EVP_sha256(), pos, len, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4050 | key, keylen, salt, saltlen, label, labellen); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4051 | return ret; |
| 4052 | } |
| 4053 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4054 | /* Initialize the stateless reset token attached to <conn_id> connection ID. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4055 | * Returns 1 if succeeded, 0 if not. |
| 4056 | */ |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4057 | static int quic_stateless_reset_token_init(struct quic_connection_id *conn_id) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4058 | { |
Frédéric Lécaille | 0499db4 | 2023-09-07 18:43:52 +0200 | [diff] [blame] | 4059 | /* Output secret */ |
| 4060 | unsigned char *token = conn_id->stateless_reset_token; |
| 4061 | size_t tokenlen = sizeof conn_id->stateless_reset_token; |
| 4062 | /* Salt */ |
| 4063 | const unsigned char *cid = conn_id->cid.data; |
| 4064 | size_t cidlen = conn_id->cid.len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4065 | |
Frédéric Lécaille | 0499db4 | 2023-09-07 18:43:52 +0200 | [diff] [blame] | 4066 | return quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4067 | } |
| 4068 | |
Amaury Denoyelle | 1e959ad | 2023-04-13 17:34:56 +0200 | [diff] [blame] | 4069 | /* Generate a CID directly derived from <orig> CID and <addr> address. |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4070 | * |
Amaury Denoyelle | c2a9264 | 2023-04-13 15:26:18 +0200 | [diff] [blame] | 4071 | * Returns the derived CID. |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4072 | */ |
Amaury Denoyelle | c2a9264 | 2023-04-13 15:26:18 +0200 | [diff] [blame] | 4073 | struct quic_cid quic_derive_cid(const struct quic_cid *orig, |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4074 | const struct sockaddr_storage *addr) |
| 4075 | { |
Amaury Denoyelle | c2a9264 | 2023-04-13 15:26:18 +0200 | [diff] [blame] | 4076 | struct quic_cid cid; |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4077 | const struct sockaddr_in *in; |
| 4078 | const struct sockaddr_in6 *in6; |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4079 | char *pos = trash.area; |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4080 | size_t idx = 0; |
| 4081 | uint64_t hash; |
Amaury Denoyelle | c2a9264 | 2023-04-13 15:26:18 +0200 | [diff] [blame] | 4082 | int i; |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4083 | |
| 4084 | /* Prepare buffer for hash using original CID first. */ |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4085 | memcpy(pos, orig->data, orig->len); |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4086 | idx += orig->len; |
| 4087 | |
| 4088 | /* Concatenate client address. */ |
| 4089 | switch (addr->ss_family) { |
| 4090 | case AF_INET: |
| 4091 | in = (struct sockaddr_in *)addr; |
| 4092 | |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4093 | memcpy(&pos[idx], &in->sin_addr, sizeof(in->sin_addr)); |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4094 | idx += sizeof(in->sin_addr); |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4095 | memcpy(&pos[idx], &in->sin_port, sizeof(in->sin_port)); |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4096 | idx += sizeof(in->sin_port); |
| 4097 | break; |
| 4098 | |
| 4099 | case AF_INET6: |
| 4100 | in6 = (struct sockaddr_in6 *)addr; |
| 4101 | |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4102 | memcpy(&pos[idx], &in6->sin6_addr, sizeof(in6->sin6_addr)); |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4103 | idx += sizeof(in6->sin6_addr); |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4104 | memcpy(&pos[idx], &in6->sin6_port, sizeof(in6->sin6_port)); |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4105 | idx += sizeof(in6->sin6_port); |
| 4106 | break; |
| 4107 | |
| 4108 | default: |
| 4109 | /* TODO to implement */ |
| 4110 | ABORT_NOW(); |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | /* Avoid similar values between multiple haproxy process. */ |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4114 | memcpy(&pos[idx], boot_seed, sizeof(boot_seed)); |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4115 | idx += sizeof(boot_seed); |
| 4116 | |
| 4117 | /* Hash the final buffer content. */ |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4118 | hash = XXH64(pos, idx, 0); |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4119 | |
Amaury Denoyelle | c2a9264 | 2023-04-13 15:26:18 +0200 | [diff] [blame] | 4120 | for (i = 0; i < sizeof(hash); ++i) |
| 4121 | cid.data[i] = hash >> ((sizeof(hash) * 7) - (8 * i)); |
| 4122 | cid.len = sizeof(hash); |
| 4123 | |
Amaury Denoyelle | c2a9264 | 2023-04-13 15:26:18 +0200 | [diff] [blame] | 4124 | return cid; |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4125 | } |
| 4126 | |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4127 | /* Retrieve the thread ID associated to QUIC connection ID <cid> of length |
| 4128 | * <cid_len>. CID may be not found on the CID tree because it is an ODCID. In |
| 4129 | * this case, it will derived using client address <cli_addr> as hash |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4130 | * parameter. However, this is done only if <pos> points to an INITIAL or 0RTT |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4131 | * packet of length <len>. |
| 4132 | * |
| 4133 | * Returns the thread ID or a negative error code. |
| 4134 | */ |
| 4135 | int quic_get_cid_tid(const unsigned char *cid, size_t cid_len, |
| 4136 | const struct sockaddr_storage *cli_addr, |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4137 | unsigned char *pos, size_t len) |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4138 | { |
| 4139 | struct quic_cid_tree *tree; |
| 4140 | struct quic_connection_id *conn_id; |
| 4141 | struct ebmb_node *node; |
Amaury Denoyelle | fd5caa2 | 2024-06-27 17:15:55 +0200 | [diff] [blame] | 4142 | int cid_tid = -1; |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4143 | |
| 4144 | tree = &quic_cid_trees[_quic_cid_tree_idx(cid)]; |
| 4145 | HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock); |
| 4146 | node = ebmb_lookup(&tree->root, cid, cid_len); |
Amaury Denoyelle | fd5caa2 | 2024-06-27 17:15:55 +0200 | [diff] [blame] | 4147 | if (node) { |
| 4148 | conn_id = ebmb_entry(node, struct quic_connection_id, node); |
| 4149 | cid_tid = HA_ATOMIC_LOAD(&conn_id->tid); |
| 4150 | } |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4151 | HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock); |
| 4152 | |
Amaury Denoyelle | fd5caa2 | 2024-06-27 17:15:55 +0200 | [diff] [blame] | 4153 | /* If CID not found, it may be an ODCID, thus not stored in global CID |
| 4154 | * tree. Derive it to its associated DCID value and reperform a lookup. |
| 4155 | */ |
| 4156 | if (cid_tid < 0) { |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4157 | struct quic_cid orig, derive_cid; |
| 4158 | struct quic_rx_packet pkt; |
| 4159 | |
Frédéric Lécaille | 81a02b5 | 2023-04-24 15:29:56 +0200 | [diff] [blame] | 4160 | if (!qc_parse_hd_form(&pkt, &pos, pos + len)) |
Amaury Denoyelle | fd5caa2 | 2024-06-27 17:15:55 +0200 | [diff] [blame] | 4161 | goto out; |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4162 | |
Amaury Denoyelle | fd5caa2 | 2024-06-27 17:15:55 +0200 | [diff] [blame] | 4163 | /* ODCID are only used in INITIAL or 0-RTT packets */ |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4164 | if (pkt.type != QUIC_PACKET_TYPE_INITIAL && |
| 4165 | pkt.type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | fd5caa2 | 2024-06-27 17:15:55 +0200 | [diff] [blame] | 4166 | goto out; |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | memcpy(orig.data, cid, cid_len); |
| 4170 | orig.len = cid_len; |
| 4171 | derive_cid = quic_derive_cid(&orig, cli_addr); |
| 4172 | |
| 4173 | tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)]; |
| 4174 | HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock); |
| 4175 | node = ebmb_lookup(&tree->root, cid, cid_len); |
Amaury Denoyelle | fd5caa2 | 2024-06-27 17:15:55 +0200 | [diff] [blame] | 4176 | if (node) { |
| 4177 | conn_id = ebmb_entry(node, struct quic_connection_id, node); |
| 4178 | cid_tid = HA_ATOMIC_LOAD(&conn_id->tid); |
| 4179 | } |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4180 | HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock); |
| 4181 | } |
| 4182 | |
Amaury Denoyelle | fd5caa2 | 2024-06-27 17:15:55 +0200 | [diff] [blame] | 4183 | out: |
| 4184 | return cid_tid; |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4185 | } |
| 4186 | |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame] | 4187 | /* Allocate a new CID and attach it to <root> ebtree. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4188 | * |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4189 | * If <orig> and <addr> params are non null, the new CID value is directly |
| 4190 | * derived from them. Else a random value is generated. The CID is then marked |
| 4191 | * with the current thread ID. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4192 | * |
| 4193 | * Returns the new CID if succeeded, NULL if not. |
| 4194 | */ |
| 4195 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4196 | struct quic_conn *qc, |
| 4197 | const struct quic_cid *orig, |
| 4198 | const struct sockaddr_storage *addr) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4199 | { |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4200 | struct quic_connection_id *conn_id; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4201 | |
| 4202 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4203 | |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4204 | /* Caller must set either none or both values. */ |
| 4205 | BUG_ON(!!orig != !!addr); |
| 4206 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4207 | conn_id = pool_alloc(pool_head_quic_connection_id); |
| 4208 | if (!conn_id) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4209 | TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 4210 | goto err; |
| 4211 | } |
| 4212 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4213 | conn_id->cid.len = QUIC_HAP_CID_LEN; |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4214 | |
| 4215 | if (!orig) { |
| 4216 | /* TODO: RAND_bytes() should be replaced */ |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4217 | if (RAND_bytes(conn_id->cid.data, conn_id->cid.len) != 1) { |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4218 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc); |
| 4219 | goto err; |
| 4220 | } |
Amaury Denoyelle | 162baaf | 2023-04-03 18:49:39 +0200 | [diff] [blame] | 4221 | } |
| 4222 | else { |
| 4223 | /* Derive the new CID value from original CID. */ |
Amaury Denoyelle | c2a9264 | 2023-04-13 15:26:18 +0200 | [diff] [blame] | 4224 | conn_id->cid = quic_derive_cid(orig, addr); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4225 | } |
| 4226 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4227 | if (quic_stateless_reset_token_init(conn_id) != 1) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4228 | TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc); |
| 4229 | goto err; |
| 4230 | } |
| 4231 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4232 | conn_id->qc = qc; |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4233 | HA_ATOMIC_STORE(&conn_id->tid, tid); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4234 | |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 4235 | conn_id->seq_num.key = qc ? qc->next_cid_seq_num++ : 0; |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4236 | conn_id->retire_prior_to = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4237 | /* insert the allocated CID in the quic_conn tree */ |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 4238 | if (root) |
| 4239 | eb64_insert(root, &conn_id->seq_num); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4240 | |
| 4241 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4242 | return conn_id; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4243 | |
| 4244 | err: |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4245 | pool_free(pool_head_quic_connection_id, conn_id); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4246 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4247 | return NULL; |
| 4248 | } |
| 4249 | |
| 4250 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 4251 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 4252 | * a HANDSHAKE_DONE frame. |
| 4253 | * Return 1 if succeeded, 0 if not. |
| 4254 | */ |
| 4255 | static int quic_build_post_handshake_frames(struct quic_conn *qc) |
| 4256 | { |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame] | 4257 | int ret = 0, max; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4258 | struct quic_enc_level *qel; |
| 4259 | struct quic_frame *frm, *frmbak; |
| 4260 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 4261 | struct eb64_node *node; |
| 4262 | |
| 4263 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 4264 | |
| 4265 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4266 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
| 4267 | if (qc_is_listener(qc)) { |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 4268 | frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4269 | if (!frm) { |
| 4270 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 4271 | goto leave; |
| 4272 | } |
| 4273 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4274 | LIST_APPEND(&frm_list, &frm->list); |
| 4275 | } |
| 4276 | |
| 4277 | /* Initialize <max> connection IDs minus one: there is |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame] | 4278 | * already one connection ID used for the current connection. Also limit |
| 4279 | * the number of connection IDs sent to the peer to 4 (3 from this function |
| 4280 | * plus 1 for the current connection. |
| 4281 | * Note that active_connection_id_limit >= 2: this has been already checked |
| 4282 | * when receiving this parameter. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4283 | */ |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame] | 4284 | max = QUIC_MIN(qc->tx.params.active_connection_id_limit - 1, (uint64_t)3); |
| 4285 | while (max--) { |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4286 | struct quic_connection_id *conn_id; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4287 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 4288 | frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4289 | if (!frm) { |
| 4290 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 4291 | goto err; |
| 4292 | } |
| 4293 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4294 | conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL); |
| 4295 | if (!conn_id) { |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 4296 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4297 | TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 4298 | goto err; |
| 4299 | } |
| 4300 | |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4301 | /* TODO To prevent CID tree locking, all CIDs created here |
| 4302 | * could be allocated at the same time as the first one. |
| 4303 | */ |
| 4304 | quic_cid_insert(conn_id); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4305 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4306 | quic_connection_id_to_frm_cpy(frm, conn_id); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4307 | LIST_APPEND(&frm_list, &frm->list); |
| 4308 | } |
| 4309 | |
| 4310 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Amaury Denoyelle | 1304d19 | 2023-04-11 16:46:03 +0200 | [diff] [blame] | 4311 | qc->flags &= ~QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4312 | |
| 4313 | ret = 1; |
| 4314 | leave: |
| 4315 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 4316 | return ret; |
| 4317 | |
| 4318 | err: |
| 4319 | /* free the frames */ |
| 4320 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 4321 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4322 | |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame] | 4323 | /* The first CID sequence number value used to allocated CIDs by this function is 1, |
| 4324 | * 0 being the sequence number of the CID for this connection. |
| 4325 | */ |
| 4326 | node = eb64_lookup_ge(&qc->cids, 1); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4327 | while (node) { |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4328 | struct quic_connection_id *conn_id; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4329 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4330 | conn_id = eb64_entry(node, struct quic_connection_id, seq_num); |
| 4331 | if (conn_id->seq_num.key >= max) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4332 | break; |
| 4333 | |
| 4334 | node = eb64_next(node); |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 4335 | quic_cid_delete(conn_id); |
| 4336 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 4337 | eb64_delete(&conn_id->seq_num); |
| 4338 | pool_free(pool_head_quic_connection_id, conn_id); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4339 | } |
| 4340 | goto leave; |
| 4341 | } |
| 4342 | |
| 4343 | /* Deallocate <l> list of ACK ranges. */ |
| 4344 | void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs) |
| 4345 | { |
| 4346 | struct eb64_node *n; |
| 4347 | struct quic_arng_node *ar; |
| 4348 | |
| 4349 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4350 | |
| 4351 | n = eb64_first(&arngs->root); |
| 4352 | while (n) { |
| 4353 | struct eb64_node *next; |
| 4354 | |
| 4355 | ar = eb64_entry(n, struct quic_arng_node, first); |
| 4356 | next = eb64_next(n); |
| 4357 | eb64_delete(n); |
| 4358 | pool_free(pool_head_quic_arng, ar); |
| 4359 | n = next; |
| 4360 | } |
| 4361 | |
| 4362 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4363 | } |
| 4364 | |
| 4365 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 4366 | * descending order. |
| 4367 | */ |
| 4368 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 4369 | struct quic_arng_node *q) |
| 4370 | { |
| 4371 | return p->first.key - q->last - 2; |
| 4372 | } |
| 4373 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4374 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 4375 | static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs) |
| 4376 | { |
| 4377 | struct eb64_node *node, *next; |
| 4378 | struct quic_arng_node *ar, *ar_next; |
| 4379 | |
| 4380 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4381 | |
| 4382 | node = eb64_last(&arngs->root); |
| 4383 | if (!node) |
| 4384 | goto leave; |
| 4385 | |
| 4386 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 4387 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 4388 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 4389 | |
| 4390 | while ((next = eb64_prev(node))) { |
| 4391 | ar_next = eb64_entry(next, struct quic_arng_node, first); |
| 4392 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 4393 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 4394 | node = next; |
| 4395 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 4396 | } |
| 4397 | |
| 4398 | leave: |
| 4399 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4400 | } |
| 4401 | |
| 4402 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 4403 | * Returns the ack range node which has been inserted if succeeded, NULL if not. |
| 4404 | */ |
| 4405 | static inline |
| 4406 | struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc, |
| 4407 | struct quic_arngs *arngs, |
| 4408 | struct quic_arng *ar) |
| 4409 | { |
| 4410 | struct quic_arng_node *new_ar; |
| 4411 | |
| 4412 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 4413 | |
Frédéric Lécaille | 0ed9403 | 2023-04-17 14:10:14 +0200 | [diff] [blame] | 4414 | if (arngs->sz >= QUIC_MAX_ACK_RANGES) { |
Frederic Lecaille | 3ce4943 | 2024-02-05 14:07:51 +0100 | [diff] [blame] | 4415 | struct eb64_node *first; |
Frédéric Lécaille | 0ed9403 | 2023-04-17 14:10:14 +0200 | [diff] [blame] | 4416 | |
Frederic Lecaille | 3ce4943 | 2024-02-05 14:07:51 +0100 | [diff] [blame] | 4417 | first = eb64_first(&arngs->root); |
| 4418 | BUG_ON(first == NULL); |
| 4419 | eb64_delete(first); |
| 4420 | pool_free(pool_head_quic_arng, first); |
Frédéric Lécaille | 0ed9403 | 2023-04-17 14:10:14 +0200 | [diff] [blame] | 4421 | arngs->sz--; |
| 4422 | } |
| 4423 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4424 | new_ar = pool_alloc(pool_head_quic_arng); |
| 4425 | if (!new_ar) { |
| 4426 | TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc); |
| 4427 | goto leave; |
| 4428 | } |
| 4429 | |
| 4430 | new_ar->first.key = ar->first; |
| 4431 | new_ar->last = ar->last; |
| 4432 | eb64_insert(&arngs->root, &new_ar->first); |
| 4433 | arngs->sz++; |
| 4434 | |
| 4435 | leave: |
| 4436 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 4437 | return new_ar; |
| 4438 | } |
| 4439 | |
| 4440 | /* Update <arngs> tree of ACK ranges with <ar> as new ACK range value. |
| 4441 | * Note that this function computes the number of bytes required to encode |
| 4442 | * this tree of ACK ranges in descending order. |
| 4443 | * |
| 4444 | * Descending order |
| 4445 | * -------------> |
| 4446 | * range1 range2 |
| 4447 | * ..........|--------|..............|--------| |
| 4448 | * ^ ^ ^ ^ |
| 4449 | * | | | | |
| 4450 | * last1 first1 last2 first2 |
| 4451 | * ..........+--------+--------------+--------+...... |
| 4452 | * diff1 gap12 diff2 |
| 4453 | * |
| 4454 | * To encode the previous list of ranges we must encode integers as follows in |
| 4455 | * descending order: |
| 4456 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
| 4457 | * with diff1 = last1 - first1 |
| 4458 | * diff2 = last2 - first2 |
| 4459 | * gap12 = first1 - last2 - 2 (>= 0) |
| 4460 | * |
| 4461 | |
| 4462 | returns 0 on error |
| 4463 | |
| 4464 | */ |
| 4465 | int quic_update_ack_ranges_list(struct quic_conn *qc, |
| 4466 | struct quic_arngs *arngs, |
| 4467 | struct quic_arng *ar) |
| 4468 | { |
| 4469 | int ret = 0; |
| 4470 | struct eb64_node *le; |
| 4471 | struct quic_arng_node *new_node; |
| 4472 | struct eb64_node *new; |
| 4473 | |
| 4474 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 4475 | |
| 4476 | new = NULL; |
| 4477 | if (eb_is_empty(&arngs->root)) { |
| 4478 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 4479 | if (new_node) |
| 4480 | ret = 1; |
| 4481 | |
| 4482 | goto leave; |
| 4483 | } |
| 4484 | |
| 4485 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 4486 | if (!le) { |
| 4487 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 4488 | if (!new_node) |
| 4489 | goto leave; |
| 4490 | |
| 4491 | new = &new_node->first; |
| 4492 | } |
| 4493 | else { |
| 4494 | struct quic_arng_node *le_ar = |
| 4495 | eb64_entry(le, struct quic_arng_node, first); |
| 4496 | |
| 4497 | /* Already existing range */ |
| 4498 | if (le_ar->last >= ar->last) { |
| 4499 | ret = 1; |
| 4500 | } |
| 4501 | else if (le_ar->last + 1 >= ar->first) { |
| 4502 | le_ar->last = ar->last; |
| 4503 | new = le; |
| 4504 | new_node = le_ar; |
| 4505 | } |
| 4506 | else { |
| 4507 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 4508 | if (!new_node) |
| 4509 | goto leave; |
| 4510 | |
| 4511 | new = &new_node->first; |
| 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | /* Verify that the new inserted node does not overlap the nodes |
| 4516 | * which follow it. |
| 4517 | */ |
| 4518 | if (new) { |
| 4519 | struct eb64_node *next; |
| 4520 | struct quic_arng_node *next_node; |
| 4521 | |
| 4522 | while ((next = eb64_next(new))) { |
| 4523 | next_node = |
| 4524 | eb64_entry(next, struct quic_arng_node, first); |
| 4525 | if (new_node->last + 1 < next_node->first.key) |
| 4526 | break; |
| 4527 | |
| 4528 | if (next_node->last > new_node->last) |
| 4529 | new_node->last = next_node->last; |
| 4530 | eb64_delete(next); |
| 4531 | pool_free(pool_head_quic_arng, next_node); |
| 4532 | /* Decrement the size of these ranges. */ |
| 4533 | arngs->sz--; |
| 4534 | } |
| 4535 | } |
| 4536 | |
| 4537 | ret = 1; |
| 4538 | leave: |
| 4539 | quic_arngs_set_enc_sz(qc, arngs); |
| 4540 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 4541 | return ret; |
| 4542 | } |
Frédéric Lécaille | ece86e6 | 2023-03-07 11:53:43 +0100 | [diff] [blame] | 4543 | |
| 4544 | /* Detect the value of the spin bit to be used. */ |
| 4545 | static inline void qc_handle_spin_bit(struct quic_conn *qc, struct quic_rx_packet *pkt, |
| 4546 | struct quic_enc_level *qel) |
| 4547 | { |
| 4548 | uint64_t largest_pn = qel->pktns->rx.largest_pn; |
| 4549 | |
| 4550 | if (qel != &qc->els[QUIC_TLS_ENC_LEVEL_APP] || largest_pn == -1 || |
| 4551 | pkt->pn <= largest_pn) |
| 4552 | return; |
| 4553 | |
| 4554 | if (qc_is_listener(qc)) { |
| 4555 | if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT) |
| 4556 | qc->flags |= QUIC_FL_CONN_SPIN_BIT; |
| 4557 | else |
| 4558 | qc->flags &= ~QUIC_FL_CONN_SPIN_BIT; |
| 4559 | } |
| 4560 | else { |
| 4561 | if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT) |
| 4562 | qc->flags &= ~QUIC_FL_CONN_SPIN_BIT; |
| 4563 | else |
| 4564 | qc->flags |= QUIC_FL_CONN_SPIN_BIT; |
| 4565 | } |
| 4566 | } |
| 4567 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4568 | /* Remove the header protection of packets at <el> encryption level. |
| 4569 | * Always succeeds. |
| 4570 | */ |
| 4571 | static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el) |
| 4572 | { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4573 | struct quic_rx_packet *pqpkt, *pkttmp; |
| 4574 | struct quic_enc_level *app_qel; |
| 4575 | |
| 4576 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 4577 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4578 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
| 4579 | if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 4580 | TRACE_PROTO("RX hp not removed (handshake not completed)", |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4581 | QUIC_EV_CONN_ELRMHP, qc); |
| 4582 | goto out; |
| 4583 | } |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 4584 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4585 | list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) { |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 4586 | struct quic_tls_ctx *tls_ctx; |
| 4587 | |
| 4588 | tls_ctx = qc_select_tls_ctx(qc, el, pqpkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4589 | if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
| 4590 | pqpkt->data + pqpkt->pn_offset, pqpkt->data)) { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 4591 | TRACE_ERROR("RX hp removing error", QUIC_EV_CONN_ELRMHP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4592 | } |
| 4593 | else { |
Frédéric Lécaille | ece86e6 | 2023-03-07 11:53:43 +0100 | [diff] [blame] | 4594 | qc_handle_spin_bit(qc, pqpkt, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4595 | /* The AAD includes the packet number field */ |
| 4596 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 4597 | /* Store the packet into the tree of packets to decrypt. */ |
| 4598 | pqpkt->pn_node.key = pqpkt->pn; |
| 4599 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 4600 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 4601 | TRACE_PROTO("RX hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4602 | } |
Amaury Denoyelle | 9042465 | 2024-07-29 10:42:50 +0200 | [diff] [blame] | 4603 | LIST_DEL_INIT(&pqpkt->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4604 | quic_rx_packet_refdec(pqpkt); |
| 4605 | } |
| 4606 | |
| 4607 | out: |
| 4608 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
| 4609 | } |
| 4610 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4611 | /* Process all the CRYPTO frame at <el> encryption level. This is the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4612 | * 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] | 4613 | * stream for this level. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4614 | * Return 1 if succeeded, 0 if not. |
| 4615 | */ |
| 4616 | static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc, |
| 4617 | struct quic_enc_level *el, |
| 4618 | struct ssl_sock_ctx *ctx) |
| 4619 | { |
| 4620 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4621 | struct ncbuf *ncbuf; |
| 4622 | struct quic_cstream *cstream = el->cstream; |
| 4623 | ncb_sz_t data; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4624 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 4625 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4626 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4627 | BUG_ON(!cstream); |
| 4628 | ncbuf = &cstream->rx.ncbuf; |
| 4629 | if (ncb_is_null(ncbuf)) |
| 4630 | goto done; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4631 | |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4632 | /* TODO not working if buffer is wrapping */ |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4633 | while ((data = ncb_data(ncbuf, 0))) { |
| 4634 | const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4635 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4636 | if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4637 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4638 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4639 | cstream->rx.offset += data; |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4640 | TRACE_DEVEL("buffered crypto data were provided to TLS stack", |
| 4641 | QUIC_EV_CONN_PHPKTS, qc, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4642 | } |
| 4643 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4644 | done: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4645 | ret = 1; |
| 4646 | leave: |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4647 | if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) { |
| 4648 | 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] | 4649 | quic_free_ncbuf(ncbuf); |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4650 | } |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4651 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4652 | return ret; |
| 4653 | } |
| 4654 | |
| 4655 | /* Process all the packets at <el> and <next_el> encryption level. |
| 4656 | * This is the caller responsibility to check that <cur_el> is different of <next_el> |
| 4657 | * as pointer value. |
| 4658 | * Return 1 if succeeded, 0 if not. |
| 4659 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4660 | int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el, |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 4661 | struct quic_enc_level *next_el) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4662 | { |
| 4663 | int ret = 0; |
| 4664 | struct eb64_node *node; |
| 4665 | int64_t largest_pn = -1; |
| 4666 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4667 | struct quic_enc_level *qel = cur_el; |
| 4668 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4669 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4670 | qel = cur_el; |
| 4671 | next_tel: |
| 4672 | if (!qel) |
| 4673 | goto out; |
| 4674 | |
| 4675 | node = eb64_first(&qel->rx.pkts); |
| 4676 | while (node) { |
| 4677 | struct quic_rx_packet *pkt; |
| 4678 | |
| 4679 | pkt = eb64_entry(node, struct quic_rx_packet, pn_node); |
| 4680 | TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT, |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4681 | qc, pkt, NULL, qc->xprt_ctx->ssl); |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 4682 | if (!qc_pkt_decrypt(qc, qel, pkt)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4683 | /* Drop the packet */ |
| 4684 | TRACE_ERROR("packet decryption failed -> dropped", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4685 | QUIC_EV_CONN_RXPKT, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4686 | } |
| 4687 | else { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4688 | if (!qc_parse_pkt_frms(qc, pkt, qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4689 | /* Drop the packet */ |
| 4690 | TRACE_ERROR("packet parsing failed -> dropped", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4691 | QUIC_EV_CONN_RXPKT, qc, pkt); |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 4692 | qc->cntrs.dropped_parsing++; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4693 | } |
| 4694 | else { |
| 4695 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 4696 | |
Frédéric Lécaille | ddb9b1b | 2023-11-07 18:27:50 +0100 | [diff] [blame] | 4697 | /* Update the list of ranges to acknowledge. */ |
| 4698 | if (quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar)) { |
| 4699 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) { |
| 4700 | int arm_ack_timer = |
| 4701 | qc->state >= QUIC_HS_ST_COMPLETE && |
| 4702 | qel->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]; |
Frédéric Lécaille | b5efe79 | 2023-04-14 09:56:17 +0200 | [diff] [blame] | 4703 | |
Frédéric Lécaille | ddb9b1b | 2023-11-07 18:27:50 +0100 | [diff] [blame] | 4704 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 4705 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
| 4706 | qc_idle_timer_rearm(qc, 1, arm_ack_timer); |
| 4707 | } |
| 4708 | |
| 4709 | if (pkt->pn > largest_pn) { |
| 4710 | largest_pn = pkt->pn; |
| 4711 | largest_pn_time_received = pkt->time_received; |
| 4712 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4713 | } |
Frédéric Lécaille | ddb9b1b | 2023-11-07 18:27:50 +0100 | [diff] [blame] | 4714 | else { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4715 | TRACE_ERROR("Could not update ack range list", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4716 | QUIC_EV_CONN_RXPKT, qc); |
Frédéric Lécaille | ddb9b1b | 2023-11-07 18:27:50 +0100 | [diff] [blame] | 4717 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4718 | } |
| 4719 | } |
| 4720 | node = eb64_next(node); |
| 4721 | eb64_delete(&pkt->pn_node); |
| 4722 | quic_rx_packet_refdec(pkt); |
| 4723 | } |
| 4724 | |
| 4725 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 4726 | /* Update the largest packet number. */ |
| 4727 | qel->pktns->rx.largest_pn = largest_pn; |
| 4728 | /* Update the largest acknowledged packet timestamps */ |
| 4729 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 4730 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 4731 | } |
| 4732 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4733 | 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] | 4734 | // trace already emitted by function above |
| 4735 | goto leave; |
| 4736 | } |
| 4737 | |
| 4738 | if (qel == cur_el) { |
| 4739 | BUG_ON(qel == next_el); |
| 4740 | qel = next_el; |
| 4741 | largest_pn = -1; |
| 4742 | goto next_tel; |
| 4743 | } |
| 4744 | |
| 4745 | out: |
| 4746 | ret = 1; |
| 4747 | leave: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4748 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4749 | return ret; |
| 4750 | } |
| 4751 | |
| 4752 | /* Check if it's possible to remove header protection for packets related to |
| 4753 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 4754 | * |
| 4755 | * Return true if the operation is possible else false. |
| 4756 | */ |
| 4757 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4758 | { |
| 4759 | int ret = 0; |
| 4760 | enum quic_tls_enc_level tel; |
| 4761 | |
| 4762 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
| 4763 | |
| 4764 | if (!qel) |
| 4765 | goto cant_rm_hp; |
| 4766 | |
| 4767 | tel = ssl_to_quic_enc_level(qel->level); |
| 4768 | |
| 4769 | /* check if tls secrets are available */ |
| 4770 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 4771 | TRACE_PROTO("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4772 | goto cant_rm_hp; |
| 4773 | } |
| 4774 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4775 | if (!quic_tls_has_rx_sec(qel)) { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 4776 | TRACE_PROTO("non available secrets", QUIC_EV_CONN_TRMHP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4777 | goto cant_rm_hp; |
| 4778 | } |
| 4779 | |
Frédéric Lécaille | 8417beb | 2023-02-01 10:31:35 +0100 | [diff] [blame] | 4780 | if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 4781 | TRACE_PROTO("handshake not complete", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 8417beb | 2023-02-01 10:31:35 +0100 | [diff] [blame] | 4782 | goto cant_rm_hp; |
| 4783 | } |
| 4784 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4785 | /* check if the connection layer is ready before using app level */ |
| 4786 | if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) && |
| 4787 | qc->mux_state == QC_MUX_NULL) { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 4788 | TRACE_PROTO("connection layer not ready", QUIC_EV_CONN_TRMHP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4789 | goto cant_rm_hp; |
| 4790 | } |
| 4791 | |
| 4792 | ret = 1; |
| 4793 | cant_rm_hp: |
| 4794 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc); |
| 4795 | return ret; |
| 4796 | } |
| 4797 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 4798 | /* Flush txbuf for <qc> connection. This must be called prior to a packet |
| 4799 | * preparation when txbuf contains older data. A send will be conducted for |
| 4800 | * these data. |
| 4801 | * |
| 4802 | * Returns 1 on success : buffer is empty and can be use for packet |
| 4803 | * preparation. On error 0 is returned. |
| 4804 | */ |
| 4805 | static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf) |
| 4806 | { |
| 4807 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4808 | |
| 4809 | /* This operation can only be conducted if txbuf is not empty. This |
| 4810 | * case only happens for connection with their owned socket due to an |
| 4811 | * older transient sendto() error. |
| 4812 | */ |
| 4813 | BUG_ON(!qc_test_fd(qc)); |
| 4814 | |
| 4815 | if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) { |
| 4816 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 4817 | qc_txb_release(qc); |
| 4818 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc); |
| 4819 | return 0; |
| 4820 | } |
| 4821 | |
| 4822 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4823 | return 1; |
| 4824 | } |
| 4825 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4826 | /* Try to send application frames from list <frms> on connection <qc>. |
| 4827 | * |
| 4828 | * Use qc_send_app_probing wrapper when probing with old data. |
| 4829 | * |
| 4830 | * Returns 1 on success. Some data might not have been sent due to congestion, |
| 4831 | * in this case they are left in <frms> input list. The caller may subscribe on |
| 4832 | * quic-conn to retry later. |
| 4833 | * |
| 4834 | * Returns 0 on critical error. |
| 4835 | * TODO review and classify more distinctly transient from definitive errors to |
| 4836 | * allow callers to properly handle it. |
| 4837 | */ |
| 4838 | static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms) |
| 4839 | { |
Amaury Denoyelle | 7385ff3 | 2023-04-19 15:56:30 +0200 | [diff] [blame] | 4840 | int status = 0, ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4841 | struct buffer *buf; |
| 4842 | |
| 4843 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4844 | |
| 4845 | buf = qc_txb_alloc(qc); |
| 4846 | if (!buf) { |
| 4847 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4848 | goto err; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4849 | } |
| 4850 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 4851 | if (b_data(buf) && !qc_purge_txbuf(qc, buf)) |
| 4852 | goto err; |
| 4853 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4854 | /* Prepare and send packets until we could not further prepare packets. */ |
Amaury Denoyelle | 7385ff3 | 2023-04-19 15:56:30 +0200 | [diff] [blame] | 4855 | do { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4856 | /* Currently buf cannot be non-empty at this stage. Even if a |
| 4857 | * previous sendto() has failed it is emptied to simulate |
| 4858 | * packet emission and rely on QUIC lost detection to try to |
| 4859 | * emit it. |
| 4860 | */ |
| 4861 | BUG_ON_HOT(b_data(buf)); |
| 4862 | b_reset(buf); |
| 4863 | |
| 4864 | ret = qc_prep_app_pkts(qc, buf, frms); |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4865 | |
Amaury Denoyelle | 7385ff3 | 2023-04-19 15:56:30 +0200 | [diff] [blame] | 4866 | if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 4867 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 4868 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4869 | goto err; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4870 | } |
Amaury Denoyelle | 7385ff3 | 2023-04-19 15:56:30 +0200 | [diff] [blame] | 4871 | } while (ret > 0); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4872 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4873 | qc_txb_release(qc); |
Amaury Denoyelle | 7385ff3 | 2023-04-19 15:56:30 +0200 | [diff] [blame] | 4874 | if (ret < 0) |
| 4875 | goto err; |
| 4876 | |
| 4877 | status = 1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4878 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4879 | return status; |
| 4880 | |
| 4881 | err: |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4882 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc); |
| 4883 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | /* Try to send application frames from list <frms> on connection <qc>. Use this |
| 4887 | * function when probing is required. |
| 4888 | * |
| 4889 | * Returns the result from qc_send_app_pkts function. |
| 4890 | */ |
| 4891 | static forceinline int qc_send_app_probing(struct quic_conn *qc, |
| 4892 | struct list *frms) |
| 4893 | { |
| 4894 | int ret; |
| 4895 | |
| 4896 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4897 | |
Frédéric Lécaille | e95e00e | 2023-04-24 10:59:33 +0200 | [diff] [blame] | 4898 | TRACE_PROTO("preparing old data (probing)", QUIC_EV_CONN_FRMLIST, qc, frms); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4899 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4900 | ret = qc_send_app_pkts(qc, frms); |
| 4901 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4902 | |
| 4903 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4904 | return ret; |
| 4905 | } |
| 4906 | |
| 4907 | /* Try to send application frames from list <frms> on connection <qc>. This |
| 4908 | * function is provided for MUX upper layer usage only. |
| 4909 | * |
| 4910 | * Returns the result from qc_send_app_pkts function. |
| 4911 | */ |
| 4912 | int qc_send_mux(struct quic_conn *qc, struct list *frms) |
| 4913 | { |
| 4914 | int ret; |
| 4915 | |
| 4916 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4917 | BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */ |
| 4918 | |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 4919 | if (qc->conn->flags & CO_FL_SOCK_WR_SH) { |
| 4920 | qc->conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH; |
| 4921 | TRACE_DEVEL("connection on error", QUIC_EV_CONN_TXPKT, qc); |
| 4922 | return 0; |
| 4923 | } |
| 4924 | |
Amaury Denoyelle | 1304d19 | 2023-04-11 16:46:03 +0200 | [diff] [blame] | 4925 | /* Try to send post handshake frames first unless on 0-RTT. */ |
| 4926 | if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) && |
| 4927 | qc->state >= QUIC_HS_ST_COMPLETE) { |
| 4928 | struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4929 | quic_build_post_handshake_frames(qc); |
| 4930 | qc_send_app_pkts(qc, &qel->pktns->tx.frms); |
| 4931 | } |
| 4932 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4933 | TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc); |
| 4934 | qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4935 | ret = qc_send_app_pkts(qc, frms); |
| 4936 | qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4937 | |
| 4938 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4939 | return ret; |
| 4940 | } |
| 4941 | |
| 4942 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 4943 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
| 4944 | * QUIC connection. <old_data> is used as boolean to send data already sent but |
| 4945 | * not already acknowledged (in flight). |
| 4946 | * Returns 1 if succeeded, 0 if not. |
| 4947 | */ |
| 4948 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 4949 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 4950 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 4951 | { |
| 4952 | int ret, status = 0; |
| 4953 | struct buffer *buf = qc_txb_alloc(qc); |
| 4954 | |
| 4955 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4956 | |
| 4957 | if (!buf) { |
| 4958 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 4959 | goto leave; |
| 4960 | } |
| 4961 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 4962 | if (b_data(buf) && !qc_purge_txbuf(qc, buf)) |
| 4963 | goto out; |
| 4964 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4965 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4966 | * sendto() has failed it is emptied to simulate packet emission and |
| 4967 | * rely on QUIC lost detection to try to emit it. |
| 4968 | */ |
| 4969 | BUG_ON_HOT(b_data(buf)); |
| 4970 | b_reset(buf); |
| 4971 | |
| 4972 | if (old_data) { |
| 4973 | TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc); |
| 4974 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4975 | } |
| 4976 | |
| 4977 | ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms); |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4978 | if (ret == -1) { |
| 4979 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4980 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4981 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4982 | |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4983 | if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 4984 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 4985 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4986 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4987 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4988 | |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4989 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4990 | status = 1; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4991 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4992 | out: |
| 4993 | TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc); |
| 4994 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4995 | leave: |
| 4996 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4997 | return status; |
| 4998 | } |
| 4999 | |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 5000 | /* Retransmit up to two datagrams depending on packet number space. |
| 5001 | * Return 0 when failed, 0 if not. |
| 5002 | */ |
| 5003 | static int qc_dgrams_retransmit(struct quic_conn *qc) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5004 | { |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 5005 | int ret = 0; |
Frédéric Lécaille | fe97c6b | 2023-11-21 20:31:32 +0100 | [diff] [blame] | 5006 | int sret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5007 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 5008 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 5009 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 5010 | |
| 5011 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 5012 | |
| 5013 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 5014 | int i; |
| 5015 | |
| 5016 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 5017 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 5018 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5019 | |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 5020 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
| 5021 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 5022 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 5023 | if (!LIST_ISEMPTY(&ifrms)) { |
| 5024 | iqel->pktns->tx.pto_probe = 1; |
| 5025 | if (!LIST_ISEMPTY(&hfrms)) |
| 5026 | hqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | fe97c6b | 2023-11-21 20:31:32 +0100 | [diff] [blame] | 5027 | sret = qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 5028 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms); |
| 5029 | qc_free_frm_list(&ifrms); |
| 5030 | qc_free_frm_list(&hfrms); |
| 5031 | if (!sret) |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 5032 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5033 | } |
Frédéric Lécaille | ec93721 | 2023-03-03 17:34:41 +0100 | [diff] [blame] | 5034 | else { |
| 5035 | if (!(qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
| 5036 | iqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | fe97c6b | 2023-11-21 20:31:32 +0100 | [diff] [blame] | 5037 | sret = qc_send_hdshk_pkts(qc, 0, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 5038 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 5039 | qc_free_frm_list(&hfrms); |
| 5040 | if (!sret) |
Frédéric Lécaille | ec93721 | 2023-03-03 17:34:41 +0100 | [diff] [blame] | 5041 | goto leave; |
| 5042 | } |
| 5043 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5044 | } |
| 5045 | TRACE_STATE("no more need to probe Initial packet number space", |
| 5046 | QUIC_EV_CONN_TXPKT, qc); |
| 5047 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 5048 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5049 | } |
| 5050 | else { |
| 5051 | int i; |
| 5052 | |
| 5053 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5054 | hqel->pktns->tx.pto_probe = 0; |
| 5055 | 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] | 5056 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 5057 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5058 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
| 5059 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 5060 | if (!LIST_ISEMPTY(&frms1)) { |
| 5061 | hqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | fe97c6b | 2023-11-21 20:31:32 +0100 | [diff] [blame] | 5062 | sret = qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 5063 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 5064 | qc_free_frm_list(&frms1); |
| 5065 | if (!sret) |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 5066 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5067 | } |
| 5068 | } |
| 5069 | TRACE_STATE("no more need to probe Handshake packet number space", |
| 5070 | QUIC_EV_CONN_TXPKT, qc); |
| 5071 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 5072 | } |
| 5073 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 5074 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 5075 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 5076 | |
| 5077 | aqel->pktns->tx.pto_probe = 0; |
| 5078 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 5079 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 5080 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 5081 | if (!LIST_ISEMPTY(&frms1)) { |
| 5082 | aqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | fe97c6b | 2023-11-21 20:31:32 +0100 | [diff] [blame] | 5083 | sret = qc_send_app_probing(qc, &frms1); |
| 5084 | qc_free_frm_list(&frms1); |
| 5085 | if (!sret) { |
Frédéric Lécaille | e66d67a | 2023-04-24 12:05:46 +0200 | [diff] [blame] | 5086 | qc_free_frm_list(&frms2); |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 5087 | goto leave; |
Frédéric Lécaille | c6bec2a | 2023-04-24 11:20:32 +0200 | [diff] [blame] | 5088 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5089 | } |
| 5090 | if (!LIST_ISEMPTY(&frms2)) { |
| 5091 | aqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | fe97c6b | 2023-11-21 20:31:32 +0100 | [diff] [blame] | 5092 | sret = qc_send_app_probing(qc, &frms2); |
| 5093 | qc_free_frm_list(&frms2); |
| 5094 | if (!sret) |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 5095 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5096 | } |
| 5097 | TRACE_STATE("no more need to probe 01RTT packet number space", |
| 5098 | QUIC_EV_CONN_TXPKT, qc); |
| 5099 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 5100 | } |
| 5101 | } |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 5102 | |
| 5103 | ret = 1; |
| 5104 | leave: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5105 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 5106 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5107 | } |
| 5108 | |
| 5109 | /* QUIC connection packet handler task (post handshake) */ |
| 5110 | struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 5111 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5112 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5113 | struct quic_enc_level *qel; |
| 5114 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5115 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 5116 | |
| 5117 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5118 | TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state); |
| 5119 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 5120 | if (qc_test_fd(qc)) |
| 5121 | qc_rcv_buf(qc); |
| 5122 | |
Amaury Denoyelle | 1304d19 | 2023-04-11 16:46:03 +0200 | [diff] [blame] | 5123 | /* Prepare post-handshake frames |
| 5124 | * - after connection is instantiated (accept is done) |
| 5125 | * - handshake state is completed (may not be the case here in 0-RTT) |
| 5126 | */ |
| 5127 | if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) && qc->conn && |
| 5128 | qc->state >= QUIC_HS_ST_COMPLETE) { |
| 5129 | quic_build_post_handshake_frames(qc); |
| 5130 | } |
| 5131 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5132 | /* Retranmissions */ |
| 5133 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 5134 | TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
| 5135 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 5136 | if (!qc_dgrams_retransmit(qc)) |
| 5137 | goto out; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5138 | } |
| 5139 | |
| 5140 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 5141 | qc_rm_hp_pkts(qc, qel); |
| 5142 | |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 5143 | if (!qc_treat_rx_pkts(qc, qel, NULL)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5144 | TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 5145 | goto out; |
| 5146 | } |
| 5147 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 5148 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 5149 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc); |
| 5150 | goto out; |
| 5151 | } |
| 5152 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5153 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 5154 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) { |
| 5155 | TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc); |
| 5156 | goto out; |
| 5157 | } |
| 5158 | |
| 5159 | /* XXX TODO: how to limit the list frames to send */ |
| 5160 | if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) { |
| 5161 | TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 5162 | goto out; |
| 5163 | } |
| 5164 | |
| 5165 | out: |
| 5166 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 5167 | return t; |
| 5168 | } |
| 5169 | |
| 5170 | /* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */ |
| 5171 | static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel) |
| 5172 | { |
| 5173 | return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) || |
| 5174 | (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) || |
| 5175 | qel->pktns->tx.pto_probe || |
| 5176 | !LIST_ISEMPTY(&qel->pktns->tx.frms); |
| 5177 | } |
| 5178 | |
| 5179 | /* QUIC connection packet handler task. */ |
| 5180 | struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state) |
| 5181 | { |
| 5182 | int ret, ssl_err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5183 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5184 | enum quic_tls_enc_level tel, next_tel; |
| 5185 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 5186 | /* Early-data encryption level */ |
| 5187 | struct quic_enc_level *eqel; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5188 | struct buffer *buf = NULL; |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 5189 | int st, zero_rtt; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5190 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5191 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 5192 | |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 5193 | eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5194 | st = qc->state; |
| 5195 | TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st); |
| 5196 | |
| 5197 | /* Retranmissions */ |
| 5198 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 5199 | TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
| 5200 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 5201 | if (!qc_dgrams_retransmit(qc)) |
| 5202 | goto out; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5203 | } |
| 5204 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5205 | ssl_err = SSL_ERROR_NONE; |
| 5206 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 5207 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 5208 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 5209 | |
| 5210 | if (qc_test_fd(qc)) |
| 5211 | qc_rcv_buf(qc); |
| 5212 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5213 | if (st >= QUIC_HS_ST_COMPLETE && |
| 5214 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 5215 | TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 5216 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 5217 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 5218 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 5219 | } |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 5220 | 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] | 5221 | goto out; |
| 5222 | |
| 5223 | qel = &qc->els[tel]; |
| 5224 | next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel]; |
| 5225 | |
| 5226 | next_level: |
| 5227 | /* Treat packets waiting for header packet protection decryption */ |
| 5228 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 5229 | qc_rm_hp_pkts(qc, qel); |
| 5230 | |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 5231 | if (!qc_treat_rx_pkts(qc, qel, next_qel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5232 | goto out; |
| 5233 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 5234 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 5235 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc); |
| 5236 | goto out; |
| 5237 | } |
| 5238 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5239 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 5240 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 5241 | goto out; |
| 5242 | |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 5243 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 5244 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 5245 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
| 5246 | if (next_qel && next_qel == eqel && zero_rtt) { |
| 5247 | TRACE_DEVEL("select 0RTT as next encryption level", |
| 5248 | QUIC_EV_CONN_PHPKTS, qc); |
| 5249 | qel = next_qel; |
| 5250 | next_qel = NULL; |
| 5251 | goto next_level; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5252 | } |
| 5253 | |
| 5254 | st = qc->state; |
| 5255 | if (st >= QUIC_HS_ST_COMPLETE) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5256 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags & |
| 5257 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 5258 | /* Discard the Handshake keys. */ |
| 5259 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 5260 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 5261 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 5262 | qc_set_timer(qc); |
| 5263 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 5264 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
| 5265 | } |
| 5266 | |
| 5267 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 5268 | /* There may be remaining handshake to build (acks) */ |
| 5269 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 5270 | } |
Amaury Denoyelle | 9042465 | 2024-07-29 10:42:50 +0200 | [diff] [blame] | 5271 | |
| 5272 | /* Release 0RTT packets still waiting for HP removal. These |
| 5273 | * packets are considered unneeded after handshake completion. |
| 5274 | * They will be freed later from Rx buf via quic_rx_pkts_del(). |
| 5275 | */ |
| 5276 | if (!LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts)) { |
| 5277 | struct quic_rx_packet *pqpkt, *pkttmp; |
| 5278 | list_for_each_entry_safe(pqpkt, pkttmp, &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts, list) { |
| 5279 | LIST_DEL_INIT(&pqpkt->list); |
| 5280 | quic_rx_packet_refdec(pqpkt); |
| 5281 | } |
| 5282 | |
| 5283 | /* RFC 9001 4.9.3. Discarding 0-RTT Keys |
| 5284 | * Additionally, a server MAY discard 0-RTT keys as soon as it receives |
| 5285 | * a 1-RTT packet. However, due to packet reordering, a 0-RTT packet |
| 5286 | * could arrive after a 1-RTT packet. Servers MAY temporarily retain 0- |
| 5287 | * RTT keys to allow decrypting reordered packets without requiring |
| 5288 | * their contents to be retransmitted with 1-RTT keys. After receiving a |
| 5289 | * 1-RTT packet, servers MUST discard 0-RTT keys within a short time; |
| 5290 | * the RECOMMENDED time period is three times the Probe Timeout (PTO, |
| 5291 | * see [QUIC-RECOVERY]). A server MAY discard 0-RTT keys earlier if it |
| 5292 | * determines that it has received all 0-RTT packets, which can be done |
| 5293 | * by keeping track of missing packet numbers. |
| 5294 | * |
| 5295 | * TODO implement discarding of 0-RTT keys |
| 5296 | */ |
| 5297 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 5301 | * be considered. |
| 5302 | */ |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 5303 | if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5304 | goto out; |
| 5305 | |
| 5306 | if (!qc_need_sending(qc, qel) && |
| 5307 | (!next_qel || !qc_need_sending(qc, next_qel))) { |
| 5308 | goto skip_send; |
| 5309 | } |
| 5310 | |
| 5311 | buf = qc_txb_alloc(qc); |
| 5312 | if (!buf) |
| 5313 | goto out; |
| 5314 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 5315 | if (b_data(buf) && !qc_purge_txbuf(qc, buf)) |
| 5316 | goto skip_send; |
| 5317 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5318 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 5319 | * sendto() has failed it is emptied to simulate packet emission and |
| 5320 | * rely on QUIC lost detection to try to emit it. |
| 5321 | */ |
| 5322 | BUG_ON_HOT(b_data(buf)); |
| 5323 | b_reset(buf); |
| 5324 | |
| 5325 | ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms, |
| 5326 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 5327 | if (ret == -1) { |
| 5328 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5329 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 5330 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5331 | |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 5332 | if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 5333 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 5334 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5335 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5339 | |
| 5340 | skip_send: |
| 5341 | /* Check if there is something to do for the next level. |
| 5342 | */ |
| 5343 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 5344 | quic_tls_has_rx_sec(next_qel) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5345 | (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) { |
| 5346 | qel = next_qel; |
| 5347 | next_qel = NULL; |
| 5348 | goto next_level; |
| 5349 | } |
| 5350 | |
| 5351 | out: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 5352 | TRACE_PROTO("ssl error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err); |
| 5353 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5354 | return t; |
| 5355 | } |
| 5356 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 5357 | /* Release the memory allocated for <cs> CRYPTO stream */ |
| 5358 | void quic_cstream_free(struct quic_cstream *cs) |
| 5359 | { |
| 5360 | if (!cs) { |
| 5361 | /* This is the case for ORTT encryption level */ |
| 5362 | return; |
| 5363 | } |
| 5364 | |
Amaury Denoyelle | bc174b2 | 2022-11-17 10:12:52 +0100 | [diff] [blame] | 5365 | quic_free_ncbuf(&cs->rx.ncbuf); |
| 5366 | |
Amaury Denoyelle | bbb1820 | 2024-01-26 14:41:04 +0100 | [diff] [blame] | 5367 | qc_stream_desc_release(cs->desc, 0); |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 5368 | pool_free(pool_head_quic_cstream, cs); |
| 5369 | } |
| 5370 | |
| 5371 | /* Allocate a new QUIC stream for <qc>. |
| 5372 | * Return it if succeeded, NULL if not. |
| 5373 | */ |
| 5374 | struct quic_cstream *quic_cstream_new(struct quic_conn *qc) |
| 5375 | { |
| 5376 | struct quic_cstream *cs, *ret_cs = NULL; |
| 5377 | |
| 5378 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 5379 | cs = pool_alloc(pool_head_quic_cstream); |
| 5380 | if (!cs) { |
| 5381 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 5382 | goto leave; |
| 5383 | } |
| 5384 | |
| 5385 | cs->rx.offset = 0; |
| 5386 | cs->rx.ncbuf = NCBUF_NULL; |
| 5387 | cs->rx.offset = 0; |
| 5388 | |
| 5389 | cs->tx.offset = 0; |
| 5390 | cs->tx.sent_offset = 0; |
| 5391 | cs->tx.buf = BUF_NULL; |
| 5392 | cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc); |
| 5393 | if (!cs->desc) { |
| 5394 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 5395 | goto err; |
| 5396 | } |
| 5397 | |
| 5398 | ret_cs = cs; |
| 5399 | leave: |
| 5400 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 5401 | return ret_cs; |
| 5402 | |
| 5403 | err: |
| 5404 | pool_free(pool_head_quic_cstream, cs); |
| 5405 | goto leave; |
| 5406 | } |
| 5407 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5408 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
| 5409 | static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel) |
| 5410 | { |
| 5411 | int i; |
| 5412 | |
| 5413 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 5414 | |
| 5415 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 5416 | if (qel->tx.crypto.bufs[i]) { |
| 5417 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 5418 | qel->tx.crypto.bufs[i] = NULL; |
| 5419 | } |
| 5420 | } |
| 5421 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 5422 | quic_cstream_free(qel->cstream); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5423 | |
| 5424 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 5425 | } |
| 5426 | |
| 5427 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
| 5428 | * connection allocating everything needed. |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5429 | * |
| 5430 | * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use |
| 5431 | * quic_conn_enc_level_uninit() to cleanup partially allocated content. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5432 | */ |
| 5433 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 5434 | enum quic_tls_enc_level level) |
| 5435 | { |
| 5436 | int ret = 0; |
| 5437 | struct quic_enc_level *qel; |
| 5438 | |
Willy Tarreau | a4fc3a5 | 2024-08-06 15:22:50 +0200 | [diff] [blame] | 5439 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5440 | |
| 5441 | qel = &qc->els[level]; |
| 5442 | qel->level = quic_to_ssl_enc_level(level); |
| 5443 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 5444 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 5445 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 5446 | qel->tls_ctx.flags = 0; |
| 5447 | |
| 5448 | qel->rx.pkts = EB_ROOT; |
| 5449 | LIST_INIT(&qel->rx.pqpkts); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5450 | |
| 5451 | /* Allocate only one buffer. */ |
| 5452 | /* TODO: use a pool */ |
| 5453 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 5454 | if (!qel->tx.crypto.bufs) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5455 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5456 | |
| 5457 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 5458 | if (!qel->tx.crypto.bufs[0]) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5459 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5460 | |
| 5461 | qel->tx.crypto.bufs[0]->sz = 0; |
| 5462 | qel->tx.crypto.nb_buf = 1; |
| 5463 | |
| 5464 | qel->tx.crypto.sz = 0; |
| 5465 | qel->tx.crypto.offset = 0; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 5466 | /* No CRYPTO data for early data TLS encryption level */ |
| 5467 | if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA) |
| 5468 | qel->cstream = NULL; |
| 5469 | else { |
| 5470 | qel->cstream = quic_cstream_new(qc); |
| 5471 | if (!qel->cstream) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5472 | goto leave; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 5473 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5474 | |
| 5475 | ret = 1; |
| 5476 | leave: |
Willy Tarreau | a4fc3a5 | 2024-08-06 15:22:50 +0200 | [diff] [blame] | 5477 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5478 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5479 | } |
| 5480 | |
Frédéric Lécaille | 7c6d8f8 | 2023-02-14 16:00:18 +0100 | [diff] [blame] | 5481 | /* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not. |
| 5482 | * This is not the case if the remote peer address is not validated and if |
| 5483 | * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes. |
| 5484 | */ |
| 5485 | static int qc_may_probe_ipktns(struct quic_conn *qc) |
| 5486 | { |
| 5487 | return quic_peer_validated_addr(qc) || |
| 5488 | (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN; |
| 5489 | } |
| 5490 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5491 | /* Callback called upon loss detection and PTO timer expirations. */ |
| 5492 | struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state) |
| 5493 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5494 | struct quic_conn *qc = ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5495 | struct quic_pktns *pktns; |
| 5496 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 5497 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc); |
| 5498 | TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5499 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | d21c628 | 2023-04-24 11:26:06 +0200 | [diff] [blame] | 5500 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5501 | task->expire = TICK_ETERNITY; |
| 5502 | pktns = quic_loss_pktns(qc); |
Frédéric Lécaille | d21c628 | 2023-04-24 11:26:06 +0200 | [diff] [blame] | 5503 | |
| 5504 | if (qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL)) { |
| 5505 | TRACE_PROTO("cancelled action (draining state)", QUIC_EV_CONN_PTIMER, qc); |
Frédéric Lécaille | d21c628 | 2023-04-24 11:26:06 +0200 | [diff] [blame] | 5506 | goto out; |
| 5507 | } |
| 5508 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5509 | if (tick_isset(pktns->tx.loss_time)) { |
| 5510 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 5511 | |
| 5512 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 5513 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5514 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 5515 | if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms)) |
| 5516 | qc_set_timer(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5517 | goto out; |
| 5518 | } |
| 5519 | |
| 5520 | if (qc->path->in_flight) { |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 5521 | pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL); |
Frédéric Lécaille | 6873731 | 2023-04-07 16:28:46 +0200 | [diff] [blame] | 5522 | if (!pktns->tx.in_flight) { |
| 5523 | TRACE_PROTO("No in flight packets to probe with", QUIC_EV_CONN_TXPKT, qc); |
| 5524 | goto out; |
| 5525 | } |
| 5526 | |
Amaury Denoyelle | 2a19b6e | 2023-03-20 18:29:36 +0100 | [diff] [blame] | 5527 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 5528 | if (qc_may_probe_ipktns(qc)) { |
| 5529 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 5530 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 5531 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 5532 | } |
| 5533 | else { |
| 5534 | TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 5535 | } |
| 5536 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) { |
| 5537 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 5538 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 5539 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 5540 | } |
Frédéric Lécaille | e25fce0 | 2023-03-20 17:23:19 +0100 | [diff] [blame] | 5541 | } |
Amaury Denoyelle | 2a19b6e | 2023-03-20 18:29:36 +0100 | [diff] [blame] | 5542 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) { |
| 5543 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 5544 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 5545 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 5546 | if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) { |
Frédéric Lécaille | 7c6d8f8 | 2023-02-14 16:00:18 +0100 | [diff] [blame] | 5547 | if (qc_may_probe_ipktns(qc)) { |
Amaury Denoyelle | 2a19b6e | 2023-03-20 18:29:36 +0100 | [diff] [blame] | 5548 | qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
Frédéric Lécaille | 7c6d8f8 | 2023-02-14 16:00:18 +0100 | [diff] [blame] | 5549 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 5550 | } |
| 5551 | else { |
| 5552 | TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 5553 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5554 | } |
Amaury Denoyelle | 2a19b6e | 2023-03-20 18:29:36 +0100 | [diff] [blame] | 5555 | } |
| 5556 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) { |
Amaury Denoyelle | 8afe4b8 | 2023-03-21 11:39:24 +0100 | [diff] [blame] | 5557 | pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS; |
Amaury Denoyelle | 2a19b6e | 2023-03-20 18:29:36 +0100 | [diff] [blame] | 5558 | /* Wake up upper layer if waiting to send new data. */ |
| 5559 | if (!qc_notify_send(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5560 | TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 7c6d8f8 | 2023-02-14 16:00:18 +0100 | [diff] [blame] | 5561 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 5562 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5563 | } |
| 5564 | } |
| 5565 | } |
| 5566 | else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) { |
| 5567 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 5568 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 5569 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 5570 | if (quic_tls_has_tx_sec(hel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5571 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 5572 | if (quic_tls_has_tx_sec(iel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5573 | iel->pktns->tx.pto_probe = 1; |
| 5574 | } |
| 5575 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5576 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5577 | qc->path->loss.pto_count++; |
| 5578 | |
| 5579 | out: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 5580 | TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, pktns); |
| 5581 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5582 | |
| 5583 | return task; |
| 5584 | } |
| 5585 | |
| 5586 | /* Parse the Retry token from buffer <token> with <end> a pointer to |
| 5587 | * one byte past the end of this buffer. This will extract the ODCID |
| 5588 | * which will be stored into <odcid> |
| 5589 | * |
| 5590 | * Returns 0 on success else non-zero. |
| 5591 | */ |
| 5592 | static int parse_retry_token(struct quic_conn *qc, |
| 5593 | const unsigned char *token, const unsigned char *end, |
| 5594 | struct quic_cid *odcid) |
| 5595 | { |
| 5596 | int ret = 0; |
| 5597 | uint64_t odcid_len; |
| 5598 | uint32_t timestamp; |
Emeric Brun | 7875f12 | 2023-07-11 16:13:19 +0200 | [diff] [blame] | 5599 | uint32_t now_sec = (uint32_t)date.tv_sec; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5600 | |
| 5601 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 5602 | |
| 5603 | if (!quic_dec_int(&odcid_len, &token, end)) { |
| 5604 | TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc); |
| 5605 | goto leave; |
| 5606 | } |
| 5607 | |
| 5608 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 5609 | * When an Initial packet is sent by a client that has not previously |
| 5610 | * received an Initial or Retry packet from the server, the client |
| 5611 | * populates the Destination Connection ID field with an unpredictable |
| 5612 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 5613 | */ |
| 5614 | if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) { |
| 5615 | TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 5616 | goto leave; |
| 5617 | } |
| 5618 | |
| 5619 | if (end - token < odcid_len + sizeof timestamp) { |
| 5620 | TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 5621 | goto leave; |
| 5622 | } |
| 5623 | |
| 5624 | timestamp = ntohl(read_u32(token + odcid_len)); |
Emeric Brun | 7875f12 | 2023-07-11 16:13:19 +0200 | [diff] [blame] | 5625 | /* check if elapsed time is +/- QUIC_RETRY_DURATION_SEC |
| 5626 | * to tolerate token generator is not perfectly time synced |
| 5627 | */ |
| 5628 | if ((uint32_t)(now_sec - timestamp) > QUIC_RETRY_DURATION_SEC && |
| 5629 | (uint32_t)(timestamp - now_sec) > QUIC_RETRY_DURATION_SEC) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5630 | TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc); |
| 5631 | goto leave; |
| 5632 | } |
| 5633 | |
| 5634 | ret = 1; |
| 5635 | memcpy(odcid->data, token, odcid_len); |
| 5636 | odcid->len = odcid_len; |
| 5637 | leave: |
| 5638 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 5639 | return !ret; |
| 5640 | } |
| 5641 | |
| 5642 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 5643 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 5644 | * for QUIC servers (or haproxy listeners). |
Frédéric Lécaille | 8a58658 | 2023-06-26 10:39:56 +0200 | [diff] [blame] | 5645 | * <dcid> is the destination connection ID, <scid> is the source connection ID. |
| 5646 | * This latter <scid> CID as the same value on the wire as the one for <conn_id> |
| 5647 | * which is the first CID of this connection but a different internal representation used to build |
| 5648 | * NEW_CONNECTION_ID frames. This is the responsability of the caller to insert |
| 5649 | * <conn_id> in the CIDs tree for this connection (qc->cids). |
| 5650 | * <token> is 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] | 5651 | * length. Endpoints addresses are specified via <local_addr> and <peer_addr>. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5652 | * Returns the connection if succeeded, NULL if not. |
| 5653 | */ |
| 5654 | static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4, |
| 5655 | struct quic_cid *dcid, struct quic_cid *scid, |
| 5656 | const struct quic_cid *token_odcid, |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 5657 | struct quic_connection_id *conn_id, |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 5658 | struct sockaddr_storage *local_addr, |
| 5659 | struct sockaddr_storage *peer_addr, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5660 | int server, int token, void *owner) |
| 5661 | { |
| 5662 | int i; |
Amaury Denoyelle | dc654e8 | 2023-11-06 17:45:14 +0100 | [diff] [blame] | 5663 | struct quic_conn *qc = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5664 | /* Initial CID. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5665 | char *buf_area = NULL; |
| 5666 | struct listener *l = NULL; |
| 5667 | struct quic_cc_algo *cc_algo = NULL; |
| 5668 | struct quic_tls_ctx *ictx; |
Amaury Denoyelle | 62b950b | 2023-11-06 17:47:17 +0100 | [diff] [blame] | 5669 | unsigned int next_actconn = 0, next_sslconn = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5670 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | dc654e8 | 2023-11-06 17:45:14 +0100 | [diff] [blame] | 5671 | |
| 5672 | next_actconn = increment_actconn(); |
| 5673 | if (!next_actconn) { |
| 5674 | _HA_ATOMIC_INC(&maxconn_reached); |
| 5675 | TRACE_STATE("maxconn reached", QUIC_EV_CONN_INIT); |
| 5676 | goto err; |
| 5677 | } |
| 5678 | |
Amaury Denoyelle | 62b950b | 2023-11-06 17:47:17 +0100 | [diff] [blame] | 5679 | next_sslconn = increment_sslconn(); |
| 5680 | if (!next_sslconn) { |
| 5681 | TRACE_STATE("sslconn reached", QUIC_EV_CONN_INIT); |
| 5682 | goto err; |
| 5683 | } |
| 5684 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5685 | /* TODO replace pool_zalloc by pool_alloc(). This requires special care |
| 5686 | * to properly initialized internal quic_conn members to safely use |
| 5687 | * quic_conn_release() on alloc failure. |
| 5688 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5689 | qc = pool_zalloc(pool_head_quic_conn); |
| 5690 | if (!qc) { |
| 5691 | TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 5692 | goto err; |
| 5693 | } |
| 5694 | |
Amaury Denoyelle | dc654e8 | 2023-11-06 17:45:14 +0100 | [diff] [blame] | 5695 | /* Now that quic_conn instance is allocated, quic_conn_release() will |
| 5696 | * ensure global accounting is decremented. |
| 5697 | */ |
Amaury Denoyelle | 62b950b | 2023-11-06 17:47:17 +0100 | [diff] [blame] | 5698 | next_sslconn = next_actconn = 0; |
Amaury Denoyelle | dc654e8 | 2023-11-06 17:45:14 +0100 | [diff] [blame] | 5699 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5700 | /* Initialize in priority qc members required for a safe dealloc. */ |
| 5701 | |
| 5702 | /* required to use MTLIST_IN_LIST */ |
| 5703 | MT_LIST_INIT(&qc->accept_list); |
| 5704 | |
| 5705 | LIST_INIT(&qc->rx.pkt_list); |
| 5706 | |
Amaury Denoyelle | 4244833 | 2022-12-12 11:24:05 +0100 | [diff] [blame] | 5707 | qc_init_fd(qc); |
| 5708 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5709 | LIST_INIT(&qc->back_refs); |
Amaury Denoyelle | d537ca7 | 2023-04-19 10:45:40 +0200 | [diff] [blame] | 5710 | LIST_INIT(&qc->el_th_ctx); |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5711 | |
Frédéric Lécaille | 1b0a5a0 | 2023-12-05 20:12:51 +0100 | [diff] [blame] | 5712 | /* Packet number spaces initialization. */ |
| 5713 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 5714 | quic_pktns_init(&qc->pktns[i]); |
| 5715 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5716 | /* Now proceeds to allocation of qc members. */ |
| 5717 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5718 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 5719 | if (!buf_area) { |
| 5720 | TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc); |
| 5721 | goto err; |
| 5722 | } |
| 5723 | |
| 5724 | qc->cids = EB_ROOT; |
| 5725 | /* QUIC Server (or listener). */ |
| 5726 | if (server) { |
| 5727 | struct proxy *prx; |
| 5728 | |
| 5729 | l = owner; |
| 5730 | prx = l->bind_conf->frontend; |
| 5731 | cc_algo = l->bind_conf->quic_cc_algo; |
| 5732 | |
| 5733 | qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 5734 | &quic_stats_module); |
| 5735 | qc->flags |= QUIC_FL_CONN_LISTENER; |
| 5736 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
Amaury Denoyelle | 15adc4c | 2023-04-05 09:50:17 +0200 | [diff] [blame] | 5737 | /* Copy the client original DCID. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5738 | qc->odcid.len = dcid->len; |
Amaury Denoyelle | 15adc4c | 2023-04-05 09:50:17 +0200 | [diff] [blame] | 5739 | memcpy(qc->odcid.data, dcid->data, dcid->len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5740 | |
| 5741 | /* copy the packet SCID to reuse it as DCID for sending */ |
| 5742 | if (scid->len) |
| 5743 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 5744 | qc->dcid.len = scid->len; |
| 5745 | qc->tx.buf = BUF_NULL; |
| 5746 | qc->li = l; |
| 5747 | } |
| 5748 | /* QUIC Client (outgoing connection to servers) */ |
| 5749 | else { |
| 5750 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
| 5751 | if (dcid->len) |
| 5752 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 5753 | qc->dcid.len = dcid->len; |
| 5754 | } |
| 5755 | qc->mux_state = QC_MUX_NULL; |
| 5756 | qc->err = quic_err_transport(QC_ERR_NO_ERROR); |
| 5757 | |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 5758 | conn_id->qc = qc; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5759 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5760 | if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) && |
| 5761 | is_addr(local_addr)) { |
| 5762 | TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc); |
| 5763 | qc_alloc_fd(qc, local_addr, peer_addr); |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 5764 | |
| 5765 | /* haproxy soft-stop is supported only for QUIC connections |
| 5766 | * with their owned socket. |
| 5767 | */ |
| 5768 | if (qc_test_fd(qc)) |
| 5769 | _HA_ATOMIC_INC(&jobs); |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5770 | } |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5771 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5772 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 5773 | qc->scid = conn_id->cid; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5774 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5775 | /* QUIC encryption level context initialization. */ |
| 5776 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 5777 | if (!quic_conn_enc_level_init(qc, i)) { |
| 5778 | TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc); |
| 5779 | goto err; |
| 5780 | } |
| 5781 | /* Initialize the packet number space. */ |
| 5782 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 5783 | } |
| 5784 | |
| 5785 | qc->original_version = qv; |
| 5786 | qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ? |
| 5787 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 5788 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
| 5789 | /* TX part. */ |
| 5790 | LIST_INIT(&qc->tx.frms_to_send); |
| 5791 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 5792 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 5793 | qc->tx.bytes = 0; |
| 5794 | qc->tx.buf = BUF_NULL; |
| 5795 | /* RX part. */ |
| 5796 | qc->rx.bytes = 0; |
| 5797 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 5798 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 5799 | qc->rx.strms[i].nb_streams = 0; |
| 5800 | |
| 5801 | qc->nb_pkt_for_cc = 1; |
| 5802 | qc->nb_pkt_since_cc = 0; |
| 5803 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5804 | if (!quic_tls_ku_init(qc)) { |
| 5805 | TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc); |
| 5806 | goto err; |
| 5807 | } |
| 5808 | |
| 5809 | /* XXX TO DO: Only one path at this time. */ |
| 5810 | qc->path = &qc->paths[0]; |
| 5811 | quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc); |
| 5812 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5813 | qc->streams_by_id = EB_ROOT_UNIQUE; |
| 5814 | qc->stream_buf_count = 0; |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 5815 | memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr)); |
| 5816 | memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5817 | |
| 5818 | if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params, |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 5819 | conn_id->stateless_reset_token, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5820 | dcid->data, dcid->len, |
| 5821 | qc->scid.data, qc->scid.len, token_odcid)) |
| 5822 | goto err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5823 | |
Frédéric Lécaille | deb9781 | 2023-03-22 11:29:45 +0100 | [diff] [blame] | 5824 | /* Initialize the idle timeout of the connection at the "max_idle_timeout" |
| 5825 | * value from local transport parameters. |
| 5826 | */ |
| 5827 | qc->max_idle_timeout = qc->rx.params.max_idle_timeout; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5828 | qc->wait_event.tasklet = tasklet_new(); |
| 5829 | if (!qc->wait_event.tasklet) { |
| 5830 | TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT); |
| 5831 | goto err; |
| 5832 | } |
| 5833 | qc->wait_event.tasklet->process = quic_conn_io_cb; |
| 5834 | qc->wait_event.tasklet->context = qc; |
| 5835 | qc->wait_event.events = 0; |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 5836 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5837 | |
| 5838 | if (qc_conn_alloc_ssl_ctx(qc) || |
| 5839 | !quic_conn_init_timer(qc) || |
| 5840 | !quic_conn_init_idle_timer_task(qc)) |
| 5841 | goto err; |
| 5842 | |
| 5843 | ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx; |
| 5844 | if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1)) |
| 5845 | goto err; |
| 5846 | |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 5847 | /* Counters initialization */ |
| 5848 | memset(&qc->cntrs, 0, sizeof qc->cntrs); |
| 5849 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5850 | LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx); |
| 5851 | qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch); |
| 5852 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5853 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
| 5854 | |
| 5855 | return qc; |
| 5856 | |
| 5857 | err: |
| 5858 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5859 | if (qc) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5860 | qc->rx.buf.area = NULL; |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5861 | quic_conn_release(qc); |
| 5862 | } |
Amaury Denoyelle | dc654e8 | 2023-11-06 17:45:14 +0100 | [diff] [blame] | 5863 | |
| 5864 | /* Decrement global counters. Done only for errors happening before or |
| 5865 | * on pool_head_quic_conn alloc. All other cases are covered by |
| 5866 | * quic_conn_release(). |
| 5867 | */ |
| 5868 | if (next_actconn) |
| 5869 | _HA_ATOMIC_DEC(&actconn); |
Amaury Denoyelle | 62b950b | 2023-11-06 17:47:17 +0100 | [diff] [blame] | 5870 | if (next_sslconn) |
| 5871 | _HA_ATOMIC_DEC(&global.sslconns); |
Amaury Denoyelle | dc654e8 | 2023-11-06 17:45:14 +0100 | [diff] [blame] | 5872 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5873 | TRACE_LEAVE(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5874 | return NULL; |
| 5875 | } |
| 5876 | |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 5877 | /* Update the proxy counters of <qc> QUIC connection from its counters */ |
| 5878 | static inline void quic_conn_prx_cntrs_update(struct quic_conn *qc) |
| 5879 | { |
Frédéric Lécaille | 557d30a | 2023-06-14 18:09:54 +0200 | [diff] [blame] | 5880 | if (!qc->prx_counters) |
| 5881 | return; |
| 5882 | |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 5883 | HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt, qc->cntrs.dropped_pkt); |
| 5884 | HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt_bufoverrun, qc->cntrs.dropped_pkt_bufoverrun); |
| 5885 | HA_ATOMIC_ADD(&qc->prx_counters->dropped_parsing, qc->cntrs.dropped_parsing); |
| 5886 | HA_ATOMIC_ADD(&qc->prx_counters->socket_full, qc->cntrs.socket_full); |
| 5887 | HA_ATOMIC_ADD(&qc->prx_counters->sendto_err, qc->cntrs.sendto_err); |
| 5888 | HA_ATOMIC_ADD(&qc->prx_counters->sendto_err_unknown, qc->cntrs.sendto_err_unknown); |
Frédéric Lécaille | 12a815a | 2023-05-24 15:55:14 +0200 | [diff] [blame] | 5889 | HA_ATOMIC_ADD(&qc->prx_counters->sent_pkt, qc->cntrs.sent_pkt); |
Frédéric Lécaille | a216e06 | 2023-07-03 10:40:32 +0200 | [diff] [blame] | 5890 | /* It is possible that ->path was not initialized. For instance if a |
| 5891 | * QUIC connection allocation has failed. |
| 5892 | */ |
| 5893 | if (qc->path) |
| 5894 | HA_ATOMIC_ADD(&qc->prx_counters->lost_pkt, qc->path->loss.nb_lost_pkt); |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 5895 | HA_ATOMIC_ADD(&qc->prx_counters->conn_migration_done, qc->cntrs.conn_migration_done); |
| 5896 | /* Stream related counters */ |
| 5897 | HA_ATOMIC_ADD(&qc->prx_counters->data_blocked, qc->cntrs.data_blocked); |
| 5898 | HA_ATOMIC_ADD(&qc->prx_counters->stream_data_blocked, qc->cntrs.stream_data_blocked); |
Amaury Denoyelle | 6d6ee0d | 2023-05-25 10:36:04 +0200 | [diff] [blame] | 5899 | HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_bidi, qc->cntrs.streams_blocked_bidi); |
| 5900 | HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_uni, qc->cntrs.streams_blocked_uni); |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 5901 | } |
| 5902 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5903 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 5904 | * The connection tasklet is killed. |
| 5905 | * |
| 5906 | * This function must only be called by the thread responsible of the quic_conn |
| 5907 | * tasklet. |
| 5908 | */ |
| 5909 | void quic_conn_release(struct quic_conn *qc) |
| 5910 | { |
| 5911 | int i; |
| 5912 | struct ssl_sock_ctx *conn_ctx; |
| 5913 | struct eb64_node *node; |
| 5914 | struct quic_tls_ctx *app_tls_ctx; |
| 5915 | struct quic_rx_packet *pkt, *pktback; |
| 5916 | |
| 5917 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 5918 | |
| 5919 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 5920 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 5921 | |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 5922 | if (qc_test_fd(qc)) |
| 5923 | _HA_ATOMIC_DEC(&jobs); |
| 5924 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5925 | /* Close quic-conn socket fd. */ |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 5926 | qc_release_fd(qc, 0); |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5927 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5928 | /* in the unlikely (but possible) case the connection was just added to |
| 5929 | * the accept_list we must delete it from there. |
| 5930 | */ |
| 5931 | MT_LIST_DELETE(&qc->accept_list); |
| 5932 | |
| 5933 | /* free remaining stream descriptors */ |
| 5934 | node = eb64_first(&qc->streams_by_id); |
| 5935 | while (node) { |
| 5936 | struct qc_stream_desc *stream; |
| 5937 | |
| 5938 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 5939 | node = eb64_next(node); |
| 5940 | |
| 5941 | /* all streams attached to the quic-conn are released, so |
| 5942 | * qc_stream_desc_free will liberate the stream instance. |
| 5943 | */ |
| 5944 | BUG_ON(!stream->release); |
| 5945 | qc_stream_desc_free(stream, 1); |
| 5946 | } |
| 5947 | |
| 5948 | /* Purge Rx packet list. */ |
| 5949 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 5950 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 5951 | pool_free(pool_head_quic_rx_packet, pkt); |
| 5952 | } |
| 5953 | |
| 5954 | if (qc->idle_timer_task) { |
| 5955 | task_destroy(qc->idle_timer_task); |
| 5956 | qc->idle_timer_task = NULL; |
| 5957 | } |
| 5958 | |
| 5959 | if (qc->timer_task) { |
| 5960 | task_destroy(qc->timer_task); |
| 5961 | qc->timer_task = NULL; |
| 5962 | } |
| 5963 | |
Tim Duesterhus | b1ec21d | 2023-04-22 17:47:32 +0200 | [diff] [blame] | 5964 | tasklet_free(qc->wait_event.tasklet); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5965 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5966 | /* remove the connection from receiver cids trees */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5967 | free_quic_conn_cids(qc); |
| 5968 | |
| 5969 | conn_ctx = qc->xprt_ctx; |
| 5970 | if (conn_ctx) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5971 | SSL_free(conn_ctx->ssl); |
| 5972 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
| 5973 | } |
| 5974 | |
| 5975 | quic_tls_ku_free(qc); |
| 5976 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 5977 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
| 5978 | quic_conn_enc_level_uninit(qc, &qc->els[i]); |
| 5979 | } |
| 5980 | quic_tls_ctx_secs_free(&qc->negotiated_ictx); |
| 5981 | |
| 5982 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 5983 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 5984 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
| 5985 | |
| 5986 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 5987 | quic_pktns_tx_pkts_release(&qc->pktns[i], qc); |
| 5988 | quic_free_arngs(qc, &qc->pktns[i].rx.arngs); |
Frédéric Lécaille | 73e2944 | 2023-09-13 09:28:10 +0200 | [diff] [blame] | 5989 | qc_release_pktns_frms(qc, &qc->pktns[i]); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5990 | } |
| 5991 | |
Amaury Denoyelle | efed86c | 2023-03-08 09:42:04 +0100 | [diff] [blame] | 5992 | qc_detach_th_ctx_list(qc, 0); |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5993 | |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 5994 | quic_conn_prx_cntrs_update(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5995 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 5996 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | eb3e517 | 2023-04-12 13:41:54 +0200 | [diff] [blame] | 5997 | qc = NULL; |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 5998 | |
Amaury Denoyelle | dc654e8 | 2023-11-06 17:45:14 +0100 | [diff] [blame] | 5999 | /* Decrement global counters when quic_conn is deallocated. |
| 6000 | * quic_cc_conn instances are not accounted as they run for a short |
| 6001 | * time with limited ressources. |
| 6002 | */ |
| 6003 | _HA_ATOMIC_DEC(&actconn); |
Amaury Denoyelle | 62b950b | 2023-11-06 17:47:17 +0100 | [diff] [blame] | 6004 | _HA_ATOMIC_DEC(&global.sslconns); |
Amaury Denoyelle | dc654e8 | 2023-11-06 17:45:14 +0100 | [diff] [blame] | 6005 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6006 | TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc); |
| 6007 | |
| 6008 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 6009 | } |
| 6010 | |
| 6011 | /* Initialize the timer task of <qc> QUIC connection. |
| 6012 | * Returns 1 if succeeded, 0 if not. |
| 6013 | */ |
| 6014 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 6015 | { |
| 6016 | int ret = 0; |
| 6017 | /* Attach this task to the same thread ID used for the connection */ |
| 6018 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 6019 | |
Amaury Denoyelle | 6694728 | 2023-04-13 11:48:38 +0200 | [diff] [blame] | 6020 | qc->timer_task = task_new_here(); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6021 | if (!qc->timer_task) { |
| 6022 | TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 6023 | goto leave; |
| 6024 | } |
| 6025 | |
| 6026 | qc->timer = TICK_ETERNITY; |
| 6027 | qc->timer_task->process = qc_process_timer; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 6028 | qc->timer_task->context = qc; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6029 | |
| 6030 | ret = 1; |
| 6031 | leave: |
| 6032 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 6033 | return ret; |
| 6034 | } |
| 6035 | |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6036 | /* Rearm the idle timer or the ack timer (if not already armde) for <qc> QUIC |
| 6037 | * connection. */ |
| 6038 | static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6039 | { |
| 6040 | unsigned int expire; |
| 6041 | |
Frédéric Lécaille | 11ffacf | 2023-11-06 14:16:10 +0100 | [diff] [blame] | 6042 | /* It is possible the idle timer task has been already released. */ |
| 6043 | if (!qc->idle_timer_task) |
| 6044 | return; |
| 6045 | |
Amaury Denoyelle | 77ed631 | 2023-02-01 09:28:55 +0100 | [diff] [blame] | 6046 | if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) { |
Frédéric Lécaille | 495968e | 2023-04-03 17:42:05 +0200 | [diff] [blame] | 6047 | TRACE_PROTO("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc); |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6048 | qc->ack_expire = TICK_ETERNITY; |
Amaury Denoyelle | 77ed631 | 2023-02-01 09:28:55 +0100 | [diff] [blame] | 6049 | task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG); |
| 6050 | } |
| 6051 | else { |
Amaury Denoyelle | 10197d6 | 2023-10-25 14:45:53 +0200 | [diff] [blame] | 6052 | if (qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) { |
| 6053 | /* RFC 9000 10.2. Immediate Close |
| 6054 | * |
| 6055 | * The closing and draining connection states exist to ensure that |
| 6056 | * connections close cleanly and that delayed or reordered packets are |
| 6057 | * properly discarded. These states SHOULD persist for at least three |
| 6058 | * times the current PTO interval as defined in [QUIC-RECOVERY]. |
| 6059 | */ |
| 6060 | |
| 6061 | /* Delay is limited to 1s which should cover most of |
| 6062 | * network conditions. The process should not be |
| 6063 | * impacted by a connection with a high RTT. |
| 6064 | */ |
| 6065 | expire = MIN(3 * quic_pto(qc), 1000); |
| 6066 | } |
| 6067 | else { |
| 6068 | /* RFC 9000 10.1. Idle Timeout |
| 6069 | * |
| 6070 | * To avoid excessively small idle timeout periods, endpoints MUST |
| 6071 | * increase the idle timeout period to be at least three times the |
| 6072 | * current Probe Timeout (PTO). This allows for multiple PTOs to expire, |
| 6073 | * and therefore multiple probes to be sent and lost, prior to idle |
| 6074 | * timeout. |
| 6075 | */ |
| 6076 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 6077 | } |
| 6078 | |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6079 | qc->idle_expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 6080 | if (arm_ack) { |
| 6081 | /* Arm the ack timer only if not already armed. */ |
| 6082 | if (!tick_isset(qc->ack_expire)) { |
| 6083 | qc->ack_expire = tick_add(now_ms, MS_TO_TICKS(QUIC_ACK_DELAY)); |
| 6084 | qc->idle_timer_task->expire = qc->ack_expire; |
| 6085 | task_queue(qc->idle_timer_task); |
Frédéric Lécaille | 495968e | 2023-04-03 17:42:05 +0200 | [diff] [blame] | 6086 | TRACE_PROTO("ack timer armed", QUIC_EV_CONN_IDLE_TIMER, qc); |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6087 | } |
| 6088 | } |
| 6089 | else { |
| 6090 | qc->idle_timer_task->expire = tick_first(qc->ack_expire, qc->idle_expire); |
| 6091 | task_queue(qc->idle_timer_task); |
Frédéric Lécaille | 495968e | 2023-04-03 17:42:05 +0200 | [diff] [blame] | 6092 | TRACE_PROTO("idle timer armed", QUIC_EV_CONN_IDLE_TIMER, qc); |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6093 | } |
Amaury Denoyelle | 77ed631 | 2023-02-01 09:28:55 +0100 | [diff] [blame] | 6094 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6095 | } |
| 6096 | |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6097 | /* Rearm the idle timer or ack timer for <qc> QUIC connection depending on <read> |
| 6098 | * and <arm_ack> booleans. The former is set to 1 when receiving a packet , |
| 6099 | * and 0 when sending packet. <arm_ack> is set to 1 if this is the ack timer |
| 6100 | * which must be rearmed. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6101 | */ |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6102 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6103 | { |
| 6104 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 6105 | |
| 6106 | if (read) { |
| 6107 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 6108 | } |
| 6109 | else { |
| 6110 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 6111 | } |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6112 | qc_idle_timer_do_rearm(qc, arm_ack); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6113 | |
| 6114 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 6115 | } |
| 6116 | |
| 6117 | /* The task handling the idle timeout */ |
| 6118 | struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 6119 | { |
| 6120 | struct quic_conn *qc = ctx; |
| 6121 | struct quic_counters *prx_counters = qc->prx_counters; |
| 6122 | unsigned int qc_flags = qc->flags; |
| 6123 | |
| 6124 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 6125 | |
Frédéric Lécaille | 12eca3a | 2023-04-04 10:46:54 +0200 | [diff] [blame] | 6126 | if ((state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER && !tick_is_expired(t->expire, now_ms)) |
| 6127 | goto requeue; |
| 6128 | |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6129 | if (tick_is_expired(qc->ack_expire, now_ms)) { |
Frédéric Lécaille | ce5c145 | 2023-04-05 09:44:21 +0200 | [diff] [blame] | 6130 | TRACE_PROTO("ack timer expired", QUIC_EV_CONN_IDLE_TIMER, qc); |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6131 | qc->ack_expire = TICK_ETERNITY; |
| 6132 | /* Note that ->idle_expire is always set. */ |
| 6133 | t->expire = qc->idle_expire; |
Frédéric Lécaille | b73762a | 2023-04-24 11:32:22 +0200 | [diff] [blame] | 6134 | /* Do not wakeup the I/O handler in DRAINING state or if the |
| 6135 | * connection must be killed as soon as possible. |
| 6136 | */ |
| 6137 | if (!(qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL))) { |
| 6138 | qc->flags |= QUIC_FL_CONN_ACK_TIMER_FIRED; |
| 6139 | tasklet_wakeup(qc->wait_event.tasklet); |
| 6140 | } |
| 6141 | |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6142 | goto requeue; |
| 6143 | } |
| 6144 | |
Frédéric Lécaille | 495968e | 2023-04-03 17:42:05 +0200 | [diff] [blame] | 6145 | TRACE_PROTO("idle timer task running", QUIC_EV_CONN_IDLE_TIMER, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6146 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 6147 | * might free the quic-conn too early via quic_close(). |
| 6148 | */ |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 6149 | qc_notify_err(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6150 | |
| 6151 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 6152 | * responsible to call quic_close to release it. |
| 6153 | */ |
| 6154 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
Frédéric Lécaille | ce5c145 | 2023-04-05 09:44:21 +0200 | [diff] [blame] | 6155 | if (qc->mux_state != QC_MUX_READY) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6156 | quic_conn_release(qc); |
Frédéric Lécaille | ce5c145 | 2023-04-05 09:44:21 +0200 | [diff] [blame] | 6157 | qc = NULL; |
Frédéric Lécaille | 11ffacf | 2023-11-06 14:16:10 +0100 | [diff] [blame] | 6158 | } |
| 6159 | else { |
| 6160 | task_destroy(t); |
| 6161 | qc->idle_timer_task = NULL; |
Frédéric Lécaille | ce5c145 | 2023-04-05 09:44:21 +0200 | [diff] [blame] | 6162 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6163 | |
Frédéric Lécaille | 11ffacf | 2023-11-06 14:16:10 +0100 | [diff] [blame] | 6164 | t = NULL; |
| 6165 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6166 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 6167 | * least clean some parts of it such as the tasklet. |
| 6168 | */ |
| 6169 | |
| 6170 | if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 6171 | qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
Frédéric Lécaille | ce5c145 | 2023-04-05 09:44:21 +0200 | [diff] [blame] | 6172 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IDLE_TIMER, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6173 | HA_ATOMIC_DEC(&prx_counters->half_open_conn); |
| 6174 | } |
| 6175 | |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6176 | requeue: |
| 6177 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 6178 | return t; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6179 | } |
| 6180 | |
| 6181 | /* Initialize the idle timeout task for <qc>. |
| 6182 | * Returns 1 if succeeded, 0 if not. |
| 6183 | */ |
| 6184 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 6185 | { |
| 6186 | int ret = 0; |
| 6187 | |
| 6188 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 6189 | |
| 6190 | qc->idle_timer_task = task_new_here(); |
| 6191 | if (!qc->idle_timer_task) { |
| 6192 | TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 6193 | goto leave; |
| 6194 | } |
| 6195 | |
| 6196 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 6197 | qc->idle_timer_task->context = qc; |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 6198 | qc->ack_expire = TICK_ETERNITY; |
| 6199 | qc_idle_timer_rearm(qc, 1, 0); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6200 | task_queue(qc->idle_timer_task); |
| 6201 | |
| 6202 | ret = 1; |
| 6203 | leave: |
| 6204 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 6205 | return ret; |
| 6206 | } |
| 6207 | |
Frédéric Lécaille | 6ff52f9 | 2023-04-24 15:41:07 +0200 | [diff] [blame] | 6208 | /* Parse into <pkt> a long header located at <*pos> position, <end> begin a pointer to the end |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6209 | * past one byte of this buffer. |
| 6210 | */ |
Frédéric Lécaille | 6ff52f9 | 2023-04-24 15:41:07 +0200 | [diff] [blame] | 6211 | static inline int quic_packet_read_long_header(unsigned char **pos, const unsigned char *end, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6212 | struct quic_rx_packet *pkt) |
| 6213 | { |
| 6214 | int ret = 0; |
| 6215 | unsigned char dcid_len, scid_len; |
| 6216 | |
| 6217 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 6218 | |
Frédéric Lécaille | 6ff52f9 | 2023-04-24 15:41:07 +0200 | [diff] [blame] | 6219 | if (end == *pos) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6220 | TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT); |
| 6221 | goto leave; |
| 6222 | } |
| 6223 | |
| 6224 | /* Destination Connection ID Length */ |
Frédéric Lécaille | 6ff52f9 | 2023-04-24 15:41:07 +0200 | [diff] [blame] | 6225 | dcid_len = *(*pos)++; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6226 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
Frédéric Lécaille | 6ff52f9 | 2023-04-24 15:41:07 +0200 | [diff] [blame] | 6227 | if (dcid_len > QUIC_CID_MAXLEN || end - *pos < dcid_len + 1) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6228 | TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT); |
| 6229 | goto leave; |
| 6230 | } |
| 6231 | |
| 6232 | if (dcid_len) { |
| 6233 | /* Check that the length of this received DCID matches the CID lengths |
| 6234 | * of our implementation for non Initials packets only. |
| 6235 | */ |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 6236 | if (pkt->version && pkt->version->num && |
| 6237 | pkt->type != QUIC_PACKET_TYPE_INITIAL && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6238 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
| 6239 | dcid_len != QUIC_HAP_CID_LEN) { |
| 6240 | TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT); |
| 6241 | goto leave; |
| 6242 | } |
| 6243 | |
Frédéric Lécaille | 6ff52f9 | 2023-04-24 15:41:07 +0200 | [diff] [blame] | 6244 | memcpy(pkt->dcid.data, *pos, dcid_len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6245 | } |
| 6246 | |
| 6247 | pkt->dcid.len = dcid_len; |
Frédéric Lécaille | 6ff52f9 | 2023-04-24 15:41:07 +0200 | [diff] [blame] | 6248 | *pos += dcid_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6249 | |
| 6250 | /* Source Connection ID Length */ |
Frédéric Lécaille | 6ff52f9 | 2023-04-24 15:41:07 +0200 | [diff] [blame] | 6251 | scid_len = *(*pos)++; |
| 6252 | if (scid_len > QUIC_CID_MAXLEN || end - *pos < scid_len) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6253 | TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT); |
| 6254 | goto leave; |
| 6255 | } |
| 6256 | |
| 6257 | if (scid_len) |
Frédéric Lécaille | 6ff52f9 | 2023-04-24 15:41:07 +0200 | [diff] [blame] | 6258 | memcpy(pkt->scid.data, *pos, scid_len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6259 | pkt->scid.len = scid_len; |
Frédéric Lécaille | 6ff52f9 | 2023-04-24 15:41:07 +0200 | [diff] [blame] | 6260 | *pos += scid_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6261 | |
| 6262 | ret = 1; |
| 6263 | leave: |
| 6264 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 6265 | return ret; |
| 6266 | } |
| 6267 | |
| 6268 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 6269 | static void qc_pkt_insert(struct quic_conn *qc, |
| 6270 | struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 6271 | { |
| 6272 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 6273 | |
| 6274 | pkt->pn_node.key = pkt->pn; |
| 6275 | quic_rx_packet_refinc(pkt); |
| 6276 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 6277 | |
| 6278 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 6279 | } |
| 6280 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6281 | /* Try to remove the header protection of <pkt> QUIC packet with <beg> the |
| 6282 | * address of the packet first byte, using the keys from encryption level <el>. |
| 6283 | * |
| 6284 | * If header protection has been successfully removed, packet data are copied |
| 6285 | * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also |
| 6286 | * proceeded, and the packet is inserted into <qc> protected packets tree. In |
| 6287 | * both cases, packet can now be considered handled by the <qc> connection. |
| 6288 | * |
| 6289 | * If header protection cannot be removed due to <el> secrets already |
| 6290 | * discarded, no operation is conducted. |
| 6291 | * |
| 6292 | * Returns 1 on success : packet data is now handled by the connection. On |
| 6293 | * error 0 is returned : packet should be dropped by the caller. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6294 | */ |
| 6295 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 6296 | struct quic_rx_packet *pkt, |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6297 | unsigned char *beg, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6298 | struct quic_enc_level **el) |
| 6299 | { |
| 6300 | int ret = 0; |
| 6301 | unsigned char *pn = NULL; /* Packet number field */ |
| 6302 | enum quic_tls_enc_level tel; |
| 6303 | struct quic_enc_level *qel; |
| 6304 | /* Only for traces. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6305 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6306 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6307 | BUG_ON(!pkt->pn_offset); |
| 6308 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6309 | /* The packet number is here. This is also the start minus |
| 6310 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 6311 | * protection. |
| 6312 | */ |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6313 | pn = beg + pkt->pn_offset; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6314 | |
| 6315 | tel = quic_packet_type_enc_level(pkt->type); |
| 6316 | qel = &qc->els[tel]; |
| 6317 | |
| 6318 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 6319 | struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt); |
| 6320 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6321 | /* Note that the following function enables us to unprotect the packet |
| 6322 | * number and its length subsequently used to decrypt the entire |
| 6323 | * packets. |
| 6324 | */ |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 6325 | if (!qc_do_rm_hp(qc, pkt, tls_ctx, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6326 | qel->pktns->rx.largest_pn, pn, beg)) { |
| 6327 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
| 6328 | goto out; |
| 6329 | } |
| 6330 | |
Frédéric Lécaille | ece86e6 | 2023-03-07 11:53:43 +0100 | [diff] [blame] | 6331 | qc_handle_spin_bit(qc, pkt, qel); |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6332 | /* The AAD includes the packet number field. */ |
| 6333 | pkt->aad_len = pkt->pn_offset + pkt->pnl; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6334 | if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) { |
| 6335 | TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc); |
| 6336 | goto out; |
| 6337 | } |
| 6338 | |
Frédéric Lécaille | c0aaa07 | 2023-04-07 17:58:49 +0200 | [diff] [blame] | 6339 | TRACE_PROTO("RX hp removed", QUIC_EV_CONN_TRMHP, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6340 | } |
| 6341 | else { |
| 6342 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 6343 | /* If the packet number space has been discarded, this packet |
| 6344 | * will be not parsed. |
| 6345 | */ |
| 6346 | TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt); |
| 6347 | goto out; |
| 6348 | } |
| 6349 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 6350 | TRACE_PROTO("RX hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6351 | LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 6352 | quic_rx_packet_refinc(pkt); |
| 6353 | } |
| 6354 | |
| 6355 | *el = qel; |
| 6356 | /* No reference counter incrementation here!!! */ |
| 6357 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 6358 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 6359 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 6360 | b_add(&qc->rx.buf, pkt->len); |
Amaury Denoyelle | dc654e8 | 2023-11-06 17:45:14 +0100 | [diff] [blame] | 6361 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6362 | ret = 1; |
| 6363 | out: |
Frédéric Lécaille | c0aaa07 | 2023-04-07 17:58:49 +0200 | [diff] [blame] | 6364 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6365 | return ret; |
| 6366 | } |
| 6367 | |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 6368 | /* Return the QUIC version (quic_version struct) with <version> as version number |
| 6369 | * if supported or NULL if not. |
| 6370 | */ |
| 6371 | static inline const struct quic_version *qc_supported_version(uint32_t version) |
| 6372 | { |
| 6373 | int i; |
| 6374 | |
| 6375 | if (unlikely(!version)) |
| 6376 | return &quic_version_VN_reserved; |
| 6377 | |
| 6378 | for (i = 0; i < quic_versions_nb; i++) |
| 6379 | if (quic_versions[i].num == version) |
| 6380 | return &quic_versions[i]; |
| 6381 | |
| 6382 | return NULL; |
| 6383 | } |
| 6384 | |
Willy Tarreau | dd9f921 | 2023-05-07 07:07:44 +0200 | [diff] [blame] | 6385 | /* Parse a QUIC packet header starting at <pos> position without exceeding <end>. |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 6386 | * Version and type are stored in <pkt> packet instance. Type is set to unknown |
| 6387 | * on two occasions : for unsupported version, in this case version field is |
| 6388 | * set to NULL; for Version Negotiation packet with version number set to 0. |
| 6389 | * |
| 6390 | * Returns 1 on success else 0. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6391 | */ |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 6392 | int qc_parse_hd_form(struct quic_rx_packet *pkt, |
Frédéric Lécaille | bb426aa | 2023-04-24 15:44:18 +0200 | [diff] [blame] | 6393 | unsigned char **pos, const unsigned char *end) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6394 | { |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 6395 | uint32_t version; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6396 | int ret = 0; |
Frédéric Lécaille | bb426aa | 2023-04-24 15:44:18 +0200 | [diff] [blame] | 6397 | const unsigned char byte0 = **pos; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6398 | |
| 6399 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 6400 | pkt->version = NULL; |
| 6401 | pkt->type = QUIC_PACKET_TYPE_UNKNOWN; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6402 | |
Frédéric Lécaille | bb426aa | 2023-04-24 15:44:18 +0200 | [diff] [blame] | 6403 | (*pos)++; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6404 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 6405 | unsigned char type = |
| 6406 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 6407 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6408 | /* Version */ |
Frédéric Lécaille | bb426aa | 2023-04-24 15:44:18 +0200 | [diff] [blame] | 6409 | if (!quic_read_uint32(&version, (const unsigned char **)pos, end)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6410 | TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT); |
| 6411 | goto out; |
| 6412 | } |
| 6413 | |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 6414 | pkt->version = qc_supported_version(version); |
| 6415 | if (version && pkt->version) { |
| 6416 | if (version != QUIC_PROTOCOL_VERSION_2) { |
| 6417 | pkt->type = type; |
| 6418 | } |
| 6419 | else { |
| 6420 | switch (type) { |
| 6421 | case 0: |
| 6422 | pkt->type = QUIC_PACKET_TYPE_RETRY; |
| 6423 | break; |
| 6424 | case 1: |
| 6425 | pkt->type = QUIC_PACKET_TYPE_INITIAL; |
| 6426 | break; |
| 6427 | case 2: |
| 6428 | pkt->type = QUIC_PACKET_TYPE_0RTT; |
| 6429 | break; |
| 6430 | case 3: |
| 6431 | pkt->type = QUIC_PACKET_TYPE_HANDSHAKE; |
| 6432 | break; |
| 6433 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6434 | } |
| 6435 | } |
| 6436 | } |
| 6437 | else { |
Frédéric Lécaille | ece86e6 | 2023-03-07 11:53:43 +0100 | [diff] [blame] | 6438 | if (byte0 & QUIC_PACKET_SPIN_BIT) |
| 6439 | pkt->flags |= QUIC_FL_RX_PACKET_SPIN_BIT; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6440 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6441 | } |
| 6442 | |
| 6443 | ret = 1; |
| 6444 | out: |
| 6445 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 6446 | return ret; |
| 6447 | } |
| 6448 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6449 | /* |
| 6450 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 6451 | * address <addr>. |
| 6452 | * Implementation of RFC9000 6. Version Negotiation |
| 6453 | * |
| 6454 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 6455 | * |
| 6456 | * Returns 0 on success else non-zero |
| 6457 | */ |
| 6458 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 6459 | struct quic_rx_packet *pkt) |
| 6460 | { |
| 6461 | char buf[256]; |
| 6462 | int ret = 0, i = 0, j; |
| 6463 | uint32_t version; |
| 6464 | const socklen_t addrlen = get_addr_len(addr); |
| 6465 | |
| 6466 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 6467 | /* |
| 6468 | * header form |
| 6469 | * long header, fixed bit to 0 for Version Negotiation |
| 6470 | */ |
| 6471 | /* TODO: RAND_bytes() should be replaced? */ |
| 6472 | if (RAND_bytes((unsigned char *)buf, 1) != 1) { |
| 6473 | TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT); |
| 6474 | goto out; |
| 6475 | } |
| 6476 | |
| 6477 | buf[i++] |= '\x80'; |
| 6478 | /* null version for Version Negotiation */ |
| 6479 | buf[i++] = '\x00'; |
| 6480 | buf[i++] = '\x00'; |
| 6481 | buf[i++] = '\x00'; |
| 6482 | buf[i++] = '\x00'; |
| 6483 | |
| 6484 | /* source connection id */ |
| 6485 | buf[i++] = pkt->scid.len; |
| 6486 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 6487 | i += pkt->scid.len; |
| 6488 | |
| 6489 | /* destination connection id */ |
| 6490 | buf[i++] = pkt->dcid.len; |
| 6491 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 6492 | i += pkt->dcid.len; |
| 6493 | |
| 6494 | /* supported version */ |
| 6495 | for (j = 0; j < quic_versions_nb; j++) { |
| 6496 | version = htonl(quic_versions[j].num); |
| 6497 | memcpy(&buf[i], &version, sizeof(version)); |
| 6498 | i += sizeof(version); |
| 6499 | } |
| 6500 | |
| 6501 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 6502 | goto out; |
| 6503 | |
| 6504 | ret = 1; |
| 6505 | out: |
| 6506 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 6507 | return !ret; |
| 6508 | } |
| 6509 | |
| 6510 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 6511 | * from <fd> UDP socket to <dst> |
| 6512 | * Return 1 if succeeded, 0 if not. |
| 6513 | */ |
| 6514 | static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr, |
| 6515 | struct quic_rx_packet *rxpkt) |
| 6516 | { |
| 6517 | int ret = 0, pktlen, rndlen; |
| 6518 | unsigned char pkt[64]; |
| 6519 | const socklen_t addrlen = get_addr_len(dstaddr); |
| 6520 | struct proxy *prx; |
| 6521 | struct quic_counters *prx_counters; |
| 6522 | |
| 6523 | TRACE_ENTER(QUIC_EV_STATELESS_RST); |
| 6524 | |
Amaury Denoyelle | 53b7b8e | 2024-05-22 15:55:58 +0200 | [diff] [blame] | 6525 | /* RFC 9000 10.3. Stateless Reset |
| 6526 | * |
| 6527 | * Endpoints MUST discard packets that are too small to be valid QUIC |
| 6528 | * packets. To give an example, with the set of AEAD functions defined |
| 6529 | * in [QUIC-TLS], short header packets that are smaller than 21 bytes |
| 6530 | * are never valid. |
| 6531 | * |
| 6532 | * [...] |
| 6533 | * |
| 6534 | * RFC 9000 10.3.3. Looping |
| 6535 | * |
| 6536 | * An endpoint MUST ensure that every Stateless Reset that it sends is |
| 6537 | * smaller than the packet that triggered it, unless it maintains state |
| 6538 | * sufficient to prevent looping. In the event of a loop, this results |
| 6539 | * in packets eventually being too small to trigger a response. |
| 6540 | */ |
| 6541 | if (rxpkt->len <= QUIC_STATELESS_RESET_PACKET_MINLEN) { |
| 6542 | TRACE_DEVEL("rxpkt too short", QUIC_EV_STATELESS_RST); |
| 6543 | goto leave; |
| 6544 | } |
| 6545 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6546 | prx = l->bind_conf->frontend; |
| 6547 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
Amaury Denoyelle | 53b7b8e | 2024-05-22 15:55:58 +0200 | [diff] [blame] | 6548 | |
| 6549 | /* RFC 9000 10.3. Stateless Reset |
| 6550 | * |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6551 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 6552 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 6553 | * than the packet it responds to. |
| 6554 | */ |
Amaury Denoyelle | 53b7b8e | 2024-05-22 15:55:58 +0200 | [diff] [blame] | 6555 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : |
| 6556 | QUIC_STATELESS_RESET_PACKET_MINLEN; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6557 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
| 6558 | |
| 6559 | /* Put a header of random bytes */ |
| 6560 | /* TODO: RAND_bytes() should be replaced */ |
| 6561 | if (RAND_bytes(pkt, rndlen) != 1) { |
| 6562 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST); |
| 6563 | goto leave; |
| 6564 | } |
| 6565 | |
| 6566 | /* Clear the most significant bit, and set the second one */ |
| 6567 | *pkt = (*pkt & ~0x80) | 0x40; |
Amaury Denoyelle | 9b68b64 | 2023-04-12 15:48:51 +0200 | [diff] [blame] | 6568 | if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6569 | rxpkt->dcid.data, rxpkt->dcid.len)) |
| 6570 | goto leave; |
| 6571 | |
| 6572 | if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0) |
| 6573 | goto leave; |
| 6574 | |
| 6575 | ret = 1; |
| 6576 | HA_ATOMIC_INC(&prx_counters->stateless_reset_sent); |
| 6577 | TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid); |
| 6578 | leave: |
| 6579 | TRACE_LEAVE(QUIC_EV_STATELESS_RST); |
| 6580 | return ret; |
| 6581 | } |
| 6582 | |
| 6583 | /* QUIC server only function. |
| 6584 | * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address. |
| 6585 | * This is the responsibility of the caller to check <aad> size is big enough |
| 6586 | * to contain these data. |
| 6587 | * Return the number of bytes copied to <aad>. |
| 6588 | */ |
| 6589 | static int quic_generate_retry_token_aad(unsigned char *aad, |
| 6590 | uint32_t version, |
Emeric Brun | a0dda4b | 2023-09-28 15:29:53 +0200 | [diff] [blame] | 6591 | const struct quic_cid *cid, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6592 | const struct sockaddr_storage *addr) |
| 6593 | { |
| 6594 | unsigned char *p; |
| 6595 | |
| 6596 | p = aad; |
Willy Tarreau | b4cf4ba | 2024-04-05 23:54:17 +0200 | [diff] [blame] | 6597 | write_u32(p, htonl(version)); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6598 | p += sizeof version; |
| 6599 | p += quic_saddr_cpy(p, addr); |
Emeric Brun | a0dda4b | 2023-09-28 15:29:53 +0200 | [diff] [blame] | 6600 | memcpy(p, cid->data, cid->len); |
| 6601 | p += cid->len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6602 | |
| 6603 | return p - aad; |
| 6604 | } |
| 6605 | |
| 6606 | /* QUIC server only function. |
| 6607 | * Generate the token to be used in Retry packets. The token is written to |
Frédéric Lécaille | dad0ede | 2023-04-24 14:35:18 +0200 | [diff] [blame] | 6608 | * <token> with <len> as length. <odcid> is the original destination connection |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6609 | * ID and <dcid> is our side destination connection ID (or client source |
| 6610 | * connection ID). |
| 6611 | * Returns the length of the encoded token or 0 on error. |
| 6612 | */ |
Frédéric Lécaille | dad0ede | 2023-04-24 14:35:18 +0200 | [diff] [blame] | 6613 | static int quic_generate_retry_token(unsigned char *token, size_t len, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6614 | const uint32_t version, |
| 6615 | const struct quic_cid *odcid, |
| 6616 | const struct quic_cid *dcid, |
| 6617 | struct sockaddr_storage *addr) |
| 6618 | { |
| 6619 | int ret = 0; |
| 6620 | unsigned char *p; |
Emeric Brun | a0dda4b | 2023-09-28 15:29:53 +0200 | [diff] [blame] | 6621 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 6622 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6623 | size_t aadlen; |
| 6624 | unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN]; |
| 6625 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 6626 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | 0499db4 | 2023-09-07 18:43:52 +0200 | [diff] [blame] | 6627 | const unsigned char *sec = global.cluster_secret; |
| 6628 | size_t seclen = sizeof global.cluster_secret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6629 | EVP_CIPHER_CTX *ctx = NULL; |
| 6630 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
Emeric Brun | 7875f12 | 2023-07-11 16:13:19 +0200 | [diff] [blame] | 6631 | uint32_t timestamp = (uint32_t)date.tv_sec; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6632 | |
| 6633 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 6634 | |
Frédéric Lécaille | 6d6ddb2 | 2023-05-15 17:40:00 +0200 | [diff] [blame] | 6635 | /* The token is made of the token format byte, the ODCID prefixed by its one byte |
| 6636 | * length, the creation timestamp, an AEAD TAG, and finally |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6637 | * the random bytes used to derive the secret to encrypt the token. |
| 6638 | */ |
Frédéric Lécaille | 6d6ddb2 | 2023-05-15 17:40:00 +0200 | [diff] [blame] | 6639 | if (1 + odcid->len + 1 + sizeof(timestamp) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN > len) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6640 | goto err; |
| 6641 | |
Emeric Brun | a0dda4b | 2023-09-28 15:29:53 +0200 | [diff] [blame] | 6642 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6643 | /* TODO: RAND_bytes() should be replaced */ |
| 6644 | if (RAND_bytes(salt, sizeof salt) != 1) { |
| 6645 | TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT); |
| 6646 | goto err; |
| 6647 | } |
| 6648 | |
| 6649 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 6650 | salt, sizeof salt, sec, seclen)) { |
| 6651 | TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT); |
| 6652 | goto err; |
| 6653 | } |
| 6654 | |
| 6655 | if (!quic_tls_tx_ctx_init(&ctx, aead, key)) { |
| 6656 | TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT); |
| 6657 | goto err; |
| 6658 | } |
| 6659 | |
| 6660 | /* Token build */ |
Frédéric Lécaille | dad0ede | 2023-04-24 14:35:18 +0200 | [diff] [blame] | 6661 | p = token; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6662 | *p++ = QUIC_TOKEN_FMT_RETRY, |
| 6663 | *p++ = odcid->len; |
| 6664 | memcpy(p, odcid->data, odcid->len); |
| 6665 | p += odcid->len; |
| 6666 | write_u32(p, htonl(timestamp)); |
| 6667 | p += sizeof timestamp; |
| 6668 | |
| 6669 | /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */ |
Emeric Brun | e0190c6 | 2023-07-11 14:53:41 +0200 | [diff] [blame] | 6670 | if (!quic_tls_encrypt(token + 1, p - token - 1, aad, aadlen, ctx, aead, iv)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6671 | TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT); |
| 6672 | goto err; |
| 6673 | } |
| 6674 | |
| 6675 | p += QUIC_TLS_TAG_LEN; |
| 6676 | memcpy(p, salt, sizeof salt); |
| 6677 | p += sizeof salt; |
| 6678 | EVP_CIPHER_CTX_free(ctx); |
| 6679 | |
Frédéric Lécaille | dad0ede | 2023-04-24 14:35:18 +0200 | [diff] [blame] | 6680 | ret = p - token; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6681 | leave: |
| 6682 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 6683 | return ret; |
| 6684 | |
| 6685 | err: |
| 6686 | if (ctx) |
| 6687 | EVP_CIPHER_CTX_free(ctx); |
| 6688 | goto leave; |
| 6689 | } |
| 6690 | |
| 6691 | /* QUIC server only function. |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6692 | * |
| 6693 | * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is |
| 6694 | * the UDP datagram containing <pkt> and <l> is the listener instance on which |
| 6695 | * it was received. If the token is valid, the ODCID of <qc> QUIC connection |
| 6696 | * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed |
| 6697 | * to validate the token but it can be NULL : in this case the version will be |
| 6698 | * retrieved from the packet. |
| 6699 | * |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6700 | * Return 1 if succeeded, 0 if not. |
| 6701 | */ |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6702 | |
| 6703 | static int quic_retry_token_check(struct quic_rx_packet *pkt, |
| 6704 | struct quic_dgram *dgram, |
| 6705 | struct listener *l, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6706 | struct quic_conn *qc, |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6707 | struct quic_cid *odcid) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6708 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6709 | struct proxy *prx; |
| 6710 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6711 | int ret = 0; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6712 | unsigned char *token = pkt->token; |
| 6713 | const uint64_t tokenlen = pkt->token_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6714 | unsigned char buf[128]; |
Emeric Brun | a0dda4b | 2023-09-28 15:29:53 +0200 | [diff] [blame] | 6715 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
| 6716 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6717 | size_t aadlen; |
| 6718 | const unsigned char *salt; |
| 6719 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 6720 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | 0499db4 | 2023-09-07 18:43:52 +0200 | [diff] [blame] | 6721 | const unsigned char *sec = global.cluster_secret; |
| 6722 | size_t seclen = sizeof global.cluster_secret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6723 | EVP_CIPHER_CTX *ctx = NULL; |
| 6724 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6725 | const struct quic_version *qv = qc ? qc->original_version : |
| 6726 | pkt->version; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6727 | |
| 6728 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6729 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6730 | /* The caller must ensure this. */ |
Frédéric Lécaille | 0499db4 | 2023-09-07 18:43:52 +0200 | [diff] [blame] | 6731 | BUG_ON(!pkt->token_len); |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6732 | |
| 6733 | prx = l->bind_conf->frontend; |
| 6734 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6735 | |
| 6736 | if (*pkt->token != QUIC_TOKEN_FMT_RETRY) { |
| 6737 | /* TODO: New token check */ |
| 6738 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 6739 | goto leave; |
| 6740 | } |
| 6741 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6742 | if (sizeof buf < tokenlen) { |
| 6743 | TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc); |
| 6744 | goto err; |
| 6745 | } |
| 6746 | |
Frédéric Lécaille | 35b6396 | 2023-05-15 18:11:21 +0200 | [diff] [blame] | 6747 | /* The token is made of the token format byte, the ODCID prefixed by its one byte |
| 6748 | * length, the creation timestamp, an AEAD TAG, and finally |
| 6749 | * the random bytes used to derive the secret to encrypt the token. |
| 6750 | */ |
| 6751 | if (tokenlen < 2 + QUIC_ODCID_MINLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN || |
| 6752 | tokenlen > 2 + QUIC_CID_MAXLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN) { |
| 6753 | TRACE_ERROR("invalid token length", QUIC_EV_CONN_LPKT, qc); |
| 6754 | goto err; |
| 6755 | } |
| 6756 | |
Emeric Brun | a0dda4b | 2023-09-28 15:29:53 +0200 | [diff] [blame] | 6757 | 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] | 6758 | salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN; |
| 6759 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 6760 | salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) { |
| 6761 | TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc); |
| 6762 | goto err; |
| 6763 | } |
| 6764 | |
| 6765 | if (!quic_tls_rx_ctx_init(&ctx, aead, key)) { |
| 6766 | TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc); |
| 6767 | goto err; |
| 6768 | } |
| 6769 | |
Frédéric Lécaille | 35b6396 | 2023-05-15 18:11:21 +0200 | [diff] [blame] | 6770 | /* The token is prefixed by a one-byte length format which is not ciphered. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6771 | if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen, |
| 6772 | ctx, aead, key, iv)) { |
| 6773 | TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc); |
| 6774 | goto err; |
| 6775 | } |
| 6776 | |
| 6777 | if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) { |
| 6778 | TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 6779 | goto err; |
| 6780 | } |
| 6781 | |
| 6782 | EVP_CIPHER_CTX_free(ctx); |
| 6783 | |
| 6784 | ret = 1; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6785 | HA_ATOMIC_INC(&prx_counters->retry_validated); |
| 6786 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6787 | leave: |
| 6788 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6789 | return ret; |
| 6790 | |
| 6791 | err: |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6792 | HA_ATOMIC_INC(&prx_counters->retry_error); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6793 | if (ctx) |
| 6794 | EVP_CIPHER_CTX_free(ctx); |
| 6795 | goto leave; |
| 6796 | } |
| 6797 | |
| 6798 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 6799 | * the Initial <pkt> packet. |
| 6800 | * |
| 6801 | * Returns 0 on success else non-zero. |
| 6802 | */ |
| 6803 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 6804 | struct quic_rx_packet *pkt, const struct quic_version *qv) |
| 6805 | { |
| 6806 | int ret = 0; |
| 6807 | unsigned char buf[128]; |
| 6808 | int i = 0, token_len; |
| 6809 | const socklen_t addrlen = get_addr_len(addr); |
| 6810 | struct quic_cid scid; |
| 6811 | |
| 6812 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 6813 | |
Frédéric Lécaille | 2b22054 | 2023-06-30 12:17:36 +0200 | [diff] [blame] | 6814 | /* long header(1) | fixed bit(1) | packet type QUIC_PACKET_TYPE_RETRY(2) | unused random bits(4)*/ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6815 | buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) | |
Frédéric Lécaille | 2b22054 | 2023-06-30 12:17:36 +0200 | [diff] [blame] | 6816 | (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT) | |
| 6817 | statistical_prng_range(16); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6818 | /* version */ |
Emeric Brun | 2b6d45e | 2023-07-17 18:33:44 +0200 | [diff] [blame] | 6819 | write_n32(&buf[i], qv->num); |
Frédéric Lécaille | 966e468 | 2023-06-30 14:41:31 +0200 | [diff] [blame] | 6820 | i += sizeof(uint32_t); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6821 | |
| 6822 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 6823 | buf[i++] = pkt->scid.len; |
| 6824 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 6825 | i += pkt->scid.len; |
| 6826 | |
| 6827 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 6828 | scid.len = QUIC_HAP_CID_LEN; |
| 6829 | /* TODO: RAND_bytes() should be replaced */ |
| 6830 | if (RAND_bytes(scid.data, scid.len) != 1) { |
| 6831 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT); |
| 6832 | goto out; |
| 6833 | } |
| 6834 | |
| 6835 | buf[i++] = scid.len; |
| 6836 | memcpy(&buf[i], scid.data, scid.len); |
| 6837 | i += scid.len; |
| 6838 | |
| 6839 | /* token */ |
| 6840 | if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num, |
Emeric Brun | a0dda4b | 2023-09-28 15:29:53 +0200 | [diff] [blame] | 6841 | &pkt->dcid, &pkt->scid, addr))) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6842 | TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT); |
| 6843 | goto out; |
| 6844 | } |
| 6845 | |
| 6846 | i += token_len; |
| 6847 | |
| 6848 | /* token integrity tag */ |
Emeric Brun | a47f5cd | 2023-06-27 15:24:05 +0200 | [diff] [blame] | 6849 | if ((sizeof(buf) - i < QUIC_TLS_TAG_LEN) || |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6850 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 6851 | pkt->dcid.len, buf, i, qv)) { |
| 6852 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 6853 | goto out; |
| 6854 | } |
| 6855 | |
| 6856 | i += QUIC_TLS_TAG_LEN; |
| 6857 | |
| 6858 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) { |
| 6859 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 6860 | goto out; |
| 6861 | } |
| 6862 | |
| 6863 | ret = 1; |
| 6864 | out: |
| 6865 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 6866 | return !ret; |
| 6867 | } |
| 6868 | |
Amaury Denoyelle | 2c98209 | 2023-04-03 18:50:58 +0200 | [diff] [blame] | 6869 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is an |
| 6870 | * INITIAL or 0RTT type, we may have to use client address <saddr> if an ODCID |
| 6871 | * is used. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6872 | * |
| 6873 | * Returns the instance or NULL if not found. |
| 6874 | */ |
| 6875 | static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt, |
| 6876 | struct listener *l, |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 6877 | struct sockaddr_storage *saddr, |
| 6878 | int *new_tid) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6879 | { |
| 6880 | struct quic_conn *qc = NULL; |
| 6881 | struct ebmb_node *node; |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 6882 | struct quic_connection_id *conn_id; |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 6883 | struct quic_cid_tree *tree; |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 6884 | uint conn_id_tid; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6885 | |
| 6886 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 6887 | *new_tid = -1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6888 | |
Amaury Denoyelle | 2c98209 | 2023-04-03 18:50:58 +0200 | [diff] [blame] | 6889 | /* First look into DCID tree. */ |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 6890 | tree = &quic_cid_trees[_quic_cid_tree_idx(pkt->dcid.data)]; |
| 6891 | HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock); |
| 6892 | node = ebmb_lookup(&tree->root, pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6893 | |
Amaury Denoyelle | 2c98209 | 2023-04-03 18:50:58 +0200 | [diff] [blame] | 6894 | /* If not found on an Initial/0-RTT packet, it could be because an |
| 6895 | * ODCID is reused by the client. Calculate the derived CID value to |
| 6896 | * retrieve it from the DCID tree. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6897 | */ |
Amaury Denoyelle | 2c98209 | 2023-04-03 18:50:58 +0200 | [diff] [blame] | 6898 | if (!node && (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 6899 | pkt->type == QUIC_PACKET_TYPE_0RTT)) { |
Amaury Denoyelle | c2a9264 | 2023-04-13 15:26:18 +0200 | [diff] [blame] | 6900 | const struct quic_cid derive_cid = quic_derive_cid(&pkt->dcid, saddr); |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 6901 | |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 6902 | HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock); |
| 6903 | |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 6904 | tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)]; |
| 6905 | HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock); |
| 6906 | node = ebmb_lookup(&tree->root, derive_cid.data, derive_cid.len); |
Amaury Denoyelle | 2c98209 | 2023-04-03 18:50:58 +0200 | [diff] [blame] | 6907 | } |
| 6908 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6909 | if (!node) |
| 6910 | goto end; |
| 6911 | |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 6912 | conn_id = ebmb_entry(node, struct quic_connection_id, node); |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 6913 | conn_id_tid = HA_ATOMIC_LOAD(&conn_id->tid); |
| 6914 | if (conn_id_tid != tid) { |
| 6915 | *new_tid = conn_id_tid; |
| 6916 | goto end; |
| 6917 | } |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 6918 | qc = conn_id->qc; |
Amaury Denoyelle | 806c5c5 | 2024-06-27 18:52:23 +0200 | [diff] [blame] | 6919 | TRACE_DEVEL("found connection", QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6920 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6921 | end: |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 6922 | HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock); |
Amaury Denoyelle | 806c5c5 | 2024-06-27 18:52:23 +0200 | [diff] [blame] | 6923 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6924 | return qc; |
| 6925 | } |
| 6926 | |
| 6927 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 6928 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 6929 | * parameters of this session. |
| 6930 | * This is the responsibility of the caller to check the validity of all the |
| 6931 | * pointers passed as parameter to this function. |
| 6932 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 6933 | * CO_ER_SSL_NO_MEM. |
| 6934 | */ |
| 6935 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 6936 | unsigned char *params, size_t params_len) |
| 6937 | { |
| 6938 | int retry, ret = -1; |
| 6939 | |
| 6940 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 6941 | |
| 6942 | retry = 1; |
| 6943 | retry: |
| 6944 | *ssl = SSL_new(ssl_ctx); |
| 6945 | if (!*ssl) { |
| 6946 | if (!retry--) |
Frédéric Lécaille | e418527 | 2023-06-02 16:56:16 +0200 | [diff] [blame] | 6947 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6948 | |
| 6949 | pool_gc(NULL); |
| 6950 | goto retry; |
| 6951 | } |
| 6952 | |
Frédéric Lécaille | ca87a62 | 2023-06-02 17:00:04 +0200 | [diff] [blame] | 6953 | if (!SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) || |
| 6954 | !SSL_set_quic_method(*ssl, &ha_quic_method)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6955 | SSL_free(*ssl); |
| 6956 | *ssl = NULL; |
| 6957 | if (!retry--) |
Frédéric Lécaille | e418527 | 2023-06-02 16:56:16 +0200 | [diff] [blame] | 6958 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6959 | |
| 6960 | pool_gc(NULL); |
| 6961 | goto retry; |
| 6962 | } |
| 6963 | |
| 6964 | ret = 0; |
| 6965 | leave: |
| 6966 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 6967 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6968 | } |
| 6969 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6970 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 6971 | * used to process <qc> received packets. The allocated context is stored in |
| 6972 | * <qc.xprt_ctx>. |
| 6973 | * |
| 6974 | * Returns 0 on success else non-zero. |
| 6975 | */ |
| 6976 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 6977 | { |
| 6978 | int ret = 0; |
| 6979 | struct bind_conf *bc = qc->li->bind_conf; |
| 6980 | struct ssl_sock_ctx *ctx = NULL; |
| 6981 | |
| 6982 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 6983 | |
| 6984 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 6985 | if (!ctx) { |
| 6986 | TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT); |
| 6987 | goto err; |
| 6988 | } |
| 6989 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6990 | ctx->subs = NULL; |
| 6991 | ctx->xprt_ctx = NULL; |
| 6992 | ctx->qc = qc; |
| 6993 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6994 | if (qc_is_listener(qc)) { |
| 6995 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 6996 | qc->enc_params, qc->enc_params_len) == -1) { |
| 6997 | goto err; |
| 6998 | } |
| 6999 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Frédéric Lécaille | 65a8b9a | 2023-06-02 17:05:38 +0200 | [diff] [blame] | 7000 | #ifndef USE_QUIC_OPENSSL_COMPAT |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7001 | /* Enabling 0-RTT */ |
| 7002 | if (bc->ssl_conf.early_data) |
| 7003 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 7004 | #endif |
Frédéric Lécaille | 65a8b9a | 2023-06-02 17:05:38 +0200 | [diff] [blame] | 7005 | #endif |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7006 | |
| 7007 | SSL_set_accept_state(ctx->ssl); |
| 7008 | } |
| 7009 | |
| 7010 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 7011 | |
| 7012 | /* Store the allocated context in <qc>. */ |
| 7013 | qc->xprt_ctx = ctx; |
| 7014 | |
Amaury Denoyelle | aff66bb | 2023-10-25 15:38:50 +0200 | [diff] [blame] | 7015 | /* global.sslconns is already incremented on INITIAL packet parsing. */ |
| 7016 | _HA_ATOMIC_INC(&global.totalsslconns); |
| 7017 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7018 | ret = 1; |
| 7019 | leave: |
| 7020 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 7021 | return !ret; |
| 7022 | |
| 7023 | err: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7024 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 7025 | goto leave; |
| 7026 | } |
| 7027 | |
Frédéric Lécaille | 7f0b1c7 | 2023-04-24 14:38:33 +0200 | [diff] [blame] | 7028 | /* Check that all the bytes between <pos> included and <end> address |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7029 | * excluded are null. This is the responsibility of the caller to |
Frédéric Lécaille | 7f0b1c7 | 2023-04-24 14:38:33 +0200 | [diff] [blame] | 7030 | * check that there is at least one byte between <pos> end <end>. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7031 | * Return 1 if this all the bytes are null, 0 if not. |
| 7032 | */ |
Frédéric Lécaille | 7f0b1c7 | 2023-04-24 14:38:33 +0200 | [diff] [blame] | 7033 | static inline int quic_padding_check(const unsigned char *pos, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7034 | const unsigned char *end) |
| 7035 | { |
Frédéric Lécaille | 7f0b1c7 | 2023-04-24 14:38:33 +0200 | [diff] [blame] | 7036 | while (pos < end && !*pos) |
| 7037 | pos++; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7038 | |
Frédéric Lécaille | 7f0b1c7 | 2023-04-24 14:38:33 +0200 | [diff] [blame] | 7039 | return pos == end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7040 | } |
| 7041 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7042 | /* Find the associated connection to the packet <pkt> or create a new one if |
| 7043 | * this is an Initial packet. <dgram> is the datagram containing the packet and |
| 7044 | * <l> is the listener instance on which it was received. |
| 7045 | * |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 7046 | * By default, <new_tid> is set to -1. However, if thread affinity has been |
| 7047 | * chanbed, it will be set to its new thread ID. |
| 7048 | * |
| 7049 | * Returns the quic-conn instance or NULL if not found or thread affinity |
| 7050 | * changed. |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7051 | */ |
| 7052 | static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt, |
| 7053 | struct quic_dgram *dgram, |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 7054 | struct listener *l, |
| 7055 | int *new_tid) |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7056 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 7057 | struct quic_cid token_odcid = { .len = 0 }; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7058 | struct quic_conn *qc = NULL; |
| 7059 | struct proxy *prx; |
| 7060 | struct quic_counters *prx_counters; |
| 7061 | |
| 7062 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 7063 | |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 7064 | *new_tid = -1; |
| 7065 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7066 | prx = l->bind_conf->frontend; |
| 7067 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 7068 | |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 7069 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr, new_tid); |
| 7070 | |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 7071 | /* If connection already created or rebinded on another thread. */ |
Frédéric Lécaille | ab3aa0f | 2023-05-24 09:06:06 +0200 | [diff] [blame] | 7072 | if (!qc && *new_tid != -1 && tid != *new_tid) |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 7073 | goto out; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7074 | |
| 7075 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 7076 | BUG_ON(!pkt->version); /* This must not happen. */ |
| 7077 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7078 | if (!qc) { |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 7079 | struct quic_cid_tree *tree; |
| 7080 | struct ebmb_node *node; |
| 7081 | struct quic_connection_id *conn_id; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7082 | int ipv4; |
| 7083 | |
Amaury Denoyelle | 6b240e4 | 2023-11-09 16:44:50 +0100 | [diff] [blame] | 7084 | if (pkt->token_len) { |
| 7085 | /* Validate the token only when connection is unknown. */ |
| 7086 | if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid)) |
| 7087 | goto err; |
| 7088 | } |
| 7089 | else if (!(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) && |
| 7090 | HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7091 | TRACE_PROTO("Initial without token, sending retry", |
| 7092 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 7093 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) { |
| 7094 | TRACE_ERROR("Error during Retry generation", |
| 7095 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 7096 | goto out; |
| 7097 | } |
| 7098 | |
| 7099 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 7100 | goto out; |
| 7101 | } |
| 7102 | |
| 7103 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 7104 | * When an Initial packet is sent by a client that has not previously |
| 7105 | * received an Initial or Retry packet from the server, the client |
| 7106 | * populates the Destination Connection ID field with an unpredictable |
| 7107 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 7108 | */ |
| 7109 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 7110 | TRACE_PROTO("dropped packet", |
| 7111 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 7112 | goto err; |
| 7113 | } |
| 7114 | |
| 7115 | pkt->saddr = dgram->saddr; |
| 7116 | ipv4 = dgram->saddr.ss_family == AF_INET; |
| 7117 | |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 7118 | /* Generate the first connection CID. This is derived from the client |
| 7119 | * ODCID and address. This allows to retrieve the connection from the |
| 7120 | * ODCID without storing it in the CID tree. This is an interesting |
| 7121 | * optimization as the client is expected to stop using its ODCID in |
| 7122 | * favor of our generated value. |
| 7123 | */ |
| 7124 | conn_id = new_quic_cid(NULL, NULL, &pkt->dcid, &pkt->saddr); |
| 7125 | if (!conn_id) |
| 7126 | goto err; |
| 7127 | |
Frédéric Lécaille | fd212a7 | 2023-06-16 16:10:58 +0200 | [diff] [blame] | 7128 | qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid, |
| 7129 | conn_id, &dgram->daddr, &pkt->saddr, 1, |
| 7130 | !!pkt->token_len, l); |
| 7131 | if (qc == NULL) { |
Frédéric Lécaille | fd212a7 | 2023-06-16 16:10:58 +0200 | [diff] [blame] | 7132 | pool_free(pool_head_quic_connection_id, conn_id); |
| 7133 | goto err; |
| 7134 | } |
| 7135 | |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 7136 | tree = &quic_cid_trees[quic_cid_tree_idx(&conn_id->cid)]; |
| 7137 | HA_RWLOCK_WRLOCK(QC_CID_LOCK, &tree->lock); |
| 7138 | node = ebmb_insert(&tree->root, &conn_id->node, conn_id->cid.len); |
| 7139 | if (node != &conn_id->node) { |
| 7140 | pool_free(pool_head_quic_connection_id, conn_id); |
| 7141 | |
| 7142 | conn_id = ebmb_entry(node, struct quic_connection_id, node); |
| 7143 | *new_tid = HA_ATOMIC_LOAD(&conn_id->tid); |
Frédéric Lécaille | fd212a7 | 2023-06-16 16:10:58 +0200 | [diff] [blame] | 7144 | quic_conn_release(qc); |
| 7145 | qc = NULL; |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 7146 | } |
Frédéric Lécaille | 8a58658 | 2023-06-26 10:39:56 +0200 | [diff] [blame] | 7147 | else { |
| 7148 | /* From here, <qc> is the correct connection for this <pkt> Initial |
| 7149 | * packet. <conn_id> must be inserted in the CIDs tree for this |
| 7150 | * connection. |
| 7151 | */ |
| 7152 | eb64_insert(&qc->cids, &conn_id->seq_num); |
| 7153 | /* Initialize the next CID sequence number to be used for this connection. */ |
| 7154 | qc->next_cid_seq_num = 1; |
| 7155 | } |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 7156 | HA_RWLOCK_WRUNLOCK(QC_CID_LOCK, &tree->lock); |
| 7157 | |
| 7158 | if (*new_tid != -1) |
| 7159 | goto out; |
| 7160 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7161 | HA_ATOMIC_INC(&prx_counters->half_open_conn); |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7162 | } |
| 7163 | } |
| 7164 | else if (!qc) { |
Amaury Denoyelle | 53b7b8e | 2024-05-22 15:55:58 +0200 | [diff] [blame] | 7165 | /* Stateless Reset sent even for Long header packets as haproxy |
| 7166 | * emits stateless_reset_token in its TPs. |
| 7167 | */ |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 7168 | TRACE_PROTO("RX non Initial pkt without connection", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
Frédéric Lécaille | 0499db4 | 2023-09-07 18:43:52 +0200 | [diff] [blame] | 7169 | if (!send_stateless_reset(l, &dgram->saddr, pkt)) |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7170 | TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc); |
| 7171 | goto err; |
| 7172 | } |
| 7173 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7174 | out: |
| 7175 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 7176 | return qc; |
| 7177 | |
| 7178 | err: |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 7179 | if (qc) |
| 7180 | qc->cntrs.dropped_pkt++; |
| 7181 | else |
| 7182 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Amaury Denoyelle | 331b8b1 | 2023-10-25 10:52:23 +0200 | [diff] [blame] | 7183 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7184 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 7185 | return NULL; |
| 7186 | } |
| 7187 | |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7188 | /* Parse a QUIC packet starting at <pos>. Data won't be read after <end> even |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7189 | * if the packet is incomplete. This function will populate fields of <pkt> |
| 7190 | * instance, most notably its length. <dgram> is the UDP datagram which |
| 7191 | * contains the parsed packet. <l> is the listener instance on which it was |
| 7192 | * received. |
| 7193 | * |
| 7194 | * Returns 0 on success else non-zero. Packet length is guaranteed to be set to |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7195 | * the real packet value or to cover all data between <pos> and <end> : this is |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7196 | * useful to reject a whole datagram. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7197 | */ |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7198 | static int quic_rx_pkt_parse(struct quic_rx_packet *pkt, |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7199 | unsigned char *pos, const unsigned char *end, |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7200 | struct quic_dgram *dgram, struct listener *l) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7201 | { |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7202 | const unsigned char *beg = pos; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7203 | struct proxy *prx; |
| 7204 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7205 | |
| 7206 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 7207 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7208 | prx = l->bind_conf->frontend; |
| 7209 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 7210 | /* This ist only to please to traces and distinguish the |
| 7211 | * packet with parsed packet number from others. |
| 7212 | */ |
| 7213 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7214 | if (end <= pos) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7215 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 7216 | goto drop; |
| 7217 | } |
| 7218 | |
| 7219 | /* Fixed bit */ |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7220 | if (!(*pos & QUIC_PACKET_FIXED_BIT)) { |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 7221 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7222 | quic_padding_check(pos, end)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7223 | /* Some browsers may pad the remaining datagram space with null bytes. |
| 7224 | * That is what we called add padding out of QUIC packets. Such |
| 7225 | * datagrams must be considered as valid. But we can only consume |
| 7226 | * the remaining space. |
| 7227 | */ |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7228 | pkt->len = end - pos; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7229 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7230 | } |
| 7231 | |
| 7232 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 7233 | goto drop; |
| 7234 | } |
| 7235 | |
| 7236 | /* Header form */ |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7237 | if (!qc_parse_hd_form(pkt, &pos, end)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7238 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 7239 | goto drop; |
| 7240 | } |
| 7241 | |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 7242 | if (pkt->type != QUIC_PACKET_TYPE_SHORT) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7243 | uint64_t len; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7244 | TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 7245 | |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7246 | if (!quic_packet_read_long_header(&pos, end, pkt)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7247 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 7248 | goto drop; |
| 7249 | } |
| 7250 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7251 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 7252 | * they must have the same DCID. |
| 7253 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 7254 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7255 | (pkt->dcid.len != dgram->dcid_len || |
| 7256 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7257 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7258 | goto drop; |
| 7259 | } |
| 7260 | |
| 7261 | /* Retry of Version Negotiation packets are only sent by servers */ |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 7262 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || |
| 7263 | (pkt->version && !pkt->version->num)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7264 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 7265 | goto drop; |
| 7266 | } |
| 7267 | |
| 7268 | /* RFC9000 6. Version Negotiation */ |
Amaury Denoyelle | 1a5cc19 | 2023-04-17 15:03:51 +0200 | [diff] [blame] | 7269 | if (!pkt->version) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7270 | /* unsupported version, send Negotiation packet */ |
| 7271 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
| 7272 | TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7273 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7274 | } |
| 7275 | |
| 7276 | TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7277 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7278 | } |
| 7279 | |
| 7280 | /* For Initial packets, and for servers (QUIC clients connections), |
| 7281 | * there is no Initial connection IDs storage. |
| 7282 | */ |
| 7283 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 7284 | uint64_t token_len; |
| 7285 | |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7286 | if (!quic_dec_int(&token_len, (const unsigned char **)&pos, end) || |
| 7287 | end - pos < token_len) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7288 | TRACE_PROTO("Packet dropped", |
Amaury Denoyelle | 89e48ff | 2023-04-19 10:04:41 +0200 | [diff] [blame] | 7289 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7290 | goto drop; |
| 7291 | } |
| 7292 | |
| 7293 | /* TODO Retry should be automatically activated if |
| 7294 | * suspect network usage is detected. |
| 7295 | */ |
Frédéric Lécaille | 0499db4 | 2023-09-07 18:43:52 +0200 | [diff] [blame] | 7296 | if (!token_len) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7297 | if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) { |
| 7298 | TRACE_PROTO("Initial without token, sending retry", |
Amaury Denoyelle | 89e48ff | 2023-04-19 10:04:41 +0200 | [diff] [blame] | 7299 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 7300 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7301 | TRACE_PROTO("Error during Retry generation", |
Amaury Denoyelle | 89e48ff | 2023-04-19 10:04:41 +0200 | [diff] [blame] | 7302 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7303 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7304 | } |
| 7305 | |
| 7306 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7307 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7308 | } |
| 7309 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7310 | |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7311 | pkt->token = pos; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7312 | pkt->token_len = token_len; |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7313 | pos += pkt->token_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7314 | } |
| 7315 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
| 7316 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
| 7317 | TRACE_PROTO("Packet dropped", |
Amaury Denoyelle | 89e48ff | 2023-04-19 10:04:41 +0200 | [diff] [blame] | 7318 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7319 | goto drop; |
| 7320 | } |
| 7321 | } |
| 7322 | |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7323 | if (!quic_dec_int(&len, (const unsigned char **)&pos, end) || |
| 7324 | end - pos < len) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7325 | TRACE_PROTO("Packet dropped", |
Amaury Denoyelle | 89e48ff | 2023-04-19 10:04:41 +0200 | [diff] [blame] | 7326 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7327 | goto drop; |
| 7328 | } |
| 7329 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 7330 | /* Packet Number is stored here. Packet Length totalizes the |
| 7331 | * rest of the content. |
| 7332 | */ |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7333 | pkt->pn_offset = pos - beg; |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 7334 | pkt->len = pkt->pn_offset + len; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7335 | |
Frédéric Lécaille | 35218c6 | 2023-02-16 11:40:11 +0100 | [diff] [blame] | 7336 | /* RFC 9000. Initial Datagram Size |
| 7337 | * |
| 7338 | * A server MUST discard an Initial packet that is carried in a UDP datagram |
| 7339 | * with a payload that is smaller than the smallest allowed maximum datagram |
| 7340 | * size of 1200 bytes. |
| 7341 | */ |
| 7342 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 7343 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 7344 | TRACE_PROTO("RX too short datagram with an Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 35218c6 | 2023-02-16 11:40:11 +0100 | [diff] [blame] | 7345 | HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram); |
| 7346 | goto drop; |
| 7347 | } |
| 7348 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7349 | /* Interrupt parsing after packet length retrieval : this |
| 7350 | * ensures that only the packet is dropped but not the whole |
| 7351 | * datagram. |
| 7352 | */ |
| 7353 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 7354 | TRACE_PROTO("RX 0-RTT packet not supported", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7355 | goto drop; |
| 7356 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7357 | } |
| 7358 | else { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 7359 | TRACE_PROTO("RX short header packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7360 | if (end - pos < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 7361 | TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7362 | goto drop; |
| 7363 | } |
| 7364 | |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7365 | memcpy(pkt->dcid.data, pos, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7366 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
| 7367 | |
| 7368 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 7369 | * they must have the same DCID. |
| 7370 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 7371 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7372 | (pkt->dcid.len != dgram->dcid_len || |
| 7373 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 7374 | TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7375 | goto drop; |
| 7376 | } |
| 7377 | |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7378 | pos += QUIC_HAP_CID_LEN; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7379 | |
Frédéric Lécaille | bef3098 | 2023-04-24 14:43:57 +0200 | [diff] [blame] | 7380 | pkt->pn_offset = pos - beg; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7381 | /* A short packet is the last one of a UDP datagram. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7382 | pkt->len = end - beg; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 7383 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7384 | |
Amaury Denoyelle | 89e48ff | 2023-04-19 10:04:41 +0200 | [diff] [blame] | 7385 | TRACE_PROTO("RX pkt parsed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version); |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 7386 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7387 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7388 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7389 | drop: |
| 7390 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7391 | drop_silent: |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7392 | if (!pkt->len) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7393 | pkt->len = end - beg; |
Amaury Denoyelle | 89e48ff | 2023-04-19 10:04:41 +0200 | [diff] [blame] | 7394 | TRACE_PROTO("RX pkt parsing failed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version); |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 7395 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7396 | return -1; |
| 7397 | } |
| 7398 | |
| 7399 | /* Check if received packet <pkt> should be drop due to <qc> already in closing |
| 7400 | * state. This can be true if a CONNECTION_CLOSE has already been emitted for |
| 7401 | * this connection. |
| 7402 | * |
| 7403 | * Returns false if connection is not in closing state else true. The caller |
| 7404 | * should drop the whole datagram in the last case to not mess up <qc> |
| 7405 | * CONNECTION_CLOSE rate limit counter. |
| 7406 | */ |
| 7407 | static int qc_rx_check_closing(struct quic_conn *qc, |
| 7408 | struct quic_rx_packet *pkt) |
| 7409 | { |
| 7410 | if (!(qc->flags & QUIC_FL_CONN_CLOSING)) |
| 7411 | return 0; |
| 7412 | |
| 7413 | TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 7414 | |
| 7415 | /* Check if CONNECTION_CLOSE rate reemission is reached. */ |
| 7416 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 7417 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 7418 | qc->nb_pkt_for_cc++; |
| 7419 | qc->nb_pkt_since_cc = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7420 | } |
| 7421 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7422 | return 1; |
| 7423 | } |
| 7424 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7425 | /* React to a connection migration initiated on <qc> by a client with the new |
| 7426 | * path addresses <peer_addr>/<local_addr>. |
| 7427 | * |
| 7428 | * Returns 0 on success else non-zero. |
| 7429 | */ |
| 7430 | static int qc_handle_conn_migration(struct quic_conn *qc, |
| 7431 | const struct sockaddr_storage *peer_addr, |
| 7432 | const struct sockaddr_storage *local_addr) |
| 7433 | { |
| 7434 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 7435 | |
Frédéric Lécaille | 6fc8697 | 2023-01-12 08:29:23 +0100 | [diff] [blame] | 7436 | /* RFC 9000. Connection Migration |
| 7437 | * |
| 7438 | * If the peer sent the disable_active_migration transport parameter, |
| 7439 | * an endpoint also MUST NOT send packets (including probing packets; |
| 7440 | * see Section 9.1) from a different local address to the address the peer |
| 7441 | * used during the handshake, unless the endpoint has acted on a |
| 7442 | * preferred_address transport parameter from the peer. |
| 7443 | */ |
| 7444 | if (qc->li->bind_conf->quic_params.disable_active_migration) { |
| 7445 | TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc); |
| 7446 | goto err; |
| 7447 | } |
| 7448 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7449 | /* RFC 9000 9. Connection Migration |
| 7450 | * |
Amaury Denoyelle | eb6be98 | 2022-11-21 11:14:45 +0100 | [diff] [blame] | 7451 | * The design of QUIC relies on endpoints retaining a stable address for |
| 7452 | * the duration of the handshake. An endpoint MUST NOT initiate |
| 7453 | * connection migration before the handshake is confirmed, as defined in |
| 7454 | * Section 4.1.2 of [QUIC-TLS]. |
| 7455 | */ |
| 7456 | if (qc->state < QUIC_HS_ST_COMPLETE) { |
| 7457 | TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc); |
| 7458 | goto err; |
| 7459 | } |
| 7460 | |
| 7461 | /* RFC 9000 9. Connection Migration |
| 7462 | * |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7463 | * TODO |
| 7464 | * An endpoint MUST |
| 7465 | * perform path validation (Section 8.2) if it detects any change to a |
| 7466 | * peer's address, unless it has previously validated that address. |
| 7467 | */ |
| 7468 | |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 7469 | /* Update quic-conn owned socket if in used. |
| 7470 | * TODO try to reuse it instead of closing and opening a new one. |
| 7471 | */ |
| 7472 | if (qc_test_fd(qc)) { |
| 7473 | /* TODO try to reuse socket instead of closing it and opening a new one. */ |
| 7474 | TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc); |
| 7475 | qc_release_fd(qc, 1); |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 7476 | /* TODO need to adjust <jobs> on socket allocation failure. */ |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 7477 | qc_alloc_fd(qc, local_addr, peer_addr); |
| 7478 | } |
| 7479 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7480 | qc->local_addr = *local_addr; |
| 7481 | qc->peer_addr = *peer_addr; |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 7482 | qc->cntrs.conn_migration_done++; |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7483 | |
| 7484 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 7485 | return 0; |
| 7486 | |
| 7487 | err: |
| 7488 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 7489 | return 1; |
| 7490 | } |
| 7491 | |
Frédéric Lécaille | 1dbeb35 | 2023-02-07 11:40:21 +0100 | [diff] [blame] | 7492 | /* Release the memory for the RX packets which are no more referenced |
| 7493 | * and consume their payloads which have been copied to the RX buffer |
| 7494 | * for the connection. |
| 7495 | * Always succeeds. |
| 7496 | */ |
| 7497 | static inline void quic_rx_pkts_del(struct quic_conn *qc) |
| 7498 | { |
| 7499 | struct quic_rx_packet *pkt, *pktback; |
| 7500 | |
| 7501 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 7502 | TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0, |
Frédéric Lécaille | b7a13be | 2023-02-22 17:24:23 +0100 | [diff] [blame] | 7503 | "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)", |
Frédéric Lécaille | 1dbeb35 | 2023-02-07 11:40:21 +0100 | [diff] [blame] | 7504 | (long long)pkt->pn_node.key, |
Frédéric Lécaille | b7a13be | 2023-02-22 17:24:23 +0100 | [diff] [blame] | 7505 | pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt, |
Frédéric Lécaille | 1dbeb35 | 2023-02-07 11:40:21 +0100 | [diff] [blame] | 7506 | (unsigned char *)b_head(&qc->rx.buf) - pkt->data); |
| 7507 | if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) { |
| 7508 | size_t cdata; |
| 7509 | |
| 7510 | cdata = b_contig_data(&qc->rx.buf, 0); |
| 7511 | TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0, |
Frédéric Lécaille | b7a13be | 2023-02-22 17:24:23 +0100 | [diff] [blame] | 7512 | "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf)); |
Frédéric Lécaille | 1dbeb35 | 2023-02-07 11:40:21 +0100 | [diff] [blame] | 7513 | if (cdata && !*b_head(&qc->rx.buf)) { |
| 7514 | /* Consume the remaining data */ |
| 7515 | b_del(&qc->rx.buf, cdata); |
| 7516 | } |
| 7517 | break; |
| 7518 | } |
| 7519 | |
| 7520 | if (pkt->refcnt) |
| 7521 | break; |
| 7522 | |
| 7523 | b_del(&qc->rx.buf, pkt->raw_len); |
| 7524 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 7525 | pool_free(pool_head_quic_rx_packet, pkt); |
| 7526 | } |
| 7527 | |
| 7528 | /* In frequent cases the buffer will be emptied at this stage. */ |
| 7529 | b_realign_if_empty(&qc->rx.buf); |
| 7530 | } |
| 7531 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7532 | /* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied |
| 7533 | * into <qc> receive buffer after header protection removal procedure. |
| 7534 | * |
| 7535 | * <dgram> must be set to the datagram which contains the QUIC packet. <beg> |
| 7536 | * must point to packet buffer first byte. |
| 7537 | * |
| 7538 | * <tasklist_head> may be non-NULL when the caller treat several datagrams for |
| 7539 | * different quic-conn. In this case, each quic-conn tasklet will be appended |
| 7540 | * to it in order to be woken up after the current task. |
| 7541 | * |
| 7542 | * The caller can safely removed the packet data. If packet refcount was not |
| 7543 | * incremented by this function, it means that the connection did not handled |
| 7544 | * it and it should be freed by the caller. |
| 7545 | */ |
| 7546 | static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt, |
| 7547 | struct quic_dgram *dgram, unsigned char *beg, |
| 7548 | struct list **tasklist_head) |
| 7549 | { |
| 7550 | const struct quic_version *qv = pkt->version; |
| 7551 | struct quic_enc_level *qel = NULL; |
| 7552 | size_t b_cspace; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7553 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 7554 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 7555 | TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
Amaury Denoyelle | 3f474e6 | 2022-11-24 17:15:08 +0100 | [diff] [blame] | 7556 | |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 7557 | if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7558 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
| 7559 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 7560 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | a65b71f | 2023-03-03 10:16:32 +0100 | [diff] [blame] | 7561 | TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached", |
| 7562 | QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7563 | /* Reset the anti-amplification bit. It will be set again |
| 7564 | * when sending the next packet if reached again. |
| 7565 | */ |
| 7566 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | a65b71f | 2023-03-03 10:16:32 +0100 | [diff] [blame] | 7567 | qc_set_timer(qc); |
| 7568 | if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 7569 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7570 | } |
| 7571 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7572 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 7573 | TRACE_PROTO("Connection error", |
| 7574 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 7575 | goto out; |
| 7576 | } |
| 7577 | |
| 7578 | pkt->raw_len = pkt->len; |
| 7579 | quic_rx_pkts_del(qc); |
| 7580 | b_cspace = b_contig_space(&qc->rx.buf); |
| 7581 | if (b_cspace < pkt->len) { |
Frédéric Lécaille | 1dbeb35 | 2023-02-07 11:40:21 +0100 | [diff] [blame] | 7582 | TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0, |
Frédéric Lécaille | b7a13be | 2023-02-22 17:24:23 +0100 | [diff] [blame] | 7583 | "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7584 | /* Do not consume buf if space not at the end. */ |
| 7585 | if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) { |
| 7586 | TRACE_PROTO("Packet dropped", |
| 7587 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 7588 | qc->cntrs.dropped_pkt_bufoverrun++; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7589 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7590 | } |
| 7591 | |
| 7592 | /* Let us consume the remaining contiguous space. */ |
| 7593 | if (b_cspace) { |
| 7594 | b_putchr(&qc->rx.buf, 0x00); |
| 7595 | b_cspace--; |
| 7596 | } |
| 7597 | b_add(&qc->rx.buf, b_cspace); |
| 7598 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 7599 | TRACE_PROTO("Too big packet", |
| 7600 | QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv); |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 7601 | qc->cntrs.dropped_pkt_bufoverrun++; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7602 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7603 | } |
| 7604 | } |
| 7605 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 7606 | if (!qc_try_rm_hp(qc, pkt, beg, &qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7607 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 7608 | goto drop; |
| 7609 | } |
| 7610 | |
| 7611 | TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 7612 | if (pkt->aad_len) |
| 7613 | qc_pkt_insert(qc, pkt, qel); |
| 7614 | out: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 7615 | *tasklist_head = tasklet_wakeup_after(*tasklist_head, |
| 7616 | qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7617 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 7618 | drop_silent: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 7619 | TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
| 7620 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7621 | return; |
| 7622 | |
| 7623 | drop: |
Frédéric Lécaille | bdd64fd | 2023-05-24 11:10:19 +0200 | [diff] [blame] | 7624 | qc->cntrs.dropped_pkt++; |
Frédéric Lécaille | 464281a | 2023-05-24 10:24:42 +0200 | [diff] [blame] | 7625 | TRACE_PROTO("packet drop", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 7626 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7627 | } |
| 7628 | |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7629 | /* This function builds into a buffer at <pos> position a QUIC long packet header, |
| 7630 | * <end> being one byte past the end of this buffer. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7631 | * Return 1 if enough room to build this header, 0 if not. |
| 7632 | */ |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7633 | static int quic_build_packet_long_header(unsigned char **pos, const unsigned char *end, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7634 | int type, size_t pn_len, |
| 7635 | struct quic_conn *qc, const struct quic_version *ver) |
| 7636 | { |
| 7637 | int ret = 0; |
| 7638 | |
| 7639 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 7640 | |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7641 | if (end - *pos < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7642 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 7643 | goto leave; |
| 7644 | } |
| 7645 | |
| 7646 | type = quic_pkt_type(type, ver->num); |
| 7647 | /* #0 byte flags */ |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7648 | *(*pos)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7649 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 7650 | /* Version */ |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7651 | quic_write_uint32(pos, end, ver->num); |
| 7652 | *(*pos)++ = qc->dcid.len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7653 | /* Destination connection ID */ |
| 7654 | if (qc->dcid.len) { |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7655 | memcpy(*pos, qc->dcid.data, qc->dcid.len); |
| 7656 | *pos += qc->dcid.len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7657 | } |
| 7658 | /* Source connection ID */ |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7659 | *(*pos)++ = qc->scid.len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7660 | if (qc->scid.len) { |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7661 | memcpy(*pos, qc->scid.data, qc->scid.len); |
| 7662 | *pos += qc->scid.len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7663 | } |
| 7664 | |
| 7665 | ret = 1; |
| 7666 | leave: |
| 7667 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 7668 | return ret; |
| 7669 | } |
| 7670 | |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7671 | /* This function builds into a buffer at <pos> position a QUIC short packet header, |
| 7672 | * <end> being one byte past the end of this buffer. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7673 | * Return 1 if enough room to build this header, 0 if not. |
| 7674 | */ |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7675 | static int quic_build_packet_short_header(unsigned char **pos, const unsigned char *end, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7676 | size_t pn_len, struct quic_conn *qc, |
| 7677 | unsigned char tls_flags) |
| 7678 | { |
| 7679 | int ret = 0; |
Frédéric Lécaille | ece86e6 | 2023-03-07 11:53:43 +0100 | [diff] [blame] | 7680 | unsigned char spin_bit = |
| 7681 | (qc->flags & QUIC_FL_CONN_SPIN_BIT) ? QUIC_PACKET_SPIN_BIT : 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7682 | |
| 7683 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 7684 | |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7685 | if (end - *pos < 1 + qc->dcid.len) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7686 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 7687 | goto leave; |
| 7688 | } |
| 7689 | |
| 7690 | /* #0 byte flags */ |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7691 | *(*pos)++ = QUIC_PACKET_FIXED_BIT | spin_bit | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7692 | ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1); |
| 7693 | /* Destination connection ID */ |
| 7694 | if (qc->dcid.len) { |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7695 | memcpy(*pos, qc->dcid.data, qc->dcid.len); |
| 7696 | *pos += qc->dcid.len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7697 | } |
| 7698 | |
| 7699 | ret = 1; |
| 7700 | leave: |
| 7701 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 7702 | return ret; |
| 7703 | } |
| 7704 | |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7705 | /* Apply QUIC header protection to the packet with <pos> as first byte address, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7706 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 7707 | * with <aead> as AEAD cipher and <key> as secret key. |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 7708 | * |
| 7709 | * TODO no error is expected as encryption is done in place but encryption |
| 7710 | * manual is unclear. <fail> will be set to true if an error is detected. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7711 | */ |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 7712 | void quic_apply_header_protection(struct quic_conn *qc, unsigned char *pos, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7713 | unsigned char *pn, size_t pnlen, |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 7714 | struct quic_tls_ctx *tls_ctx, int *fail) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7715 | |
| 7716 | { |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 7717 | int i; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7718 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 7719 | * and at most 4 bytes for the packet number |
| 7720 | */ |
| 7721 | unsigned char mask[5] = {0}; |
| 7722 | EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx; |
| 7723 | |
| 7724 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 7725 | |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 7726 | *fail = 0; |
| 7727 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7728 | if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) { |
| 7729 | TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc); |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 7730 | *fail = 1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7731 | goto out; |
| 7732 | } |
| 7733 | |
Frédéric Lécaille | 3adb9e8 | 2023-04-24 14:54:48 +0200 | [diff] [blame] | 7734 | *pos ^= mask[0] & (*pos & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7735 | for (i = 0; i < pnlen; i++) |
| 7736 | pn[i] ^= mask[i + 1]; |
| 7737 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7738 | out: |
| 7739 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7740 | } |
| 7741 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7742 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 7743 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 7744 | * with <room> as available room, and <*len> the packet Length field initialized |
| 7745 | * with the number of bytes already present in this buffer which must be taken |
| 7746 | * into an account for the Length packet field value. <headlen> is the number of |
| 7747 | * bytes already present in this packet before building frames. |
| 7748 | * |
| 7749 | * Update consequently <*len> to reflect the size of these frames built |
| 7750 | * by this function. Also attach these frames to <l> frame list. |
| 7751 | * Return 1 if at least one ack-eleciting frame could be built, 0 if not. |
| 7752 | */ |
| 7753 | static inline int qc_build_frms(struct list *outlist, struct list *inlist, |
| 7754 | size_t room, size_t *len, size_t headlen, |
| 7755 | struct quic_enc_level *qel, |
| 7756 | struct quic_conn *qc) |
| 7757 | { |
| 7758 | int ret; |
| 7759 | struct quic_frame *cf, *cfbak; |
| 7760 | |
| 7761 | TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc); |
| 7762 | |
| 7763 | ret = 0; |
| 7764 | if (*len > room) |
| 7765 | goto leave; |
Amaury Denoyelle | 8c94dfc | 2024-06-05 11:37:44 +0200 | [diff] [blame] | 7766 | room -= *len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7767 | |
| 7768 | /* If we are not probing we must take into an account the congestion |
| 7769 | * control window. |
| 7770 | */ |
| 7771 | if (!qel->pktns->tx.pto_probe) { |
| 7772 | size_t remain = quic_path_prep_data(qc->path); |
| 7773 | |
| 7774 | if (headlen > remain) |
| 7775 | goto leave; |
| 7776 | |
| 7777 | room = QUIC_MIN(room, remain - headlen); |
| 7778 | } |
| 7779 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 7780 | TRACE_PROTO("TX frms build (headlen)", |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7781 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
| 7782 | |
| 7783 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 7784 | * returned by this function only if at least one frame could be built |
| 7785 | * in the switch/case block. |
| 7786 | */ |
| 7787 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
| 7788 | /* header length, data length, frame length. */ |
| 7789 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
| 7790 | |
| 7791 | if (!room) |
| 7792 | break; |
| 7793 | |
| 7794 | switch (cf->type) { |
| 7795 | case QUIC_FT_CRYPTO: |
| 7796 | TRACE_DEVEL(" New CRYPTO frame build (room, len)", |
| 7797 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
| 7798 | /* Compute the length of this CRYPTO frame header */ |
| 7799 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
Amaury Denoyelle | 8c94dfc | 2024-06-05 11:37:44 +0200 | [diff] [blame] | 7800 | /* Compute the data length of this CRYPTO frame. */ |
| 7801 | dlen = max_stream_data_size(room, hlen, cf->crypto.len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7802 | TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)", |
| 7803 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen); |
| 7804 | if (!dlen) |
| 7805 | continue; |
| 7806 | |
| 7807 | /* CRYPTO frame length. */ |
| 7808 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 7809 | TRACE_DEVEL(" CRYPTO frame length (flen)", |
| 7810 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 7811 | /* Add the CRYPTO data length and its encoded length to the packet |
| 7812 | * length and the length of this length. |
| 7813 | */ |
| 7814 | *len += flen; |
| 7815 | room -= flen; |
| 7816 | if (dlen == cf->crypto.len) { |
| 7817 | /* <cf> CRYPTO data have been consumed. */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7818 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7819 | LIST_APPEND(outlist, &cf->list); |
| 7820 | } |
| 7821 | else { |
| 7822 | struct quic_frame *new_cf; |
| 7823 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 7824 | new_cf = qc_frm_alloc(QUIC_FT_CRYPTO); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7825 | if (!new_cf) { |
| 7826 | TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
| 7827 | continue; |
| 7828 | } |
| 7829 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7830 | new_cf->crypto.len = dlen; |
| 7831 | new_cf->crypto.offset = cf->crypto.offset; |
| 7832 | new_cf->crypto.qel = qel; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 7833 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7834 | if (cf->origin) { |
| 7835 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 7836 | /* This <cf> frame was duplicated */ |
| 7837 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 7838 | new_cf->origin = cf->origin; |
Frédéric Lécaille | dd41946 | 2023-01-26 15:07:39 +0100 | [diff] [blame] | 7839 | /* Detach the remaining CRYPTO frame from its original frame */ |
| 7840 | LIST_DEL_INIT(&cf->ref); |
| 7841 | cf->origin = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7842 | } |
| 7843 | LIST_APPEND(outlist, &new_cf->list); |
| 7844 | /* Consume <dlen> bytes of the current frame. */ |
| 7845 | cf->crypto.len -= dlen; |
| 7846 | cf->crypto.offset += dlen; |
| 7847 | } |
| 7848 | break; |
| 7849 | |
| 7850 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Amaury Denoyelle | c8a0efb | 2023-02-22 10:44:27 +0100 | [diff] [blame] | 7851 | if (cf->stream.dup) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7852 | struct eb64_node *node = NULL; |
| 7853 | struct qc_stream_desc *stream_desc = NULL; |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 7854 | struct qf_stream *strm_frm = &cf->stream; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7855 | |
| 7856 | /* As this frame has been already lost, ensure the stream is always |
| 7857 | * available or the range of this frame is not consumed before |
| 7858 | * resending it. |
| 7859 | */ |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 7860 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7861 | if (!node) { |
| 7862 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7863 | qc_frm_free(&cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7864 | continue; |
| 7865 | } |
| 7866 | |
| 7867 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 7868 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7869 | TRACE_DEVEL("ignored frame frame in already acked range", |
| 7870 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7871 | qc_frm_free(&cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7872 | continue; |
| 7873 | } |
Amaury Denoyelle | d5f03cd | 2023-04-24 15:32:23 +0200 | [diff] [blame] | 7874 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 7875 | uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key; |
Frédéric Lécaille | ca07979 | 2023-03-17 08:56:50 +0100 | [diff] [blame] | 7876 | |
Frédéric Lécaille | c425e03 | 2023-03-20 14:32:59 +0100 | [diff] [blame] | 7877 | qc_stream_frm_mv_fwd(cf, diff); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7878 | TRACE_DEVEL("updated partially acked frame", |
| 7879 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
| 7880 | } |
| 7881 | } |
| 7882 | /* Note that these frames are accepted in short packets only without |
| 7883 | * "Length" packet field. Here, <*len> is used only to compute the |
| 7884 | * sum of the lengths of the already built frames for this packet. |
| 7885 | * |
| 7886 | * Compute the length of this STREAM frame "header" made a all the field |
| 7887 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 7888 | */ |
| 7889 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
| 7890 | ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0); |
| 7891 | /* Compute the data length of this STREAM frame. */ |
Amaury Denoyelle | 8c94dfc | 2024-06-05 11:37:44 +0200 | [diff] [blame] | 7892 | avail_room = room - hlen; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7893 | if ((ssize_t)avail_room <= 0) |
| 7894 | continue; |
| 7895 | |
| 7896 | TRACE_DEVEL(" New STREAM frame build (room, len)", |
| 7897 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 7898 | |
| 7899 | /* hlen contains STREAM id and offset. Ensure there is |
| 7900 | * enough room for length field. |
| 7901 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7902 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 7903 | dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz), |
| 7904 | cf->stream.len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7905 | dlen_sz = quic_int_getsize(dlen); |
| 7906 | flen = hlen + dlen_sz + dlen; |
| 7907 | } |
| 7908 | else { |
| 7909 | dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len); |
| 7910 | flen = hlen + dlen; |
| 7911 | } |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 7912 | |
| 7913 | if (cf->stream.len && !dlen) { |
| 7914 | /* Only a small gap is left on buffer, not |
| 7915 | * enough to encode the STREAM data length. |
| 7916 | */ |
| 7917 | continue; |
| 7918 | } |
| 7919 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7920 | TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)", |
| 7921 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen); |
| 7922 | TRACE_DEVEL(" STREAM frame length (flen)", |
| 7923 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 7924 | /* Add the STREAM data length and its encoded length to the packet |
| 7925 | * length and the length of this length. |
| 7926 | */ |
| 7927 | *len += flen; |
| 7928 | room -= flen; |
| 7929 | if (dlen == cf->stream.len) { |
| 7930 | /* <cf> STREAM data have been consumed. */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7931 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7932 | LIST_APPEND(outlist, &cf->list); |
| 7933 | |
| 7934 | /* Do not notify MUX on retransmission. */ |
| 7935 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 7936 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 7937 | cf->stream.len, |
| 7938 | cf->stream.offset.key); |
| 7939 | } |
| 7940 | } |
| 7941 | else { |
| 7942 | struct quic_frame *new_cf; |
| 7943 | struct buffer cf_buf; |
| 7944 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 7945 | new_cf = qc_frm_alloc(cf->type); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7946 | if (!new_cf) { |
| 7947 | TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
| 7948 | continue; |
| 7949 | } |
| 7950 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7951 | new_cf->stream.stream = cf->stream.stream; |
| 7952 | new_cf->stream.buf = cf->stream.buf; |
| 7953 | new_cf->stream.id = cf->stream.id; |
Amaury Denoyelle | 1dac018 | 2023-02-02 16:45:07 +0100 | [diff] [blame] | 7954 | new_cf->stream.offset = cf->stream.offset; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7955 | new_cf->stream.len = dlen; |
| 7956 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 7957 | /* FIN bit reset */ |
| 7958 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 7959 | new_cf->stream.data = cf->stream.data; |
Amaury Denoyelle | c8a0efb | 2023-02-22 10:44:27 +0100 | [diff] [blame] | 7960 | new_cf->stream.dup = cf->stream.dup; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 7961 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7962 | if (cf->origin) { |
| 7963 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 7964 | /* This <cf> frame was duplicated */ |
| 7965 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 7966 | new_cf->origin = cf->origin; |
Frédéric Lécaille | dd41946 | 2023-01-26 15:07:39 +0100 | [diff] [blame] | 7967 | /* Detach this STREAM frame from its origin */ |
| 7968 | LIST_DEL_INIT(&cf->ref); |
| 7969 | cf->origin = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7970 | } |
| 7971 | LIST_APPEND(outlist, &new_cf->list); |
| 7972 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 7973 | /* Consume <dlen> bytes of the current frame. */ |
| 7974 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 7975 | b_size(cf->stream.buf), |
| 7976 | (char *)cf->stream.data - b_orig(cf->stream.buf), 0); |
| 7977 | cf->stream.len -= dlen; |
| 7978 | cf->stream.offset.key += dlen; |
| 7979 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
| 7980 | |
| 7981 | /* Do not notify MUX on retransmission. */ |
| 7982 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 7983 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 7984 | new_cf->stream.len, |
| 7985 | new_cf->stream.offset.key); |
| 7986 | } |
| 7987 | } |
| 7988 | |
| 7989 | /* TODO the MUX is notified about the frame sending via |
| 7990 | * previous qcc_streams_sent_done call. However, the |
| 7991 | * sending can fail later, for example if the sendto |
| 7992 | * system call returns an error. As the MUX has been |
| 7993 | * notified, the transport layer is responsible to |
| 7994 | * bufferize and resent the announced data later. |
| 7995 | */ |
| 7996 | |
| 7997 | break; |
| 7998 | |
| 7999 | default: |
| 8000 | flen = qc_frm_len(cf); |
| 8001 | BUG_ON(!flen); |
| 8002 | if (flen > room) |
| 8003 | continue; |
| 8004 | |
| 8005 | *len += flen; |
| 8006 | room -= flen; |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 8007 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8008 | LIST_APPEND(outlist, &cf->list); |
| 8009 | break; |
| 8010 | } |
| 8011 | |
| 8012 | /* Successful status as soon as a frame could be built */ |
| 8013 | ret = 1; |
| 8014 | } |
| 8015 | |
| 8016 | leave: |
| 8017 | TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc); |
| 8018 | return ret; |
| 8019 | } |
| 8020 | |
| 8021 | /* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out> |
| 8022 | * is used as return parameter and should be zero'ed by the caller. |
| 8023 | */ |
| 8024 | static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel, |
| 8025 | struct quic_frame *out) |
| 8026 | { |
| 8027 | /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels |
| 8028 | * |
| 8029 | * A CONNECTION_CLOSE frame should be sent in several packets with |
| 8030 | * different encryption levels depending on the client context. This is |
| 8031 | * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for |
| 8032 | * more details on how to implement it. |
| 8033 | */ |
| 8034 | TRACE_ENTER(QUIC_EV_CONN_BFRM, qc); |
| 8035 | |
| 8036 | |
| 8037 | if (qc->err.app) { |
| 8038 | if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 8039 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 8040 | /* RFC 9000 10.2.3. Immediate Close during the Handshake |
| 8041 | * |
| 8042 | * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake |
| 8043 | * packet could expose application state or be used to alter application |
| 8044 | * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a |
| 8045 | * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or |
| 8046 | * Handshake packets. Otherwise, information about the application |
| 8047 | * state might be revealed. Endpoints MUST clear the value of the |
| 8048 | * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when |
| 8049 | * converting to a CONNECTION_CLOSE of type 0x1c. |
| 8050 | */ |
| 8051 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 8052 | out->connection_close.error_code = QC_ERR_APPLICATION_ERROR; |
| 8053 | out->connection_close.reason_phrase_len = 0; |
| 8054 | } |
| 8055 | else { |
| 8056 | out->type = QUIC_FT_CONNECTION_CLOSE_APP; |
Amaury Denoyelle | 1d40fc9 | 2023-11-28 11:23:41 +0100 | [diff] [blame] | 8057 | out->connection_close_app.error_code = qc->err.code; |
| 8058 | out->connection_close_app.reason_phrase_len = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8059 | } |
| 8060 | } |
| 8061 | else { |
| 8062 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 8063 | out->connection_close.error_code = qc->err.code; |
| 8064 | } |
| 8065 | TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc); |
| 8066 | |
| 8067 | } |
| 8068 | |
| 8069 | /* This function builds a clear packet from <pkt> information (its type) |
| 8070 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 8071 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 8072 | * filling the buffer with as much frames as possible from <frms> list of |
| 8073 | * prebuilt frames. |
| 8074 | * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are |
| 8075 | * reserved so that to ensure there is enough room to build this AEAD TAG after |
| 8076 | * having returned from this function. |
| 8077 | * This function also updates the value of <buf_pn> pointer to point to the packet |
| 8078 | * number field in this packet. <pn_len> will also have the packet number |
| 8079 | * length as value. |
| 8080 | * |
| 8081 | * Return 1 if succeeded (enough room to buile this packet), O if not. |
| 8082 | */ |
| 8083 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 8084 | size_t dglen, struct quic_tx_packet *pkt, |
| 8085 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 8086 | int must_ack, int padding, int cc, int probe, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8087 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 8088 | const struct quic_version *ver, struct list *frms) |
| 8089 | { |
| 8090 | unsigned char *beg, *payload; |
| 8091 | size_t len, len_sz, len_frms, padding_len; |
| 8092 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 8093 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
| 8094 | struct quic_frame cc_frm = { }; |
| 8095 | size_t ack_frm_len, head_len; |
| 8096 | int64_t rx_largest_acked_pn; |
| 8097 | int add_ping_frm; |
| 8098 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 8099 | struct quic_frame *cf; |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 8100 | int ret = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8101 | |
| 8102 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 8103 | |
| 8104 | /* Length field value with CRYPTO frames if present. */ |
| 8105 | len_frms = 0; |
| 8106 | beg = pos; |
| 8107 | /* When not probing, and no immediate close is required, reduce the size of this |
| 8108 | * buffer to respect the congestion controller window. |
| 8109 | * This size will be limited if we have ack-eliciting frames to send from <frms>. |
| 8110 | */ |
| 8111 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
| 8112 | size_t path_room; |
| 8113 | |
| 8114 | path_room = quic_path_prep_data(qc->path); |
| 8115 | if (end - beg > path_room) |
| 8116 | end = beg + path_room; |
| 8117 | } |
| 8118 | |
| 8119 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 8120 | * length field if any. |
| 8121 | */ |
| 8122 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 8123 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 8124 | goto no_room; |
| 8125 | |
| 8126 | end -= QUIC_TLS_TAG_LEN; |
| 8127 | rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn; |
| 8128 | /* packet number length */ |
| 8129 | *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn); |
| 8130 | /* Build the header */ |
| 8131 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
| 8132 | !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) || |
| 8133 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
| 8134 | !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver))) |
| 8135 | goto no_room; |
| 8136 | |
| 8137 | /* Encode the token length (0) for an Initial packet. */ |
Frédéric Lécaille | 45662ef | 2023-04-18 14:42:40 +0200 | [diff] [blame] | 8138 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 8139 | if (end <= pos) |
| 8140 | goto no_room; |
| 8141 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8142 | *pos++ = 0; |
Frédéric Lécaille | 45662ef | 2023-04-18 14:42:40 +0200 | [diff] [blame] | 8143 | } |
| 8144 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8145 | head_len = pos - beg; |
| 8146 | /* Build an ACK frame if required. */ |
| 8147 | ack_frm_len = 0; |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 8148 | /* Do not ack and probe at the same time. */ |
| 8149 | if ((must_ack || (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) && !qel->pktns->tx.pto_probe) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8150 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
| 8151 | BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root)); |
| 8152 | ack_frm.tx_ack.arngs = arngs; |
| 8153 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 8154 | qel->pktns->tx.ack_delay = |
| 8155 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 8156 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 8157 | } |
| 8158 | ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay; |
| 8159 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 8160 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 8161 | * that from here, we do not know if we will have to send a PING frame. |
| 8162 | * This will be decided after having computed the ack-eliciting frames |
| 8163 | * to be added to this packet. |
| 8164 | */ |
Frédéric Lécaille | 9d68c6a | 2023-04-12 20:49:29 +0200 | [diff] [blame] | 8165 | if (end - pos <= 1 + *pn_len) |
| 8166 | goto no_room; |
| 8167 | |
Frédéric Lécaille | 4b2627b | 2023-04-17 13:42:42 +0200 | [diff] [blame] | 8168 | ack_frm_len = qc_frm_len(&ack_frm); |
| 8169 | if (ack_frm_len > end - 1 - *pn_len - pos) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8170 | goto no_room; |
| 8171 | } |
| 8172 | |
| 8173 | /* Length field value without the ack-eliciting frames. */ |
| 8174 | len = ack_frm_len + *pn_len; |
| 8175 | len_frms = 0; |
| 8176 | if (!cc && !LIST_ISEMPTY(frms)) { |
| 8177 | ssize_t room = end - pos; |
| 8178 | |
Frédéric Lécaille | e95e00e | 2023-04-24 10:59:33 +0200 | [diff] [blame] | 8179 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8180 | /* Initialize the length of the frames built below to <len>. |
| 8181 | * If any frame could be successfully built by qc_build_frms(), |
| 8182 | * we will have len_frms > len. |
| 8183 | */ |
| 8184 | len_frms = len; |
| 8185 | if (!qc_build_frms(&frm_list, frms, |
| 8186 | end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | e95e00e | 2023-04-24 10:59:33 +0200 | [diff] [blame] | 8187 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8188 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 30cc665 | 2023-11-07 18:29:28 +0100 | [diff] [blame] | 8189 | if (padding) { |
| 8190 | len_frms = 0; |
| 8191 | goto comp_pkt_len; |
| 8192 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8193 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 8194 | goto no_room; |
| 8195 | } |
| 8196 | } |
| 8197 | |
Frédéric Lécaille | 30cc665 | 2023-11-07 18:29:28 +0100 | [diff] [blame] | 8198 | comp_pkt_len: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8199 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 8200 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 8201 | * for the encryption tag. It must be taken into an account for the length |
| 8202 | * of this packet. |
| 8203 | */ |
| 8204 | if (len_frms) |
| 8205 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 8206 | else |
| 8207 | len += QUIC_TLS_TAG_LEN; |
| 8208 | /* CONNECTION_CLOSE frame */ |
| 8209 | if (cc) { |
| 8210 | qc_build_cc_frm(qc, qel, &cc_frm); |
| 8211 | len += qc_frm_len(&cc_frm); |
| 8212 | } |
| 8213 | add_ping_frm = 0; |
| 8214 | padding_len = 0; |
| 8215 | len_sz = quic_int_getsize(len); |
| 8216 | /* Add this packet size to <dglen> */ |
| 8217 | dglen += head_len + len_sz + len; |
Frédéric Lécaille | ec93721 | 2023-03-03 17:34:41 +0100 | [diff] [blame] | 8218 | /* Note that <padding> is true only when building an Handshake packet |
| 8219 | * coalesced to an Initial packet. |
| 8220 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8221 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 8222 | /* This is a maximum padding size */ |
| 8223 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 8224 | /* The length field value is of this packet is <len> + <padding_len> |
| 8225 | * the size of which may be greater than the initial computed size |
| 8226 | * <len_sz>. So, let's deduce the difference between these to packet |
| 8227 | * sizes from <padding_len>. |
| 8228 | */ |
| 8229 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 8230 | len += padding_len; |
| 8231 | } |
Frédéric Lécaille | 5faf577 | 2023-02-16 17:30:53 +0100 | [diff] [blame] | 8232 | else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) { |
| 8233 | len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms; |
| 8234 | } |
| 8235 | else if (LIST_ISEMPTY(&frm_list)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8236 | if (qel->pktns->tx.pto_probe) { |
| 8237 | /* If we cannot send a frame, we send a PING frame. */ |
| 8238 | add_ping_frm = 1; |
| 8239 | len += 1; |
Frédéric Lécaille | ec93721 | 2023-03-03 17:34:41 +0100 | [diff] [blame] | 8240 | dglen += 1; |
| 8241 | /* Note that only we are in the case where this Initial packet |
| 8242 | * is not coalesced to an Handshake packet. We must directly |
| 8243 | * pad the datragram. |
| 8244 | */ |
Frédéric Lécaille | 9c317b1 | 2023-03-28 15:39:11 +0200 | [diff] [blame] | 8245 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 8246 | if (dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 8247 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 8248 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 8249 | len += padding_len; |
| 8250 | } |
| 8251 | } |
| 8252 | else { |
| 8253 | /* Note that +1 is for the PING frame */ |
| 8254 | if (*pn_len + 1 < QUIC_PACKET_PN_MAXLEN) |
| 8255 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len - 1; |
Frédéric Lécaille | ec93721 | 2023-03-03 17:34:41 +0100 | [diff] [blame] | 8256 | } |
| 8257 | } |
| 8258 | else { |
| 8259 | /* If there is no frame at all to follow, add at least a PADDING frame. */ |
| 8260 | if (!ack_frm_len && !cc) |
| 8261 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8262 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8263 | } |
| 8264 | |
| 8265 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 8266 | goto no_room; |
| 8267 | |
| 8268 | /* Packet number field address. */ |
| 8269 | *buf_pn = pos; |
| 8270 | |
| 8271 | /* Packet number encoding. */ |
| 8272 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 8273 | goto no_room; |
| 8274 | |
| 8275 | /* payload building (ack-eliciting or not frames) */ |
| 8276 | payload = pos; |
| 8277 | if (ack_frm_len) { |
| 8278 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 8279 | goto no_room; |
| 8280 | |
| 8281 | pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns); |
| 8282 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
| 8283 | } |
| 8284 | |
| 8285 | /* Ack-eliciting frames */ |
| 8286 | if (!LIST_ISEMPTY(&frm_list)) { |
| 8287 | struct quic_frame *tmp_cf; |
| 8288 | list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) { |
| 8289 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
| 8290 | ssize_t room = end - pos; |
Frédéric Lécaille | e95e00e | 2023-04-24 10:59:33 +0200 | [diff] [blame] | 8291 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8292 | qc, NULL, NULL, &room); |
| 8293 | /* Note that <cf> was added from <frms> to <frm_list> list by |
| 8294 | * qc_build_frms(). |
| 8295 | */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 8296 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8297 | LIST_INSERT(frms, &cf->list); |
| 8298 | continue; |
| 8299 | } |
| 8300 | |
| 8301 | quic_tx_packet_refinc(pkt); |
| 8302 | cf->pkt = pkt; |
| 8303 | } |
| 8304 | } |
| 8305 | |
| 8306 | /* Build a PING frame if needed. */ |
| 8307 | if (add_ping_frm) { |
| 8308 | frm.type = QUIC_FT_PING; |
| 8309 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 8310 | goto no_room; |
| 8311 | } |
| 8312 | |
| 8313 | /* Build a CONNECTION_CLOSE frame if needed. */ |
| 8314 | if (cc) { |
| 8315 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 8316 | goto no_room; |
| 8317 | |
| 8318 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 8319 | } |
| 8320 | |
| 8321 | /* Build a PADDING frame if needed. */ |
| 8322 | if (padding_len) { |
| 8323 | frm.type = QUIC_FT_PADDING; |
| 8324 | frm.padding.len = padding_len; |
| 8325 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 8326 | goto no_room; |
| 8327 | } |
| 8328 | |
| 8329 | if (pos == payload) { |
| 8330 | /* No payload was built because of congestion control */ |
Frédéric Lécaille | e95e00e | 2023-04-24 10:59:33 +0200 | [diff] [blame] | 8331 | TRACE_PROTO("limited by congestion control", QUIC_EV_CONN_TXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8332 | goto no_room; |
| 8333 | } |
| 8334 | |
| 8335 | /* If this packet is ack-eliciting and we are probing let's |
| 8336 | * decrement the PTO probe counter. |
| 8337 | */ |
Frédéric Lécaille | 0e7f9da | 2023-07-20 15:45:41 +0200 | [diff] [blame] | 8338 | if ((pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8339 | qel->pktns->tx.pto_probe) |
| 8340 | qel->pktns->tx.pto_probe--; |
| 8341 | |
| 8342 | pkt->len = pos - beg; |
| 8343 | LIST_SPLICE(&pkt->frms, &frm_list); |
| 8344 | |
| 8345 | ret = 1; |
Frédéric Lécaille | e95e00e | 2023-04-24 10:59:33 +0200 | [diff] [blame] | 8346 | TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8347 | leave: |
| 8348 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 8349 | return ret; |
| 8350 | |
| 8351 | no_room: |
| 8352 | /* Replace the pre-built frames which could not be add to this packet */ |
| 8353 | LIST_SPLICE(frms, &frm_list); |
Frédéric Lécaille | e95e00e | 2023-04-24 10:59:33 +0200 | [diff] [blame] | 8354 | TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8355 | goto leave; |
| 8356 | } |
| 8357 | |
| 8358 | static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type) |
| 8359 | { |
| 8360 | pkt->type = type; |
| 8361 | pkt->len = 0; |
| 8362 | pkt->in_flight_len = 0; |
| 8363 | pkt->pn_node.key = (uint64_t)-1; |
| 8364 | LIST_INIT(&pkt->frms); |
| 8365 | pkt->time_sent = TICK_ETERNITY; |
| 8366 | pkt->next = NULL; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 8367 | pkt->prev = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8368 | pkt->largest_acked_pn = -1; |
| 8369 | pkt->flags = 0; |
| 8370 | pkt->refcnt = 0; |
| 8371 | } |
| 8372 | |
Frédéric Lécaille | 1e0f825 | 2023-04-24 15:02:34 +0200 | [diff] [blame] | 8373 | /* Build a packet into a buffer at <pos> position, <end> pointing to one byte past |
| 8374 | * the end of this buffer, with <pkt_type> as packet type for <qc> QUIC connection |
| 8375 | * at <qel> encryption level with <frms> list of prebuilt frames. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8376 | * |
Frédéric Lécaille | 2101700 | 2023-11-08 11:31:21 +0100 | [diff] [blame] | 8377 | + * Return -3 if the packet could not be allocated, -2 if could not be encrypted for |
| 8378 | + * any reason, -1 if there was not enough room to build a packet. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8379 | * XXX NOTE XXX |
| 8380 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 8381 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 8382 | * control window limitation. |
| 8383 | */ |
| 8384 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
Frédéric Lécaille | 1e0f825 | 2023-04-24 15:02:34 +0200 | [diff] [blame] | 8385 | const unsigned char *end, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8386 | struct quic_enc_level *qel, |
| 8387 | struct quic_tls_ctx *tls_ctx, struct list *frms, |
| 8388 | struct quic_conn *qc, const struct quic_version *ver, |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 8389 | size_t dglen, int pkt_type, int must_ack, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8390 | int padding, int probe, int cc, int *err) |
| 8391 | { |
| 8392 | struct quic_tx_packet *ret_pkt = NULL; |
| 8393 | /* The pointer to the packet number field. */ |
| 8394 | unsigned char *buf_pn; |
Frédéric Lécaille | 1e0f825 | 2023-04-24 15:02:34 +0200 | [diff] [blame] | 8395 | unsigned char *first_byte, *last_byte, *payload; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8396 | int64_t pn; |
| 8397 | size_t pn_len, payload_len, aad_len; |
| 8398 | struct quic_tx_packet *pkt; |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 8399 | int encrypt_failure = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8400 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 8401 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 8402 | TRACE_PROTO("TX pkt build", QUIC_EV_CONN_TXPKT, qc, NULL, qel); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8403 | *err = 0; |
| 8404 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 8405 | if (!pkt) { |
| 8406 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 2101700 | 2023-11-08 11:31:21 +0100 | [diff] [blame] | 8407 | *err = -3; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8408 | goto err; |
| 8409 | } |
| 8410 | |
| 8411 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | 1e0f825 | 2023-04-24 15:02:34 +0200 | [diff] [blame] | 8412 | first_byte = *pos; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8413 | pn_len = 0; |
| 8414 | buf_pn = NULL; |
| 8415 | |
| 8416 | pn = qel->pktns->tx.next_pn + 1; |
Frédéric Lécaille | 1e0f825 | 2023-04-24 15:02:34 +0200 | [diff] [blame] | 8417 | if (!qc_do_build_pkt(*pos, end, dglen, pkt, pn, &pn_len, &buf_pn, |
Frédéric Lécaille | 45bf1a8 | 2023-04-12 18:51:49 +0200 | [diff] [blame] | 8418 | must_ack, padding, cc, probe, qel, qc, ver, frms)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8419 | // trace already emitted by function above |
| 8420 | *err = -1; |
| 8421 | goto err; |
| 8422 | } |
| 8423 | |
Frédéric Lécaille | 1e0f825 | 2023-04-24 15:02:34 +0200 | [diff] [blame] | 8424 | last_byte = first_byte + pkt->len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8425 | payload = buf_pn + pn_len; |
Frédéric Lécaille | 1e0f825 | 2023-04-24 15:02:34 +0200 | [diff] [blame] | 8426 | payload_len = last_byte - payload; |
| 8427 | aad_len = payload - first_byte; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8428 | |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 8429 | quic_packet_encrypt(payload, payload_len, first_byte, aad_len, pn, tls_ctx, qc, &encrypt_failure); |
| 8430 | if (encrypt_failure) { |
Amaury Denoyelle | 7385ff3 | 2023-04-19 15:56:30 +0200 | [diff] [blame] | 8431 | /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */ |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 8432 | WARN_ON("quic_packet_encrypt failure"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8433 | *err = -2; |
| 8434 | goto err; |
| 8435 | } |
| 8436 | |
Frédéric Lécaille | 1e0f825 | 2023-04-24 15:02:34 +0200 | [diff] [blame] | 8437 | last_byte += QUIC_TLS_TAG_LEN; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8438 | pkt->len += QUIC_TLS_TAG_LEN; |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 8439 | quic_apply_header_protection(qc, first_byte, buf_pn, pn_len, tls_ctx, &encrypt_failure); |
| 8440 | if (encrypt_failure) { |
Amaury Denoyelle | 7385ff3 | 2023-04-19 15:56:30 +0200 | [diff] [blame] | 8441 | /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */ |
Amaury Denoyelle | f8fbb0b | 2023-05-16 18:23:37 +0200 | [diff] [blame] | 8442 | WARN_ON("quic_apply_header_protection failure"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8443 | *err = -2; |
| 8444 | goto err; |
| 8445 | } |
| 8446 | |
| 8447 | /* Consume a packet number */ |
| 8448 | qel->pktns->tx.next_pn++; |
| 8449 | qc->tx.prep_bytes += pkt->len; |
| 8450 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) { |
| 8451 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 8452 | TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc); |
| 8453 | } |
Frédéric Lécaille | 1e0f825 | 2023-04-24 15:02:34 +0200 | [diff] [blame] | 8454 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8455 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
Frédéric Lécaille | 1e0f825 | 2023-04-24 15:02:34 +0200 | [diff] [blame] | 8456 | *pos = last_byte; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8457 | /* Attach the built packet to its tree. */ |
| 8458 | pkt->pn_node.key = pn; |
| 8459 | /* Set the packet in fligth length for in flight packet only. */ |
| 8460 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
| 8461 | pkt->in_flight_len = pkt->len; |
| 8462 | qc->path->prep_in_flight += pkt->len; |
| 8463 | } |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 8464 | /* Always reset this flag */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8465 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 8466 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 8467 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 8468 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 8469 | qc->flags &= ~QUIC_FL_CONN_ACK_TIMER_FIRED; |
| 8470 | if (tick_isset(qc->ack_expire)) { |
| 8471 | qc->ack_expire = TICK_ETERNITY; |
| 8472 | qc->idle_timer_task->expire = qc->idle_expire; |
| 8473 | task_queue(qc->idle_timer_task); |
Frédéric Lécaille | 495968e | 2023-04-03 17:42:05 +0200 | [diff] [blame] | 8474 | TRACE_PROTO("ack timer cancelled", QUIC_EV_CONN_IDLE_TIMER, qc); |
Frédéric Lécaille | d721571 | 2023-03-24 18:13:37 +0100 | [diff] [blame] | 8475 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8476 | } |
| 8477 | |
| 8478 | pkt->pktns = qel->pktns; |
| 8479 | |
| 8480 | ret_pkt = pkt; |
| 8481 | leave: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 8482 | TRACE_PROTO("TX pkt built", QUIC_EV_CONN_TXPKT, qc, ret_pkt); |
| 8483 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8484 | return ret_pkt; |
| 8485 | |
| 8486 | err: |
| 8487 | /* TODO: what about the frames which have been built |
| 8488 | * for this packet. |
| 8489 | */ |
| 8490 | free_quic_tx_packet(qc, pkt); |
| 8491 | goto leave; |
| 8492 | } |
| 8493 | |
| 8494 | |
| 8495 | static void __quic_conn_init(void) |
| 8496 | { |
| 8497 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 8498 | } |
| 8499 | INITCALL0(STG_REGISTER, __quic_conn_init); |
| 8500 | |
| 8501 | static void __quic_conn_deinit(void) |
| 8502 | { |
| 8503 | BIO_meth_free(ha_quic_meth); |
| 8504 | } |
| 8505 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
| 8506 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8507 | /* Handle a new <dgram> received. Parse each QUIC packets and copied their |
| 8508 | * content to a quic-conn instance. The datagram content can be released after |
| 8509 | * this function. |
| 8510 | * |
| 8511 | * If datagram has been received on a quic-conn owned FD, <from_qc> must be set |
| 8512 | * to the connection instance. <li> is the attached listener. The caller is |
| 8513 | * responsible to ensure that the first packet is destined to this connection |
| 8514 | * by comparing CIDs. |
| 8515 | * |
| 8516 | * If datagram has been received on a receiver FD, <from_qc> will be NULL. This |
| 8517 | * function will thus retrieve the connection from the CID tree or allocate a |
| 8518 | * new one if possible. <li> is the listener attached to the receiver. |
| 8519 | * |
| 8520 | * Returns 0 on success else non-zero. If an error happens, some packets from |
| 8521 | * the datagram may not have been parsed. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8522 | */ |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8523 | int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc, |
| 8524 | struct listener *li) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8525 | { |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8526 | struct quic_rx_packet *pkt; |
| 8527 | struct quic_conn *qc = NULL; |
| 8528 | unsigned char *pos, *end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8529 | struct list *tasklist_head = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8530 | |
| 8531 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 8532 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8533 | pos = dgram->buf; |
| 8534 | end = pos + dgram->len; |
| 8535 | do { |
| 8536 | /* TODO replace zalloc -> alloc. */ |
| 8537 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 8538 | if (!pkt) { |
| 8539 | TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT); |
| 8540 | goto err; |
| 8541 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8542 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8543 | pkt->version = NULL; |
| 8544 | pkt->pn_offset = 0; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 8545 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8546 | /* Set flag if pkt is the first one in dgram. */ |
| 8547 | if (pos == dgram->buf) |
| 8548 | pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 8549 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8550 | LIST_INIT(&pkt->qc_rx_pkt_list); |
| 8551 | pkt->time_received = now_ms; |
| 8552 | quic_rx_packet_refinc(pkt); |
| 8553 | if (quic_rx_pkt_parse(pkt, pos, end, dgram, li)) |
| 8554 | goto next; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 8555 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8556 | /* Search quic-conn instance for first packet of the datagram. |
| 8557 | * quic_rx_packet_parse() is responsible to discard packets |
| 8558 | * with different DCID as the first one in the same datagram. |
| 8559 | */ |
| 8560 | if (!qc) { |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 8561 | int new_tid = -1; |
| 8562 | |
| 8563 | qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li, &new_tid); |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8564 | /* qc is NULL if receiving a non Initial packet for an |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8565 | * unknown connection or on connection affinity rebind. |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8566 | */ |
| 8567 | if (!qc) { |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 8568 | if (new_tid >= 0) { |
| 8569 | MT_LIST_APPEND(&quic_dghdlrs[new_tid].dgrams, |
| 8570 | &dgram->handler_list); |
| 8571 | tasklet_wakeup(quic_dghdlrs[new_tid].task); |
Frédéric Lécaille | 4f40113 | 2023-11-22 16:29:08 +0100 | [diff] [blame] | 8572 | pool_free(pool_head_quic_rx_packet, pkt); |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 8573 | goto out; |
| 8574 | } |
| 8575 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 8576 | /* Skip the entire datagram. */ |
| 8577 | pkt->len = end - pos; |
| 8578 | goto next; |
| 8579 | } |
| 8580 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8581 | dgram->qc = qc; |
| 8582 | } |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 8583 | |
Amaury Denoyelle | 8fc267b | 2023-11-20 14:56:49 +0100 | [diff] [blame] | 8584 | /* Ensure thread connection migration is finalized ASAP. */ |
Amaury Denoyelle | d6646dd | 2023-04-26 17:15:37 +0200 | [diff] [blame] | 8585 | if (qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED) |
| 8586 | qc_finalize_affinity_rebind(qc); |
| 8587 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8588 | if (qc_rx_check_closing(qc, pkt)) { |
| 8589 | /* Skip the entire datagram. */ |
| 8590 | pkt->len = end - pos; |
| 8591 | goto next; |
| 8592 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8593 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 8594 | /* Detect QUIC connection migration. */ |
Frédéric Lécaille | f676954 | 2023-01-12 10:36:26 +0100 | [diff] [blame] | 8595 | if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) { |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 8596 | if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) { |
| 8597 | /* Skip the entire datagram. */ |
| 8598 | TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc); |
| 8599 | pkt->len = end - pos; |
| 8600 | goto next; |
| 8601 | } |
| 8602 | } |
| 8603 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8604 | qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8605 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8606 | next: |
| 8607 | pos += pkt->len; |
| 8608 | quic_rx_packet_refdec(pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8609 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8610 | /* Free rejected packets */ |
| 8611 | if (!pkt->refcnt) { |
| 8612 | BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list)); |
| 8613 | pool_free(pool_head_quic_rx_packet, pkt); |
| 8614 | } |
| 8615 | } while (pos < end); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8616 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8617 | /* Increasing the received bytes counter by the UDP datagram length |
| 8618 | * if this datagram could be associated to a connection. |
| 8619 | */ |
| 8620 | if (dgram->qc) |
| 8621 | dgram->qc->rx.bytes += dgram->len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8622 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8623 | /* This must never happen. */ |
| 8624 | BUG_ON(pos > end); |
| 8625 | BUG_ON(pos < end || pos > dgram->buf + dgram->len); |
| 8626 | /* Mark this datagram as consumed */ |
| 8627 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8628 | |
Amaury Denoyelle | f16ec34 | 2023-04-13 17:42:34 +0200 | [diff] [blame] | 8629 | out: |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8630 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 8631 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8632 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8633 | err: |
Amaury Denoyelle | a65dd3a | 2023-04-19 14:26:16 +0200 | [diff] [blame] | 8634 | /* Mark this datagram as consumed as maybe at least some packets were parsed. */ |
| 8635 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8636 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 8637 | return -1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8638 | } |
| 8639 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 8640 | /* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local |
| 8641 | * CIDs. This can be used to determine if a datagram is addressed to the right |
| 8642 | * connection instance. |
| 8643 | * |
| 8644 | * Returns a boolean value. |
| 8645 | */ |
| 8646 | int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len) |
| 8647 | { |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 8648 | const uchar idx = _quic_cid_tree_idx(dcid); |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 8649 | struct quic_connection_id *conn_id; |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 8650 | struct ebmb_node *node = NULL; |
| 8651 | struct quic_cid_tree *tree = &quic_cid_trees[idx]; |
Willy Tarreau | 9203357 | 2024-06-30 06:23:30 +0200 | [diff] [blame] | 8652 | int ret; |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 8653 | |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 8654 | /* Test against our default CID or client ODCID. */ |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 8655 | if ((qc->scid.len == dcid_len && |
| 8656 | memcmp(qc->scid.data, dcid, dcid_len) == 0) || |
| 8657 | (qc->odcid.len == dcid_len && |
Frédéric Lécaille | 07846cb | 2023-02-13 16:14:24 +0100 | [diff] [blame] | 8658 | memcmp(qc->odcid.data, dcid, dcid_len) == 0)) { |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 8659 | return 1; |
| 8660 | } |
| 8661 | |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 8662 | /* Test against our other CIDs. This can happen if the client has |
| 8663 | * decided to switch to a new one. |
| 8664 | * |
| 8665 | * TODO to avoid locking, loop through qc.cids as an alternative. |
| 8666 | * |
| 8667 | * TODO set it to our default CID to avoid this operation next time. |
| 8668 | */ |
Willy Tarreau | 9203357 | 2024-06-30 06:23:30 +0200 | [diff] [blame] | 8669 | ret = 0; |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 8670 | HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock); |
| 8671 | node = ebmb_lookup(&tree->root, dcid, dcid_len); |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 8672 | if (node) { |
Amaury Denoyelle | 591e798 | 2023-04-12 10:04:49 +0200 | [diff] [blame] | 8673 | conn_id = ebmb_entry(node, struct quic_connection_id, node); |
| 8674 | if (qc == conn_id->qc) |
Willy Tarreau | 9203357 | 2024-06-30 06:23:30 +0200 | [diff] [blame] | 8675 | ret = 1; |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 8676 | } |
Amaury Denoyelle | 60938b7 | 2024-06-27 18:15:08 +0200 | [diff] [blame] | 8677 | HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock); |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 8678 | |
Willy Tarreau | 9203357 | 2024-06-30 06:23:30 +0200 | [diff] [blame] | 8679 | return ret; |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 8680 | } |
| 8681 | |
Willy Tarreau | dd9f921 | 2023-05-07 07:07:44 +0200 | [diff] [blame] | 8682 | /* Retrieve the DCID from a QUIC datagram or packet at <pos> position, |
Frédéric Lécaille | 182934d | 2023-04-24 15:24:58 +0200 | [diff] [blame] | 8683 | * <end> being at one byte past the end of this datagram. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8684 | * Returns 1 if succeeded, 0 if not. |
| 8685 | */ |
Frédéric Lécaille | 182934d | 2023-04-24 15:24:58 +0200 | [diff] [blame] | 8686 | int quic_get_dgram_dcid(unsigned char *pos, const unsigned char *end, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8687 | unsigned char **dcid, size_t *dcid_len) |
| 8688 | { |
| 8689 | int ret = 0, long_header; |
| 8690 | size_t minlen, skip; |
| 8691 | |
| 8692 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 8693 | |
Frédéric Lécaille | 182934d | 2023-04-24 15:24:58 +0200 | [diff] [blame] | 8694 | if (!(*pos & QUIC_PACKET_FIXED_BIT)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8695 | TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT); |
| 8696 | goto err; |
| 8697 | } |
| 8698 | |
Frédéric Lécaille | 182934d | 2023-04-24 15:24:58 +0200 | [diff] [blame] | 8699 | long_header = *pos & QUIC_PACKET_LONG_HEADER_BIT; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8700 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 8701 | QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN; |
| 8702 | skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF; |
Frédéric Lécaille | 182934d | 2023-04-24 15:24:58 +0200 | [diff] [blame] | 8703 | if (end - pos < minlen) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8704 | goto err; |
| 8705 | |
Frédéric Lécaille | 182934d | 2023-04-24 15:24:58 +0200 | [diff] [blame] | 8706 | pos += skip; |
| 8707 | *dcid_len = long_header ? *pos++ : QUIC_HAP_CID_LEN; |
| 8708 | if (*dcid_len > QUIC_CID_MAXLEN || end - pos <= *dcid_len) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8709 | goto err; |
| 8710 | |
Frédéric Lécaille | 182934d | 2023-04-24 15:24:58 +0200 | [diff] [blame] | 8711 | *dcid = pos; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8712 | |
| 8713 | ret = 1; |
| 8714 | leave: |
| 8715 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 8716 | return ret; |
| 8717 | |
| 8718 | err: |
| 8719 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT); |
| 8720 | goto leave; |
| 8721 | } |
| 8722 | |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 8723 | /* Notify upper layer of a fatal error which forces to close the connection. */ |
| 8724 | void qc_notify_err(struct quic_conn *qc) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8725 | { |
| 8726 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 8727 | |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 8728 | if (qc->mux_state == QC_MUX_READY) { |
| 8729 | TRACE_STATE("error notified to mux", QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8730 | |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 8731 | /* Mark socket as closed. */ |
| 8732 | qc->conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 8733 | |
| 8734 | /* TODO quic-conn layer must stay active until MUX is released. |
| 8735 | * Thus, we have to wake up directly to ensure upper stream |
| 8736 | * layer will be notified of the error. If a proper separation |
| 8737 | * is made between MUX and quic-conn layer, wake up could be |
| 8738 | * conducted only with qc.subs. |
| 8739 | */ |
| 8740 | tasklet_wakeup(qc->qcc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8741 | } |
Amaury Denoyelle | b2e31d3 | 2023-05-10 11:57:40 +0200 | [diff] [blame] | 8742 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8743 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 8744 | } |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8745 | |
Amaury Denoyelle | 8afe4b8 | 2023-03-21 11:39:24 +0100 | [diff] [blame] | 8746 | /* Wake-up upper layer for sending if all conditions are met : |
| 8747 | * - room in congestion window or probe packet to sent |
| 8748 | * - socket FD ready to sent or listener socket used |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 8749 | * |
| 8750 | * Returns 1 if upper layer has been woken up else 0. |
| 8751 | */ |
| 8752 | int qc_notify_send(struct quic_conn *qc) |
| 8753 | { |
Amaury Denoyelle | 8afe4b8 | 2023-03-21 11:39:24 +0100 | [diff] [blame] | 8754 | const struct quic_pktns *pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT]; |
| 8755 | |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 8756 | if (qc->subs && qc->subs->events & SUB_RETRY_SEND) { |
Amaury Denoyelle | 8afe4b8 | 2023-03-21 11:39:24 +0100 | [diff] [blame] | 8757 | /* RFC 9002 7.5. Probe Timeout |
| 8758 | * |
| 8759 | * Probe packets MUST NOT be blocked by the congestion controller. |
| 8760 | */ |
| 8761 | if ((quic_path_prep_data(qc->path) || pktns->tx.pto_probe) && |
Amaury Denoyelle | caa1654 | 2023-02-28 15:11:26 +0100 | [diff] [blame] | 8762 | (!qc_test_fd(qc) || !fd_send_active(qc->fd))) { |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 8763 | tasklet_wakeup(qc->subs->tasklet); |
| 8764 | qc->subs->events &= ~SUB_RETRY_SEND; |
| 8765 | if (!qc->subs->events) |
| 8766 | qc->subs = NULL; |
| 8767 | |
| 8768 | return 1; |
| 8769 | } |
| 8770 | } |
| 8771 | |
| 8772 | return 0; |
| 8773 | } |
| 8774 | |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8775 | /* Move a <qc> QUIC connection and its resources from the current thread to the |
Willy Tarreau | 77d37b0 | 2023-04-20 19:03:49 +0200 | [diff] [blame] | 8776 | * new one <new_tid> optionally in association with <new_li> (since it may need |
| 8777 | * to change when migrating to a thread from a different group, otherwise leave |
| 8778 | * it NULL). After this call, the connection cannot be dereferenced anymore on |
| 8779 | * the current thread. |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8780 | * |
| 8781 | * Returns 0 on success else non-zero. |
| 8782 | */ |
Willy Tarreau | 77d37b0 | 2023-04-20 19:03:49 +0200 | [diff] [blame] | 8783 | int qc_set_tid_affinity(struct quic_conn *qc, uint new_tid, struct listener *new_li) |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8784 | { |
| 8785 | struct task *t1 = NULL, *t2 = NULL; |
| 8786 | struct tasklet *t3 = NULL; |
| 8787 | |
| 8788 | struct quic_connection_id *conn_id; |
| 8789 | struct eb64_node *node; |
| 8790 | |
| 8791 | TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc); |
| 8792 | |
| 8793 | /* Pre-allocate all required resources. This ensures we do not left a |
| 8794 | * connection with only some of its field rebinded. |
| 8795 | */ |
| 8796 | if (((t1 = task_new_on(new_tid)) == NULL) || |
| 8797 | (qc->timer_task && (t2 = task_new_on(new_tid)) == NULL) || |
| 8798 | (t3 = tasklet_new()) == NULL) { |
| 8799 | goto err; |
| 8800 | } |
| 8801 | |
| 8802 | /* Reinit idle timer task. */ |
| 8803 | task_kill(qc->idle_timer_task); |
| 8804 | t1->expire = qc->idle_timer_task->expire; |
| 8805 | qc->idle_timer_task = t1; |
| 8806 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 8807 | qc->idle_timer_task->context = qc; |
| 8808 | |
| 8809 | /* Reinit timer task if allocated. */ |
| 8810 | if (qc->timer_task) { |
| 8811 | task_kill(qc->timer_task); |
| 8812 | qc->timer_task = t2; |
| 8813 | qc->timer_task->process = qc_process_timer; |
| 8814 | qc->timer_task->context = qc; |
| 8815 | } |
| 8816 | |
| 8817 | /* Reinit IO tasklet. */ |
Amaury Denoyelle | 739de3f | 2023-04-11 14:42:31 +0200 | [diff] [blame] | 8818 | if (qc->wait_event.tasklet->state & TASK_IN_LIST) |
| 8819 | qc->flags |= QUIC_FL_CONN_IO_TO_REQUEUE; |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8820 | tasklet_kill(qc->wait_event.tasklet); |
| 8821 | /* In most cases quic_conn_app_io_cb is used but for 0-RTT quic_conn_io_cb can be still activated. */ |
| 8822 | t3->process = qc->wait_event.tasklet->process; |
| 8823 | qc->wait_event.tasklet = t3; |
| 8824 | qc->wait_event.tasklet->tid = new_tid; |
| 8825 | qc->wait_event.tasklet->context = qc; |
| 8826 | qc->wait_event.events = 0; |
| 8827 | |
| 8828 | /* Rebind the connection FD. */ |
| 8829 | if (qc_test_fd(qc)) { |
Amaury Denoyelle | 739de3f | 2023-04-11 14:42:31 +0200 | [diff] [blame] | 8830 | /* Reading is reactivated by the new thread. */ |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8831 | fd_migrate_on(qc->fd, new_tid); |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8832 | } |
| 8833 | |
Amaury Denoyelle | 7b516d3 | 2023-04-26 16:12:12 +0200 | [diff] [blame] | 8834 | /* Remove conn from per-thread list instance. It will be hidden from |
| 8835 | * "show quic" until rebinding is completed. |
| 8836 | */ |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8837 | qc_detach_th_ctx_list(qc, 0); |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8838 | |
| 8839 | node = eb64_first(&qc->cids); |
Amaury Denoyelle | b75338e | 2024-03-04 18:41:39 +0100 | [diff] [blame] | 8840 | /* One and only one CID must be present before affinity rebind. |
| 8841 | * |
| 8842 | * This could be triggered fairly easily if tasklet is scheduled just |
| 8843 | * before thread migration for post-handshake state to generate new |
| 8844 | * CIDs. In this case, QUIC_FL_CONN_IO_TO_REQUEUE should be used |
| 8845 | * instead of tasklet_wakeup(). |
| 8846 | */ |
| 8847 | BUG_ON(!node || eb64_next(node)); |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8848 | conn_id = eb64_entry(node, struct quic_connection_id, seq_num); |
Willy Tarreau | 77d37b0 | 2023-04-20 19:03:49 +0200 | [diff] [blame] | 8849 | |
| 8850 | /* At this point no connection was accounted for yet on this |
| 8851 | * listener so it's OK to just swap the pointer. |
| 8852 | */ |
| 8853 | if (new_li && new_li != qc->li) |
| 8854 | qc->li = new_li; |
| 8855 | |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8856 | /* Rebinding is considered done when CID points to the new thread. No |
| 8857 | * access should be done to quic-conn instance after it. |
| 8858 | */ |
Amaury Denoyelle | d6646dd | 2023-04-26 17:15:37 +0200 | [diff] [blame] | 8859 | qc->flags |= QUIC_FL_CONN_AFFINITY_CHANGED; |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8860 | HA_ATOMIC_STORE(&conn_id->tid, new_tid); |
| 8861 | qc = NULL; |
| 8862 | |
| 8863 | TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, NULL); |
| 8864 | return 0; |
| 8865 | |
| 8866 | err: |
| 8867 | task_destroy(t1); |
| 8868 | task_destroy(t2); |
Tim Duesterhus | b1ec21d | 2023-04-22 17:47:32 +0200 | [diff] [blame] | 8869 | tasklet_free(t3); |
Amaury Denoyelle | 25174d5 | 2023-04-05 17:52:05 +0200 | [diff] [blame] | 8870 | |
| 8871 | TRACE_DEVEL("leaving on error", QUIC_EV_CONN_SET_AFFINITY, qc); |
| 8872 | return 1; |
| 8873 | } |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8874 | |
Amaury Denoyelle | 739de3f | 2023-04-11 14:42:31 +0200 | [diff] [blame] | 8875 | /* Must be called after qc_set_tid_affinity() on the new thread. */ |
| 8876 | void qc_finalize_affinity_rebind(struct quic_conn *qc) |
| 8877 | { |
| 8878 | TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc); |
| 8879 | |
Amaury Denoyelle | d6646dd | 2023-04-26 17:15:37 +0200 | [diff] [blame] | 8880 | /* This function must not be called twice after an affinity rebind. */ |
| 8881 | BUG_ON(!(qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED)); |
| 8882 | qc->flags &= ~QUIC_FL_CONN_AFFINITY_CHANGED; |
| 8883 | |
Amaury Denoyelle | 7b516d3 | 2023-04-26 16:12:12 +0200 | [diff] [blame] | 8884 | /* A connection must not pass to closing state until affinity rebind |
| 8885 | * is completed. Else quic_handle_stopping() may miss it during process |
| 8886 | * stopping cleanup. |
| 8887 | */ |
| 8888 | BUG_ON(qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)); |
| 8889 | |
| 8890 | /* Reinsert connection in ha_thread_ctx global list. */ |
| 8891 | LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx); |
| 8892 | qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch); |
| 8893 | |
Amaury Denoyelle | 739de3f | 2023-04-11 14:42:31 +0200 | [diff] [blame] | 8894 | /* Reactivate FD polling if connection socket is active. */ |
| 8895 | qc_want_recv(qc); |
| 8896 | |
| 8897 | /* Reactivate timer task if needed. */ |
| 8898 | qc_set_timer(qc); |
| 8899 | |
| 8900 | /* Idle timer task is always active. */ |
| 8901 | task_queue(qc->idle_timer_task); |
| 8902 | |
| 8903 | /* Reactivate IO tasklet if needed. */ |
| 8904 | if (qc->flags & QUIC_FL_CONN_IO_TO_REQUEUE) { |
| 8905 | tasklet_wakeup(qc->wait_event.tasklet); |
| 8906 | qc->flags &= ~QUIC_FL_CONN_IO_TO_REQUEUE; |
| 8907 | } |
| 8908 | |
| 8909 | TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, qc); |
| 8910 | } |
| 8911 | |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 8912 | enum quic_dump_format { |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 8913 | QUIC_DUMP_FMT_ONELINE, |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 8914 | QUIC_DUMP_FMT_FULL, |
| 8915 | }; |
| 8916 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8917 | /* appctx context used by "show quic" command */ |
| 8918 | struct show_quic_ctx { |
| 8919 | unsigned int epoch; |
| 8920 | struct bref bref; /* back-reference to the quic-conn being dumped */ |
| 8921 | unsigned int thr; |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 8922 | int flags; |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 8923 | enum quic_dump_format format; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8924 | }; |
| 8925 | |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 8926 | #define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */ |
| 8927 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8928 | static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private) |
| 8929 | { |
| 8930 | struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 8931 | int argc = 2; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8932 | |
| 8933 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 8934 | return 1; |
| 8935 | |
| 8936 | ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1); |
| 8937 | ctx->thr = 0; |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 8938 | ctx->flags = 0; |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 8939 | ctx->format = QUIC_DUMP_FMT_ONELINE; |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 8940 | |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 8941 | if (strcmp(args[argc], "oneline") == 0) { |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 8942 | /* format already used as default value */ |
| 8943 | ++argc; |
| 8944 | } |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 8945 | else if (strcmp(args[argc], "full") == 0) { |
| 8946 | ctx->format = QUIC_DUMP_FMT_FULL; |
| 8947 | ++argc; |
| 8948 | } |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8949 | |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 8950 | while (*args[argc]) { |
| 8951 | if (strcmp(args[argc], "all") == 0) |
| 8952 | ctx->flags |= QC_CLI_FL_SHOW_ALL; |
| 8953 | |
| 8954 | ++argc; |
| 8955 | } |
Amaury Denoyelle | 10a46de | 2023-02-09 18:18:45 +0100 | [diff] [blame] | 8956 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8957 | LIST_INIT(&ctx->bref.users); |
| 8958 | |
| 8959 | return 0; |
| 8960 | } |
| 8961 | |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 8962 | /* Dump for "show quic" with "oneline" format. */ |
| 8963 | static void dump_quic_oneline(struct show_quic_ctx *ctx, struct quic_conn *qc) |
| 8964 | { |
| 8965 | char bufaddr[INET6_ADDRSTRLEN], bufport[6]; |
Amaury Denoyelle | aa39cc9 | 2023-05-22 10:57:56 +0200 | [diff] [blame] | 8966 | int ret; |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 8967 | unsigned char cid_len; |
| 8968 | |
Amaury Denoyelle | aa39cc9 | 2023-05-22 10:57:56 +0200 | [diff] [blame] | 8969 | ret = chunk_appendf(&trash, "%p[%02u]/%-.12s ", qc, ctx->thr, |
| 8970 | qc->li->bind_conf->frontend->id); |
| 8971 | chunk_appendf(&trash, "%*s", 36 - ret, " "); /* align output */ |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 8972 | |
| 8973 | /* State */ |
| 8974 | if (qc->flags & QUIC_FL_CONN_CLOSING) |
| 8975 | chunk_appendf(&trash, "CLOSE "); |
| 8976 | else if (qc->flags & QUIC_FL_CONN_DRAINING) |
| 8977 | chunk_appendf(&trash, "DRAIN "); |
| 8978 | else if (qc->state < QUIC_HS_ST_COMPLETE) |
| 8979 | chunk_appendf(&trash, "HDSHK "); |
| 8980 | else |
| 8981 | chunk_appendf(&trash, "ESTAB "); |
| 8982 | |
| 8983 | /* Bytes in flight / Lost packets */ |
| 8984 | chunk_appendf(&trash, "%9llu %6llu %6llu ", |
| 8985 | (ullong)qc->path->in_flight, |
| 8986 | (ullong)qc->path->ifae_pkts, |
| 8987 | (ullong)qc->path->loss.nb_lost_pkt); |
| 8988 | |
| 8989 | /* Socket */ |
| 8990 | if (qc->local_addr.ss_family == AF_INET || |
| 8991 | qc->local_addr.ss_family == AF_INET6) { |
Frédéric Lécaille | 8f8e3fa | 2023-06-14 09:17:20 +0200 | [diff] [blame] | 8992 | addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr)); |
| 8993 | port_to_str(&qc->local_addr, bufport, sizeof(bufport)); |
| 8994 | chunk_appendf(&trash, "%15s:%-5s ", bufaddr, bufport); |
| 8995 | |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 8996 | addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr)); |
| 8997 | port_to_str(&qc->peer_addr, bufport, sizeof(bufport)); |
Amaury Denoyelle | aa39cc9 | 2023-05-22 10:57:56 +0200 | [diff] [blame] | 8998 | chunk_appendf(&trash, "%15s:%-5s ", bufaddr, bufport); |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 8999 | |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 9000 | } |
| 9001 | |
| 9002 | /* CIDs */ |
| 9003 | for (cid_len = 0; cid_len < qc->scid.len; ++cid_len) |
| 9004 | chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]); |
| 9005 | |
| 9006 | chunk_appendf(&trash, " "); |
| 9007 | for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len) |
| 9008 | chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]); |
| 9009 | |
| 9010 | chunk_appendf(&trash, "\n"); |
| 9011 | } |
| 9012 | |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 9013 | /* Dump for "show quic" with "full" format. */ |
| 9014 | static void dump_quic_full(struct show_quic_ctx *ctx, struct quic_conn *qc) |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9015 | { |
Frédéric Lécaille | a3772e1 | 2023-03-21 13:42:43 +0100 | [diff] [blame] | 9016 | struct quic_pktns *pktns; |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 9017 | struct eb64_node *node; |
| 9018 | struct qc_stream_desc *stream; |
Amaury Denoyelle | b89c0e2 | 2023-02-01 17:04:26 +0100 | [diff] [blame] | 9019 | char bufaddr[INET6_ADDRSTRLEN], bufport[6]; |
Frédéric Lécaille | a73563b | 2023-05-25 16:10:03 +0200 | [diff] [blame] | 9020 | int expire, i, addnl; |
Amaury Denoyelle | 58d9d5d | 2023-02-01 11:54:43 +0100 | [diff] [blame] | 9021 | unsigned char cid_len; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9022 | |
Frédéric Lécaille | a73563b | 2023-05-25 16:10:03 +0200 | [diff] [blame] | 9023 | addnl = 0; |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 9024 | /* CIDs */ |
| 9025 | chunk_appendf(&trash, "* %p[%02u]: scid=", qc, ctx->thr); |
| 9026 | for (cid_len = 0; cid_len < qc->scid.len; ++cid_len) |
| 9027 | chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]); |
| 9028 | while (cid_len++ < 20) |
| 9029 | chunk_appendf(&trash, ".."); |
| 9030 | |
| 9031 | chunk_appendf(&trash, " dcid="); |
| 9032 | for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len) |
| 9033 | chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]); |
| 9034 | while (cid_len++ < 20) |
| 9035 | chunk_appendf(&trash, ".."); |
| 9036 | |
| 9037 | chunk_appendf(&trash, "\n"); |
| 9038 | |
| 9039 | chunk_appendf(&trash, " loc. TPs:"); |
| 9040 | quic_transport_params_dump(&trash, qc, &qc->rx.params); |
| 9041 | chunk_appendf(&trash, "\n"); |
| 9042 | chunk_appendf(&trash, " rem. TPs:"); |
| 9043 | quic_transport_params_dump(&trash, qc, &qc->tx.params); |
| 9044 | chunk_appendf(&trash, "\n"); |
| 9045 | |
| 9046 | /* Connection state */ |
| 9047 | if (qc->flags & QUIC_FL_CONN_CLOSING) |
| 9048 | chunk_appendf(&trash, " st=closing "); |
| 9049 | else if (qc->flags & QUIC_FL_CONN_DRAINING) |
| 9050 | chunk_appendf(&trash, " st=draining "); |
| 9051 | else if (qc->state < QUIC_HS_ST_CONFIRMED) |
| 9052 | chunk_appendf(&trash, " st=handshake "); |
| 9053 | else |
| 9054 | chunk_appendf(&trash, " st=opened "); |
| 9055 | |
| 9056 | if (qc->mux_state == QC_MUX_NULL) |
| 9057 | chunk_appendf(&trash, "mux=null "); |
| 9058 | else if (qc->mux_state == QC_MUX_READY) |
| 9059 | chunk_appendf(&trash, "mux=ready "); |
| 9060 | else |
| 9061 | chunk_appendf(&trash, "mux=released "); |
| 9062 | |
| 9063 | expire = qc->idle_expire; |
| 9064 | chunk_appendf(&trash, "expire=%02ds ", |
| 9065 | TICKS_TO_MS(tick_remain(now_ms, expire)) / 1000); |
| 9066 | |
| 9067 | chunk_appendf(&trash, "\n"); |
| 9068 | |
| 9069 | /* Socket */ |
| 9070 | chunk_appendf(&trash, " fd=%d", qc->fd); |
| 9071 | if (qc->local_addr.ss_family == AF_INET || |
| 9072 | qc->local_addr.ss_family == AF_INET6) { |
| 9073 | addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr)); |
| 9074 | port_to_str(&qc->local_addr, bufport, sizeof(bufport)); |
Frédéric Lécaille | 8f8e3fa | 2023-06-14 09:17:20 +0200 | [diff] [blame] | 9075 | chunk_appendf(&trash, " local_addr=%s:%s", bufaddr, bufport); |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 9076 | |
| 9077 | addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr)); |
| 9078 | port_to_str(&qc->peer_addr, bufport, sizeof(bufport)); |
Frédéric Lécaille | 8f8e3fa | 2023-06-14 09:17:20 +0200 | [diff] [blame] | 9079 | chunk_appendf(&trash, " foreign_addr=%s:%s", bufaddr, bufport); |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 9080 | } |
| 9081 | |
| 9082 | chunk_appendf(&trash, "\n"); |
| 9083 | |
| 9084 | /* Packet number spaces information */ |
| 9085 | pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL]; |
| 9086 | chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu", |
| 9087 | pktns->rx.arngs.sz, pktns->tx.in_flight); |
| 9088 | pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]; |
| 9089 | chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n", |
| 9090 | pktns->rx.arngs.sz, pktns->tx.in_flight); |
| 9091 | pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT]; |
| 9092 | chunk_appendf(&trash, " [01rtt] rx.ackrng=%-6zu tx.inflight=%-6zu\n", |
| 9093 | pktns->rx.arngs.sz, pktns->tx.in_flight); |
| 9094 | |
| 9095 | chunk_appendf(&trash, " srtt=%-4u rttvar=%-4u rttmin=%-4u ptoc=%-4u cwnd=%-6llu" |
Amaury Denoyelle | 64f6741 | 2024-02-23 17:28:49 +0100 | [diff] [blame] | 9096 | " mcwnd=%-6llu sentpkts=%-6llu lostpkts=%-6llu reorderedpkts=%-6llu\n", |
Frédéric Lécaille | 35fb593 | 2023-09-05 15:24:11 +0200 | [diff] [blame] | 9097 | qc->path->loss.srtt, qc->path->loss.rtt_var, |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 9098 | qc->path->loss.rtt_min, qc->path->loss.pto_count, (ullong)qc->path->cwnd, |
Frederic Lecaille | dfeda3a | 2024-02-13 21:24:40 +0100 | [diff] [blame] | 9099 | (ullong)qc->path->mcwnd, (ullong)qc->cntrs.sent_pkt, (ullong)qc->path->loss.nb_lost_pkt, (ullong)qc->path->loss.nb_reordered_pkt); |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 9100 | |
Frédéric Lécaille | a73563b | 2023-05-25 16:10:03 +0200 | [diff] [blame] | 9101 | if (qc->cntrs.dropped_pkt) { |
| 9102 | chunk_appendf(&trash, " droppkts=%-6llu", qc->cntrs.dropped_pkt); |
| 9103 | addnl = 1; |
| 9104 | } |
| 9105 | if (qc->cntrs.dropped_pkt_bufoverrun) { |
| 9106 | chunk_appendf(&trash, " dropbuff=%-6llu", qc->cntrs.dropped_pkt_bufoverrun); |
| 9107 | addnl = 1; |
| 9108 | } |
| 9109 | if (qc->cntrs.dropped_parsing) { |
| 9110 | chunk_appendf(&trash, " droppars=%-6llu", qc->cntrs.dropped_parsing); |
| 9111 | addnl = 1; |
| 9112 | } |
| 9113 | if (qc->cntrs.socket_full) { |
| 9114 | chunk_appendf(&trash, " sockfull=%-6llu", qc->cntrs.socket_full); |
| 9115 | addnl = 1; |
| 9116 | } |
| 9117 | if (qc->cntrs.sendto_err) { |
| 9118 | chunk_appendf(&trash, " sendtoerr=%-6llu", qc->cntrs.sendto_err); |
| 9119 | addnl = 1; |
| 9120 | } |
| 9121 | if (qc->cntrs.sendto_err_unknown) { |
| 9122 | chunk_appendf(&trash, " sendtounknerr=%-6llu", qc->cntrs.sendto_err); |
| 9123 | addnl = 1; |
| 9124 | } |
| 9125 | if (qc->cntrs.conn_migration_done) { |
| 9126 | chunk_appendf(&trash, " migrdone=%-6llu", qc->cntrs.conn_migration_done); |
| 9127 | addnl = 1; |
| 9128 | } |
| 9129 | if (qc->cntrs.data_blocked) { |
| 9130 | chunk_appendf(&trash, " datablocked=%-6llu", qc->cntrs.data_blocked); |
| 9131 | addnl = 1; |
| 9132 | } |
| 9133 | if (qc->cntrs.stream_data_blocked) { |
| 9134 | chunk_appendf(&trash, " sdatablocked=%-6llu", qc->cntrs.stream_data_blocked); |
| 9135 | addnl = 1; |
| 9136 | } |
| 9137 | if (qc->cntrs.streams_blocked_bidi) { |
| 9138 | chunk_appendf(&trash, " sblockebidi=%-6llu", qc->cntrs.streams_blocked_bidi); |
| 9139 | addnl = 1; |
| 9140 | } |
| 9141 | if (qc->cntrs.streams_blocked_uni) { |
| 9142 | chunk_appendf(&trash, " sblockeduni=%-6llu", qc->cntrs.streams_blocked_uni); |
| 9143 | addnl = 1; |
| 9144 | } |
| 9145 | if (addnl) |
| 9146 | chunk_appendf(&trash, "\n"); |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 9147 | |
| 9148 | /* Streams */ |
| 9149 | node = eb64_first(&qc->streams_by_id); |
| 9150 | i = 0; |
| 9151 | while (node) { |
| 9152 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 9153 | node = eb64_next(node); |
| 9154 | |
| 9155 | chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key); |
| 9156 | chunk_appendf(&trash, " off=%-8llu ack=%-8llu", |
| 9157 | (unsigned long long)stream->buf_offset, |
| 9158 | (unsigned long long)stream->ack_offset); |
| 9159 | |
| 9160 | if (!(++i % 3)) { |
| 9161 | chunk_appendf(&trash, "\n"); |
| 9162 | i = 0; |
| 9163 | } |
| 9164 | } |
| 9165 | |
| 9166 | chunk_appendf(&trash, "\n"); |
| 9167 | } |
| 9168 | |
| 9169 | static int cli_io_handler_dump_quic(struct appctx *appctx) |
| 9170 | { |
| 9171 | struct show_quic_ctx *ctx = appctx->svcctx; |
| 9172 | struct stconn *sc = appctx_sc(appctx); |
| 9173 | struct quic_conn *qc; |
| 9174 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9175 | thread_isolate(); |
| 9176 | |
| 9177 | if (ctx->thr >= global.nbthread) |
| 9178 | goto done; |
| 9179 | |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 9180 | /* FIXME: Don't watch the other side !*/ |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 9181 | if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) { |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9182 | /* If we're forced to shut down, we might have to remove our |
| 9183 | * reference to the last stream being dumped. |
| 9184 | */ |
| 9185 | if (!LIST_ISEMPTY(&ctx->bref.users)) |
| 9186 | LIST_DEL_INIT(&ctx->bref.users); |
| 9187 | goto done; |
| 9188 | } |
| 9189 | |
| 9190 | chunk_reset(&trash); |
| 9191 | |
| 9192 | if (!LIST_ISEMPTY(&ctx->bref.users)) { |
| 9193 | /* Remove show_quic_ctx from previous quic_conn instance. */ |
| 9194 | LIST_DEL_INIT(&ctx->bref.users); |
| 9195 | } |
| 9196 | else if (!ctx->bref.ref) { |
| 9197 | /* First invocation. */ |
| 9198 | ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n; |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 9199 | |
| 9200 | /* Print legend for oneline format. */ |
| 9201 | if (ctx->format == QUIC_DUMP_FMT_ONELINE) { |
Amaury Denoyelle | aa39cc9 | 2023-05-22 10:57:56 +0200 | [diff] [blame] | 9202 | chunk_appendf(&trash, "# conn/frontend state " |
Frédéric Lécaille | 8f8e3fa | 2023-06-14 09:17:20 +0200 | [diff] [blame] | 9203 | "in_flight infl_p lost_p " |
| 9204 | "Local Address Foreign Address " |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 9205 | "local & remote CIDs\n"); |
| 9206 | applet_putchk(appctx, &trash); |
| 9207 | } |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9208 | } |
| 9209 | |
| 9210 | while (1) { |
| 9211 | int done = 0; |
| 9212 | |
| 9213 | if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) { |
Amaury Denoyelle | 2d37629 | 2023-03-08 09:42:31 +0100 | [diff] [blame] | 9214 | /* If closing connections requested through "all", move |
| 9215 | * to quic_conns_clo list after browsing quic_conns. |
| 9216 | * Else move directly to the next quic_conns thread. |
| 9217 | */ |
| 9218 | if (ctx->flags & QC_CLI_FL_SHOW_ALL) { |
| 9219 | ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns_clo.n; |
| 9220 | continue; |
| 9221 | } |
| 9222 | |
| 9223 | done = 1; |
| 9224 | } |
| 9225 | else if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns_clo) { |
| 9226 | /* Closing list entirely browsed, go to next quic_conns |
| 9227 | * thread. |
| 9228 | */ |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9229 | done = 1; |
| 9230 | } |
| 9231 | else { |
Amaury Denoyelle | 2d37629 | 2023-03-08 09:42:31 +0100 | [diff] [blame] | 9232 | /* Retrieve next element of the current list. */ |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9233 | qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx); |
| 9234 | if ((int)(qc->qc_epoch - ctx->epoch) > 0) |
| 9235 | done = 1; |
| 9236 | } |
| 9237 | |
| 9238 | if (done) { |
| 9239 | ++ctx->thr; |
| 9240 | if (ctx->thr >= global.nbthread) |
| 9241 | break; |
Amaury Denoyelle | 2d37629 | 2023-03-08 09:42:31 +0100 | [diff] [blame] | 9242 | /* Switch to next thread quic_conns list. */ |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9243 | ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n; |
| 9244 | continue; |
| 9245 | } |
| 9246 | |
Amaury Denoyelle | bc1f5fe | 2023-05-05 16:07:58 +0200 | [diff] [blame] | 9247 | switch (ctx->format) { |
| 9248 | case QUIC_DUMP_FMT_FULL: |
| 9249 | dump_quic_full(ctx, qc); |
| 9250 | break; |
Amaury Denoyelle | 2273af1 | 2023-05-05 16:08:34 +0200 | [diff] [blame] | 9251 | case QUIC_DUMP_FMT_ONELINE: |
| 9252 | dump_quic_oneline(ctx, qc); |
| 9253 | break; |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 9254 | } |
| 9255 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9256 | if (applet_putchk(appctx, &trash) == -1) { |
| 9257 | /* Register show_quic_ctx to quic_conn instance. */ |
| 9258 | LIST_APPEND(&qc->back_refs, &ctx->bref.users); |
| 9259 | goto full; |
| 9260 | } |
| 9261 | |
| 9262 | ctx->bref.ref = qc->el_th_ctx.n; |
| 9263 | } |
| 9264 | |
| 9265 | done: |
| 9266 | thread_release(); |
| 9267 | return 1; |
| 9268 | |
| 9269 | full: |
| 9270 | thread_release(); |
| 9271 | return 0; |
| 9272 | } |
| 9273 | |
| 9274 | static void cli_release_show_quic(struct appctx *appctx) |
| 9275 | { |
| 9276 | struct show_quic_ctx *ctx = appctx->svcctx; |
| 9277 | |
| 9278 | if (ctx->thr < global.nbthread) { |
| 9279 | thread_isolate(); |
| 9280 | if (!LIST_ISEMPTY(&ctx->bref.users)) |
| 9281 | LIST_DEL_INIT(&ctx->bref.users); |
| 9282 | thread_release(); |
| 9283 | } |
| 9284 | } |
| 9285 | |
| 9286 | static struct cli_kw_list cli_kws = {{ }, { |
Willy Tarreau | 6ccc862 | 2023-05-31 15:54:48 +0200 | [diff] [blame] | 9287 | { { "show", "quic", NULL }, "show quic [oneline|full] [all] : display quic connections status", cli_parse_show_quic, cli_io_handler_dump_quic, cli_release_show_quic }, |
Frédéric Lécaille | 91376d6 | 2023-02-11 20:24:42 +0100 | [diff] [blame] | 9288 | {{},} |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9289 | }}; |
| 9290 | |
| 9291 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 9292 | |
| 9293 | static void init_quic() |
| 9294 | { |
| 9295 | int thr; |
| 9296 | |
Amaury Denoyelle | efed86c | 2023-03-08 09:42:04 +0100 | [diff] [blame] | 9297 | for (thr = 0; thr < MAX_THREADS; ++thr) { |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9298 | LIST_INIT(&ha_thread_ctx[thr].quic_conns); |
Amaury Denoyelle | efed86c | 2023-03-08 09:42:04 +0100 | [diff] [blame] | 9299 | LIST_INIT(&ha_thread_ctx[thr].quic_conns_clo); |
| 9300 | } |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 9301 | } |
| 9302 | INITCALL0(STG_INIT, init_quic); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 9303 | |
| 9304 | /* |
| 9305 | * Local variables: |
| 9306 | * c-indent-level: 8 |
| 9307 | * c-basic-offset: 8 |
| 9308 | * End: |
| 9309 | */ |