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> |
| 35 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 36 | #include <haproxy/applet-t.h> |
| 37 | #include <haproxy/cli.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 38 | #include <haproxy/connection.h> |
| 39 | #include <haproxy/fd.h> |
| 40 | #include <haproxy/freq_ctr.h> |
| 41 | #include <haproxy/global.h> |
| 42 | #include <haproxy/h3.h> |
| 43 | #include <haproxy/hq_interop.h> |
| 44 | #include <haproxy/log.h> |
| 45 | #include <haproxy/mux_quic.h> |
| 46 | #include <haproxy/ncbuf.h> |
| 47 | #include <haproxy/pipe.h> |
| 48 | #include <haproxy/proxy.h> |
| 49 | #include <haproxy/quic_cc.h> |
| 50 | #include <haproxy/quic_frame.h> |
| 51 | #include <haproxy/quic_enc.h> |
| 52 | #include <haproxy/quic_loss.h> |
| 53 | #include <haproxy/quic_sock.h> |
| 54 | #include <haproxy/quic_stats.h> |
| 55 | #include <haproxy/quic_stream.h> |
| 56 | #include <haproxy/quic_tp.h> |
| 57 | #include <haproxy/cbuf.h> |
| 58 | #include <haproxy/proto_quic.h> |
| 59 | #include <haproxy/quic_tls.h> |
| 60 | #include <haproxy/ssl_sock.h> |
| 61 | #include <haproxy/task.h> |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 62 | #include <haproxy/thread.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 63 | #include <haproxy/trace.h> |
| 64 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 65 | /* incremented by each "show quic". */ |
| 66 | static unsigned int qc_epoch = 0; |
| 67 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 68 | /* list of supported QUIC versions by this implementation */ |
| 69 | const struct quic_version quic_versions[] = { |
| 70 | { |
| 71 | .num = QUIC_PROTOCOL_VERSION_DRAFT_29, |
| 72 | .initial_salt = initial_salt_draft_29, |
| 73 | .initial_salt_len = sizeof initial_salt_draft_29, |
| 74 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
| 75 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
| 76 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
| 77 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
| 78 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
| 79 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
| 80 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
| 81 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
| 82 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT, |
| 83 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT, |
| 84 | }, |
| 85 | { |
| 86 | .num = QUIC_PROTOCOL_VERSION_1, |
| 87 | .initial_salt = initial_salt_v1, |
| 88 | .initial_salt_len = sizeof initial_salt_v1, |
| 89 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
| 90 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
| 91 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
| 92 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
| 93 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
| 94 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
| 95 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
| 96 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
| 97 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1, |
| 98 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1, |
| 99 | }, |
| 100 | { |
Frédéric Lécaille | 21c4c9b | 2023-01-13 16:37:02 +0100 | [diff] [blame] | 101 | .num = QUIC_PROTOCOL_VERSION_2, |
| 102 | .initial_salt = initial_salt_v2, |
| 103 | .initial_salt_len = sizeof initial_salt_v2, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 104 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2, |
| 105 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1, |
| 106 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2, |
| 107 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1, |
| 108 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2, |
| 109 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1, |
| 110 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2, |
| 111 | .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] | 112 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2, |
| 113 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 114 | }, |
| 115 | }; |
| 116 | |
| 117 | /* The total number of supported versions */ |
| 118 | const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions; |
| 119 | /* Listener only preferred version */ |
| 120 | const struct quic_version *preferred_version; |
| 121 | |
| 122 | /* trace source and events */ |
| 123 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 124 | const struct trace_source *src, |
| 125 | const struct ist where, const struct ist func, |
| 126 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 127 | |
| 128 | static const struct trace_event quic_trace_events[] = { |
| 129 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 130 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 131 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 132 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 133 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 134 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 135 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
| 136 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
| 137 | { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" }, |
| 138 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 139 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 140 | { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" }, |
| 141 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 142 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 143 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 144 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 145 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 146 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 147 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 148 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 149 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 150 | { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" }, |
| 151 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 152 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 153 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 154 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 155 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
| 156 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 157 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 158 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 159 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 160 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 161 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
| 162 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
| 163 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 164 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
| 165 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 166 | { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." }, |
| 167 | { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."}, |
| 168 | { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"}, |
| 169 | { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"}, |
| 170 | { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"}, |
| 171 | { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"}, |
| 172 | { .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] | 173 | { .mask = QUIC_EV_CONN_RCV, .name = "conn_recv", .desc = "RX on connection" }, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 174 | { /* end */ } |
| 175 | }; |
| 176 | |
| 177 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 178 | /* arg1 */ { /* already used by the connection */ }, |
| 179 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 180 | /* arg3 */ { }, |
| 181 | /* arg4 */ { } |
| 182 | }; |
| 183 | |
| 184 | static const struct name_desc quic_trace_decoding[] = { |
| 185 | #define QUIC_VERB_CLEAN 1 |
| 186 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 187 | { /* end */ } |
| 188 | }; |
| 189 | |
| 190 | |
| 191 | struct trace_source trace_quic = { |
| 192 | .name = IST("quic"), |
| 193 | .desc = "QUIC xprt", |
| 194 | .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */ |
| 195 | .default_cb = quic_trace, |
| 196 | .known_events = quic_trace_events, |
| 197 | .lockon_args = quic_trace_lockon_args, |
| 198 | .decoding = quic_trace_decoding, |
| 199 | .report_events = ~0, /* report everything by default */ |
| 200 | }; |
| 201 | |
| 202 | #define TRACE_SOURCE &trace_quic |
| 203 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 204 | |
| 205 | static BIO_METHOD *ha_quic_meth; |
| 206 | |
| 207 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ); |
| 208 | DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ); |
| 209 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
| 210 | "quic_conn_ctx", sizeof(struct ssl_sock_ctx)); |
| 211 | DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn)); |
| 212 | DECLARE_POOL(pool_head_quic_connection_id, |
| 213 | "quic_connnection_id", sizeof(struct quic_connection_id)); |
| 214 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
| 215 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet)); |
| 216 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet)); |
| 217 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm)); |
| 218 | 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] | 219 | 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] | 220 | DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame)); |
| 221 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node)); |
| 222 | |
| 223 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
| 224 | struct quic_enc_level *qel, struct quic_tls_ctx *ctx, |
| 225 | struct list *frms, struct quic_conn *qc, |
| 226 | const struct quic_version *ver, size_t dglen, int pkt_type, |
| 227 | int force_ack, int padding, int probe, int cc, int *err); |
| 228 | struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state); |
| 229 | static void qc_idle_timer_do_rearm(struct quic_conn *qc); |
| 230 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read); |
| 231 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc); |
| 232 | static int quic_conn_init_timer(struct quic_conn *qc); |
| 233 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc); |
| 234 | |
| 235 | /* Only for debug purpose */ |
| 236 | struct enc_debug_info { |
| 237 | unsigned char *payload; |
| 238 | size_t payload_len; |
| 239 | unsigned char *aad; |
| 240 | size_t aad_len; |
| 241 | uint64_t pn; |
| 242 | }; |
| 243 | |
| 244 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 245 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 246 | unsigned char *payload, size_t payload_len, |
| 247 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 248 | { |
| 249 | edi->payload = payload; |
| 250 | edi->payload_len = payload_len; |
| 251 | edi->aad = aad; |
| 252 | edi->aad_len = aad_len; |
| 253 | edi->pn = pn; |
| 254 | } |
| 255 | |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 256 | /* Used only for QUIC TLS key phase traces */ |
| 257 | struct quic_kp_trace { |
| 258 | const unsigned char *rx_sec; |
| 259 | size_t rx_seclen; |
| 260 | const struct quic_tls_kp *rx; |
| 261 | const unsigned char *tx_sec; |
| 262 | size_t tx_seclen; |
| 263 | const struct quic_tls_kp *tx; |
| 264 | }; |
| 265 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 266 | /* Trace callback for QUIC. |
| 267 | * These traces always expect that arg1, if non-null, is of type connection. |
| 268 | */ |
| 269 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 270 | const struct ist where, const struct ist func, |
| 271 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 272 | { |
| 273 | const struct quic_conn *qc = a1; |
| 274 | |
| 275 | if (qc) { |
| 276 | const struct quic_tls_ctx *tls_ctx; |
| 277 | |
| 278 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
| 279 | if (mask & QUIC_EV_CONN_INIT) { |
| 280 | chunk_appendf(&trace_buf, "\n odcid"); |
| 281 | quic_cid_dump(&trace_buf, &qc->odcid); |
| 282 | chunk_appendf(&trace_buf, "\n dcid"); |
| 283 | quic_cid_dump(&trace_buf, &qc->dcid); |
| 284 | chunk_appendf(&trace_buf, "\n scid"); |
| 285 | quic_cid_dump(&trace_buf, &qc->scid); |
| 286 | } |
| 287 | |
| 288 | if (mask & QUIC_EV_TRANSP_PARAMS) { |
| 289 | const struct quic_transport_params *p = a2; |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 290 | |
| 291 | if (p) |
| 292 | quic_transport_params_dump(&trace_buf, qc, p); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 296 | const enum ssl_encryption_level_t *level = a2; |
| 297 | const size_t *len = a3; |
| 298 | |
| 299 | if (level) { |
| 300 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 301 | |
| 302 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 303 | } |
| 304 | if (len) |
| 305 | chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len); |
| 306 | } |
| 307 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 308 | /* Initial read & write secrets. */ |
| 309 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 310 | const unsigned char *rx_sec = a2; |
| 311 | const unsigned char *tx_sec = a3; |
| 312 | |
| 313 | tls_ctx = &qc->els[level].tls_ctx; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 314 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 315 | if (rx_sec) |
| 316 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 317 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
| 318 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 319 | if (tx_sec) |
| 320 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 321 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 322 | } |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 323 | |
| 324 | if ((mask & QUIC_EV_CONN_KP) && qc) { |
| 325 | /* Initial read & write secrets. */ |
| 326 | const struct quic_kp_trace *kp = a2; |
| 327 | |
| 328 | if (kp) { |
| 329 | if (kp->rx) { |
| 330 | chunk_appendf(&trace_buf, "\n RX kp"); |
| 331 | if (kp->rx_sec) |
| 332 | quic_tls_secret_hexdump(&trace_buf, kp->rx_sec, kp->rx_seclen); |
| 333 | quic_tls_kp_keys_hexdump(&trace_buf, kp->rx); |
| 334 | } |
| 335 | if (kp->tx) { |
| 336 | chunk_appendf(&trace_buf, "\n TX kp"); |
| 337 | if (kp->tx_sec) |
| 338 | quic_tls_secret_hexdump(&trace_buf, kp->tx_sec, kp->tx_seclen); |
| 339 | quic_tls_kp_keys_hexdump(&trace_buf, kp->tx); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 344 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 345 | const enum ssl_encryption_level_t *level = a2; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 346 | |
| 347 | if (level) { |
| 348 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 349 | |
| 350 | 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] | 351 | if (quic_tls_has_rx_sec(&qc->els[lvl])) { |
| 352 | tls_ctx = &qc->els[lvl].tls_ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 353 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 354 | } |
| 355 | else |
| 356 | chunk_appendf(&trace_buf, " (none)"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 361 | const enum ssl_encryption_level_t *level = a2; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 362 | |
| 363 | if (level) { |
| 364 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 365 | |
| 366 | 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] | 367 | if (quic_tls_has_tx_sec(&qc->els[lvl])) { |
| 368 | tls_ctx = &qc->els[lvl].tls_ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 369 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 370 | } |
| 371 | else |
| 372 | chunk_appendf(&trace_buf, " (none)"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | } |
| 376 | |
| 377 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 378 | const struct list *l = a2; |
| 379 | |
| 380 | if (l) { |
| 381 | const struct quic_frame *frm; |
| 382 | list_for_each_entry(frm, l, list) { |
| 383 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 384 | chunk_frm_appendf(&trace_buf, frm); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) { |
| 390 | const struct quic_tx_packet *pkt = a2; |
| 391 | const struct quic_enc_level *qel = a3; |
| 392 | const ssize_t *room = a4; |
| 393 | |
| 394 | if (qel) { |
| 395 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | 4540053 | 2023-02-13 18:39:19 +0100 | [diff] [blame] | 396 | chunk_appendf(&trace_buf, " qel=%c pto_count=%d cwnd=%llu ppif=%lld pif=%llu " |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 397 | "if=%llu pp=%u", |
| 398 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 4540053 | 2023-02-13 18:39:19 +0100 | [diff] [blame] | 399 | qc->path->loss.pto_count, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 400 | (unsigned long long)qc->path->cwnd, |
| 401 | (unsigned long long)qc->path->prep_in_flight, |
| 402 | (unsigned long long)qc->path->in_flight, |
| 403 | (unsigned long long)pktns->tx.in_flight, |
| 404 | pktns->tx.pto_probe); |
| 405 | } |
| 406 | if (pkt) { |
| 407 | const struct quic_frame *frm; |
| 408 | if (pkt->pn_node.key != (uint64_t)-1) |
| 409 | chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key); |
| 410 | list_for_each_entry(frm, &pkt->frms, list) { |
| 411 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 412 | chunk_frm_appendf(&trace_buf, frm); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | if (room) { |
| 417 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 418 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 419 | (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | if (mask & QUIC_EV_CONN_IO_CB) { |
| 424 | const enum quic_handshake_state *state = a2; |
| 425 | const int *err = a3; |
| 426 | |
| 427 | if (state) |
| 428 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 429 | if (err) |
| 430 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 431 | } |
| 432 | |
| 433 | if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) { |
| 434 | const struct quic_rx_packet *pkt = a2; |
| 435 | const unsigned long *pktlen = a3; |
| 436 | const SSL *ssl = a4; |
| 437 | |
| 438 | if (pkt) { |
| 439 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 440 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 441 | chunk_appendf(&trace_buf, " kp=%d", |
| 442 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 443 | chunk_appendf(&trace_buf, " el=%c", |
| 444 | quic_packet_type_enc_level_char(pkt->type)); |
| 445 | if (pkt->pnl) |
| 446 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 447 | (unsigned long long)pkt->pn); |
| 448 | if (pkt->token_len) |
| 449 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 450 | (unsigned long long)pkt->token_len); |
| 451 | if (pkt->aad_len) |
| 452 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 453 | (unsigned long long)pkt->aad_len); |
| 454 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 455 | pkt->flags, (unsigned long long)pkt->len); |
| 456 | } |
| 457 | if (pktlen) |
| 458 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 459 | if (ssl) { |
| 460 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 461 | chunk_appendf(&trace_buf, " el=%c", |
| 462 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 467 | const struct quic_rx_packet *pkt = a2; |
| 468 | const struct quic_rx_crypto_frm *cf = a3; |
| 469 | const SSL *ssl = a4; |
| 470 | |
| 471 | if (pkt) |
| 472 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 473 | quic_packet_type_enc_level_char(pkt->type), |
| 474 | (unsigned long long)pkt->pn); |
| 475 | if (cf) |
| 476 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 477 | (unsigned long long)cf->offset_node.key, |
| 478 | (unsigned long long)cf->len); |
| 479 | if (ssl) { |
| 480 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 481 | chunk_appendf(&trace_buf, " rel=%c", |
| 482 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 483 | } |
| 484 | |
| 485 | if (qc->err.code) |
| 486 | chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code); |
| 487 | } |
| 488 | |
| 489 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 490 | const struct quic_frame *frm = a2; |
| 491 | |
| 492 | if (frm) |
| 493 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 494 | } |
| 495 | |
| 496 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 497 | const struct quic_enc_level *qel = a2; |
| 498 | |
| 499 | if (qel) { |
| 500 | const struct quic_pktns *pktns = qel->pktns; |
| 501 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 4540053 | 2023-02-13 18:39:19 +0100 | [diff] [blame] | 502 | " qel=%c 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] | 503 | quic_enc_level_char_from_qel(qel, qc), |
| 504 | quic_hdshk_state_str(qc->state), |
| 505 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 4540053 | 2023-02-13 18:39:19 +0100 | [diff] [blame] | 506 | qc->path->loss.pto_count, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 507 | (unsigned long long)qc->path->cwnd, |
| 508 | (unsigned long long)qc->path->prep_in_flight, |
| 509 | (unsigned long long)qc->path->in_flight, |
| 510 | (unsigned long long)pktns->tx.in_flight, |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 511 | pktns->tx.pto_probe, |
| 512 | qel->cstream ? (unsigned long long)qel->cstream->rx.offset : 0); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 517 | const struct enc_debug_info *edi = a2; |
| 518 | |
| 519 | if (edi) |
| 520 | chunk_appendf(&trace_buf, |
| 521 | " payload=@%p payload_len=%llu" |
| 522 | " aad=@%p aad_len=%llu pn=%llu", |
| 523 | edi->payload, (unsigned long long)edi->payload_len, |
| 524 | edi->aad, (unsigned long long)edi->aad_len, |
| 525 | (unsigned long long)edi->pn); |
| 526 | } |
| 527 | |
| 528 | if (mask & QUIC_EV_CONN_RMHP) { |
| 529 | const struct quic_rx_packet *pkt = a2; |
| 530 | |
| 531 | if (pkt) { |
| 532 | const int *ret = a3; |
| 533 | |
| 534 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 535 | if (ret && *ret) |
| 536 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 537 | pkt->pnl, (unsigned long long)pkt->pn); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
| 542 | const struct quic_frame *frm = a2; |
| 543 | const unsigned long *val1 = a3; |
| 544 | const unsigned long *val2 = a4; |
| 545 | |
| 546 | if (frm) { |
| 547 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 548 | chunk_frm_appendf(&trace_buf, frm); |
| 549 | } |
| 550 | if (val1) |
| 551 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 552 | if (val2) |
| 553 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 554 | } |
| 555 | |
| 556 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 557 | const struct quic_stream *s = a2; |
| 558 | const struct qc_stream_desc *stream = a3; |
| 559 | |
| 560 | if (s) |
| 561 | chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len); |
| 562 | if (stream) |
| 563 | chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset); |
| 564 | } |
| 565 | |
| 566 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 567 | const unsigned int *rtt_sample = a2; |
| 568 | const unsigned int *ack_delay = a3; |
| 569 | const struct quic_loss *ql = a4; |
| 570 | |
| 571 | if (rtt_sample) |
| 572 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 573 | if (ack_delay) |
| 574 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 575 | if (ql) |
| 576 | chunk_appendf(&trace_buf, |
| 577 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 578 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 579 | } |
| 580 | if (mask & QUIC_EV_CONN_CC) { |
| 581 | const struct quic_cc_event *ev = a2; |
| 582 | const struct quic_cc *cc = a3; |
| 583 | |
| 584 | if (a2) |
| 585 | quic_cc_event_trace(&trace_buf, ev); |
| 586 | if (a3) |
| 587 | quic_cc_state_trace(&trace_buf, cc); |
| 588 | } |
| 589 | |
| 590 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 591 | const struct quic_pktns *pktns = a2; |
| 592 | const struct list *lost_pkts = a3; |
| 593 | |
| 594 | if (pktns) { |
| 595 | chunk_appendf(&trace_buf, " pktns=%s", |
| 596 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 597 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 598 | if (pktns->tx.loss_time) |
| 599 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 600 | TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time))); |
| 601 | } |
| 602 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 603 | struct quic_tx_packet *pkt; |
| 604 | |
| 605 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 606 | list_for_each_entry(pkt, lost_pkts, list) |
| 607 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) { |
| 612 | const struct quic_pktns *pktns = a2; |
| 613 | const int *duration = a3; |
| 614 | const uint64_t *ifae_pkts = a4; |
| 615 | |
| 616 | if (ifae_pkts) |
| 617 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 618 | (unsigned long long)*ifae_pkts); |
| 619 | if (pktns) { |
| 620 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
| 621 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 622 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 623 | pktns->tx.pto_probe); |
| 624 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 625 | if (pktns->tx.in_flight) |
| 626 | chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight); |
| 627 | if (pktns->tx.loss_time) |
| 628 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 629 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 630 | } |
| 631 | if (mask & QUIC_EV_CONN_SPTO) { |
| 632 | if (pktns->tx.time_of_last_eliciting) |
| 633 | chunk_appendf(&trace_buf, " tole=%dms", |
| 634 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 635 | if (duration) |
| 636 | chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration)); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) { |
| 641 | chunk_appendf(&trace_buf, |
| 642 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 647 | const struct quic_tx_packet *pkt = a2; |
| 648 | |
Frédéric Lécaille | 4540053 | 2023-02-13 18:39:19 +0100 | [diff] [blame] | 649 | chunk_appendf(&trace_buf, " pto_count=%d cwnd=%llu ppif=%llu pif=%llu", |
| 650 | qc->path->loss.pto_count, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 651 | (unsigned long long)qc->path->cwnd, |
| 652 | (unsigned long long)qc->path->prep_in_flight, |
| 653 | (unsigned long long)qc->path->in_flight); |
| 654 | if (pkt) { |
| 655 | const struct quic_frame *frm; |
| 656 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
| 657 | (unsigned long)pkt->pn_node.key, |
| 658 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 659 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 660 | (unsigned long long)pkt->in_flight_len); |
| 661 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 662 | (unsigned long long)qc->rx.bytes, |
| 663 | (unsigned long long)qc->tx.bytes); |
| 664 | list_for_each_entry(frm, &pkt->frms, list) { |
| 665 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 666 | chunk_frm_appendf(&trace_buf, frm); |
| 667 | } |
Frédéric Lécaille | bc09f74 | 2023-02-13 17:45:36 +0100 | [diff] [blame] | 668 | |
| 669 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 670 | chunk_appendf(&trace_buf, " with scid"); |
| 671 | quic_cid_dump(&trace_buf, &qc->scid); |
| 672 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 677 | const uint8_t *alert = a2; |
| 678 | const enum ssl_encryption_level_t *level = a3; |
| 679 | |
| 680 | if (alert) |
| 681 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 682 | if (level) |
| 683 | chunk_appendf(&trace_buf, " el=%c", |
| 684 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 685 | } |
| 686 | |
| 687 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 688 | const size_t *sz1 = a2; |
| 689 | const size_t *sz2 = a3; |
| 690 | const size_t *sz3 = a4; |
| 691 | |
| 692 | if (sz1) |
| 693 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 694 | if (sz2) |
| 695 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 696 | if (sz3) |
| 697 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 698 | } |
| 699 | |
| 700 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 701 | const struct quic_frame *frm = a2; |
| 702 | |
| 703 | if (frm) { |
| 704 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 705 | chunk_frm_appendf(&trace_buf, frm); |
| 706 | } |
| 707 | } |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 708 | |
| 709 | if (mask & QUIC_EV_CONN_ELEVELSEL) { |
| 710 | const enum quic_handshake_state *state = a2; |
| 711 | const enum quic_tls_enc_level *level = a3; |
| 712 | const enum quic_tls_enc_level *next_level = a4; |
| 713 | |
| 714 | if (state) |
| 715 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(qc->state)); |
| 716 | if (level) |
| 717 | chunk_appendf(&trace_buf, " level=%c", quic_enc_level_char(*level)); |
| 718 | if (next_level) |
| 719 | chunk_appendf(&trace_buf, " next_level=%c", quic_enc_level_char(*next_level)); |
| 720 | |
| 721 | } |
Amaury Denoyelle | 5b41486 | 2022-10-24 17:40:37 +0200 | [diff] [blame] | 722 | |
| 723 | if (mask & QUIC_EV_CONN_RCV) { |
| 724 | const struct quic_dgram *dgram = a2; |
| 725 | |
| 726 | if (dgram) |
| 727 | chunk_appendf(&trace_buf, " dgram.len=%zu", dgram->len); |
| 728 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 729 | } |
| 730 | if (mask & QUIC_EV_CONN_LPKT) { |
| 731 | const struct quic_rx_packet *pkt = a2; |
| 732 | const uint64_t *len = a3; |
| 733 | const struct quic_version *ver = a4; |
| 734 | |
| 735 | if (pkt) { |
| 736 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 737 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 738 | if (pkt->pn_node.key != (uint64_t)-1) |
| 739 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 740 | } |
| 741 | |
| 742 | if (len) |
| 743 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
| 744 | |
| 745 | if (ver) |
| 746 | chunk_appendf(&trace_buf, " ver=0x%08x", ver->num); |
| 747 | } |
| 748 | |
| 749 | if (mask & QUIC_EV_STATELESS_RST) { |
| 750 | const struct quic_cid *cid = a2; |
| 751 | |
| 752 | if (cid) |
| 753 | quic_cid_dump(&trace_buf, cid); |
| 754 | } |
| 755 | |
| 756 | } |
| 757 | |
| 758 | /* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */ |
| 759 | static inline int quic_peer_validated_addr(struct quic_conn *qc) |
| 760 | { |
| 761 | struct quic_pktns *hdshk_pktns, *app_pktns; |
| 762 | |
| 763 | if (!qc_is_listener(qc)) |
| 764 | return 1; |
| 765 | |
| 766 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 767 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 768 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 769 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 770 | qc->state >= QUIC_HS_ST_COMPLETE) |
| 771 | return 1; |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 776 | /* To be called to kill a connection as soon as possible (without sending any packet). */ |
| 777 | void qc_kill_conn(struct quic_conn *qc) |
| 778 | { |
Frédéric Lécaille | 2f53111 | 2023-02-10 14:44:51 +0100 | [diff] [blame] | 779 | TRACE_ENTER(QUIC_EV_CONN_KILL, qc); |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 780 | qc->flags |= QUIC_FL_CONN_TO_KILL; |
| 781 | task_wakeup(qc->idle_timer_task, TASK_WOKEN_OTHER); |
Frédéric Lécaille | 2f53111 | 2023-02-10 14:44:51 +0100 | [diff] [blame] | 782 | TRACE_LEAVE(QUIC_EV_CONN_KILL, qc); |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 785 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 786 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 787 | */ |
| 788 | static inline void qc_set_timer(struct quic_conn *qc) |
| 789 | { |
| 790 | struct quic_pktns *pktns; |
| 791 | unsigned int pto; |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 792 | int handshake_confirmed; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 793 | |
| 794 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
| 795 | NULL, NULL, &qc->path->ifae_pkts); |
| 796 | |
Frédéric Lécaille | dd41a45 | 2023-02-09 07:48:33 +0100 | [diff] [blame] | 797 | pktns = NULL; |
| 798 | if (!qc->timer_task) { |
| 799 | TRACE_PROTO("already released timer task", QUIC_EV_CONN_STIMER, qc); |
| 800 | goto leave; |
| 801 | } |
| 802 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 803 | pktns = quic_loss_pktns(qc); |
| 804 | if (tick_isset(pktns->tx.loss_time)) { |
| 805 | qc->timer = pktns->tx.loss_time; |
| 806 | goto out; |
| 807 | } |
| 808 | |
| 809 | /* anti-amplification: the timer must be |
| 810 | * cancelled for a server which reached the anti-amplification limit. |
| 811 | */ |
| 812 | if (!quic_peer_validated_addr(qc) && |
| 813 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
| 814 | TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc); |
| 815 | qc->timer = TICK_ETERNITY; |
| 816 | goto out; |
| 817 | } |
| 818 | |
| 819 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
| 820 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
| 821 | /* Timer cancellation. */ |
| 822 | qc->timer = TICK_ETERNITY; |
| 823 | goto out; |
| 824 | } |
| 825 | |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 826 | handshake_confirmed = qc->state >= QUIC_HS_ST_CONFIRMED; |
| 827 | pktns = quic_pto_pktns(qc, handshake_confirmed, &pto); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 828 | if (tick_isset(pto)) |
| 829 | qc->timer = pto; |
| 830 | out: |
Frédéric Lécaille | dd41a45 | 2023-02-09 07:48:33 +0100 | [diff] [blame] | 831 | if (qc->timer == TICK_ETERNITY) { |
| 832 | qc->timer_task->expire = TICK_ETERNITY; |
| 833 | } |
| 834 | else if (tick_is_expired(qc->timer, now_ms)) { |
| 835 | TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc); |
| 836 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 837 | } |
| 838 | else { |
| 839 | TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc); |
| 840 | task_schedule(qc->timer_task, qc->timer); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 841 | } |
Frédéric Lécaille | dd41a45 | 2023-02-09 07:48:33 +0100 | [diff] [blame] | 842 | leave: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 843 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
| 844 | } |
| 845 | |
| 846 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 847 | * connection. |
| 848 | * Return 1 if succeeded, 0 if not. |
| 849 | */ |
| 850 | static int quic_tls_key_update(struct quic_conn *qc) |
| 851 | { |
| 852 | 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] | 853 | struct quic_tls_secrets *rx = &tls_ctx->rx; |
| 854 | struct quic_tls_secrets *tx = &tls_ctx->tx; |
| 855 | /* Used only for the traces */ |
| 856 | struct quic_kp_trace kp_trace = { |
| 857 | .rx_sec = rx->secret, |
| 858 | .rx_seclen = rx->secretlen, |
| 859 | .tx_sec = tx->secret, |
| 860 | .tx_seclen = tx->secretlen, |
| 861 | }; |
| 862 | /* The next key phase secrets to be derived */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 863 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 864 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 865 | const struct quic_version *ver = |
| 866 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
| 867 | int ret = 0; |
| 868 | |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 869 | TRACE_ENTER(QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 870 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 871 | nxt_rx = &qc->ku.nxt_rx; |
| 872 | nxt_tx = &qc->ku.nxt_tx; |
| 873 | |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 874 | TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0, |
| 875 | "nxt_rx->secretlen=%llu rx->secretlen=%llu", |
| 876 | (ull)nxt_rx->secretlen, (ull)rx->secretlen); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 877 | /* Prepare new RX secrets */ |
| 878 | if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen, |
| 879 | rx->secret, rx->secretlen)) { |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 880 | TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 881 | goto leave; |
| 882 | } |
| 883 | |
| 884 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver, |
| 885 | nxt_rx->key, nxt_rx->keylen, |
| 886 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 887 | nxt_rx->secret, nxt_rx->secretlen)) { |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 888 | TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 889 | goto leave; |
| 890 | } |
| 891 | |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 892 | kp_trace.rx = nxt_rx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 893 | /* Prepare new TX secrets */ |
| 894 | if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen, |
| 895 | tx->secret, tx->secretlen)) { |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 896 | TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 897 | goto leave; |
| 898 | } |
| 899 | |
| 900 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver, |
| 901 | nxt_tx->key, nxt_tx->keylen, |
| 902 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 903 | nxt_tx->secret, nxt_tx->secretlen)) { |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 904 | TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 905 | goto leave; |
| 906 | } |
| 907 | |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 908 | kp_trace.tx = nxt_tx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 909 | if (nxt_rx->ctx) { |
| 910 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 911 | nxt_rx->ctx = NULL; |
| 912 | } |
| 913 | |
| 914 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 915 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 916 | goto leave; |
| 917 | } |
| 918 | |
| 919 | if (nxt_tx->ctx) { |
| 920 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 921 | nxt_tx->ctx = NULL; |
| 922 | } |
| 923 | |
| 924 | if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 925 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_KP, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 926 | goto leave; |
| 927 | } |
| 928 | |
| 929 | ret = 1; |
| 930 | leave: |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 931 | TRACE_LEAVE(QUIC_EV_CONN_KP, qc, &kp_trace); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 932 | return ret; |
| 933 | } |
| 934 | |
| 935 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 936 | * Must be used after having updated them. |
| 937 | * Always succeeds. |
| 938 | */ |
| 939 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 940 | { |
| 941 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 942 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 943 | EVP_CIPHER_CTX *curr_ctx; |
| 944 | |
| 945 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 946 | |
| 947 | /* Rotate the RX secrets */ |
| 948 | curr_ctx = tls_ctx->rx.ctx; |
| 949 | curr_secret = tls_ctx->rx.secret; |
| 950 | curr_iv = tls_ctx->rx.iv; |
| 951 | curr_key = tls_ctx->rx.key; |
| 952 | |
| 953 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
| 954 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 955 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 956 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 957 | |
| 958 | qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx; |
| 959 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 960 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 961 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 962 | |
| 963 | qc->ku.prv_rx.ctx = curr_ctx; |
| 964 | qc->ku.prv_rx.secret = curr_secret; |
| 965 | qc->ku.prv_rx.iv = curr_iv; |
| 966 | qc->ku.prv_rx.key = curr_key; |
| 967 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 968 | |
| 969 | /* Update the TX secrets */ |
| 970 | curr_ctx = tls_ctx->tx.ctx; |
| 971 | curr_secret = tls_ctx->tx.secret; |
| 972 | curr_iv = tls_ctx->tx.iv; |
| 973 | curr_key = tls_ctx->tx.key; |
| 974 | |
| 975 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
| 976 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 977 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 978 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 979 | |
| 980 | qc->ku.nxt_tx.ctx = curr_ctx; |
| 981 | qc->ku.nxt_tx.secret = curr_secret; |
| 982 | qc->ku.nxt_tx.iv = curr_iv; |
| 983 | qc->ku.nxt_tx.key = curr_key; |
| 984 | |
| 985 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 986 | } |
| 987 | |
| 988 | /* returns 0 on error, 1 on success */ |
| 989 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 990 | const uint8_t *read_secret, |
| 991 | const uint8_t *write_secret, size_t secret_len) |
| 992 | { |
| 993 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 994 | struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 995 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 996 | struct quic_tls_secrets *rx = NULL, *tx = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 997 | const struct quic_version *ver = |
| 998 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
| 999 | int ret = 0; |
| 1000 | |
| 1001 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
| 1002 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 1003 | |
| 1004 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 1005 | TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc); |
| 1006 | goto out; |
| 1007 | } |
| 1008 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1009 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 1010 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1011 | goto out; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1012 | } |
| 1013 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1014 | if (!read_secret) |
| 1015 | goto write; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1016 | |
| 1017 | rx = &tls_ctx->rx; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1018 | if (!quic_tls_secrets_keys_alloc(rx)) { |
| 1019 | TRACE_ERROR("RX keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 1020 | goto leave; |
| 1021 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1022 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1023 | rx->aead = tls_aead(cipher); |
| 1024 | rx->md = tls_md(cipher); |
| 1025 | rx->hp = tls_hp(cipher); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1026 | |
| 1027 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen, |
| 1028 | rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key, |
| 1029 | read_secret, secret_len)) { |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1030 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1031 | goto leave; |
| 1032 | } |
| 1033 | |
| 1034 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 1035 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 1036 | goto leave; |
| 1037 | } |
| 1038 | |
| 1039 | if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) { |
| 1040 | TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 1041 | goto leave; |
| 1042 | } |
| 1043 | |
| 1044 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 1045 | * listener. Note that a listener derives only RX secrets for this |
| 1046 | * level. |
| 1047 | */ |
| 1048 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) { |
| 1049 | TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc); |
| 1050 | quic_accept_push_qc(qc); |
| 1051 | } |
| 1052 | |
| 1053 | write: |
| 1054 | |
| 1055 | if (!write_secret) |
| 1056 | goto out; |
| 1057 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1058 | tx = &tls_ctx->tx; |
| 1059 | if (!quic_tls_secrets_keys_alloc(tx)) { |
| 1060 | TRACE_ERROR("TX keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 1061 | goto leave; |
| 1062 | } |
| 1063 | |
| 1064 | tx->aead = tls_aead(cipher); |
| 1065 | tx->md = tls_md(cipher); |
| 1066 | tx->hp = tls_hp(cipher); |
| 1067 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1068 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen, |
| 1069 | tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key, |
| 1070 | write_secret, secret_len)) { |
| 1071 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 1072 | goto leave; |
| 1073 | } |
| 1074 | |
| 1075 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 1076 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 1077 | goto leave; |
| 1078 | } |
| 1079 | |
| 1080 | if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) { |
| 1081 | TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 1082 | goto leave; |
| 1083 | } |
| 1084 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 1085 | if (level == ssl_encryption_handshake && qc_is_listener(qc)) { |
| 1086 | qc->enc_params_len = |
| 1087 | quic_transport_params_encode(qc->enc_params, |
| 1088 | qc->enc_params + sizeof qc->enc_params, |
| 1089 | &qc->rx.params, ver, 1); |
| 1090 | if (!qc->enc_params_len) { |
| 1091 | TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_RWSEC); |
| 1092 | goto leave; |
| 1093 | } |
| 1094 | |
| 1095 | if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) { |
| 1096 | TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_RWSEC); |
| 1097 | goto leave; |
| 1098 | } |
| 1099 | } |
| 1100 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1101 | if (level == ssl_encryption_application) { |
| 1102 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 1103 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 1104 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 1105 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1106 | if (rx) { |
| 1107 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
| 1108 | TRACE_ERROR("Could not allocate RX Application secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 1109 | goto leave; |
| 1110 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1111 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1112 | memcpy(rx->secret, read_secret, secret_len); |
| 1113 | rx->secretlen = secret_len; |
| 1114 | } |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1115 | |
| 1116 | if (tx) { |
| 1117 | if (!(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
| 1118 | TRACE_ERROR("Could not allocate TX Application secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 1119 | goto leave; |
| 1120 | } |
| 1121 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1122 | memcpy(tx->secret, write_secret, secret_len); |
| 1123 | tx->secretlen = secret_len; |
| 1124 | } |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1125 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1126 | /* Initialize all the secret keys lengths */ |
| 1127 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | out: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1131 | ret = 1; |
| 1132 | leave: |
| 1133 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
| 1137 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 1138 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 1139 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 1140 | * It fails (returns 0) only if it could not managed to allocate enough CRYPTO |
| 1141 | * buffers to store all the data. |
| 1142 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 1143 | */ |
| 1144 | static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel, |
| 1145 | const unsigned char *data, size_t len) |
| 1146 | { |
| 1147 | struct quic_crypto_buf **qcb; |
| 1148 | /* The remaining byte to store in CRYPTO buffers. */ |
| 1149 | size_t cf_offset, cf_len, *nb_buf; |
| 1150 | unsigned char *pos; |
| 1151 | int ret = 0; |
| 1152 | |
| 1153 | nb_buf = &qel->tx.crypto.nb_buf; |
| 1154 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 1155 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 1156 | cf_len = len; |
| 1157 | |
| 1158 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1159 | |
| 1160 | while (len) { |
| 1161 | size_t to_copy, room; |
| 1162 | |
| 1163 | pos = (*qcb)->data + (*qcb)->sz; |
| 1164 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1165 | to_copy = len > room ? room : len; |
| 1166 | if (to_copy) { |
| 1167 | memcpy(pos, data, to_copy); |
| 1168 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1169 | qel->tx.crypto.sz += to_copy; |
| 1170 | (*qcb)->sz += to_copy; |
| 1171 | len -= to_copy; |
| 1172 | data += to_copy; |
| 1173 | } |
| 1174 | else { |
| 1175 | struct quic_crypto_buf **tmp; |
| 1176 | |
| 1177 | // FIXME: realloc! |
| 1178 | tmp = realloc(qel->tx.crypto.bufs, |
| 1179 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1180 | if (tmp) { |
| 1181 | qel->tx.crypto.bufs = tmp; |
| 1182 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1183 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1184 | if (!*qcb) { |
| 1185 | TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc); |
| 1186 | goto leave; |
| 1187 | } |
| 1188 | |
| 1189 | (*qcb)->sz = 0; |
| 1190 | ++*nb_buf; |
| 1191 | } |
| 1192 | else { |
| 1193 | break; |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1199 | * have been buffered. |
| 1200 | */ |
| 1201 | if (!len) { |
| 1202 | struct quic_frame *frm; |
| 1203 | struct quic_frame *found = NULL; |
| 1204 | |
| 1205 | /* There is at most one CRYPTO frame in this packet number |
| 1206 | * space. Let's look for it. |
| 1207 | */ |
| 1208 | list_for_each_entry(frm, &qel->pktns->tx.frms, list) { |
| 1209 | if (frm->type != QUIC_FT_CRYPTO) |
| 1210 | continue; |
| 1211 | |
| 1212 | /* Found */ |
| 1213 | found = frm; |
| 1214 | break; |
| 1215 | } |
| 1216 | |
| 1217 | if (found) { |
| 1218 | found->crypto.len += cf_len; |
| 1219 | } |
| 1220 | else { |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 1221 | frm = qc_frm_alloc(QUIC_FT_CRYPTO); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1222 | if (!frm) { |
| 1223 | TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc); |
| 1224 | goto leave; |
| 1225 | } |
| 1226 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1227 | frm->crypto.offset = cf_offset; |
| 1228 | frm->crypto.len = cf_len; |
| 1229 | frm->crypto.qel = qel; |
| 1230 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
| 1231 | } |
| 1232 | } |
| 1233 | ret = len == 0; |
| 1234 | leave: |
| 1235 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1236 | return ret; |
| 1237 | } |
| 1238 | |
| 1239 | /* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive |
| 1240 | * activity for <qc> will be interrupted. |
| 1241 | */ |
| 1242 | void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err) |
| 1243 | { |
| 1244 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1245 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) |
| 1246 | goto leave; |
| 1247 | |
| 1248 | TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc); |
| 1249 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 1250 | qc->err.code = err.code; |
| 1251 | qc->err.app = err.app; |
| 1252 | leave: |
| 1253 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 1254 | } |
| 1255 | |
| 1256 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1257 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1258 | { |
| 1259 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1260 | |
| 1261 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 1262 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 1263 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
| 1264 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 1265 | } |
| 1266 | quic_set_connection_close(qc, quic_err_tls(alert)); |
| 1267 | qc->flags |= QUIC_FL_CONN_TLS_ALERT; |
| 1268 | TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc); |
| 1269 | |
| 1270 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
| 1271 | } |
| 1272 | |
| 1273 | /* Set the application for <qc> QUIC connection. |
| 1274 | * Return 1 if succeeded, 0 if not. |
| 1275 | */ |
| 1276 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1277 | { |
| 1278 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1279 | qc->app_ops = &h3_ops; |
| 1280 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1281 | qc->app_ops = &hq_interop_ops; |
| 1282 | else |
| 1283 | return 0; |
| 1284 | |
| 1285 | return 1; |
| 1286 | } |
| 1287 | |
| 1288 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1289 | * wants to provide the QUIC layer with CRYPTO data. |
| 1290 | * Returns 1 if succeeded, 0 if not. |
| 1291 | */ |
| 1292 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1293 | const uint8_t *data, size_t len) |
| 1294 | { |
| 1295 | struct quic_conn *qc; |
| 1296 | enum quic_tls_enc_level tel; |
| 1297 | struct quic_enc_level *qel; |
| 1298 | int ret = 0; |
| 1299 | |
| 1300 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1301 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1302 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 1303 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 1304 | TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc); |
| 1305 | goto out; |
| 1306 | } |
| 1307 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1308 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 1309 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
| 1310 | goto out; |
| 1311 | } |
| 1312 | |
| 1313 | tel = ssl_to_quic_enc_level(level); |
| 1314 | if (tel == -1) { |
| 1315 | TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc); |
| 1316 | goto leave; |
| 1317 | } |
| 1318 | |
| 1319 | qel = &qc->els[tel]; |
| 1320 | if (!quic_crypto_data_cpy(qc, qel, data, len)) { |
| 1321 | TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc); |
| 1322 | goto leave; |
| 1323 | } |
| 1324 | |
| 1325 | TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
| 1326 | qc, &level, &len); |
| 1327 | out: |
| 1328 | ret = 1; |
| 1329 | leave: |
| 1330 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1331 | return ret; |
| 1332 | } |
| 1333 | |
| 1334 | int ha_quic_flush_flight(SSL *ssl) |
| 1335 | { |
| 1336 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1337 | |
| 1338 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1339 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
| 1340 | |
| 1341 | return 1; |
| 1342 | } |
| 1343 | |
| 1344 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1345 | { |
| 1346 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1347 | |
| 1348 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1349 | |
| 1350 | TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1351 | |
| 1352 | quic_set_tls_alert(qc, alert); |
| 1353 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
| 1354 | return 1; |
| 1355 | } |
| 1356 | |
| 1357 | /* QUIC TLS methods */ |
| 1358 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1359 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1360 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1361 | .flush_flight = ha_quic_flush_flight, |
| 1362 | .send_alert = ha_quic_send_alert, |
| 1363 | }; |
| 1364 | |
| 1365 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1366 | * Returns an error count. |
| 1367 | */ |
| 1368 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1369 | { |
| 1370 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1371 | int cfgerr = 0; |
| 1372 | |
| 1373 | long options = |
| 1374 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1375 | SSL_OP_SINGLE_ECDH_USE | |
| 1376 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1377 | SSL_CTX *ctx; |
| 1378 | |
| 1379 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1380 | bind_conf->initial_ctx = ctx; |
| 1381 | |
| 1382 | SSL_CTX_set_options(ctx, options); |
| 1383 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1384 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1385 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1386 | |
| 1387 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1388 | # if defined(HAVE_SSL_CLIENT_HELLO_CB) |
| 1389 | # if defined(SSL_OP_NO_ANTI_REPLAY) |
| 1390 | if (bind_conf->ssl_conf.early_data) { |
| 1391 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
| 1392 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
| 1393 | } |
| 1394 | # endif /* !SSL_OP_NO_ANTI_REPLAY */ |
| 1395 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1396 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1397 | # else /* ! HAVE_SSL_CLIENT_HELLO_CB */ |
| 1398 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1399 | # endif |
| 1400 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1401 | #endif |
| 1402 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1403 | |
| 1404 | return cfgerr; |
| 1405 | } |
| 1406 | |
| 1407 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1408 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1409 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1410 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1411 | */ |
| 1412 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1413 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1414 | { |
| 1415 | uint64_t expected_pn = largest_pn + 1; |
| 1416 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1417 | uint64_t pn_hwin = pn_win / 2; |
| 1418 | uint64_t pn_mask = pn_win - 1; |
| 1419 | uint64_t candidate_pn; |
| 1420 | |
| 1421 | |
| 1422 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1423 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1424 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1425 | candidate_pn + pn_hwin <= expected_pn) |
| 1426 | return candidate_pn + pn_win; |
| 1427 | |
| 1428 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1429 | return candidate_pn - pn_win; |
| 1430 | |
| 1431 | return candidate_pn; |
| 1432 | } |
| 1433 | |
| 1434 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1435 | * cryptographic context. |
| 1436 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1437 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1438 | * <end> points to one byte past the end of this packet. |
| 1439 | * Returns 1 if succeeded, 0 if not. |
| 1440 | */ |
| 1441 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1442 | struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
| 1443 | int64_t largest_pn, unsigned char *pn, unsigned char *byte0) |
| 1444 | { |
| 1445 | int ret, i, pnlen; |
| 1446 | uint64_t packet_number; |
| 1447 | uint32_t truncated_pn = 0; |
| 1448 | unsigned char mask[5] = {0}; |
| 1449 | unsigned char *sample; |
| 1450 | EVP_CIPHER_CTX *cctx = NULL; |
| 1451 | |
| 1452 | TRACE_ENTER(QUIC_EV_CONN_RMHP, qc); |
| 1453 | |
| 1454 | ret = 0; |
| 1455 | |
| 1456 | /* Check there is enough data in this packet. */ |
| 1457 | if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
| 1458 | TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1459 | goto leave; |
| 1460 | } |
| 1461 | |
| 1462 | cctx = EVP_CIPHER_CTX_new(); |
| 1463 | if (!cctx) { |
| 1464 | TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1465 | goto leave; |
| 1466 | } |
| 1467 | |
| 1468 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1469 | |
| 1470 | if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) { |
| 1471 | TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1472 | goto leave; |
| 1473 | } |
| 1474 | |
| 1475 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1476 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1477 | for (i = 0; i < pnlen; i++) { |
| 1478 | pn[i] ^= mask[i + 1]; |
| 1479 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1480 | } |
| 1481 | |
| 1482 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1483 | /* Store remaining information for this unprotected header */ |
| 1484 | pkt->pn = packet_number; |
| 1485 | pkt->pnl = pnlen; |
| 1486 | |
| 1487 | ret = 1; |
| 1488 | leave: |
| 1489 | if (cctx) |
| 1490 | EVP_CIPHER_CTX_free(cctx); |
| 1491 | TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc); |
| 1492 | return ret; |
| 1493 | } |
| 1494 | |
| 1495 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1496 | * address, with <payload_len> as payload length, <aad> as address of |
| 1497 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1498 | * context. |
| 1499 | * Returns 1 if succeeded, 0 if not. |
| 1500 | */ |
| 1501 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1502 | unsigned char *aad, size_t aad_len, uint64_t pn, |
| 1503 | struct quic_tls_ctx *tls_ctx, struct quic_conn *qc) |
| 1504 | { |
| 1505 | int ret = 0; |
| 1506 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 1507 | unsigned char *tx_iv = tls_ctx->tx.iv; |
| 1508 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
| 1509 | struct enc_debug_info edi; |
| 1510 | |
| 1511 | TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc); |
| 1512 | |
| 1513 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
| 1514 | TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc); |
| 1515 | goto err; |
| 1516 | } |
| 1517 | |
| 1518 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1519 | tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
| 1520 | TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc); |
| 1521 | goto err; |
| 1522 | } |
| 1523 | |
| 1524 | ret = 1; |
| 1525 | leave: |
| 1526 | TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc); |
| 1527 | return ret; |
| 1528 | |
| 1529 | err: |
| 1530 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
| 1531 | goto leave; |
| 1532 | } |
| 1533 | |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 1534 | /* Select the correct TLS cipher context to used to decipher <pkt> packet |
| 1535 | * attached to <qc> connection from <qel> encryption level. |
| 1536 | */ |
| 1537 | static inline struct quic_tls_ctx *qc_select_tls_ctx(struct quic_conn *qc, |
| 1538 | struct quic_enc_level *qel, |
| 1539 | struct quic_rx_packet *pkt) |
| 1540 | { |
| 1541 | return pkt->type != QUIC_PACKET_TYPE_INITIAL ? &qel->tls_ctx : |
| 1542 | pkt->version == qc->negotiated_version ? &qc->negotiated_ictx : &qel->tls_ctx; |
| 1543 | } |
| 1544 | |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1545 | /* Decrypt <pkt> packet using encryption level <qel> for <qc> connection. |
| 1546 | * Decryption is done in place in packet buffer. |
| 1547 | * |
Ilya Shipitsin | 5fa29b8 | 2022-12-07 09:46:19 +0500 | [diff] [blame] | 1548 | * Returns 1 on success else 0. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1549 | */ |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1550 | static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel, |
| 1551 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1552 | { |
| 1553 | int ret, kp_changed; |
| 1554 | unsigned char iv[QUIC_TLS_IV_LEN]; |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 1555 | 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] | 1556 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
| 1557 | unsigned char *rx_iv = tls_ctx->rx.iv; |
| 1558 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1559 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1560 | |
| 1561 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 1562 | |
| 1563 | ret = 0; |
| 1564 | kp_changed = 0; |
| 1565 | |
| 1566 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1567 | /* The two tested bits are not at the same position, |
| 1568 | * this is why they are first both inversed. |
| 1569 | */ |
| 1570 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1571 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1572 | /* The lowest packet number of a previous key phase |
| 1573 | * cannot be null if it really stores previous key phase |
| 1574 | * secrets. |
| 1575 | */ |
| 1576 | // TODO: check if BUG_ON() more suitable |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1577 | if (!qc->ku.prv_rx.pn) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1578 | TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc); |
| 1579 | goto leave; |
| 1580 | } |
| 1581 | |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1582 | rx_ctx = qc->ku.prv_rx.ctx; |
| 1583 | rx_iv = qc->ku.prv_rx.iv; |
| 1584 | rx_key = qc->ku.prv_rx.key; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1585 | } |
| 1586 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1587 | /* Next key phase */ |
Frédéric Lécaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 1588 | TRACE_PROTO("Key phase changed", QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1589 | kp_changed = 1; |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1590 | rx_ctx = qc->ku.nxt_rx.ctx; |
| 1591 | rx_iv = qc->ku.nxt_rx.iv; |
| 1592 | rx_key = qc->ku.nxt_rx.key; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) { |
| 1598 | TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1599 | goto leave; |
| 1600 | } |
| 1601 | |
| 1602 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1603 | pkt->data, pkt->aad_len, |
| 1604 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
| 1605 | if (!ret) { |
| 1606 | TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1607 | goto leave; |
| 1608 | } |
| 1609 | |
| 1610 | /* Update the keys only if the packet decryption succeeded. */ |
| 1611 | if (kp_changed) { |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1612 | quic_tls_rotate_keys(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1613 | /* Toggle the Key Phase bit */ |
| 1614 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1615 | /* Store the lowest packet number received for the current key phase */ |
| 1616 | tls_ctx->rx.pn = pkt->pn; |
| 1617 | /* Prepare the next key update */ |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1618 | if (!quic_tls_key_update(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1619 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1620 | goto leave; |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | /* Update the packet length (required to parse the frames). */ |
| 1625 | pkt->len -= QUIC_TLS_TAG_LEN; |
| 1626 | ret = 1; |
| 1627 | leave: |
| 1628 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 1629 | return ret; |
| 1630 | } |
| 1631 | |
| 1632 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1633 | /* Release <frm> frame and mark its copies as acknowledged */ |
| 1634 | void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm) |
| 1635 | { |
| 1636 | uint64_t pn; |
| 1637 | struct quic_frame *origin, *f, *tmp; |
| 1638 | |
| 1639 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1640 | |
| 1641 | /* Identify this frame: a frame copy or one of its copies */ |
| 1642 | origin = frm->origin ? frm->origin : frm; |
| 1643 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1644 | * possibly a copy of <origin> |
| 1645 | */ |
| 1646 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1647 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1648 | * not release the packets (releasing the frames) at this time as |
| 1649 | * they are possibly also to be acknowledged alongside the |
| 1650 | * the current one. |
| 1651 | */ |
| 1652 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
| 1653 | if (f->pkt) { |
| 1654 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1655 | f->origin = NULL; |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1656 | LIST_DEL_INIT(&f->ref); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1657 | pn = f->pkt->pn_node.key; |
| 1658 | TRACE_DEVEL("mark frame as acked from packet", |
| 1659 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1660 | } |
| 1661 | else { |
| 1662 | TRACE_DEVEL("freeing unsent frame", |
| 1663 | QUIC_EV_CONN_PRSAFRM, qc, f); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1664 | LIST_DEL_INIT(&f->ref); |
| 1665 | qc_frm_free(&f); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1666 | } |
| 1667 | } |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1668 | LIST_DEL_INIT(&frm->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1669 | pn = frm->pkt->pn_node.key; |
| 1670 | quic_tx_packet_refdec(frm->pkt); |
| 1671 | TRACE_DEVEL("freeing frame from packet", |
| 1672 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1673 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1674 | |
| 1675 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1676 | } |
| 1677 | |
| 1678 | /* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released |
| 1679 | * and all STREAM data are acknowledged. The MUX is responsible to have set |
| 1680 | * <qc.err> before as it is reused for the CONNECTION_CLOSE frame. |
| 1681 | * |
| 1682 | * TODO this should also be called on lost packet detection |
| 1683 | */ |
| 1684 | void qc_check_close_on_released_mux(struct quic_conn *qc) |
| 1685 | { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1686 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1687 | |
| 1688 | if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) { |
| 1689 | /* Reuse errcode which should have been previously set by the MUX on release. */ |
| 1690 | quic_set_connection_close(qc, qc->err); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 1691 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 1695 | } |
| 1696 | |
| 1697 | /* Remove from <stream> the acknowledged frames. |
| 1698 | * |
| 1699 | * Returns 1 if at least one frame was removed else 0. |
| 1700 | */ |
| 1701 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1702 | struct qc_stream_desc *stream) |
| 1703 | { |
| 1704 | int ret; |
| 1705 | struct eb64_node *frm_node; |
| 1706 | |
| 1707 | TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc); |
| 1708 | |
| 1709 | ret = 0; |
| 1710 | frm_node = eb64_first(&stream->acked_frms); |
| 1711 | while (frm_node) { |
| 1712 | struct quic_stream *strm; |
| 1713 | struct quic_frame *frm; |
| 1714 | size_t offset, len; |
| 1715 | |
| 1716 | strm = eb64_entry(frm_node, struct quic_stream, offset); |
| 1717 | offset = strm->offset.key; |
| 1718 | len = strm->len; |
| 1719 | |
| 1720 | if (offset > stream->ack_offset) |
| 1721 | break; |
| 1722 | |
| 1723 | if (qc_stream_desc_ack(&stream, offset, len)) { |
| 1724 | /* cf. next comment : frame may be freed at this stage. */ |
| 1725 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1726 | qc, stream ? strm : NULL, stream); |
| 1727 | ret = 1; |
| 1728 | } |
| 1729 | |
| 1730 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1731 | * has been freed. with the stream frames tree. Nothing to do |
| 1732 | * anymore in here. |
| 1733 | */ |
| 1734 | if (!stream) { |
| 1735 | qc_check_close_on_released_mux(qc); |
| 1736 | ret = 1; |
| 1737 | goto leave; |
| 1738 | } |
| 1739 | |
| 1740 | frm_node = eb64_next(frm_node); |
| 1741 | eb64_delete(&strm->offset); |
| 1742 | |
| 1743 | frm = container_of(strm, struct quic_frame, stream); |
| 1744 | qc_release_frm(qc, frm); |
| 1745 | } |
| 1746 | |
| 1747 | leave: |
| 1748 | TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc); |
| 1749 | return ret; |
| 1750 | } |
| 1751 | |
| 1752 | /* Treat <frm> frame whose packet it is attached to has just been acknowledged. */ |
| 1753 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1754 | struct quic_frame *frm) |
| 1755 | { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1756 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1757 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1758 | switch (frm->type) { |
| 1759 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1760 | { |
| 1761 | struct quic_stream *strm_frm = &frm->stream; |
| 1762 | struct eb64_node *node = NULL; |
| 1763 | struct qc_stream_desc *stream = NULL; |
| 1764 | const size_t offset = strm_frm->offset.key; |
| 1765 | const size_t len = strm_frm->len; |
| 1766 | |
| 1767 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1768 | * might be freed at this stage. Use the id to do a proper |
| 1769 | * lookup. |
| 1770 | * |
| 1771 | * TODO if lookup operation impact on the perf is noticeable, |
| 1772 | * implement a refcount on qc_stream_desc instances. |
| 1773 | */ |
| 1774 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1775 | if (!node) { |
| 1776 | TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm); |
| 1777 | qc_release_frm(qc, frm); |
| 1778 | /* early return */ |
| 1779 | goto leave; |
| 1780 | } |
| 1781 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1782 | |
| 1783 | TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
| 1784 | if (offset <= stream->ack_offset) { |
| 1785 | if (qc_stream_desc_ack(&stream, offset, len)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1786 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1787 | qc, strm_frm, stream); |
| 1788 | } |
| 1789 | |
| 1790 | if (!stream) { |
| 1791 | /* no need to continue if stream freed. */ |
| 1792 | TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc); |
| 1793 | qc_release_frm(qc, frm); |
| 1794 | qc_check_close_on_released_mux(qc); |
| 1795 | break; |
| 1796 | } |
| 1797 | |
| 1798 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1799 | qc, strm_frm, stream); |
| 1800 | qc_release_frm(qc, frm); |
| 1801 | } |
| 1802 | else { |
| 1803 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
| 1804 | } |
| 1805 | |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 1806 | quic_stream_try_to_consume(qc, stream); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1807 | } |
| 1808 | break; |
| 1809 | default: |
| 1810 | qc_release_frm(qc, frm); |
| 1811 | } |
| 1812 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1813 | leave: |
| 1814 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1815 | } |
| 1816 | |
| 1817 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1818 | * deallocating them, and their TX frames. |
| 1819 | * Returns the last node reached to be used for the next range. |
| 1820 | * May be NULL if <largest> node could not be found. |
| 1821 | */ |
| 1822 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1823 | struct eb_root *pkts, |
| 1824 | unsigned int *pkt_flags, |
| 1825 | struct list *newly_acked_pkts, |
| 1826 | struct eb64_node *largest_node, |
| 1827 | uint64_t largest, uint64_t smallest) |
| 1828 | { |
| 1829 | struct eb64_node *node; |
| 1830 | struct quic_tx_packet *pkt; |
| 1831 | |
| 1832 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1833 | |
| 1834 | node = largest_node ? largest_node : eb64_lookup_le(pkts, largest); |
| 1835 | while (node && node->key >= smallest) { |
| 1836 | struct quic_frame *frm, *frmbak; |
| 1837 | |
| 1838 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 1839 | *pkt_flags |= pkt->flags; |
| 1840 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
| 1841 | TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 1842 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1843 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 1844 | /* If there are others packet in the same datagram <pkt> is attached to, |
| 1845 | * detach the previous one and the next one from <pkt>. |
| 1846 | */ |
Frédéric Lécaille | 74b5f7b | 2022-11-20 18:35:35 +0100 | [diff] [blame] | 1847 | quic_tx_packet_dgram_detach(pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1848 | node = eb64_prev(node); |
| 1849 | eb64_delete(&pkt->pn_node); |
| 1850 | } |
| 1851 | |
| 1852 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1853 | return node; |
| 1854 | } |
| 1855 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1856 | /* Remove all frames from <pkt_frm_list> and reinsert them in the same order |
| 1857 | * they have been sent into <pktns_frm_list>. The loss counter of each frame is |
| 1858 | * incremented and checked if it does not exceed retransmission limit. |
| 1859 | * |
| 1860 | * Returns 1 on success, 0 if a frame loss limit is exceeded. A |
| 1861 | * CONNECTION_CLOSE is scheduled in this case. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1862 | */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1863 | static inline int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1864 | struct quic_tx_packet *pkt, |
| 1865 | struct list *pktns_frm_list) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1866 | { |
| 1867 | struct quic_frame *frm, *frmbak; |
| 1868 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1869 | struct list *pkt_frm_list = &pkt->frms; |
| 1870 | uint64_t pn = pkt->pn_node.key; |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1871 | int close = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1872 | |
| 1873 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1874 | |
| 1875 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1876 | /* First remove this frame from the packet it was attached to */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1877 | LIST_DEL_INIT(&frm->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1878 | quic_tx_packet_refdec(pkt); |
| 1879 | /* At this time, this frame is not freed but removed from its packet */ |
| 1880 | frm->pkt = NULL; |
| 1881 | /* Remove any reference to this frame */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1882 | qc_frm_unref(frm, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1883 | switch (frm->type) { |
| 1884 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1885 | { |
| 1886 | struct quic_stream *strm_frm = &frm->stream; |
| 1887 | struct eb64_node *node = NULL; |
| 1888 | struct qc_stream_desc *stream_desc; |
| 1889 | |
| 1890 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1891 | if (!node) { |
| 1892 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1893 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1894 | qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1895 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1896 | continue; |
| 1897 | } |
| 1898 | |
| 1899 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1900 | /* Do not resend this frame if in the "already acked range" */ |
| 1901 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 1902 | TRACE_DEVEL("ignored frame in already acked range", |
| 1903 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1904 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1905 | continue; |
| 1906 | } |
| 1907 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 1908 | strm_frm->offset.key = stream_desc->ack_offset; |
| 1909 | TRACE_DEVEL("updated partially acked frame", |
| 1910 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1911 | } |
| 1912 | break; |
| 1913 | } |
| 1914 | |
| 1915 | default: |
| 1916 | break; |
| 1917 | } |
| 1918 | |
| 1919 | /* Do not resend probing packet with old data */ |
| 1920 | if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) { |
| 1921 | TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 1922 | qc, frm, &pn); |
| 1923 | if (frm->origin) |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1924 | LIST_DEL_INIT(&frm->ref); |
| 1925 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1926 | continue; |
| 1927 | } |
| 1928 | |
| 1929 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 1930 | TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1931 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1932 | qc, frm, &pn); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1933 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1934 | } |
| 1935 | else { |
Amaury Denoyelle | 24d5b72 | 2023-01-31 11:44:50 +0100 | [diff] [blame] | 1936 | if (++frm->loss_count >= global.tune.quic_max_frame_loss) { |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1937 | TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc); |
| 1938 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR)); |
| 1939 | close = 1; |
| 1940 | } |
| 1941 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1942 | LIST_APPEND(&tmp, &frm->list); |
| 1943 | TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | LIST_SPLICE(pktns_frm_list, &tmp); |
| 1948 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1949 | end: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1950 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 1951 | return !close; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | /* Free <pkt> TX packet and its attached frames. |
| 1955 | * This is the responsibility of the caller to remove this packet of |
| 1956 | * any data structure it was possibly attached to. |
| 1957 | */ |
| 1958 | static inline void free_quic_tx_packet(struct quic_conn *qc, |
| 1959 | struct quic_tx_packet *pkt) |
| 1960 | { |
| 1961 | struct quic_frame *frm, *frmbak; |
| 1962 | |
| 1963 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1964 | |
| 1965 | if (!pkt) |
| 1966 | goto leave; |
| 1967 | |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 1968 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1969 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1970 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1971 | |
| 1972 | leave: |
| 1973 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 1974 | } |
| 1975 | |
| 1976 | /* Free the TX packets of <pkts> list */ |
| 1977 | static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts) |
| 1978 | { |
| 1979 | struct quic_tx_packet *pkt, *tmp; |
| 1980 | |
| 1981 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1982 | |
| 1983 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 1984 | LIST_DELETE(&pkt->list); |
| 1985 | eb64_delete(&pkt->pn_node); |
| 1986 | free_quic_tx_packet(qc, pkt); |
| 1987 | } |
| 1988 | |
| 1989 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 1990 | } |
| 1991 | |
| 1992 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1993 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1994 | * updating the range which contains <largest_acked_pn>. |
| 1995 | * Never fails. |
| 1996 | */ |
| 1997 | static void qc_treat_ack_of_ack(struct quic_conn *qc, |
| 1998 | struct quic_pktns *pktns, |
| 1999 | int64_t largest_acked_pn) |
| 2000 | { |
| 2001 | struct eb64_node *ar, *next_ar; |
| 2002 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 2003 | |
| 2004 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2005 | |
| 2006 | ar = eb64_first(&arngs->root); |
| 2007 | while (ar) { |
| 2008 | struct quic_arng_node *ar_node; |
| 2009 | |
| 2010 | next_ar = eb64_next(ar); |
| 2011 | ar_node = eb64_entry(ar, struct quic_arng_node, first); |
| 2012 | |
| 2013 | if ((int64_t)ar_node->first.key > largest_acked_pn) { |
| 2014 | TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc); |
| 2015 | break; |
| 2016 | } |
| 2017 | |
| 2018 | if (largest_acked_pn < ar_node->last) { |
| 2019 | eb64_delete(ar); |
| 2020 | ar_node->first.key = largest_acked_pn + 1; |
| 2021 | eb64_insert(&arngs->root, ar); |
| 2022 | break; |
| 2023 | } |
| 2024 | |
| 2025 | eb64_delete(ar); |
| 2026 | pool_free(pool_head_quic_arng, ar_node); |
| 2027 | arngs->sz--; |
| 2028 | ar = next_ar; |
| 2029 | } |
| 2030 | |
| 2031 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2032 | } |
| 2033 | |
| 2034 | /* Send a packet ack event nofication for each newly acked packet of |
| 2035 | * <newly_acked_pkts> list and free them. |
| 2036 | * Always succeeds. |
| 2037 | */ |
| 2038 | static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc, |
| 2039 | struct list *newly_acked_pkts) |
| 2040 | { |
| 2041 | struct quic_tx_packet *pkt, *tmp; |
| 2042 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 2043 | |
| 2044 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2045 | |
| 2046 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 2047 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
| 2048 | qc->path->prep_in_flight -= pkt->in_flight_len; |
| 2049 | qc->path->in_flight -= pkt->in_flight_len; |
| 2050 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
| 2051 | qc->path->ifae_pkts--; |
| 2052 | /* If this packet contained an ACK frame, proceed to the |
| 2053 | * acknowledging of range of acks from the largest acknowledged |
| 2054 | * packet number which was sent in an ACK frame by this packet. |
| 2055 | */ |
| 2056 | if (pkt->largest_acked_pn != -1) |
| 2057 | qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn); |
| 2058 | ev.ack.acked = pkt->in_flight_len; |
| 2059 | ev.ack.time_sent = pkt->time_sent; |
| 2060 | quic_cc_event(&qc->path->cc, &ev); |
| 2061 | LIST_DELETE(&pkt->list); |
| 2062 | eb64_delete(&pkt->pn_node); |
| 2063 | quic_tx_packet_refdec(pkt); |
| 2064 | } |
| 2065 | |
| 2066 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2067 | |
| 2068 | } |
| 2069 | |
| 2070 | /* Release all the frames attached to <pktns> packet number space */ |
| 2071 | static inline void qc_release_pktns_frms(struct quic_conn *qc, |
| 2072 | struct quic_pktns *pktns) |
| 2073 | { |
| 2074 | struct quic_frame *frm, *frmbak; |
| 2075 | |
| 2076 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2077 | |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 2078 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) |
| 2079 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2080 | |
| 2081 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2082 | } |
| 2083 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2084 | /* Handle <pkts> list of lost packets detected at <now_us> handling their TX |
| 2085 | * frames. Send a packet loss event to the congestion controller if in flight |
| 2086 | * packet have been lost. Also frees the packet in <pkts> list. |
| 2087 | * |
| 2088 | * Returns 1 on success else 0 if loss limit has been exceeded. A |
| 2089 | * CONNECTION_CLOSE was prepared to close the connection ASAP. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2090 | */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2091 | static inline int qc_release_lost_pkts(struct quic_conn *qc, |
| 2092 | struct quic_pktns *pktns, |
| 2093 | struct list *pkts, |
| 2094 | uint64_t now_us) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2095 | { |
| 2096 | struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost; |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2097 | int close = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2098 | |
| 2099 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2100 | |
| 2101 | if (LIST_ISEMPTY(pkts)) |
| 2102 | goto leave; |
| 2103 | |
| 2104 | oldest_lost = newest_lost = NULL; |
| 2105 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 2106 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2107 | |
| 2108 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
| 2109 | qc->path->prep_in_flight -= pkt->in_flight_len; |
| 2110 | qc->path->in_flight -= pkt->in_flight_len; |
| 2111 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
| 2112 | qc->path->ifae_pkts--; |
| 2113 | /* Treat the frames of this lost packet. */ |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2114 | if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms)) |
| 2115 | close = 1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2116 | LIST_DELETE(&pkt->list); |
| 2117 | if (!oldest_lost) { |
| 2118 | oldest_lost = newest_lost = pkt; |
| 2119 | } |
| 2120 | else { |
| 2121 | if (newest_lost != oldest_lost) |
| 2122 | quic_tx_packet_refdec(newest_lost); |
| 2123 | newest_lost = pkt; |
| 2124 | } |
| 2125 | } |
| 2126 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2127 | if (!close) { |
| 2128 | if (newest_lost) { |
| 2129 | /* Sent a congestion event to the controller */ |
| 2130 | struct quic_cc_event ev = { }; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2131 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2132 | ev.type = QUIC_CC_EVT_LOSS; |
| 2133 | ev.loss.time_sent = newest_lost->time_sent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2134 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2135 | quic_cc_event(&qc->path->cc, &ev); |
| 2136 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2137 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2138 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 2139 | * We must check if we are experiencing a persistent congestion. |
| 2140 | * If this is the case, the congestion controller must re-enter |
| 2141 | * slow start state. |
| 2142 | */ |
| 2143 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 2144 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2145 | |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2146 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 2147 | now_ms, qc->max_ack_delay)) |
| 2148 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 2149 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2150 | } |
| 2151 | |
Amaury Denoyelle | 3a72ba2 | 2022-11-14 11:41:34 +0100 | [diff] [blame] | 2152 | /* <oldest_lost> cannot be NULL at this stage because we have ensured |
| 2153 | * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a |
| 2154 | * possible overflow on a 0 byte region with O2 optimization. |
| 2155 | */ |
| 2156 | ALREADY_CHECKED(oldest_lost); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2157 | quic_tx_packet_refdec(oldest_lost); |
| 2158 | if (newest_lost != oldest_lost) |
| 2159 | quic_tx_packet_refdec(newest_lost); |
| 2160 | |
| 2161 | leave: |
| 2162 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2163 | return !close; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 2167 | * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e. |
| 2168 | * if the largest acked packet was newly acked and if there was at least one newly |
| 2169 | * acked ack-eliciting packet. |
| 2170 | * Return 1, if succeeded, 0 if not. |
| 2171 | */ |
| 2172 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 2173 | struct quic_frame *frm, |
| 2174 | struct quic_enc_level *qel, |
| 2175 | unsigned int *rtt_sample, |
| 2176 | const unsigned char **pos, const unsigned char *end) |
| 2177 | { |
| 2178 | struct quic_ack *ack = &frm->ack; |
| 2179 | uint64_t smallest, largest; |
| 2180 | struct eb_root *pkts; |
| 2181 | struct eb64_node *largest_node; |
| 2182 | unsigned int time_sent, pkt_flags; |
| 2183 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 2184 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 2185 | int ret = 0; |
| 2186 | |
| 2187 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2188 | |
| 2189 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 2190 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
| 2191 | qc, NULL, &ack->largest_ack); |
| 2192 | goto err; |
| 2193 | } |
| 2194 | |
| 2195 | if (ack->first_ack_range > ack->largest_ack) { |
| 2196 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
| 2197 | qc, NULL, &ack->first_ack_range); |
| 2198 | goto err; |
| 2199 | } |
| 2200 | |
| 2201 | largest = ack->largest_ack; |
| 2202 | smallest = largest - ack->first_ack_range; |
| 2203 | pkts = &qel->pktns->tx.pkts; |
| 2204 | pkt_flags = 0; |
| 2205 | largest_node = NULL; |
| 2206 | time_sent = 0; |
| 2207 | |
| 2208 | if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) { |
| 2209 | largest_node = eb64_lookup(pkts, largest); |
| 2210 | if (!largest_node) { |
| 2211 | TRACE_DEVEL("Largest acked packet not found", |
| 2212 | QUIC_EV_CONN_PRSAFRM, qc); |
| 2213 | } |
| 2214 | else { |
| 2215 | time_sent = eb64_entry(largest_node, |
| 2216 | struct quic_tx_packet, pn_node)->time_sent; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM, |
| 2221 | qc, NULL, &largest, &smallest); |
| 2222 | do { |
| 2223 | uint64_t gap, ack_range; |
| 2224 | |
| 2225 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 2226 | largest_node, largest, smallest); |
| 2227 | if (!ack->ack_range_num--) |
| 2228 | break; |
| 2229 | |
| 2230 | if (!quic_dec_int(&gap, pos, end)) { |
| 2231 | TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc); |
| 2232 | goto err; |
| 2233 | } |
| 2234 | |
| 2235 | if (smallest < gap + 2) { |
| 2236 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
| 2237 | qc, NULL, &gap, &smallest); |
| 2238 | goto err; |
| 2239 | } |
| 2240 | |
| 2241 | largest = smallest - gap - 2; |
| 2242 | if (!quic_dec_int(&ack_range, pos, end)) { |
| 2243 | TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc); |
| 2244 | goto err; |
| 2245 | } |
| 2246 | |
| 2247 | if (largest < ack_range) { |
| 2248 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
| 2249 | qc, NULL, &largest, &ack_range); |
| 2250 | goto err; |
| 2251 | } |
| 2252 | |
| 2253 | /* Do not use this node anymore. */ |
| 2254 | largest_node = NULL; |
| 2255 | /* Next range */ |
| 2256 | smallest = largest - ack_range; |
| 2257 | |
| 2258 | TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM, |
| 2259 | qc, NULL, &largest, &smallest); |
| 2260 | } while (1); |
| 2261 | |
| 2262 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2263 | *rtt_sample = tick_remain(time_sent, now_ms); |
| 2264 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
| 2265 | } |
| 2266 | |
| 2267 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 2268 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
| 2269 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 2270 | if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms)) |
| 2271 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2272 | } |
| 2273 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 2274 | if (quic_peer_validated_addr(qc)) |
| 2275 | qc->path->loss.pto_count = 0; |
| 2276 | qc_set_timer(qc); |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 2277 | qc_notify_send(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | ret = 1; |
| 2281 | leave: |
| 2282 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2283 | return ret; |
| 2284 | |
| 2285 | err: |
| 2286 | free_quic_tx_pkts(qc, &newly_acked_pkts); |
| 2287 | goto leave; |
| 2288 | } |
| 2289 | |
| 2290 | /* This function gives the detail of the SSL error. It is used only |
| 2291 | * if the debug mode and the verbose mode are activated. It dump all |
| 2292 | * the SSL error until the stack was empty. |
| 2293 | */ |
| 2294 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2295 | { |
| 2296 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2297 | while (1) { |
| 2298 | const char *func = NULL; |
| 2299 | unsigned long ret; |
| 2300 | |
| 2301 | ERR_peek_error_func(&func); |
| 2302 | ret = ERR_get_error(); |
| 2303 | if (!ret) |
| 2304 | return; |
| 2305 | |
| 2306 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 2307 | func, ERR_reason_error_string(ret)); |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2313 | const char **str, int *len); |
| 2314 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2315 | /* Finalize <qc> QUIC connection: |
| 2316 | * - initialize the Initial QUIC TLS context for negotiated version, |
| 2317 | * - derive the secrets for this context, |
| 2318 | * - set them into the TLS stack, |
| 2319 | * |
| 2320 | * MUST be called after having received the remote transport parameters which |
| 2321 | * are parsed when the TLS callback for the ClientHello message is called upon |
| 2322 | * SSL_do_handshake() calls, not necessarily at the first time as this TLS |
| 2323 | * message may be splitted between packets |
| 2324 | * Return 1 if succeeded, 0 if not. |
| 2325 | */ |
| 2326 | static int qc_conn_finalize(struct quic_conn *qc, int server) |
| 2327 | { |
| 2328 | int ret = 0; |
| 2329 | |
| 2330 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 2331 | |
| 2332 | if (qc->flags & QUIC_FL_CONN_FINALIZED) |
| 2333 | goto finalized; |
| 2334 | |
| 2335 | if (qc->negotiated_version && |
| 2336 | !qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version, |
| 2337 | qc->odcid.data, qc->odcid.len, server)) |
| 2338 | goto out; |
| 2339 | |
| 2340 | /* This connection is functional (ready to send/receive) */ |
| 2341 | qc->flags |= QUIC_FL_CONN_FINALIZED; |
| 2342 | |
| 2343 | finalized: |
| 2344 | ret = 1; |
| 2345 | out: |
| 2346 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 2347 | return ret; |
| 2348 | } |
| 2349 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2350 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2351 | * from <qel> encryption level with <ctx> as QUIC connection context. |
| 2352 | * Remaining parameter are there for debugging purposes. |
| 2353 | * Return 1 if succeeded, 0 if not. |
| 2354 | */ |
| 2355 | static inline int qc_provide_cdata(struct quic_enc_level *el, |
| 2356 | struct ssl_sock_ctx *ctx, |
| 2357 | const unsigned char *data, size_t len, |
| 2358 | struct quic_rx_packet *pkt, |
| 2359 | struct quic_rx_crypto_frm *cf) |
| 2360 | { |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2361 | #ifdef DEBUG_STRICT |
| 2362 | enum ncb_ret ncb_ret; |
| 2363 | #endif |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2364 | int ssl_err, state; |
| 2365 | struct quic_conn *qc; |
| 2366 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2367 | struct ncbuf *ncbuf = &el->cstream->rx.ncbuf; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2368 | |
| 2369 | ssl_err = SSL_ERROR_NONE; |
| 2370 | qc = ctx->qc; |
| 2371 | |
| 2372 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2373 | |
| 2374 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2375 | TRACE_ERROR("SSL_provide_quic_data() error", |
| 2376 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
| 2377 | goto leave; |
| 2378 | } |
| 2379 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2380 | TRACE_PROTO("in order CRYPTO data", |
| 2381 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
| 2382 | |
| 2383 | state = qc->state; |
| 2384 | if (state < QUIC_HS_ST_COMPLETE) { |
| 2385 | ssl_err = SSL_do_handshake(ctx->ssl); |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2386 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 2387 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 2388 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc); |
| 2389 | goto leave; |
| 2390 | } |
| 2391 | |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2392 | /* Finalize the connection as soon as possible if the peer transport parameters |
| 2393 | * have been received. This may be useful to send packets even if this |
| 2394 | * handshake fails. |
| 2395 | */ |
| 2396 | if ((qc->flags & QUIC_FL_CONN_TX_TP_RECEIVED) && !qc_conn_finalize(qc, 1)) { |
| 2397 | TRACE_ERROR("connection finalization failed", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2398 | goto leave; |
| 2399 | } |
| 2400 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2401 | if (ssl_err != 1) { |
| 2402 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2403 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2404 | TRACE_PROTO("SSL handshake in progress", |
| 2405 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2406 | goto out; |
| 2407 | } |
| 2408 | |
| 2409 | /* TODO: Should close the connection asap */ |
| 2410 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2411 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2412 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2413 | HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail); |
| 2414 | } |
| 2415 | TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2416 | qc_ssl_dump_errors(ctx->conn); |
| 2417 | ERR_clear_error(); |
| 2418 | goto leave; |
| 2419 | } |
| 2420 | |
| 2421 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2422 | |
| 2423 | /* Check the alpn could be negotiated */ |
| 2424 | if (!qc->app_ops) { |
| 2425 | TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2426 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
| 2427 | goto leave; |
| 2428 | } |
| 2429 | |
| 2430 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2431 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2432 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2433 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2434 | } |
| 2435 | /* I/O callback switch */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2436 | qc->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2437 | if (qc_is_listener(ctx->qc)) { |
| 2438 | qc->state = QUIC_HS_ST_CONFIRMED; |
| 2439 | /* The connection is ready to be accepted. */ |
| 2440 | quic_accept_push_qc(qc); |
| 2441 | } |
| 2442 | else { |
| 2443 | qc->state = QUIC_HS_ST_COMPLETE; |
| 2444 | } |
| 2445 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 2446 | /* Prepare the next key update */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2447 | if (!quic_tls_key_update(qc)) { |
| 2448 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc); |
| 2449 | goto leave; |
| 2450 | } |
| 2451 | } else { |
| 2452 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2453 | if (ssl_err != 1) { |
| 2454 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2455 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2456 | TRACE_PROTO("SSL post handshake in progress", |
| 2457 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2458 | goto out; |
| 2459 | } |
| 2460 | |
| 2461 | TRACE_ERROR("SSL post handshake error", |
| 2462 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2463 | goto leave; |
| 2464 | } |
| 2465 | |
| 2466 | TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2467 | } |
| 2468 | |
| 2469 | out: |
| 2470 | ret = 1; |
| 2471 | leave: |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2472 | /* The CRYPTO data are consumed even in case of an error to release |
| 2473 | * the memory asap. |
| 2474 | */ |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2475 | if (!ncb_is_null(ncbuf)) { |
| 2476 | #ifdef DEBUG_STRICT |
| 2477 | ncb_ret = ncb_advance(ncbuf, len); |
| 2478 | /* ncb_advance() must always succeed. This is guaranteed as |
| 2479 | * this is only done inside a data block. If false, this will |
| 2480 | * lead to handshake failure with quic_enc_level offset shifted |
| 2481 | * from buffer data. |
| 2482 | */ |
| 2483 | BUG_ON(ncb_ret != NCB_RET_OK); |
| 2484 | #else |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2485 | ncb_advance(ncbuf, len); |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2486 | #endif |
| 2487 | } |
| 2488 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2489 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
| 2490 | return ret; |
| 2491 | } |
| 2492 | |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2493 | /* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc> |
| 2494 | * connection. <fin> is true if FIN bit is set on frame type. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2495 | * |
| 2496 | * Return 1 on success. On error, 0 is returned. In this case, the packet |
| 2497 | * containing the frame must not be acknowledged. |
| 2498 | */ |
| 2499 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2500 | struct quic_stream *strm_frm, |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2501 | struct quic_conn *qc, char fin) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2502 | { |
| 2503 | int ret; |
| 2504 | |
| 2505 | /* RFC9000 13.1. Packet Processing |
| 2506 | * |
| 2507 | * A packet MUST NOT be acknowledged until packet protection has been |
| 2508 | * successfully removed and all frames contained in the packet have |
| 2509 | * been processed. For STREAM frames, this means the data has been |
| 2510 | * enqueued in preparation to be received by the application protocol, |
| 2511 | * but it does not require that data be delivered and consumed. |
| 2512 | */ |
| 2513 | TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc); |
| 2514 | |
| 2515 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 2516 | strm_frm->offset.key, fin, (char *)strm_frm->data); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2517 | |
| 2518 | /* frame rejected - packet must not be acknowledeged */ |
| 2519 | TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc); |
| 2520 | return !ret; |
| 2521 | } |
| 2522 | |
| 2523 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2524 | * for <qc> QUIC connection. |
| 2525 | * This is a best effort function which never fails even if no memory could be |
| 2526 | * allocated to duplicate these frames. |
| 2527 | */ |
| 2528 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2529 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2530 | { |
| 2531 | struct quic_frame *frm, *frmbak; |
| 2532 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2533 | |
| 2534 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2535 | |
| 2536 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2537 | struct quic_frame *dup_frm, *origin; |
| 2538 | |
Frédéric Lécaille | e6359b6 | 2023-03-02 14:49:22 +0100 | [diff] [blame] | 2539 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 2540 | TRACE_DEVEL("already acknowledged frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2541 | continue; |
| 2542 | } |
| 2543 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2544 | switch (frm->type) { |
| 2545 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2546 | { |
| 2547 | struct quic_stream *strm_frm = &frm->stream; |
| 2548 | struct eb64_node *node = NULL; |
| 2549 | struct qc_stream_desc *stream_desc; |
| 2550 | |
| 2551 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2552 | if (!node) { |
| 2553 | TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2554 | continue; |
| 2555 | } |
| 2556 | |
| 2557 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2558 | /* Do not resend this frame if in the "already acked range" */ |
| 2559 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 2560 | TRACE_DEVEL("ignored frame in already acked range", |
| 2561 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2562 | continue; |
| 2563 | } |
| 2564 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 2565 | strm_frm->offset.key = stream_desc->ack_offset; |
| 2566 | TRACE_DEVEL("updated partially acked frame", |
| 2567 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2568 | } |
Amaury Denoyelle | c8a0efb | 2023-02-22 10:44:27 +0100 | [diff] [blame] | 2569 | |
| 2570 | strm_frm->dup = 1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2571 | break; |
| 2572 | } |
| 2573 | |
| 2574 | default: |
| 2575 | break; |
| 2576 | } |
| 2577 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2578 | /* If <frm> is already a copy of another frame, we must take |
| 2579 | * its original frame as source for the copy. |
| 2580 | */ |
| 2581 | origin = frm->origin ? frm->origin : frm; |
| 2582 | dup_frm = qc_frm_dup(origin); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2583 | if (!dup_frm) { |
| 2584 | TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2585 | break; |
| 2586 | } |
| 2587 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2588 | TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2589 | if (origin->pkt) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2590 | TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM, |
| 2591 | qc, NULL, &origin->pkt->pn_node.key); |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2592 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2593 | else { |
| 2594 | /* <origin> is a frame which was sent from a packet detected as lost. */ |
| 2595 | TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc); |
| 2596 | } |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 2597 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2598 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2599 | } |
| 2600 | |
| 2601 | LIST_SPLICE(out_frm_list, &tmp); |
| 2602 | |
| 2603 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2604 | } |
| 2605 | |
Frédéric Lécaille | e6359b6 | 2023-03-02 14:49:22 +0100 | [diff] [blame] | 2606 | /* Boolean function which return 1 if <pkt> TX packet is only made of |
| 2607 | * already acknowledged frame. |
| 2608 | */ |
| 2609 | static inline int qc_pkt_with_only_acked_frms(struct quic_tx_packet *pkt) |
| 2610 | { |
| 2611 | struct quic_frame *frm; |
| 2612 | |
| 2613 | list_for_each_entry(frm, &pkt->frms, list) |
| 2614 | if (!(frm->flags & QUIC_FL_TX_FRAME_ACKED)) |
| 2615 | return 0; |
| 2616 | |
| 2617 | return 1; |
| 2618 | } |
| 2619 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2620 | /* Prepare a fast retransmission from <qel> encryption level */ |
| 2621 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2622 | struct quic_enc_level *qel, |
| 2623 | struct list *frms1, struct list *frms2) |
| 2624 | { |
| 2625 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
| 2626 | struct list *frms = frms1; |
| 2627 | struct eb64_node *node; |
| 2628 | struct quic_tx_packet *pkt; |
| 2629 | |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2630 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2631 | |
| 2632 | BUG_ON(frms1 == frms2); |
| 2633 | |
| 2634 | pkt = NULL; |
| 2635 | node = eb64_first(pkts); |
| 2636 | start: |
| 2637 | while (node) { |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2638 | struct quic_tx_packet *p; |
| 2639 | |
| 2640 | p = eb64_entry(node, struct quic_tx_packet, pn_node); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2641 | node = eb64_next(node); |
| 2642 | /* Skip the empty and coalesced packets */ |
Frédéric Lécaille | 6dead91 | 2023-01-30 17:27:32 +0100 | [diff] [blame] | 2643 | TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0, |
Frédéric Lécaille | e6359b6 | 2023-03-02 14:49:22 +0100 | [diff] [blame] | 2644 | "--> pn=%llu (%d %d %d)", (ull)p->pn_node.key, |
| 2645 | LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED), |
| 2646 | qc_pkt_with_only_acked_frms(p)); |
| 2647 | 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] | 2648 | pkt = p; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2649 | break; |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2650 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | if (!pkt) |
| 2654 | goto leave; |
| 2655 | |
| 2656 | /* When building a packet from another one, the field which may increase the |
| 2657 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2658 | */ |
| 2659 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2660 | 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^] | 2661 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2662 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 2663 | goto leave; |
| 2664 | } |
| 2665 | |
| 2666 | TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 2667 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2668 | if (frms == frms1 && frms2) { |
| 2669 | frms = frms2; |
| 2670 | goto start; |
| 2671 | } |
| 2672 | leave: |
| 2673 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 2674 | } |
| 2675 | |
| 2676 | /* Prepare a fast retransmission during a handshake after a client |
| 2677 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2678 | * Initial packets send them coalescing with others (Handshake here). |
| 2679 | * (Listener only function). |
| 2680 | */ |
| 2681 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2682 | struct list *ifrms, struct list *hfrms) |
| 2683 | { |
| 2684 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2685 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2686 | |
| 2687 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2688 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2689 | struct quic_enc_level *qel = iqel; |
| 2690 | struct eb_root *pkts; |
| 2691 | struct eb64_node *node; |
| 2692 | struct quic_tx_packet *pkt; |
| 2693 | struct list *tmp = &itmp; |
| 2694 | |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2695 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2696 | start: |
| 2697 | pkt = NULL; |
| 2698 | pkts = &qel->pktns->tx.pkts; |
| 2699 | node = eb64_first(pkts); |
| 2700 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2701 | while (node) { |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2702 | struct quic_tx_packet *p; |
| 2703 | |
| 2704 | p = eb64_entry(node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | 6dead91 | 2023-01-30 17:27:32 +0100 | [diff] [blame] | 2705 | TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0, |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2706 | "--> pn=%llu (%d %d)", (ull)p->pn_node.key, |
| 2707 | 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] | 2708 | if (!LIST_ISEMPTY(&p->frms) && !(p->flags & QUIC_FL_TX_PACKET_COALESCED) && |
| 2709 | !qc_pkt_with_only_acked_frms(p)) { |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2710 | pkt = p; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2711 | break; |
Frédéric Lécaille | 21564be | 2023-03-02 11:53:43 +0100 | [diff] [blame] | 2712 | } |
| 2713 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2714 | node = eb64_next(node); |
| 2715 | } |
| 2716 | |
| 2717 | if (!pkt) |
| 2718 | goto end; |
| 2719 | |
| 2720 | /* When building a packet from another one, the field which may increase the |
| 2721 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2722 | */ |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2723 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 2724 | size_t dglen = pkt->len + 4; |
| 2725 | |
| 2726 | dglen += pkt->next ? pkt->next->len + 4 : 0; |
| 2727 | 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^] | 2728 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2729 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 2730 | if (pkt->next) |
| 2731 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt->next); |
| 2732 | goto end; |
| 2733 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | qel->pktns->tx.pto_probe += 1; |
| 2737 | |
| 2738 | /* No risk to loop here, #packet per datagram is bounded */ |
| 2739 | requeue: |
| 2740 | TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2741 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
| 2742 | if (qel == iqel) { |
| 2743 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2744 | pkt = pkt->next; |
| 2745 | tmp = &htmp; |
| 2746 | hqel->pktns->tx.pto_probe += 1; |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2747 | TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2748 | goto requeue; |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | end: |
| 2753 | LIST_SPLICE(ifrms, &itmp); |
| 2754 | LIST_SPLICE(hfrms, &htmp); |
| 2755 | |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 2756 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm) |
| 2760 | { |
| 2761 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 2762 | |
| 2763 | if (frm->type == QUIC_FT_CONNECTION_CLOSE) |
| 2764 | quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code); |
| 2765 | else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) { |
| 2766 | if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt) |
| 2767 | goto out; |
| 2768 | |
| 2769 | qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code); |
| 2770 | } |
| 2771 | |
| 2772 | out: |
| 2773 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 2774 | } |
| 2775 | |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2776 | /* Cancel a request on connection <qc> for stream id <id>. This is useful when |
| 2777 | * 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] | 2778 | * STOP_SENDING + RESET_STREAM frames are prepared for emission. |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2779 | * |
| 2780 | * TODO this function is closely related to H3. Its place should be in H3 layer |
| 2781 | * instead of quic-conn but this requires an architecture adjustment. |
| 2782 | * |
| 2783 | * Returns 1 on sucess else 0. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2784 | */ |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2785 | 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] | 2786 | { |
| 2787 | int ret = 0; |
Amaury Denoyelle | 7546301 | 2023-02-20 10:31:27 +0100 | [diff] [blame] | 2788 | struct quic_frame *ss, *rs; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2789 | struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2790 | const uint64_t app_error_code = H3_REQUEST_REJECTED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2791 | |
| 2792 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2793 | |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2794 | /* Do not emit rejection for unknown unidirectional stream as it is |
| 2795 | * forbidden to close some of them (H3 control stream and QPACK |
| 2796 | * encoder/decoder streams). |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2797 | */ |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 2798 | if (quic_stream_is_uni(id)) { |
| 2799 | ret = 1; |
| 2800 | goto out; |
| 2801 | } |
| 2802 | |
Amaury Denoyelle | 7546301 | 2023-02-20 10:31:27 +0100 | [diff] [blame] | 2803 | ss = qc_frm_alloc(QUIC_FT_STOP_SENDING); |
| 2804 | if (!ss) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2805 | TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2806 | goto out; |
| 2807 | } |
| 2808 | |
Amaury Denoyelle | 7546301 | 2023-02-20 10:31:27 +0100 | [diff] [blame] | 2809 | ss->stop_sending.id = id; |
| 2810 | ss->stop_sending.app_error_code = app_error_code; |
| 2811 | |
| 2812 | rs = qc_frm_alloc(QUIC_FT_RESET_STREAM); |
| 2813 | if (!rs) { |
| 2814 | TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2815 | qc_frm_free(&ss); |
| 2816 | goto out; |
| 2817 | } |
| 2818 | |
| 2819 | rs->reset_stream.id = id; |
| 2820 | rs->reset_stream.app_error_code = app_error_code; |
| 2821 | rs->reset_stream.final_size = 0; |
| 2822 | |
| 2823 | LIST_APPEND(&qel->pktns->tx.frms, &ss->list); |
| 2824 | LIST_APPEND(&qel->pktns->tx.frms, &rs->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2825 | ret = 1; |
| 2826 | out: |
| 2827 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2828 | return ret; |
| 2829 | } |
| 2830 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2831 | /* Release the underlying memory use by <ncbuf> non-contiguous buffer */ |
| 2832 | static void quic_free_ncbuf(struct ncbuf *ncbuf) |
| 2833 | { |
| 2834 | struct buffer buf; |
| 2835 | |
| 2836 | if (ncb_is_null(ncbuf)) |
| 2837 | return; |
| 2838 | |
| 2839 | buf = b_make(ncbuf->area, ncbuf->size, 0, 0); |
| 2840 | b_free(&buf); |
| 2841 | offer_buffers(NULL, 1); |
| 2842 | |
| 2843 | *ncbuf = NCBUF_NULL; |
| 2844 | } |
| 2845 | |
| 2846 | /* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */ |
| 2847 | static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf) |
| 2848 | { |
| 2849 | struct buffer buf = BUF_NULL; |
| 2850 | |
| 2851 | if (!ncb_is_null(ncbuf)) |
| 2852 | return ncbuf; |
| 2853 | |
| 2854 | b_alloc(&buf); |
| 2855 | BUG_ON(b_is_null(&buf)); |
| 2856 | |
| 2857 | *ncbuf = ncb_make(buf.area, buf.size, 0); |
| 2858 | ncb_init(ncbuf, 0); |
| 2859 | |
| 2860 | return ncbuf; |
| 2861 | } |
| 2862 | |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2863 | /* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn. |
| 2864 | * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the |
| 2865 | * speed up handshake completion may be run after having received duplicated |
| 2866 | * CRYPTO data. |
| 2867 | */ |
| 2868 | static int qc_handle_crypto_frm(struct quic_conn *qc, |
| 2869 | struct quic_crypto *frm, struct quic_rx_packet *pkt, |
| 2870 | struct quic_enc_level *qel, int *fast_retrans) |
| 2871 | { |
| 2872 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2873 | enum ncb_ret ncb_ret; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2874 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2875 | struct quic_rx_crypto_frm cfdebug = { |
| 2876 | .offset_node.key = frm->offset, |
| 2877 | .len = frm->len, |
| 2878 | }; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2879 | struct quic_cstream *cstream = qel->cstream; |
| 2880 | struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2881 | |
| 2882 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2883 | if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) { |
| 2884 | TRACE_PROTO("CRYPTO data discarded", |
| 2885 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2886 | goto done; |
| 2887 | } |
| 2888 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2889 | if (unlikely(frm->offset < cstream->rx.offset)) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2890 | size_t diff; |
| 2891 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2892 | if (frm->offset + frm->len <= cstream->rx.offset) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2893 | /* Nothing to do */ |
| 2894 | TRACE_PROTO("Already received CRYPTO data", |
| 2895 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2896 | if (qc_is_listener(qc) && qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] && |
| 2897 | !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP)) |
| 2898 | *fast_retrans = 1; |
| 2899 | goto done; |
| 2900 | } |
| 2901 | |
| 2902 | TRACE_PROTO("Partially already received CRYPTO data", |
| 2903 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2904 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2905 | diff = cstream->rx.offset - frm->offset; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2906 | frm->len -= diff; |
| 2907 | frm->data += diff; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2908 | frm->offset = cstream->rx.offset; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2909 | } |
| 2910 | |
Amaury Denoyelle | ff95f2d | 2022-11-18 14:50:06 +0100 | [diff] [blame] | 2911 | if (frm->offset == cstream->rx.offset && ncb_is_empty(ncbuf)) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2912 | if (!qc_provide_cdata(qel, qc->xprt_ctx, frm->data, frm->len, |
| 2913 | pkt, &cfdebug)) { |
| 2914 | // trace already emitted by function above |
| 2915 | goto leave; |
| 2916 | } |
| 2917 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2918 | cstream->rx.offset += frm->len; |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2919 | 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] | 2920 | goto done; |
| 2921 | } |
| 2922 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2923 | if (!quic_get_ncbuf(ncbuf) || |
| 2924 | ncb_is_null(ncbuf)) { |
| 2925 | 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] | 2926 | goto leave; |
| 2927 | } |
| 2928 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2929 | /* frm->offset > cstream-trx.offset */ |
| 2930 | ncb_ret = ncb_add(ncbuf, frm->offset - cstream->rx.offset, |
| 2931 | (const char *)frm->data, frm->len, NCB_ADD_COMPARE); |
| 2932 | if (ncb_ret != NCB_RET_OK) { |
| 2933 | if (ncb_ret == NCB_RET_DATA_REJ) { |
| 2934 | TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc); |
| 2935 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION)); |
| 2936 | } |
| 2937 | else if (ncb_ret == NCB_RET_GAP_SIZE) { |
| 2938 | TRACE_ERROR("cannot bufferize frame due to gap size limit", |
| 2939 | QUIC_EV_CONN_PRSHPKT, qc); |
| 2940 | } |
| 2941 | goto leave; |
| 2942 | } |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2943 | |
| 2944 | done: |
| 2945 | ret = 1; |
| 2946 | leave: |
| 2947 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2948 | return ret; |
| 2949 | } |
| 2950 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2951 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel> |
| 2952 | * as encryption level. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2953 | * Returns 1 if succeeded, 0 if failed. |
| 2954 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2955 | 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] | 2956 | struct quic_enc_level *qel) |
| 2957 | { |
| 2958 | struct quic_frame frm; |
| 2959 | const unsigned char *pos, *end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2960 | int fast_retrans = 0, ret = 0; |
| 2961 | |
| 2962 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2963 | /* Skip the AAD */ |
| 2964 | pos = pkt->data + pkt->aad_len; |
| 2965 | end = pkt->data + pkt->len; |
| 2966 | |
| 2967 | while (pos < end) { |
| 2968 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) { |
| 2969 | // trace already emitted by function above |
| 2970 | goto leave; |
| 2971 | } |
| 2972 | |
| 2973 | TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm); |
| 2974 | switch (frm.type) { |
| 2975 | case QUIC_FT_PADDING: |
| 2976 | break; |
| 2977 | case QUIC_FT_PING: |
| 2978 | break; |
| 2979 | case QUIC_FT_ACK: |
| 2980 | { |
| 2981 | unsigned int rtt_sample; |
| 2982 | |
| 2983 | rtt_sample = 0; |
| 2984 | if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) { |
| 2985 | // trace already emitted by function above |
| 2986 | goto leave; |
| 2987 | } |
| 2988 | |
| 2989 | if (rtt_sample) { |
| 2990 | unsigned int ack_delay; |
| 2991 | |
| 2992 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
| 2993 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
| 2994 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2995 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
| 2996 | quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc); |
| 2997 | } |
| 2998 | break; |
| 2999 | } |
| 3000 | case QUIC_FT_RESET_STREAM: |
Amaury Denoyelle | 5854fc0 | 2022-12-09 16:25:48 +0100 | [diff] [blame] | 3001 | if (qc->mux_state == QC_MUX_READY) { |
| 3002 | struct quic_reset_stream *rs = &frm.reset_stream; |
| 3003 | qcc_recv_reset_stream(qc->qcc, rs->id, rs->app_error_code, rs->final_size); |
| 3004 | } |
| 3005 | break; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3006 | case QUIC_FT_STOP_SENDING: |
| 3007 | { |
| 3008 | struct quic_stop_sending *stop_sending = &frm.stop_sending; |
| 3009 | if (qc->mux_state == QC_MUX_READY) { |
| 3010 | if (qcc_recv_stop_sending(qc->qcc, stop_sending->id, |
| 3011 | stop_sending->app_error_code)) { |
| 3012 | TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 3013 | goto leave; |
| 3014 | } |
| 3015 | } |
| 3016 | break; |
| 3017 | } |
| 3018 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 3019 | if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3020 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3021 | break; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3022 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 3023 | { |
| 3024 | struct quic_stream *stream = &frm.stream; |
| 3025 | unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams; |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 3026 | const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3027 | |
| 3028 | /* The upper layer may not be allocated. */ |
| 3029 | if (qc->mux_state != QC_MUX_READY) { |
| 3030 | if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) { |
| 3031 | TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc); |
| 3032 | break; |
| 3033 | } |
| 3034 | else { |
| 3035 | TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc); |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 3036 | if (qc->app_ops == &h3_ops) { |
Amaury Denoyelle | 156a89a | 2023-02-20 10:32:16 +0100 | [diff] [blame] | 3037 | if (!qc_h3_request_reject(qc, stream->id)) { |
| 3038 | TRACE_ERROR("error on request rejection", QUIC_EV_CONN_PRSHPKT, qc); |
| 3039 | /* This packet will not be acknowledged */ |
| 3040 | goto leave; |
| 3041 | } |
| 3042 | } |
| 3043 | else { |
| 3044 | /* This packet will not be acknowledged */ |
| 3045 | goto leave; |
Frédéric Lécaille | d18025e | 2023-01-20 15:33:50 +0100 | [diff] [blame] | 3046 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3047 | } |
| 3048 | } |
| 3049 | |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 3050 | if (!qc_handle_strm_frm(pkt, stream, qc, fin)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3051 | TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 3052 | goto leave; |
| 3053 | } |
| 3054 | |
| 3055 | break; |
| 3056 | } |
| 3057 | case QUIC_FT_MAX_DATA: |
| 3058 | if (qc->mux_state == QC_MUX_READY) { |
| 3059 | struct quic_max_data *data = &frm.max_data; |
| 3060 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 3061 | } |
| 3062 | break; |
| 3063 | case QUIC_FT_MAX_STREAM_DATA: |
| 3064 | if (qc->mux_state == QC_MUX_READY) { |
| 3065 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 3066 | if (qcc_recv_max_stream_data(qc->qcc, data->id, |
| 3067 | data->max_stream_data)) { |
| 3068 | TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 3069 | goto leave; |
| 3070 | } |
| 3071 | } |
| 3072 | break; |
| 3073 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 3074 | case QUIC_FT_MAX_STREAMS_UNI: |
| 3075 | break; |
| 3076 | case QUIC_FT_DATA_BLOCKED: |
| 3077 | HA_ATOMIC_INC(&qc->prx_counters->data_blocked); |
| 3078 | break; |
| 3079 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 3080 | HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked); |
| 3081 | break; |
| 3082 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 3083 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi); |
| 3084 | break; |
| 3085 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 3086 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni); |
| 3087 | break; |
| 3088 | case QUIC_FT_NEW_CONNECTION_ID: |
| 3089 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 3090 | /* XXX TO DO XXX */ |
| 3091 | break; |
| 3092 | case QUIC_FT_CONNECTION_CLOSE: |
| 3093 | case QUIC_FT_CONNECTION_CLOSE_APP: |
| 3094 | /* Increment the error counters */ |
| 3095 | qc_cc_err_count_inc(qc, &frm); |
| 3096 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
| 3097 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 3098 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 3099 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 3100 | } |
| 3101 | TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 3102 | /* RFC 9000 10.2. Immediate Close: |
| 3103 | * The closing and draining connection states exist to ensure |
| 3104 | * that connections close cleanly and that delayed or reordered |
| 3105 | * packets are properly discarded. These states SHOULD persist |
| 3106 | * for at least three times the current PTO interval... |
| 3107 | * |
| 3108 | * Rearm the idle timeout only one time when entering draining |
| 3109 | * state. |
| 3110 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3111 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 77ed631 | 2023-02-01 09:28:55 +0100 | [diff] [blame] | 3112 | qc_idle_timer_do_rearm(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3113 | qc_notify_close(qc); |
| 3114 | } |
| 3115 | break; |
| 3116 | case QUIC_FT_HANDSHAKE_DONE: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3117 | if (qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3118 | TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame", |
| 3119 | QUIC_EV_CONN_PRSHPKT, qc); |
| 3120 | goto leave; |
| 3121 | } |
| 3122 | |
| 3123 | qc->state = QUIC_HS_ST_CONFIRMED; |
| 3124 | break; |
| 3125 | default: |
| 3126 | TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc); |
| 3127 | goto leave; |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | /* Flag this packet number space as having received a packet. */ |
| 3132 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
| 3133 | |
| 3134 | if (fast_retrans) { |
| 3135 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3136 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3137 | |
| 3138 | TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc); |
| 3139 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 3140 | qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP; |
| 3141 | } |
| 3142 | |
| 3143 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 3144 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 3145 | * be discarded. |
| 3146 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3147 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3148 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
| 3149 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 3150 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3151 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3152 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 3153 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3154 | qc_set_timer(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3155 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3156 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 3157 | } |
| 3158 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 3159 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | ret = 1; |
| 3164 | leave: |
| 3165 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 3166 | return ret; |
| 3167 | } |
| 3168 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3169 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3170 | /* Allocate Tx buffer from <qc> quic-conn if needed. |
| 3171 | * |
| 3172 | * Returns allocated buffer or NULL on error. |
| 3173 | */ |
| 3174 | static struct buffer *qc_txb_alloc(struct quic_conn *qc) |
| 3175 | { |
| 3176 | struct buffer *buf = &qc->tx.buf; |
| 3177 | if (!b_alloc(buf)) |
| 3178 | return NULL; |
| 3179 | |
| 3180 | return buf; |
| 3181 | } |
| 3182 | |
| 3183 | /* Free Tx buffer from <qc> if it is empty. */ |
| 3184 | static void qc_txb_release(struct quic_conn *qc) |
| 3185 | { |
| 3186 | struct buffer *buf = &qc->tx.buf; |
| 3187 | |
| 3188 | /* For the moment sending function is responsible to purge the buffer |
| 3189 | * entirely. It may change in the future but this requires to be able |
| 3190 | * to reuse old data. |
Frédéric Lécaille | bbf86be | 2023-02-20 09:28:58 +0100 | [diff] [blame] | 3191 | * For the momemt we do not care to leave data in the buffer for |
| 3192 | * a connection which is supposed to be killed asap. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3193 | */ |
| 3194 | BUG_ON_HOT(buf && b_data(buf)); |
| 3195 | |
| 3196 | if (!b_data(buf)) { |
| 3197 | b_free(buf); |
| 3198 | offer_buffers(NULL, 1); |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | /* Commit a datagram payload written into <buf> of length <length>. <first_pkt> |
| 3203 | * must contains the address of the first packet stored in the payload. |
| 3204 | * |
| 3205 | * Caller is responsible that there is enough space in the buffer. |
| 3206 | */ |
| 3207 | static void qc_txb_store(struct buffer *buf, uint16_t length, |
| 3208 | struct quic_tx_packet *first_pkt) |
| 3209 | { |
| 3210 | const size_t hdlen = sizeof(uint16_t) + sizeof(void *); |
| 3211 | BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */ |
| 3212 | |
| 3213 | write_u16(b_tail(buf), length); |
| 3214 | write_ptr(b_tail(buf) + sizeof(length), first_pkt); |
| 3215 | b_add(buf, hdlen + length); |
| 3216 | } |
| 3217 | |
| 3218 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 3219 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 3220 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 3221 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 3222 | * <force_ack> may be set to 1 if you want to force an ack. |
| 3223 | */ |
| 3224 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 3225 | struct quic_enc_level *qel, int cc, int probe, int force_ack) |
| 3226 | { |
| 3227 | unsigned int must_ack = force_ack || |
| 3228 | (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)); |
| 3229 | |
| 3230 | /* Do not build any more packet if the TX secrets are not available or |
| 3231 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 3232 | * and if there is no more packets to send upon PTO expiration |
| 3233 | * and if there is no more ack-eliciting frames to send or in flight |
| 3234 | * congestion control limit is reached for prepared data |
| 3235 | */ |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 3236 | if (!quic_tls_has_tx_sec(qel) || |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3237 | (!cc && !probe && !must_ack && |
| 3238 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 3239 | return 0; |
| 3240 | } |
| 3241 | |
| 3242 | return 1; |
| 3243 | } |
| 3244 | |
| 3245 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3246 | * <frms>. Each packet is stored in a distinct datagram written to <buf>. |
| 3247 | * |
| 3248 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3249 | * the address of the packet contained in the datagram. |
| 3250 | * |
| 3251 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3252 | * -1 if something wrong happened. |
| 3253 | */ |
| 3254 | static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3255 | struct list *frms) |
| 3256 | { |
| 3257 | int ret = -1; |
| 3258 | struct quic_enc_level *qel; |
| 3259 | unsigned char *end, *pos; |
| 3260 | struct quic_tx_packet *pkt; |
| 3261 | size_t total; |
| 3262 | /* Each datagram is prepended with its length followed by the address |
| 3263 | * of the first packet in the datagram. |
| 3264 | */ |
| 3265 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt); |
| 3266 | |
| 3267 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3268 | |
| 3269 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3270 | total = 0; |
| 3271 | pos = (unsigned char *)b_tail(buf); |
| 3272 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) { |
| 3273 | int err, probe, cc; |
| 3274 | |
| 3275 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
| 3276 | probe = 0; |
| 3277 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3278 | /* We do not probe if an immediate close was asked */ |
| 3279 | if (!cc) |
| 3280 | probe = qel->pktns->tx.pto_probe; |
| 3281 | |
| 3282 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0)) |
| 3283 | break; |
| 3284 | |
| 3285 | /* Leave room for the datagram header */ |
| 3286 | pos += dg_headlen; |
| 3287 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3288 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3289 | } |
| 3290 | else { |
| 3291 | end = pos + qc->path->mtu; |
| 3292 | } |
| 3293 | |
| 3294 | pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0, |
| 3295 | QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err); |
| 3296 | switch (err) { |
| 3297 | case -2: |
| 3298 | // trace already emitted by function above |
| 3299 | goto leave; |
| 3300 | case -1: |
| 3301 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 3302 | * MTU, we are here because of the congestion control window. There is |
| 3303 | * no need to try to reuse this buffer. |
| 3304 | */ |
| 3305 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
| 3306 | goto out; |
| 3307 | default: |
| 3308 | break; |
| 3309 | } |
| 3310 | |
| 3311 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 3312 | BUG_ON(!pkt); |
| 3313 | |
| 3314 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3315 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3316 | |
| 3317 | total += pkt->len; |
| 3318 | |
| 3319 | /* Write datagram header. */ |
| 3320 | qc_txb_store(buf, pkt->len, pkt); |
| 3321 | } |
| 3322 | |
| 3323 | out: |
| 3324 | ret = total; |
| 3325 | leave: |
| 3326 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3327 | return ret; |
| 3328 | } |
| 3329 | |
| 3330 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3331 | * <frms>. Several packets can be regrouped in a single datagram. The result is |
| 3332 | * written into <buf>. |
| 3333 | * |
| 3334 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3335 | * the address of first packet in the datagram. |
| 3336 | * |
| 3337 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3338 | * -1 if something wrong happened. |
| 3339 | */ |
| 3340 | static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3341 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3342 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3343 | { |
| 3344 | struct quic_enc_level *qel; |
| 3345 | unsigned char *end, *pos; |
| 3346 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 3347 | /* length of datagrams */ |
| 3348 | uint16_t dglen; |
| 3349 | size_t total; |
| 3350 | int ret = -1, padding; |
| 3351 | /* Each datagram is prepended with its length followed by the address |
| 3352 | * of the first packet in the datagram. |
| 3353 | */ |
| 3354 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt); |
| 3355 | struct list *frms; |
| 3356 | |
| 3357 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3358 | |
| 3359 | /* Currently qc_prep_pkts() does not handle buffer wrapping so the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 3360 | * caller must ensure that buf is reset. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3361 | */ |
| 3362 | BUG_ON_HOT(buf->head || buf->data); |
| 3363 | |
| 3364 | total = 0; |
| 3365 | qel = &qc->els[tel]; |
| 3366 | frms = tel_frms; |
| 3367 | dglen = 0; |
| 3368 | padding = 0; |
| 3369 | pos = (unsigned char *)b_head(buf); |
| 3370 | first_pkt = prv_pkt = NULL; |
| 3371 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
| 3372 | int err, probe, cc; |
| 3373 | enum quic_pkt_type pkt_type; |
| 3374 | struct quic_tls_ctx *tls_ctx; |
| 3375 | const struct quic_version *ver; |
| 3376 | int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 3377 | (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3378 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3379 | |
| 3380 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
| 3381 | probe = 0; |
| 3382 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3383 | /* We do not probe if an immediate close was asked */ |
| 3384 | if (!cc) |
| 3385 | probe = qel->pktns->tx.pto_probe; |
| 3386 | |
| 3387 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) { |
| 3388 | if (prv_pkt) |
| 3389 | qc_txb_store(buf, dglen, first_pkt); |
| 3390 | /* Let's select the next encryption level */ |
| 3391 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 3392 | tel = next_tel; |
| 3393 | frms = next_tel_frms; |
| 3394 | qel = &qc->els[tel]; |
| 3395 | /* Build a new datagram */ |
| 3396 | prv_pkt = NULL; |
| 3397 | TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc); |
| 3398 | continue; |
| 3399 | } |
| 3400 | break; |
| 3401 | } |
| 3402 | |
| 3403 | pkt_type = quic_tls_level_pkt_type(tel); |
| 3404 | if (!prv_pkt) { |
| 3405 | /* Leave room for the datagram header */ |
| 3406 | pos += dg_headlen; |
| 3407 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3408 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3409 | } |
| 3410 | else { |
| 3411 | end = pos + qc->path->mtu; |
| 3412 | } |
| 3413 | } |
| 3414 | |
Frédéric Lécaille | 69e7118 | 2023-02-20 14:39:41 +0100 | [diff] [blame] | 3415 | /* RFC 9000 14.1 Initial datagram size |
| 3416 | * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting |
| 3417 | * Initial packets to at least the smallest allowed maximum datagram size of |
| 3418 | * 1200 bytes. |
| 3419 | * |
| 3420 | * Ensure that no ack-eliciting packets are sent into too small datagrams |
| 3421 | */ |
| 3422 | if (pkt_type == QUIC_PACKET_TYPE_INITIAL && !LIST_ISEMPTY(tel_frms)) { |
| 3423 | if (end - pos < QUIC_INITIAL_PACKET_MINLEN) { |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 3424 | 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] | 3425 | QUIC_EV_CONN_PHPKTS, qc); |
| 3426 | goto out; |
| 3427 | } |
| 3428 | |
| 3429 | /* Pad this Initial packet if there is no ack-eliciting frames to send from |
| 3430 | * the next packet number space. |
| 3431 | */ |
| 3432 | if (LIST_ISEMPTY(next_tel_frms)) |
| 3433 | padding = 1; |
| 3434 | } |
| 3435 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3436 | if (qc->negotiated_version) { |
| 3437 | ver = qc->negotiated_version; |
| 3438 | if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 3439 | tls_ctx = &qc->negotiated_ictx; |
| 3440 | else |
| 3441 | tls_ctx = &qel->tls_ctx; |
| 3442 | } |
| 3443 | else { |
| 3444 | ver = qc->original_version; |
| 3445 | tls_ctx = &qel->tls_ctx; |
| 3446 | } |
| 3447 | |
| 3448 | cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms, |
| 3449 | qc, ver, dglen, pkt_type, |
| 3450 | force_ack, padding, probe, cc, &err); |
| 3451 | switch (err) { |
| 3452 | case -2: |
| 3453 | // trace already emitted by function above |
| 3454 | goto leave; |
| 3455 | case -1: |
| 3456 | /* If there was already a correct packet present, set the |
| 3457 | * current datagram as prepared into <cbuf>. |
| 3458 | */ |
| 3459 | if (prv_pkt) |
| 3460 | qc_txb_store(buf, dglen, first_pkt); |
| 3461 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
| 3462 | goto out; |
| 3463 | default: |
| 3464 | break; |
| 3465 | } |
| 3466 | |
| 3467 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 3468 | BUG_ON(!cur_pkt); |
| 3469 | |
| 3470 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3471 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3472 | |
| 3473 | total += cur_pkt->len; |
| 3474 | /* keep trace of the first packet in the datagram */ |
| 3475 | if (!first_pkt) |
| 3476 | first_pkt = cur_pkt; |
Frédéric Lécaille | 74b5f7b | 2022-11-20 18:35:35 +0100 | [diff] [blame] | 3477 | /* Attach the current one to the previous one and vice versa */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3478 | if (prv_pkt) { |
| 3479 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 3480 | cur_pkt->prev = prv_pkt; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3481 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 3482 | } |
| 3483 | /* Let's say we have to build a new dgram */ |
| 3484 | prv_pkt = NULL; |
| 3485 | dglen += cur_pkt->len; |
| 3486 | /* Client: discard the Initial encryption keys as soon as |
| 3487 | * a handshake packet could be built. |
| 3488 | */ |
| 3489 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
| 3490 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 3491 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3492 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3493 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 3494 | qc_set_timer(qc); |
| 3495 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3496 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 3497 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
| 3498 | } |
| 3499 | /* If the data for the current encryption level have all been sent, |
| 3500 | * select the next level. |
| 3501 | */ |
| 3502 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
| 3503 | next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) { |
| 3504 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 3505 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 3506 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3507 | tel = next_tel; |
| 3508 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 3509 | frms = &qc->els[tel].pktns->tx.frms; |
| 3510 | else |
| 3511 | frms = next_tel_frms; |
| 3512 | qel = &qc->els[tel]; |
| 3513 | if (!LIST_ISEMPTY(frms)) { |
| 3514 | /* If there is data for the next level, do not |
| 3515 | * consume a datagram. |
| 3516 | */ |
| 3517 | prv_pkt = cur_pkt; |
| 3518 | } |
| 3519 | } |
| 3520 | |
| 3521 | /* If we have to build a new datagram, set the current datagram as |
| 3522 | * prepared into <cbuf>. |
| 3523 | */ |
| 3524 | if (!prv_pkt) { |
| 3525 | qc_txb_store(buf, dglen, first_pkt); |
| 3526 | first_pkt = NULL; |
| 3527 | dglen = 0; |
| 3528 | padding = 0; |
| 3529 | } |
| 3530 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
| 3531 | (!qc_is_listener(qc) || |
| 3532 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3533 | padding = 1; |
| 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | out: |
| 3538 | ret = total; |
| 3539 | leave: |
| 3540 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3541 | return ret; |
| 3542 | } |
| 3543 | |
| 3544 | /* Send datagrams stored in <buf>. |
| 3545 | * |
Amaury Denoyelle | 1febc2d | 2023-02-23 11:18:38 +0100 | [diff] [blame] | 3546 | * This function returns 1 for success. On error, there is several behavior |
| 3547 | * depending on underlying sendto() error : |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 3548 | * - for an unrecoverable error, 0 is returned and connection is killed. |
| 3549 | * - a transient error is handled differently if connection has its owned |
| 3550 | * socket. If this is the case, 0 is returned and socket is subscribed on the |
| 3551 | * 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] | 3552 | * Remaining data are purged from the buffer and will eventually be detected |
| 3553 | * as lost which gives the opportunity to retry sending. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3554 | */ |
| 3555 | int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx) |
| 3556 | { |
Frédéric Lécaille | a2c62c3 | 2023-02-10 14:13:43 +0100 | [diff] [blame] | 3557 | int ret = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3558 | struct quic_conn *qc; |
| 3559 | char skip_sendto = 0; |
| 3560 | |
| 3561 | qc = ctx->qc; |
| 3562 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
| 3563 | while (b_contig_data(buf, 0)) { |
| 3564 | unsigned char *pos; |
| 3565 | struct buffer tmpbuf = { }; |
| 3566 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3567 | uint16_t dglen; |
| 3568 | size_t headlen = sizeof dglen + sizeof first_pkt; |
| 3569 | unsigned int time_sent; |
| 3570 | |
| 3571 | pos = (unsigned char *)b_head(buf); |
| 3572 | dglen = read_u16(pos); |
| 3573 | BUG_ON_HOT(!dglen); /* this should not happen */ |
| 3574 | |
| 3575 | pos += sizeof dglen; |
| 3576 | first_pkt = read_ptr(pos); |
| 3577 | pos += sizeof first_pkt; |
| 3578 | tmpbuf.area = (char *)pos; |
| 3579 | tmpbuf.size = tmpbuf.data = dglen; |
| 3580 | |
| 3581 | TRACE_DATA("send dgram", QUIC_EV_CONN_SPPKTS, qc); |
| 3582 | /* If sendto is on error just skip the call to it for the rest |
| 3583 | * of the loop but continue to purge the buffer. Data will be |
| 3584 | * transmitted when QUIC packets are detected as lost on our |
| 3585 | * side. |
| 3586 | * |
| 3587 | * TODO use fd-monitoring to detect when send operation can be |
| 3588 | * retry. This should improve the bandwidth without relying on |
| 3589 | * retransmission timer. However, it requires a major rework on |
| 3590 | * quic-conn fd management. |
| 3591 | */ |
| 3592 | if (!skip_sendto) { |
Amaury Denoyelle | 1febc2d | 2023-02-23 11:18:38 +0100 | [diff] [blame] | 3593 | int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0); |
| 3594 | if (ret < 0) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 3595 | TRACE_ERROR("sendto fatal error", QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 1febc2d | 2023-02-23 11:18:38 +0100 | [diff] [blame] | 3596 | qc_kill_conn(qc); |
| 3597 | b_del(buf, buf->data); |
| 3598 | goto leave; |
| 3599 | } |
| 3600 | else if (!ret) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 3601 | /* Connection owned socket : poller will wake us up when transient error is cleared. */ |
| 3602 | if (qc_test_fd(qc)) { |
| 3603 | TRACE_ERROR("sendto error, subscribe to poller", QUIC_EV_CONN_SPPKTS, qc); |
| 3604 | goto leave; |
| 3605 | } |
| 3606 | |
| 3607 | /* No connection owned-socket : rely on retransmission to retry sending. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3608 | skip_sendto = 1; |
| 3609 | TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc); |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | b_del(buf, dglen + headlen); |
| 3614 | qc->tx.bytes += tmpbuf.data; |
| 3615 | time_sent = now_ms; |
| 3616 | |
| 3617 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
Frédéric Lécaille | ceb88b8 | 2023-02-20 14:43:55 +0100 | [diff] [blame] | 3618 | /* RFC 9000 14.1 Initial datagram size |
| 3619 | * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting |
| 3620 | * Initial packets to at least the smallest allowed maximum datagram size of |
| 3621 | * 1200 bytes. |
| 3622 | */ |
| 3623 | BUG_ON_HOT(pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 3624 | (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) && |
| 3625 | dglen < QUIC_INITIAL_PACKET_MINLEN); |
| 3626 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3627 | pkt->time_sent = time_sent; |
| 3628 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3629 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
| 3630 | qc->path->ifae_pkts++; |
| 3631 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3632 | qc_idle_timer_rearm(qc, 0); |
| 3633 | } |
| 3634 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3635 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3636 | qc->flags |= QUIC_FL_CONN_CLOSING; |
| 3637 | qc_notify_close(qc); |
| 3638 | |
| 3639 | /* RFC 9000 10.2. Immediate Close: |
| 3640 | * The closing and draining connection states exist to ensure |
| 3641 | * that connections close cleanly and that delayed or reordered |
| 3642 | * packets are properly discarded. These states SHOULD persist |
| 3643 | * for at least three times the current PTO interval... |
| 3644 | * |
| 3645 | * Rearm the idle timeout only one time when entering closing |
| 3646 | * state. |
| 3647 | */ |
| 3648 | qc_idle_timer_do_rearm(qc); |
| 3649 | if (qc->timer_task) { |
| 3650 | task_destroy(qc->timer_task); |
| 3651 | qc->timer_task = NULL; |
| 3652 | } |
| 3653 | } |
| 3654 | qc->path->in_flight += pkt->in_flight_len; |
| 3655 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3656 | if (pkt->in_flight_len) |
| 3657 | qc_set_timer(qc); |
| 3658 | TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 3659 | next_pkt = pkt->next; |
| 3660 | quic_tx_packet_refinc(pkt); |
| 3661 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
| 3662 | } |
| 3663 | } |
| 3664 | |
Frédéric Lécaille | a2c62c3 | 2023-02-10 14:13:43 +0100 | [diff] [blame] | 3665 | ret = 1; |
| 3666 | leave: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3667 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 3668 | |
Frédéric Lécaille | a2c62c3 | 2023-02-10 14:13:43 +0100 | [diff] [blame] | 3669 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | /* Copy into <buf> buffer a stateless reset token depending on the |
| 3673 | * <salt> salt input. This is the cluster secret which will be derived |
| 3674 | * as HKDF input secret to generate this token. |
| 3675 | * Return 1 if succeeded, 0 if not. |
| 3676 | */ |
| 3677 | static int quic_stateless_reset_token_cpy(struct quic_conn *qc, |
| 3678 | unsigned char *buf, size_t len, |
| 3679 | const unsigned char *salt, size_t saltlen) |
| 3680 | { |
| 3681 | /* Input secret */ |
| 3682 | const unsigned char *key = (const unsigned char *)global.cluster_secret; |
| 3683 | size_t keylen = strlen(global.cluster_secret); |
| 3684 | /* Info */ |
| 3685 | const unsigned char label[] = "stateless token"; |
| 3686 | size_t labellen = sizeof label - 1; |
| 3687 | int ret; |
| 3688 | |
| 3689 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3690 | |
| 3691 | ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len, |
| 3692 | key, keylen, salt, saltlen, label, labellen); |
| 3693 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3694 | return ret; |
| 3695 | } |
| 3696 | |
| 3697 | /* Initialize the stateless reset token attached to <cid> connection ID. |
| 3698 | * Returns 1 if succeeded, 0 if not. |
| 3699 | */ |
| 3700 | static int quic_stateless_reset_token_init(struct quic_conn *qc, |
| 3701 | struct quic_connection_id *quic_cid) |
| 3702 | { |
| 3703 | int ret; |
| 3704 | |
| 3705 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3706 | |
| 3707 | if (global.cluster_secret) { |
| 3708 | /* Output secret */ |
| 3709 | unsigned char *token = quic_cid->stateless_reset_token; |
| 3710 | size_t tokenlen = sizeof quic_cid->stateless_reset_token; |
| 3711 | /* Salt */ |
| 3712 | const unsigned char *cid = quic_cid->cid.data; |
| 3713 | size_t cidlen = quic_cid->cid.len; |
| 3714 | |
| 3715 | ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen); |
| 3716 | } |
| 3717 | else { |
| 3718 | /* TODO: RAND_bytes() should be replaced */ |
| 3719 | ret = RAND_bytes(quic_cid->stateless_reset_token, |
| 3720 | sizeof quic_cid->stateless_reset_token) == 1; |
| 3721 | } |
| 3722 | |
| 3723 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3724 | return ret; |
| 3725 | } |
| 3726 | |
| 3727 | /* Allocate a new CID with <seq_num> as sequence number and attach it to <root> |
| 3728 | * ebtree. |
| 3729 | * |
| 3730 | * The CID is randomly generated in part with the result altered to be |
| 3731 | * associated with the current thread ID. This means this function must only |
| 3732 | * be called by the quic_conn thread. |
| 3733 | * |
| 3734 | * Returns the new CID if succeeded, NULL if not. |
| 3735 | */ |
| 3736 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
| 3737 | struct quic_conn *qc, |
| 3738 | int seq_num) |
| 3739 | { |
| 3740 | struct quic_connection_id *cid; |
| 3741 | |
| 3742 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3743 | |
| 3744 | cid = pool_alloc(pool_head_quic_connection_id); |
| 3745 | if (!cid) { |
| 3746 | TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 3747 | goto err; |
| 3748 | } |
| 3749 | |
| 3750 | cid->cid.len = QUIC_HAP_CID_LEN; |
| 3751 | /* TODO: RAND_bytes() should be replaced */ |
| 3752 | if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) { |
| 3753 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc); |
| 3754 | goto err; |
| 3755 | } |
| 3756 | |
| 3757 | quic_pin_cid_to_tid(cid->cid.data, tid); |
| 3758 | if (quic_stateless_reset_token_init(qc, cid) != 1) { |
| 3759 | TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc); |
| 3760 | goto err; |
| 3761 | } |
| 3762 | |
| 3763 | cid->qc = qc; |
| 3764 | |
| 3765 | cid->seq_num.key = seq_num; |
| 3766 | cid->retire_prior_to = 0; |
| 3767 | /* insert the allocated CID in the quic_conn tree */ |
| 3768 | eb64_insert(root, &cid->seq_num); |
| 3769 | |
| 3770 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3771 | return cid; |
| 3772 | |
| 3773 | err: |
| 3774 | pool_free(pool_head_quic_connection_id, cid); |
| 3775 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3776 | return NULL; |
| 3777 | } |
| 3778 | |
| 3779 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3780 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3781 | * a HANDSHAKE_DONE frame. |
| 3782 | * Return 1 if succeeded, 0 if not. |
| 3783 | */ |
| 3784 | static int quic_build_post_handshake_frames(struct quic_conn *qc) |
| 3785 | { |
| 3786 | int ret = 0, i, first, max; |
| 3787 | struct quic_enc_level *qel; |
| 3788 | struct quic_frame *frm, *frmbak; |
| 3789 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3790 | struct eb64_node *node; |
| 3791 | |
| 3792 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 3793 | |
| 3794 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3795 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
| 3796 | if (qc_is_listener(qc)) { |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 3797 | frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3798 | if (!frm) { |
| 3799 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3800 | goto leave; |
| 3801 | } |
| 3802 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3803 | LIST_APPEND(&frm_list, &frm->list); |
| 3804 | } |
| 3805 | |
| 3806 | /* Initialize <max> connection IDs minus one: there is |
| 3807 | * already one connection ID used for the current connection. |
| 3808 | */ |
| 3809 | first = 1; |
| 3810 | max = qc->tx.params.active_connection_id_limit; |
| 3811 | |
| 3812 | /* TODO: check limit */ |
| 3813 | for (i = first; i < max; i++) { |
| 3814 | struct quic_connection_id *cid; |
| 3815 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 3816 | frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3817 | if (!frm) { |
| 3818 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3819 | goto err; |
| 3820 | } |
| 3821 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3822 | cid = new_quic_cid(&qc->cids, qc, i); |
| 3823 | if (!cid) { |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 3824 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3825 | TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3826 | goto err; |
| 3827 | } |
| 3828 | |
| 3829 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 3830 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
| 3831 | |
| 3832 | quic_connection_id_to_frm_cpy(frm, cid); |
| 3833 | LIST_APPEND(&frm_list, &frm->list); |
| 3834 | } |
| 3835 | |
| 3836 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
| 3837 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
| 3838 | |
| 3839 | ret = 1; |
| 3840 | leave: |
| 3841 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 3842 | return ret; |
| 3843 | |
| 3844 | err: |
| 3845 | /* free the frames */ |
| 3846 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 3847 | qc_frm_free(&frm); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3848 | |
| 3849 | node = eb64_lookup_ge(&qc->cids, first); |
| 3850 | while (node) { |
| 3851 | struct quic_connection_id *cid; |
| 3852 | |
| 3853 | cid = eb64_entry(node, struct quic_connection_id, seq_num); |
| 3854 | if (cid->seq_num.key >= max) |
| 3855 | break; |
| 3856 | |
| 3857 | node = eb64_next(node); |
| 3858 | ebmb_delete(&cid->node); |
| 3859 | eb64_delete(&cid->seq_num); |
| 3860 | pool_free(pool_head_quic_connection_id, cid); |
| 3861 | } |
| 3862 | goto leave; |
| 3863 | } |
| 3864 | |
| 3865 | /* Deallocate <l> list of ACK ranges. */ |
| 3866 | void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs) |
| 3867 | { |
| 3868 | struct eb64_node *n; |
| 3869 | struct quic_arng_node *ar; |
| 3870 | |
| 3871 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 3872 | |
| 3873 | n = eb64_first(&arngs->root); |
| 3874 | while (n) { |
| 3875 | struct eb64_node *next; |
| 3876 | |
| 3877 | ar = eb64_entry(n, struct quic_arng_node, first); |
| 3878 | next = eb64_next(n); |
| 3879 | eb64_delete(n); |
| 3880 | pool_free(pool_head_quic_arng, ar); |
| 3881 | n = next; |
| 3882 | } |
| 3883 | |
| 3884 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 3885 | } |
| 3886 | |
| 3887 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3888 | * descending order. |
| 3889 | */ |
| 3890 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3891 | struct quic_arng_node *q) |
| 3892 | { |
| 3893 | return p->first.key - q->last - 2; |
| 3894 | } |
| 3895 | |
| 3896 | |
| 3897 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3898 | * encoded size until it goes below <limit>. |
| 3899 | * Returns 1 if succeeded, 0 if not (no more element to remove). |
| 3900 | */ |
| 3901 | static int quic_rm_last_ack_ranges(struct quic_conn *qc, |
| 3902 | struct quic_arngs *arngs, size_t limit) |
| 3903 | { |
| 3904 | int ret = 0; |
| 3905 | struct eb64_node *last, *prev; |
| 3906 | |
| 3907 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3908 | |
| 3909 | last = eb64_last(&arngs->root); |
| 3910 | while (last && arngs->enc_sz > limit) { |
| 3911 | struct quic_arng_node *last_node, *prev_node; |
| 3912 | |
| 3913 | prev = eb64_prev(last); |
| 3914 | if (!prev) { |
| 3915 | TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc); |
| 3916 | goto out; |
| 3917 | } |
| 3918 | |
| 3919 | last_node = eb64_entry(last, struct quic_arng_node, first); |
| 3920 | prev_node = eb64_entry(prev, struct quic_arng_node, first); |
| 3921 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3922 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3923 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3924 | --arngs->sz; |
| 3925 | eb64_delete(last); |
| 3926 | pool_free(pool_head_quic_arng, last); |
| 3927 | last = prev; |
| 3928 | } |
| 3929 | |
| 3930 | ret = 1; |
| 3931 | out: |
| 3932 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3933 | return ret; |
| 3934 | } |
| 3935 | |
| 3936 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3937 | static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs) |
| 3938 | { |
| 3939 | struct eb64_node *node, *next; |
| 3940 | struct quic_arng_node *ar, *ar_next; |
| 3941 | |
| 3942 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3943 | |
| 3944 | node = eb64_last(&arngs->root); |
| 3945 | if (!node) |
| 3946 | goto leave; |
| 3947 | |
| 3948 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 3949 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3950 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3951 | |
| 3952 | while ((next = eb64_prev(node))) { |
| 3953 | ar_next = eb64_entry(next, struct quic_arng_node, first); |
| 3954 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3955 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3956 | node = next; |
| 3957 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 3958 | } |
| 3959 | |
| 3960 | leave: |
| 3961 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3962 | } |
| 3963 | |
| 3964 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3965 | * Returns the ack range node which has been inserted if succeeded, NULL if not. |
| 3966 | */ |
| 3967 | static inline |
| 3968 | struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc, |
| 3969 | struct quic_arngs *arngs, |
| 3970 | struct quic_arng *ar) |
| 3971 | { |
| 3972 | struct quic_arng_node *new_ar; |
| 3973 | |
| 3974 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3975 | |
| 3976 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3977 | if (!new_ar) { |
| 3978 | TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc); |
| 3979 | goto leave; |
| 3980 | } |
| 3981 | |
| 3982 | new_ar->first.key = ar->first; |
| 3983 | new_ar->last = ar->last; |
| 3984 | eb64_insert(&arngs->root, &new_ar->first); |
| 3985 | arngs->sz++; |
| 3986 | |
| 3987 | leave: |
| 3988 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 3989 | return new_ar; |
| 3990 | } |
| 3991 | |
| 3992 | /* Update <arngs> tree of ACK ranges with <ar> as new ACK range value. |
| 3993 | * Note that this function computes the number of bytes required to encode |
| 3994 | * this tree of ACK ranges in descending order. |
| 3995 | * |
| 3996 | * Descending order |
| 3997 | * -------------> |
| 3998 | * range1 range2 |
| 3999 | * ..........|--------|..............|--------| |
| 4000 | * ^ ^ ^ ^ |
| 4001 | * | | | | |
| 4002 | * last1 first1 last2 first2 |
| 4003 | * ..........+--------+--------------+--------+...... |
| 4004 | * diff1 gap12 diff2 |
| 4005 | * |
| 4006 | * To encode the previous list of ranges we must encode integers as follows in |
| 4007 | * descending order: |
| 4008 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
| 4009 | * with diff1 = last1 - first1 |
| 4010 | * diff2 = last2 - first2 |
| 4011 | * gap12 = first1 - last2 - 2 (>= 0) |
| 4012 | * |
| 4013 | |
| 4014 | returns 0 on error |
| 4015 | |
| 4016 | */ |
| 4017 | int quic_update_ack_ranges_list(struct quic_conn *qc, |
| 4018 | struct quic_arngs *arngs, |
| 4019 | struct quic_arng *ar) |
| 4020 | { |
| 4021 | int ret = 0; |
| 4022 | struct eb64_node *le; |
| 4023 | struct quic_arng_node *new_node; |
| 4024 | struct eb64_node *new; |
| 4025 | |
| 4026 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 4027 | |
| 4028 | new = NULL; |
| 4029 | if (eb_is_empty(&arngs->root)) { |
| 4030 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 4031 | if (new_node) |
| 4032 | ret = 1; |
| 4033 | |
| 4034 | goto leave; |
| 4035 | } |
| 4036 | |
| 4037 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 4038 | if (!le) { |
| 4039 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 4040 | if (!new_node) |
| 4041 | goto leave; |
| 4042 | |
| 4043 | new = &new_node->first; |
| 4044 | } |
| 4045 | else { |
| 4046 | struct quic_arng_node *le_ar = |
| 4047 | eb64_entry(le, struct quic_arng_node, first); |
| 4048 | |
| 4049 | /* Already existing range */ |
| 4050 | if (le_ar->last >= ar->last) { |
| 4051 | ret = 1; |
| 4052 | } |
| 4053 | else if (le_ar->last + 1 >= ar->first) { |
| 4054 | le_ar->last = ar->last; |
| 4055 | new = le; |
| 4056 | new_node = le_ar; |
| 4057 | } |
| 4058 | else { |
| 4059 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 4060 | if (!new_node) |
| 4061 | goto leave; |
| 4062 | |
| 4063 | new = &new_node->first; |
| 4064 | } |
| 4065 | } |
| 4066 | |
| 4067 | /* Verify that the new inserted node does not overlap the nodes |
| 4068 | * which follow it. |
| 4069 | */ |
| 4070 | if (new) { |
| 4071 | struct eb64_node *next; |
| 4072 | struct quic_arng_node *next_node; |
| 4073 | |
| 4074 | while ((next = eb64_next(new))) { |
| 4075 | next_node = |
| 4076 | eb64_entry(next, struct quic_arng_node, first); |
| 4077 | if (new_node->last + 1 < next_node->first.key) |
| 4078 | break; |
| 4079 | |
| 4080 | if (next_node->last > new_node->last) |
| 4081 | new_node->last = next_node->last; |
| 4082 | eb64_delete(next); |
| 4083 | pool_free(pool_head_quic_arng, next_node); |
| 4084 | /* Decrement the size of these ranges. */ |
| 4085 | arngs->sz--; |
| 4086 | } |
| 4087 | } |
| 4088 | |
| 4089 | ret = 1; |
| 4090 | leave: |
| 4091 | quic_arngs_set_enc_sz(qc, arngs); |
| 4092 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 4093 | return ret; |
| 4094 | } |
| 4095 | /* Remove the header protection of packets at <el> encryption level. |
| 4096 | * Always succeeds. |
| 4097 | */ |
| 4098 | static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el) |
| 4099 | { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4100 | struct quic_rx_packet *pqpkt, *pkttmp; |
| 4101 | struct quic_enc_level *app_qel; |
| 4102 | |
| 4103 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 4104 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4105 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
| 4106 | if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) { |
| 4107 | TRACE_DEVEL("hp not removed (handshake not completed)", |
| 4108 | QUIC_EV_CONN_ELRMHP, qc); |
| 4109 | goto out; |
| 4110 | } |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 4111 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4112 | 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] | 4113 | struct quic_tls_ctx *tls_ctx; |
| 4114 | |
| 4115 | tls_ctx = qc_select_tls_ctx(qc, el, pqpkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4116 | if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
| 4117 | pqpkt->data + pqpkt->pn_offset, pqpkt->data)) { |
| 4118 | TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc); |
| 4119 | } |
| 4120 | else { |
| 4121 | /* The AAD includes the packet number field */ |
| 4122 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 4123 | /* Store the packet into the tree of packets to decrypt. */ |
| 4124 | pqpkt->pn_node.key = pqpkt->pn; |
| 4125 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 4126 | quic_rx_packet_refinc(pqpkt); |
| 4127 | TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt); |
| 4128 | } |
| 4129 | LIST_DELETE(&pqpkt->list); |
| 4130 | quic_rx_packet_refdec(pqpkt); |
| 4131 | } |
| 4132 | |
| 4133 | out: |
| 4134 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
| 4135 | } |
| 4136 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4137 | /* Process all the CRYPTO frame at <el> encryption level. This is the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4138 | * 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] | 4139 | * stream for this level. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4140 | * Return 1 if succeeded, 0 if not. |
| 4141 | */ |
| 4142 | static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc, |
| 4143 | struct quic_enc_level *el, |
| 4144 | struct ssl_sock_ctx *ctx) |
| 4145 | { |
| 4146 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4147 | struct ncbuf *ncbuf; |
| 4148 | struct quic_cstream *cstream = el->cstream; |
| 4149 | ncb_sz_t data; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4150 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4151 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4152 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4153 | BUG_ON(!cstream); |
| 4154 | ncbuf = &cstream->rx.ncbuf; |
| 4155 | if (ncb_is_null(ncbuf)) |
| 4156 | goto done; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4157 | |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4158 | /* TODO not working if buffer is wrapping */ |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4159 | while ((data = ncb_data(ncbuf, 0))) { |
| 4160 | const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4161 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4162 | if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4163 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4164 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4165 | cstream->rx.offset += data; |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4166 | TRACE_DEVEL("buffered crypto data were provided to TLS stack", |
| 4167 | QUIC_EV_CONN_PHPKTS, qc, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4168 | } |
| 4169 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4170 | done: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4171 | ret = 1; |
| 4172 | leave: |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4173 | if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) { |
| 4174 | 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] | 4175 | quic_free_ncbuf(ncbuf); |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4176 | } |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4177 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4178 | return ret; |
| 4179 | } |
| 4180 | |
| 4181 | /* Process all the packets at <el> and <next_el> encryption level. |
| 4182 | * This is the caller responsibility to check that <cur_el> is different of <next_el> |
| 4183 | * as pointer value. |
| 4184 | * Return 1 if succeeded, 0 if not. |
| 4185 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4186 | 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] | 4187 | struct quic_enc_level *next_el) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4188 | { |
| 4189 | int ret = 0; |
| 4190 | struct eb64_node *node; |
| 4191 | int64_t largest_pn = -1; |
| 4192 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4193 | struct quic_enc_level *qel = cur_el; |
| 4194 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4195 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4196 | qel = cur_el; |
| 4197 | next_tel: |
| 4198 | if (!qel) |
| 4199 | goto out; |
| 4200 | |
| 4201 | node = eb64_first(&qel->rx.pkts); |
| 4202 | while (node) { |
| 4203 | struct quic_rx_packet *pkt; |
| 4204 | |
| 4205 | pkt = eb64_entry(node, struct quic_rx_packet, pn_node); |
| 4206 | TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT, |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4207 | qc, pkt, NULL, qc->xprt_ctx->ssl); |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 4208 | if (!qc_pkt_decrypt(qc, qel, pkt)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4209 | /* Drop the packet */ |
| 4210 | TRACE_ERROR("packet decryption failed -> dropped", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4211 | QUIC_EV_CONN_RXPKT, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4212 | } |
| 4213 | else { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4214 | if (!qc_parse_pkt_frms(qc, pkt, qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4215 | /* Drop the packet */ |
| 4216 | TRACE_ERROR("packet parsing failed -> dropped", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4217 | QUIC_EV_CONN_RXPKT, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4218 | HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing); |
| 4219 | } |
| 4220 | else { |
| 4221 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 4222 | |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 4223 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4224 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 4225 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
| 4226 | qc_idle_timer_rearm(qc, 1); |
| 4227 | } |
| 4228 | if (pkt->pn > largest_pn) { |
| 4229 | largest_pn = pkt->pn; |
| 4230 | largest_pn_time_received = pkt->time_received; |
| 4231 | } |
| 4232 | /* Update the list of ranges to acknowledge. */ |
| 4233 | if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar)) |
| 4234 | TRACE_ERROR("Could not update ack range list", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4235 | QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4236 | } |
| 4237 | } |
| 4238 | node = eb64_next(node); |
| 4239 | eb64_delete(&pkt->pn_node); |
| 4240 | quic_rx_packet_refdec(pkt); |
| 4241 | } |
| 4242 | |
| 4243 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 4244 | /* Update the largest packet number. */ |
| 4245 | qel->pktns->rx.largest_pn = largest_pn; |
| 4246 | /* Update the largest acknowledged packet timestamps */ |
| 4247 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 4248 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 4249 | } |
| 4250 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4251 | 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] | 4252 | // trace already emitted by function above |
| 4253 | goto leave; |
| 4254 | } |
| 4255 | |
| 4256 | if (qel == cur_el) { |
| 4257 | BUG_ON(qel == next_el); |
| 4258 | qel = next_el; |
| 4259 | largest_pn = -1; |
| 4260 | goto next_tel; |
| 4261 | } |
| 4262 | |
| 4263 | out: |
| 4264 | ret = 1; |
| 4265 | leave: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4266 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4267 | return ret; |
| 4268 | } |
| 4269 | |
| 4270 | /* Check if it's possible to remove header protection for packets related to |
| 4271 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 4272 | * |
| 4273 | * Return true if the operation is possible else false. |
| 4274 | */ |
| 4275 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4276 | { |
| 4277 | int ret = 0; |
| 4278 | enum quic_tls_enc_level tel; |
| 4279 | |
| 4280 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
| 4281 | |
| 4282 | if (!qel) |
| 4283 | goto cant_rm_hp; |
| 4284 | |
| 4285 | tel = ssl_to_quic_enc_level(qel->level); |
| 4286 | |
| 4287 | /* check if tls secrets are available */ |
| 4288 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 4289 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
| 4290 | goto cant_rm_hp; |
| 4291 | } |
| 4292 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4293 | if (!quic_tls_has_rx_sec(qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4294 | TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc); |
| 4295 | goto cant_rm_hp; |
| 4296 | } |
| 4297 | |
Frédéric Lécaille | 8417beb | 2023-02-01 10:31:35 +0100 | [diff] [blame] | 4298 | if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) { |
| 4299 | TRACE_DEVEL("handshake not complete", QUIC_EV_CONN_TRMHP, qc); |
| 4300 | goto cant_rm_hp; |
| 4301 | } |
| 4302 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4303 | /* check if the connection layer is ready before using app level */ |
| 4304 | if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) && |
| 4305 | qc->mux_state == QC_MUX_NULL) { |
| 4306 | TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc); |
| 4307 | goto cant_rm_hp; |
| 4308 | } |
| 4309 | |
| 4310 | ret = 1; |
| 4311 | cant_rm_hp: |
| 4312 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc); |
| 4313 | return ret; |
| 4314 | } |
| 4315 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 4316 | /* Flush txbuf for <qc> connection. This must be called prior to a packet |
| 4317 | * preparation when txbuf contains older data. A send will be conducted for |
| 4318 | * these data. |
| 4319 | * |
| 4320 | * Returns 1 on success : buffer is empty and can be use for packet |
| 4321 | * preparation. On error 0 is returned. |
| 4322 | */ |
| 4323 | static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf) |
| 4324 | { |
| 4325 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4326 | |
| 4327 | /* This operation can only be conducted if txbuf is not empty. This |
| 4328 | * case only happens for connection with their owned socket due to an |
| 4329 | * older transient sendto() error. |
| 4330 | */ |
| 4331 | BUG_ON(!qc_test_fd(qc)); |
| 4332 | |
| 4333 | if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) { |
| 4334 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 4335 | qc_txb_release(qc); |
| 4336 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc); |
| 4337 | return 0; |
| 4338 | } |
| 4339 | |
| 4340 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4341 | return 1; |
| 4342 | } |
| 4343 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4344 | /* Try to send application frames from list <frms> on connection <qc>. |
| 4345 | * |
| 4346 | * Use qc_send_app_probing wrapper when probing with old data. |
| 4347 | * |
| 4348 | * Returns 1 on success. Some data might not have been sent due to congestion, |
| 4349 | * in this case they are left in <frms> input list. The caller may subscribe on |
| 4350 | * quic-conn to retry later. |
| 4351 | * |
| 4352 | * Returns 0 on critical error. |
| 4353 | * TODO review and classify more distinctly transient from definitive errors to |
| 4354 | * allow callers to properly handle it. |
| 4355 | */ |
| 4356 | static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms) |
| 4357 | { |
| 4358 | int status = 0; |
| 4359 | struct buffer *buf; |
| 4360 | |
| 4361 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4362 | |
| 4363 | buf = qc_txb_alloc(qc); |
| 4364 | if (!buf) { |
| 4365 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4366 | goto err; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4367 | } |
| 4368 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 4369 | if (b_data(buf) && !qc_purge_txbuf(qc, buf)) |
| 4370 | goto err; |
| 4371 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4372 | /* Prepare and send packets until we could not further prepare packets. */ |
| 4373 | while (1) { |
| 4374 | int ret; |
| 4375 | /* Currently buf cannot be non-empty at this stage. Even if a |
| 4376 | * previous sendto() has failed it is emptied to simulate |
| 4377 | * packet emission and rely on QUIC lost detection to try to |
| 4378 | * emit it. |
| 4379 | */ |
| 4380 | BUG_ON_HOT(b_data(buf)); |
| 4381 | b_reset(buf); |
| 4382 | |
| 4383 | ret = qc_prep_app_pkts(qc, buf, frms); |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4384 | if (ret == -1) { |
| 4385 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4386 | goto err; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4387 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4388 | |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4389 | if (!ret) |
| 4390 | break; |
| 4391 | |
| 4392 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 4393 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 4394 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4395 | goto err; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4396 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4397 | } |
| 4398 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4399 | status = 1; |
| 4400 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4401 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4402 | return status; |
| 4403 | |
| 4404 | err: |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4405 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc); |
| 4406 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | /* Try to send application frames from list <frms> on connection <qc>. Use this |
| 4410 | * function when probing is required. |
| 4411 | * |
| 4412 | * Returns the result from qc_send_app_pkts function. |
| 4413 | */ |
| 4414 | static forceinline int qc_send_app_probing(struct quic_conn *qc, |
| 4415 | struct list *frms) |
| 4416 | { |
| 4417 | int ret; |
| 4418 | |
| 4419 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4420 | |
| 4421 | TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc); |
| 4422 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4423 | ret = qc_send_app_pkts(qc, frms); |
| 4424 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4425 | |
| 4426 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4427 | return ret; |
| 4428 | } |
| 4429 | |
| 4430 | /* Try to send application frames from list <frms> on connection <qc>. This |
| 4431 | * function is provided for MUX upper layer usage only. |
| 4432 | * |
| 4433 | * Returns the result from qc_send_app_pkts function. |
| 4434 | */ |
| 4435 | int qc_send_mux(struct quic_conn *qc, struct list *frms) |
| 4436 | { |
| 4437 | int ret; |
| 4438 | |
| 4439 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4440 | BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */ |
| 4441 | |
| 4442 | TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc); |
| 4443 | qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4444 | ret = qc_send_app_pkts(qc, frms); |
| 4445 | qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4446 | |
| 4447 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4448 | return ret; |
| 4449 | } |
| 4450 | |
| 4451 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 4452 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
| 4453 | * QUIC connection. <old_data> is used as boolean to send data already sent but |
| 4454 | * not already acknowledged (in flight). |
| 4455 | * Returns 1 if succeeded, 0 if not. |
| 4456 | */ |
| 4457 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 4458 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 4459 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 4460 | { |
| 4461 | int ret, status = 0; |
| 4462 | struct buffer *buf = qc_txb_alloc(qc); |
| 4463 | |
| 4464 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4465 | |
| 4466 | if (!buf) { |
| 4467 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 4468 | goto leave; |
| 4469 | } |
| 4470 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 4471 | if (b_data(buf) && !qc_purge_txbuf(qc, buf)) |
| 4472 | goto out; |
| 4473 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4474 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4475 | * sendto() has failed it is emptied to simulate packet emission and |
| 4476 | * rely on QUIC lost detection to try to emit it. |
| 4477 | */ |
| 4478 | BUG_ON_HOT(b_data(buf)); |
| 4479 | b_reset(buf); |
| 4480 | |
| 4481 | if (old_data) { |
| 4482 | TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc); |
| 4483 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4484 | } |
| 4485 | |
| 4486 | 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] | 4487 | if (ret == -1) { |
| 4488 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4489 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4490 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4491 | |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4492 | if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 4493 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 4494 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4495 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4496 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4497 | |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4498 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4499 | status = 1; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4500 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4501 | out: |
| 4502 | TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc); |
| 4503 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4504 | leave: |
| 4505 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4506 | return status; |
| 4507 | } |
| 4508 | |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4509 | /* Retransmit up to two datagrams depending on packet number space. |
| 4510 | * Return 0 when failed, 0 if not. |
| 4511 | */ |
| 4512 | static int qc_dgrams_retransmit(struct quic_conn *qc) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4513 | { |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4514 | int ret = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4515 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4516 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4517 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4518 | |
| 4519 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4520 | |
| 4521 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4522 | int i; |
| 4523 | |
| 4524 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 4525 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 4526 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4527 | |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4528 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
| 4529 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 4530 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 4531 | if (!LIST_ISEMPTY(&ifrms)) { |
| 4532 | iqel->pktns->tx.pto_probe = 1; |
| 4533 | if (!LIST_ISEMPTY(&hfrms)) |
| 4534 | hqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4535 | if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 4536 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms)) |
| 4537 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4538 | /* Put back unsent frames in their packet number spaces */ |
| 4539 | LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms); |
| 4540 | LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms); |
| 4541 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4542 | } |
| 4543 | TRACE_STATE("no more need to probe Initial packet number space", |
| 4544 | QUIC_EV_CONN_TXPKT, qc); |
| 4545 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4546 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4547 | } |
| 4548 | else { |
| 4549 | int i; |
| 4550 | |
| 4551 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4552 | hqel->pktns->tx.pto_probe = 0; |
| 4553 | 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] | 4554 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4555 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4556 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
| 4557 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4558 | if (!LIST_ISEMPTY(&frms1)) { |
| 4559 | hqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4560 | if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 4561 | QUIC_TLS_ENC_LEVEL_NONE, NULL)) |
| 4562 | goto leave; |
| 4563 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4564 | /* Put back unsent frames into their packet number spaces */ |
| 4565 | LIST_SPLICE(&hqel->pktns->tx.frms, &frms1); |
| 4566 | } |
| 4567 | } |
| 4568 | TRACE_STATE("no more need to probe Handshake packet number space", |
| 4569 | QUIC_EV_CONN_TXPKT, qc); |
| 4570 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4571 | } |
| 4572 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4573 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 4574 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4575 | |
| 4576 | aqel->pktns->tx.pto_probe = 0; |
| 4577 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 4578 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4579 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 4580 | if (!LIST_ISEMPTY(&frms1)) { |
| 4581 | aqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4582 | if (!qc_send_app_probing(qc, &frms1)) |
| 4583 | goto leave; |
| 4584 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4585 | /* Put back unsent frames into their packet number spaces */ |
| 4586 | LIST_SPLICE(&aqel->pktns->tx.frms, &frms1); |
| 4587 | } |
| 4588 | if (!LIST_ISEMPTY(&frms2)) { |
| 4589 | aqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4590 | if (!qc_send_app_probing(qc, &frms2)) |
| 4591 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4592 | /* Put back unsent frames into their packet number spaces */ |
| 4593 | LIST_SPLICE(&aqel->pktns->tx.frms, &frms2); |
| 4594 | } |
| 4595 | TRACE_STATE("no more need to probe 01RTT packet number space", |
| 4596 | QUIC_EV_CONN_TXPKT, qc); |
| 4597 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4598 | } |
| 4599 | } |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4600 | |
| 4601 | ret = 1; |
| 4602 | leave: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4603 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4604 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | /* QUIC connection packet handler task (post handshake) */ |
| 4608 | struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 4609 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4610 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4611 | struct quic_enc_level *qel; |
| 4612 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4613 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4614 | |
| 4615 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 4616 | TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state); |
| 4617 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 4618 | if (qc_test_fd(qc)) |
| 4619 | qc_rcv_buf(qc); |
| 4620 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4621 | /* Retranmissions */ |
| 4622 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 4623 | TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
| 4624 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4625 | if (!qc_dgrams_retransmit(qc)) |
| 4626 | goto out; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 4630 | qc_rm_hp_pkts(qc, qel); |
| 4631 | |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 4632 | if (!qc_treat_rx_pkts(qc, qel, NULL)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4633 | TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4634 | goto out; |
| 4635 | } |
| 4636 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 4637 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 4638 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc); |
| 4639 | goto out; |
| 4640 | } |
| 4641 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4642 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4643 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) { |
| 4644 | TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc); |
| 4645 | goto out; |
| 4646 | } |
| 4647 | |
| 4648 | /* XXX TODO: how to limit the list frames to send */ |
| 4649 | if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) { |
| 4650 | TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4651 | goto out; |
| 4652 | } |
| 4653 | |
| 4654 | out: |
| 4655 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 4656 | return t; |
| 4657 | } |
| 4658 | |
| 4659 | /* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */ |
| 4660 | static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4661 | { |
| 4662 | return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) || |
| 4663 | (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) || |
| 4664 | qel->pktns->tx.pto_probe || |
| 4665 | !LIST_ISEMPTY(&qel->pktns->tx.frms); |
| 4666 | } |
| 4667 | |
| 4668 | /* QUIC connection packet handler task. */ |
| 4669 | struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state) |
| 4670 | { |
| 4671 | int ret, ssl_err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4672 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4673 | enum quic_tls_enc_level tel, next_tel; |
| 4674 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4675 | /* Early-data encryption level */ |
| 4676 | struct quic_enc_level *eqel; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4677 | struct buffer *buf = NULL; |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 4678 | int st, zero_rtt; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4679 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4680 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4681 | eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4682 | st = qc->state; |
| 4683 | TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st); |
| 4684 | |
| 4685 | /* Retranmissions */ |
| 4686 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 4687 | TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
| 4688 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4689 | if (!qc_dgrams_retransmit(qc)) |
| 4690 | goto out; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4691 | } |
| 4692 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4693 | ssl_err = SSL_ERROR_NONE; |
| 4694 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4695 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4696 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 4697 | |
| 4698 | if (qc_test_fd(qc)) |
| 4699 | qc_rcv_buf(qc); |
| 4700 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4701 | if (st >= QUIC_HS_ST_COMPLETE && |
| 4702 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 4703 | TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 4704 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 4705 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 4706 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 4707 | } |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4708 | 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] | 4709 | goto out; |
| 4710 | |
| 4711 | qel = &qc->els[tel]; |
| 4712 | next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel]; |
| 4713 | |
| 4714 | next_level: |
| 4715 | /* Treat packets waiting for header packet protection decryption */ |
| 4716 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 4717 | qc_rm_hp_pkts(qc, qel); |
| 4718 | |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 4719 | if (!qc_treat_rx_pkts(qc, qel, next_qel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4720 | goto out; |
| 4721 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 4722 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 4723 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc); |
| 4724 | goto out; |
| 4725 | } |
| 4726 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4727 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4728 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 4729 | goto out; |
| 4730 | |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4731 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4732 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4733 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
| 4734 | if (next_qel && next_qel == eqel && zero_rtt) { |
| 4735 | TRACE_DEVEL("select 0RTT as next encryption level", |
| 4736 | QUIC_EV_CONN_PHPKTS, qc); |
| 4737 | qel = next_qel; |
| 4738 | next_qel = NULL; |
| 4739 | goto next_level; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4740 | } |
| 4741 | |
| 4742 | st = qc->state; |
| 4743 | if (st >= QUIC_HS_ST_COMPLETE) { |
| 4744 | if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) && |
| 4745 | !quic_build_post_handshake_frames(qc)) |
| 4746 | goto out; |
| 4747 | |
| 4748 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags & |
| 4749 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 4750 | /* Discard the Handshake keys. */ |
| 4751 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4752 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 4753 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 4754 | qc_set_timer(qc); |
| 4755 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4756 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
| 4757 | } |
| 4758 | |
| 4759 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 4760 | /* There may be remaining handshake to build (acks) */ |
| 4761 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 4762 | } |
| 4763 | } |
| 4764 | |
| 4765 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 4766 | * be considered. |
| 4767 | */ |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4768 | if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4769 | goto out; |
| 4770 | |
| 4771 | if (!qc_need_sending(qc, qel) && |
| 4772 | (!next_qel || !qc_need_sending(qc, next_qel))) { |
| 4773 | goto skip_send; |
| 4774 | } |
| 4775 | |
| 4776 | buf = qc_txb_alloc(qc); |
| 4777 | if (!buf) |
| 4778 | goto out; |
| 4779 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 4780 | if (b_data(buf) && !qc_purge_txbuf(qc, buf)) |
| 4781 | goto skip_send; |
| 4782 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4783 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4784 | * sendto() has failed it is emptied to simulate packet emission and |
| 4785 | * rely on QUIC lost detection to try to emit it. |
| 4786 | */ |
| 4787 | BUG_ON_HOT(b_data(buf)); |
| 4788 | b_reset(buf); |
| 4789 | |
| 4790 | ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms, |
| 4791 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4792 | if (ret == -1) { |
| 4793 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4794 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4795 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4796 | |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4797 | if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 4798 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 4799 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4800 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4804 | |
| 4805 | skip_send: |
| 4806 | /* Check if there is something to do for the next level. |
| 4807 | */ |
| 4808 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4809 | quic_tls_has_rx_sec(next_qel) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4810 | (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) { |
| 4811 | qel = next_qel; |
| 4812 | next_qel = NULL; |
| 4813 | goto next_level; |
| 4814 | } |
| 4815 | |
| 4816 | out: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4817 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err); |
| 4818 | return t; |
| 4819 | } |
| 4820 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4821 | /* Release the memory allocated for <cs> CRYPTO stream */ |
| 4822 | void quic_cstream_free(struct quic_cstream *cs) |
| 4823 | { |
| 4824 | if (!cs) { |
| 4825 | /* This is the case for ORTT encryption level */ |
| 4826 | return; |
| 4827 | } |
| 4828 | |
Amaury Denoyelle | bc174b2 | 2022-11-17 10:12:52 +0100 | [diff] [blame] | 4829 | quic_free_ncbuf(&cs->rx.ncbuf); |
| 4830 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4831 | qc_stream_desc_release(cs->desc); |
| 4832 | pool_free(pool_head_quic_cstream, cs); |
| 4833 | } |
| 4834 | |
| 4835 | /* Allocate a new QUIC stream for <qc>. |
| 4836 | * Return it if succeeded, NULL if not. |
| 4837 | */ |
| 4838 | struct quic_cstream *quic_cstream_new(struct quic_conn *qc) |
| 4839 | { |
| 4840 | struct quic_cstream *cs, *ret_cs = NULL; |
| 4841 | |
| 4842 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 4843 | cs = pool_alloc(pool_head_quic_cstream); |
| 4844 | if (!cs) { |
| 4845 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 4846 | goto leave; |
| 4847 | } |
| 4848 | |
| 4849 | cs->rx.offset = 0; |
| 4850 | cs->rx.ncbuf = NCBUF_NULL; |
| 4851 | cs->rx.offset = 0; |
| 4852 | |
| 4853 | cs->tx.offset = 0; |
| 4854 | cs->tx.sent_offset = 0; |
| 4855 | cs->tx.buf = BUF_NULL; |
| 4856 | cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc); |
| 4857 | if (!cs->desc) { |
| 4858 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 4859 | goto err; |
| 4860 | } |
| 4861 | |
| 4862 | ret_cs = cs; |
| 4863 | leave: |
| 4864 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 4865 | return ret_cs; |
| 4866 | |
| 4867 | err: |
| 4868 | pool_free(pool_head_quic_cstream, cs); |
| 4869 | goto leave; |
| 4870 | } |
| 4871 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4872 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
| 4873 | static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4874 | { |
| 4875 | int i; |
| 4876 | |
| 4877 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4878 | |
| 4879 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4880 | if (qel->tx.crypto.bufs[i]) { |
| 4881 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4882 | qel->tx.crypto.bufs[i] = NULL; |
| 4883 | } |
| 4884 | } |
| 4885 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4886 | quic_cstream_free(qel->cstream); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4887 | |
| 4888 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4889 | } |
| 4890 | |
| 4891 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
| 4892 | * connection allocating everything needed. |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4893 | * |
| 4894 | * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use |
| 4895 | * quic_conn_enc_level_uninit() to cleanup partially allocated content. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4896 | */ |
| 4897 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4898 | enum quic_tls_enc_level level) |
| 4899 | { |
| 4900 | int ret = 0; |
| 4901 | struct quic_enc_level *qel; |
| 4902 | |
| 4903 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4904 | |
| 4905 | qel = &qc->els[level]; |
| 4906 | qel->level = quic_to_ssl_enc_level(level); |
| 4907 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4908 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4909 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 4910 | qel->tls_ctx.flags = 0; |
| 4911 | |
| 4912 | qel->rx.pkts = EB_ROOT; |
| 4913 | LIST_INIT(&qel->rx.pqpkts); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4914 | |
| 4915 | /* Allocate only one buffer. */ |
| 4916 | /* TODO: use a pool */ |
| 4917 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4918 | if (!qel->tx.crypto.bufs) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4919 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4920 | |
| 4921 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4922 | if (!qel->tx.crypto.bufs[0]) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4923 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4924 | |
| 4925 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4926 | qel->tx.crypto.nb_buf = 1; |
| 4927 | |
| 4928 | qel->tx.crypto.sz = 0; |
| 4929 | qel->tx.crypto.offset = 0; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4930 | /* No CRYPTO data for early data TLS encryption level */ |
| 4931 | if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA) |
| 4932 | qel->cstream = NULL; |
| 4933 | else { |
| 4934 | qel->cstream = quic_cstream_new(qc); |
| 4935 | if (!qel->cstream) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4936 | goto leave; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4937 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4938 | |
| 4939 | ret = 1; |
| 4940 | leave: |
| 4941 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4942 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4943 | } |
| 4944 | |
Frédéric Lécaille | 7c6d8f8 | 2023-02-14 16:00:18 +0100 | [diff] [blame] | 4945 | /* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not. |
| 4946 | * This is not the case if the remote peer address is not validated and if |
| 4947 | * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes. |
| 4948 | */ |
| 4949 | static int qc_may_probe_ipktns(struct quic_conn *qc) |
| 4950 | { |
| 4951 | return quic_peer_validated_addr(qc) || |
| 4952 | (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN; |
| 4953 | } |
| 4954 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4955 | /* Callback called upon loss detection and PTO timer expirations. */ |
| 4956 | struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state) |
| 4957 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4958 | struct quic_conn *qc = ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4959 | struct quic_pktns *pktns; |
| 4960 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4961 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
| 4962 | NULL, NULL, &qc->path->ifae_pkts); |
| 4963 | task->expire = TICK_ETERNITY; |
| 4964 | pktns = quic_loss_pktns(qc); |
| 4965 | if (tick_isset(pktns->tx.loss_time)) { |
| 4966 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4967 | |
| 4968 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 4969 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4970 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 4971 | if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms)) |
| 4972 | qc_set_timer(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4973 | goto out; |
| 4974 | } |
| 4975 | |
| 4976 | if (qc->path->in_flight) { |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 4977 | pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL); |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 4978 | if (!qc_notify_send(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4979 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
Frédéric Lécaille | 7c6d8f8 | 2023-02-14 16:00:18 +0100 | [diff] [blame] | 4980 | if (qc_may_probe_ipktns(qc)) { |
| 4981 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4982 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4983 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4984 | } |
| 4985 | else { |
| 4986 | TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4987 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4988 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) { |
Frédéric Lécaille | 7c6d8f8 | 2023-02-14 16:00:18 +0100 | [diff] [blame] | 4989 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4990 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4991 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4992 | } |
| 4993 | } |
| 4994 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) { |
| 4995 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | 7c6d8f8 | 2023-02-14 16:00:18 +0100 | [diff] [blame] | 4996 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4997 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4998 | 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] | 4999 | if (qc_may_probe_ipktns(qc)) { |
| 5000 | qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 5001 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 5002 | } |
| 5003 | else { |
| 5004 | TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 5005 | } |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 5006 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5007 | } |
| 5008 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) { |
| 5009 | 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] | 5010 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 5011 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5012 | } |
| 5013 | } |
| 5014 | } |
| 5015 | else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) { |
| 5016 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 5017 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 5018 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 5019 | if (quic_tls_has_tx_sec(hel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5020 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 5021 | if (quic_tls_has_tx_sec(iel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5022 | iel->pktns->tx.pto_probe = 1; |
| 5023 | } |
| 5024 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5025 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5026 | qc->path->loss.pto_count++; |
| 5027 | |
| 5028 | out: |
| 5029 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
| 5030 | |
| 5031 | return task; |
| 5032 | } |
| 5033 | |
| 5034 | /* Parse the Retry token from buffer <token> with <end> a pointer to |
| 5035 | * one byte past the end of this buffer. This will extract the ODCID |
| 5036 | * which will be stored into <odcid> |
| 5037 | * |
| 5038 | * Returns 0 on success else non-zero. |
| 5039 | */ |
| 5040 | static int parse_retry_token(struct quic_conn *qc, |
| 5041 | const unsigned char *token, const unsigned char *end, |
| 5042 | struct quic_cid *odcid) |
| 5043 | { |
| 5044 | int ret = 0; |
| 5045 | uint64_t odcid_len; |
| 5046 | uint32_t timestamp; |
| 5047 | |
| 5048 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 5049 | |
| 5050 | if (!quic_dec_int(&odcid_len, &token, end)) { |
| 5051 | TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc); |
| 5052 | goto leave; |
| 5053 | } |
| 5054 | |
| 5055 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 5056 | * When an Initial packet is sent by a client that has not previously |
| 5057 | * received an Initial or Retry packet from the server, the client |
| 5058 | * populates the Destination Connection ID field with an unpredictable |
| 5059 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 5060 | */ |
| 5061 | if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) { |
| 5062 | TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 5063 | goto leave; |
| 5064 | } |
| 5065 | |
| 5066 | if (end - token < odcid_len + sizeof timestamp) { |
| 5067 | TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 5068 | goto leave; |
| 5069 | } |
| 5070 | |
| 5071 | timestamp = ntohl(read_u32(token + odcid_len)); |
| 5072 | if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) { |
| 5073 | TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc); |
| 5074 | goto leave; |
| 5075 | } |
| 5076 | |
| 5077 | ret = 1; |
| 5078 | memcpy(odcid->data, token, odcid_len); |
| 5079 | odcid->len = odcid_len; |
| 5080 | leave: |
| 5081 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 5082 | return !ret; |
| 5083 | } |
| 5084 | |
| 5085 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 5086 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 5087 | * for QUIC servers (or haproxy listeners). |
| 5088 | * <dcid> is the destination connection ID, <scid> is the source connection ID, |
| 5089 | * <token> the token found to be used for this connection with <token_len> as |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 5090 | * length. Endpoints addresses are specified via <local_addr> and <peer_addr>. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5091 | * Returns the connection if succeeded, NULL if not. |
| 5092 | */ |
| 5093 | static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4, |
| 5094 | struct quic_cid *dcid, struct quic_cid *scid, |
| 5095 | const struct quic_cid *token_odcid, |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 5096 | struct sockaddr_storage *local_addr, |
| 5097 | struct sockaddr_storage *peer_addr, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5098 | int server, int token, void *owner) |
| 5099 | { |
| 5100 | int i; |
| 5101 | struct quic_conn *qc; |
| 5102 | /* Initial CID. */ |
| 5103 | struct quic_connection_id *icid; |
| 5104 | char *buf_area = NULL; |
| 5105 | struct listener *l = NULL; |
| 5106 | struct quic_cc_algo *cc_algo = NULL; |
| 5107 | struct quic_tls_ctx *ictx; |
| 5108 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5109 | /* TODO replace pool_zalloc by pool_alloc(). This requires special care |
| 5110 | * to properly initialized internal quic_conn members to safely use |
| 5111 | * quic_conn_release() on alloc failure. |
| 5112 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5113 | qc = pool_zalloc(pool_head_quic_conn); |
| 5114 | if (!qc) { |
| 5115 | TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 5116 | goto err; |
| 5117 | } |
| 5118 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5119 | /* Initialize in priority qc members required for a safe dealloc. */ |
| 5120 | |
| 5121 | /* required to use MTLIST_IN_LIST */ |
| 5122 | MT_LIST_INIT(&qc->accept_list); |
| 5123 | |
| 5124 | LIST_INIT(&qc->rx.pkt_list); |
| 5125 | |
Amaury Denoyelle | 4244833 | 2022-12-12 11:24:05 +0100 | [diff] [blame] | 5126 | qc_init_fd(qc); |
| 5127 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5128 | LIST_INIT(&qc->back_refs); |
| 5129 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5130 | /* Now proceeds to allocation of qc members. */ |
| 5131 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5132 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 5133 | if (!buf_area) { |
| 5134 | TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc); |
| 5135 | goto err; |
| 5136 | } |
| 5137 | |
| 5138 | qc->cids = EB_ROOT; |
| 5139 | /* QUIC Server (or listener). */ |
| 5140 | if (server) { |
| 5141 | struct proxy *prx; |
| 5142 | |
| 5143 | l = owner; |
| 5144 | prx = l->bind_conf->frontend; |
| 5145 | cc_algo = l->bind_conf->quic_cc_algo; |
| 5146 | |
| 5147 | qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 5148 | &quic_stats_module); |
| 5149 | qc->flags |= QUIC_FL_CONN_LISTENER; |
| 5150 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
| 5151 | /* Copy the initial DCID with the address. */ |
| 5152 | qc->odcid.len = dcid->len; |
| 5153 | qc->odcid.addrlen = dcid->addrlen; |
| 5154 | memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen); |
| 5155 | |
| 5156 | /* copy the packet SCID to reuse it as DCID for sending */ |
| 5157 | if (scid->len) |
| 5158 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 5159 | qc->dcid.len = scid->len; |
| 5160 | qc->tx.buf = BUF_NULL; |
| 5161 | qc->li = l; |
| 5162 | } |
| 5163 | /* QUIC Client (outgoing connection to servers) */ |
| 5164 | else { |
| 5165 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
| 5166 | if (dcid->len) |
| 5167 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 5168 | qc->dcid.len = dcid->len; |
| 5169 | } |
| 5170 | qc->mux_state = QC_MUX_NULL; |
| 5171 | qc->err = quic_err_transport(QC_ERR_NO_ERROR); |
| 5172 | |
| 5173 | icid = new_quic_cid(&qc->cids, qc, 0); |
| 5174 | if (!icid) { |
| 5175 | TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc); |
| 5176 | goto err; |
| 5177 | } |
| 5178 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5179 | if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) && |
| 5180 | is_addr(local_addr)) { |
| 5181 | TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc); |
| 5182 | qc_alloc_fd(qc, local_addr, peer_addr); |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 5183 | |
| 5184 | /* haproxy soft-stop is supported only for QUIC connections |
| 5185 | * with their owned socket. |
| 5186 | */ |
| 5187 | if (qc_test_fd(qc)) |
| 5188 | _HA_ATOMIC_INC(&jobs); |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5189 | } |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5190 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5191 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 5192 | if (server) |
| 5193 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
| 5194 | |
| 5195 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 5196 | qc->scid = icid->cid; |
| 5197 | |
| 5198 | /* Packet number spaces initialization. */ |
| 5199 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 5200 | quic_pktns_init(&qc->pktns[i]); |
| 5201 | /* QUIC encryption level context initialization. */ |
| 5202 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 5203 | if (!quic_conn_enc_level_init(qc, i)) { |
| 5204 | TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc); |
| 5205 | goto err; |
| 5206 | } |
| 5207 | /* Initialize the packet number space. */ |
| 5208 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 5209 | } |
| 5210 | |
| 5211 | qc->original_version = qv; |
| 5212 | qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ? |
| 5213 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 5214 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
| 5215 | /* TX part. */ |
| 5216 | LIST_INIT(&qc->tx.frms_to_send); |
| 5217 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 5218 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 5219 | qc->tx.bytes = 0; |
| 5220 | qc->tx.buf = BUF_NULL; |
| 5221 | /* RX part. */ |
| 5222 | qc->rx.bytes = 0; |
| 5223 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 5224 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 5225 | qc->rx.strms[i].nb_streams = 0; |
| 5226 | |
| 5227 | qc->nb_pkt_for_cc = 1; |
| 5228 | qc->nb_pkt_since_cc = 0; |
| 5229 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5230 | if (!quic_tls_ku_init(qc)) { |
| 5231 | TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc); |
| 5232 | goto err; |
| 5233 | } |
| 5234 | |
| 5235 | /* XXX TO DO: Only one path at this time. */ |
| 5236 | qc->path = &qc->paths[0]; |
| 5237 | quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc); |
| 5238 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5239 | qc->streams_by_id = EB_ROOT_UNIQUE; |
| 5240 | qc->stream_buf_count = 0; |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 5241 | memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr)); |
| 5242 | memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5243 | |
| 5244 | if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params, |
| 5245 | icid->stateless_reset_token, |
| 5246 | dcid->data, dcid->len, |
| 5247 | qc->scid.data, qc->scid.len, token_odcid)) |
| 5248 | goto err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5249 | |
| 5250 | qc->wait_event.tasklet = tasklet_new(); |
| 5251 | if (!qc->wait_event.tasklet) { |
| 5252 | TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT); |
| 5253 | goto err; |
| 5254 | } |
| 5255 | qc->wait_event.tasklet->process = quic_conn_io_cb; |
| 5256 | qc->wait_event.tasklet->context = qc; |
| 5257 | qc->wait_event.events = 0; |
| 5258 | /* Set tasklet tid based on the SCID selected by us for this |
| 5259 | * connection. The upper layer will also be binded on the same thread. |
| 5260 | */ |
Willy Tarreau | eed7826 | 2022-12-21 09:09:19 +0100 | [diff] [blame] | 5261 | qc->tid = quic_get_cid_tid(qc->scid.data, &l->rx); |
Willy Tarreau | f5a0c8a | 2022-10-13 16:14:11 +0200 | [diff] [blame] | 5262 | qc->wait_event.tasklet->tid = qc->tid; |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 5263 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5264 | |
| 5265 | if (qc_conn_alloc_ssl_ctx(qc) || |
| 5266 | !quic_conn_init_timer(qc) || |
| 5267 | !quic_conn_init_idle_timer_task(qc)) |
| 5268 | goto err; |
| 5269 | |
| 5270 | ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx; |
| 5271 | if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1)) |
| 5272 | goto err; |
| 5273 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5274 | LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx); |
| 5275 | qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch); |
| 5276 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5277 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
| 5278 | |
| 5279 | return qc; |
| 5280 | |
| 5281 | err: |
| 5282 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5283 | if (qc) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5284 | qc->rx.buf.area = NULL; |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5285 | quic_conn_release(qc); |
| 5286 | } |
| 5287 | TRACE_LEAVE(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5288 | return NULL; |
| 5289 | } |
| 5290 | |
| 5291 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 5292 | * The connection tasklet is killed. |
| 5293 | * |
| 5294 | * This function must only be called by the thread responsible of the quic_conn |
| 5295 | * tasklet. |
| 5296 | */ |
| 5297 | void quic_conn_release(struct quic_conn *qc) |
| 5298 | { |
| 5299 | int i; |
| 5300 | struct ssl_sock_ctx *conn_ctx; |
| 5301 | struct eb64_node *node; |
| 5302 | struct quic_tls_ctx *app_tls_ctx; |
| 5303 | struct quic_rx_packet *pkt, *pktback; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5304 | struct bref *bref, *back; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5305 | |
| 5306 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 5307 | |
| 5308 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 5309 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 5310 | |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 5311 | if (qc_test_fd(qc)) |
| 5312 | _HA_ATOMIC_DEC(&jobs); |
| 5313 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5314 | /* Close quic-conn socket fd. */ |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 5315 | qc_release_fd(qc, 0); |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5316 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5317 | /* in the unlikely (but possible) case the connection was just added to |
| 5318 | * the accept_list we must delete it from there. |
| 5319 | */ |
| 5320 | MT_LIST_DELETE(&qc->accept_list); |
| 5321 | |
| 5322 | /* free remaining stream descriptors */ |
| 5323 | node = eb64_first(&qc->streams_by_id); |
| 5324 | while (node) { |
| 5325 | struct qc_stream_desc *stream; |
| 5326 | |
| 5327 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 5328 | node = eb64_next(node); |
| 5329 | |
| 5330 | /* all streams attached to the quic-conn are released, so |
| 5331 | * qc_stream_desc_free will liberate the stream instance. |
| 5332 | */ |
| 5333 | BUG_ON(!stream->release); |
| 5334 | qc_stream_desc_free(stream, 1); |
| 5335 | } |
| 5336 | |
| 5337 | /* Purge Rx packet list. */ |
| 5338 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 5339 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 5340 | pool_free(pool_head_quic_rx_packet, pkt); |
| 5341 | } |
| 5342 | |
| 5343 | if (qc->idle_timer_task) { |
| 5344 | task_destroy(qc->idle_timer_task); |
| 5345 | qc->idle_timer_task = NULL; |
| 5346 | } |
| 5347 | |
| 5348 | if (qc->timer_task) { |
| 5349 | task_destroy(qc->timer_task); |
| 5350 | qc->timer_task = NULL; |
| 5351 | } |
| 5352 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5353 | if (qc->wait_event.tasklet) |
| 5354 | tasklet_free(qc->wait_event.tasklet); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5355 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5356 | /* remove the connection from receiver cids trees */ |
| 5357 | ebmb_delete(&qc->odcid_node); |
| 5358 | ebmb_delete(&qc->scid_node); |
| 5359 | free_quic_conn_cids(qc); |
| 5360 | |
| 5361 | conn_ctx = qc->xprt_ctx; |
| 5362 | if (conn_ctx) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5363 | SSL_free(conn_ctx->ssl); |
| 5364 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
| 5365 | } |
| 5366 | |
| 5367 | quic_tls_ku_free(qc); |
| 5368 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 5369 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
| 5370 | quic_conn_enc_level_uninit(qc, &qc->els[i]); |
| 5371 | } |
| 5372 | quic_tls_ctx_secs_free(&qc->negotiated_ictx); |
| 5373 | |
| 5374 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 5375 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 5376 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
| 5377 | |
| 5378 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 5379 | quic_pktns_tx_pkts_release(&qc->pktns[i], qc); |
| 5380 | quic_free_arngs(qc, &qc->pktns[i].rx.arngs); |
| 5381 | } |
| 5382 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5383 | /* Detach CLI context watchers currently dumping this connection. |
| 5384 | * Reattach them to the next quic_conn instance. |
| 5385 | */ |
| 5386 | list_for_each_entry_safe(bref, back, &qc->back_refs, users) { |
| 5387 | /* Remove watcher from this quic_conn instance. */ |
| 5388 | LIST_DEL_INIT(&bref->users); |
| 5389 | |
| 5390 | /* Attach it to next instance unless it was the last list element. */ |
| 5391 | if (qc->el_th_ctx.n != &th_ctx->quic_conns) { |
| 5392 | struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx, |
| 5393 | struct quic_conn *, |
| 5394 | el_th_ctx); |
| 5395 | LIST_APPEND(&next->back_refs, &bref->users); |
| 5396 | } |
| 5397 | bref->ref = qc->el_th_ctx.n; |
| 5398 | __ha_barrier_store(); |
| 5399 | } |
| 5400 | /* Remove quic_conn from global ha_thread_ctx list. */ |
| 5401 | LIST_DELETE(&qc->el_th_ctx); |
| 5402 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5403 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 5404 | pool_free(pool_head_quic_conn, qc); |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 5405 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5406 | TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc); |
| 5407 | |
| 5408 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 5409 | } |
| 5410 | |
| 5411 | /* Initialize the timer task of <qc> QUIC connection. |
| 5412 | * Returns 1 if succeeded, 0 if not. |
| 5413 | */ |
| 5414 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 5415 | { |
| 5416 | int ret = 0; |
| 5417 | /* Attach this task to the same thread ID used for the connection */ |
| 5418 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5419 | |
| 5420 | qc->timer_task = task_new_on(qc->tid); |
| 5421 | if (!qc->timer_task) { |
| 5422 | TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 5423 | goto leave; |
| 5424 | } |
| 5425 | |
| 5426 | qc->timer = TICK_ETERNITY; |
| 5427 | qc->timer_task->process = qc_process_timer; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5428 | qc->timer_task->context = qc; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5429 | |
| 5430 | ret = 1; |
| 5431 | leave: |
| 5432 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5433 | return ret; |
| 5434 | } |
| 5435 | |
| 5436 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 5437 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 5438 | { |
| 5439 | unsigned int expire; |
| 5440 | |
Amaury Denoyelle | 77ed631 | 2023-02-01 09:28:55 +0100 | [diff] [blame] | 5441 | if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) { |
| 5442 | TRACE_STATE("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5443 | task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG); |
| 5444 | } |
| 5445 | else { |
| 5446 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 5447 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 5448 | task_queue(qc->idle_timer_task); |
| 5449 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 5453 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 5454 | */ |
| 5455 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 5456 | { |
| 5457 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5458 | |
| 5459 | if (read) { |
| 5460 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 5461 | } |
| 5462 | else { |
| 5463 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 5464 | } |
| 5465 | qc_idle_timer_do_rearm(qc); |
| 5466 | |
| 5467 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5468 | } |
| 5469 | |
| 5470 | /* The task handling the idle timeout */ |
| 5471 | struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 5472 | { |
| 5473 | struct quic_conn *qc = ctx; |
| 5474 | struct quic_counters *prx_counters = qc->prx_counters; |
| 5475 | unsigned int qc_flags = qc->flags; |
| 5476 | |
| 5477 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5478 | |
| 5479 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 5480 | * might free the quic-conn too early via quic_close(). |
| 5481 | */ |
| 5482 | qc_notify_close(qc); |
| 5483 | |
| 5484 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 5485 | * responsible to call quic_close to release it. |
| 5486 | */ |
| 5487 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 5488 | if (qc->mux_state != QC_MUX_READY) |
| 5489 | quic_conn_release(qc); |
| 5490 | |
| 5491 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 5492 | * least clean some parts of it such as the tasklet. |
| 5493 | */ |
| 5494 | |
| 5495 | if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 5496 | qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 5497 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
| 5498 | HA_ATOMIC_DEC(&prx_counters->half_open_conn); |
| 5499 | } |
| 5500 | |
| 5501 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5502 | return NULL; |
| 5503 | } |
| 5504 | |
| 5505 | /* Initialize the idle timeout task for <qc>. |
| 5506 | * Returns 1 if succeeded, 0 if not. |
| 5507 | */ |
| 5508 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 5509 | { |
| 5510 | int ret = 0; |
| 5511 | |
| 5512 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5513 | |
| 5514 | qc->idle_timer_task = task_new_here(); |
| 5515 | if (!qc->idle_timer_task) { |
| 5516 | TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 5517 | goto leave; |
| 5518 | } |
| 5519 | |
| 5520 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 5521 | qc->idle_timer_task->context = qc; |
| 5522 | qc_idle_timer_rearm(qc, 1); |
| 5523 | task_queue(qc->idle_timer_task); |
| 5524 | |
| 5525 | ret = 1; |
| 5526 | leave: |
| 5527 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5528 | return ret; |
| 5529 | } |
| 5530 | |
| 5531 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 5532 | * past one byte of this buffer. |
| 5533 | */ |
| 5534 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 5535 | struct quic_rx_packet *pkt) |
| 5536 | { |
| 5537 | int ret = 0; |
| 5538 | unsigned char dcid_len, scid_len; |
| 5539 | |
| 5540 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5541 | |
| 5542 | if (end == *buf) { |
| 5543 | TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT); |
| 5544 | goto leave; |
| 5545 | } |
| 5546 | |
| 5547 | /* Destination Connection ID Length */ |
| 5548 | dcid_len = *(*buf)++; |
| 5549 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 5550 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) { |
| 5551 | TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT); |
| 5552 | goto leave; |
| 5553 | } |
| 5554 | |
| 5555 | if (dcid_len) { |
| 5556 | /* Check that the length of this received DCID matches the CID lengths |
| 5557 | * of our implementation for non Initials packets only. |
| 5558 | */ |
| 5559 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 5560 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
| 5561 | dcid_len != QUIC_HAP_CID_LEN) { |
| 5562 | TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT); |
| 5563 | goto leave; |
| 5564 | } |
| 5565 | |
| 5566 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 5567 | } |
| 5568 | |
| 5569 | pkt->dcid.len = dcid_len; |
| 5570 | *buf += dcid_len; |
| 5571 | |
| 5572 | /* Source Connection ID Length */ |
| 5573 | scid_len = *(*buf)++; |
| 5574 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) { |
| 5575 | TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT); |
| 5576 | goto leave; |
| 5577 | } |
| 5578 | |
| 5579 | if (scid_len) |
| 5580 | memcpy(pkt->scid.data, *buf, scid_len); |
| 5581 | pkt->scid.len = scid_len; |
| 5582 | *buf += scid_len; |
| 5583 | |
| 5584 | ret = 1; |
| 5585 | leave: |
| 5586 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 5587 | return ret; |
| 5588 | } |
| 5589 | |
| 5590 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 5591 | static void qc_pkt_insert(struct quic_conn *qc, |
| 5592 | struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 5593 | { |
| 5594 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 5595 | |
| 5596 | pkt->pn_node.key = pkt->pn; |
| 5597 | quic_rx_packet_refinc(pkt); |
| 5598 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 5599 | |
| 5600 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 5601 | } |
| 5602 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5603 | /* Try to remove the header protection of <pkt> QUIC packet with <beg> the |
| 5604 | * address of the packet first byte, using the keys from encryption level <el>. |
| 5605 | * |
| 5606 | * If header protection has been successfully removed, packet data are copied |
| 5607 | * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also |
| 5608 | * proceeded, and the packet is inserted into <qc> protected packets tree. In |
| 5609 | * both cases, packet can now be considered handled by the <qc> connection. |
| 5610 | * |
| 5611 | * If header protection cannot be removed due to <el> secrets already |
| 5612 | * discarded, no operation is conducted. |
| 5613 | * |
| 5614 | * Returns 1 on success : packet data is now handled by the connection. On |
| 5615 | * error 0 is returned : packet should be dropped by the caller. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5616 | */ |
| 5617 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 5618 | struct quic_rx_packet *pkt, |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5619 | unsigned char *beg, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5620 | struct quic_enc_level **el) |
| 5621 | { |
| 5622 | int ret = 0; |
| 5623 | unsigned char *pn = NULL; /* Packet number field */ |
| 5624 | enum quic_tls_enc_level tel; |
| 5625 | struct quic_enc_level *qel; |
| 5626 | /* Only for traces. */ |
| 5627 | struct quic_rx_packet *qpkt_trace; |
| 5628 | |
| 5629 | qpkt_trace = NULL; |
| 5630 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5631 | BUG_ON(!pkt->pn_offset); |
| 5632 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5633 | /* The packet number is here. This is also the start minus |
| 5634 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 5635 | * protection. |
| 5636 | */ |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5637 | pn = beg + pkt->pn_offset; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5638 | |
| 5639 | tel = quic_packet_type_enc_level(pkt->type); |
| 5640 | qel = &qc->els[tel]; |
| 5641 | |
| 5642 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 5643 | struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt); |
| 5644 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5645 | /* Note that the following function enables us to unprotect the packet |
| 5646 | * number and its length subsequently used to decrypt the entire |
| 5647 | * packets. |
| 5648 | */ |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 5649 | if (!qc_do_rm_hp(qc, pkt, tls_ctx, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5650 | qel->pktns->rx.largest_pn, pn, beg)) { |
| 5651 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
| 5652 | goto out; |
| 5653 | } |
| 5654 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5655 | /* The AAD includes the packet number field. */ |
| 5656 | pkt->aad_len = pkt->pn_offset + pkt->pnl; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5657 | if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) { |
| 5658 | TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc); |
| 5659 | goto out; |
| 5660 | } |
| 5661 | |
| 5662 | qpkt_trace = pkt; |
| 5663 | } |
| 5664 | else { |
| 5665 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 5666 | /* If the packet number space has been discarded, this packet |
| 5667 | * will be not parsed. |
| 5668 | */ |
| 5669 | TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt); |
| 5670 | goto out; |
| 5671 | } |
| 5672 | |
| 5673 | TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5674 | LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 5675 | quic_rx_packet_refinc(pkt); |
| 5676 | } |
| 5677 | |
| 5678 | *el = qel; |
| 5679 | /* No reference counter incrementation here!!! */ |
| 5680 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 5681 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 5682 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 5683 | b_add(&qc->rx.buf, pkt->len); |
| 5684 | |
| 5685 | ret = 1; |
| 5686 | out: |
| 5687 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
| 5688 | return ret; |
| 5689 | } |
| 5690 | |
| 5691 | /* Parse the header form from <byte0> first byte of <pkt> packet to set its type. |
| 5692 | * Also set <*long_header> to 1 if this form is long, 0 if not and the version |
| 5693 | * of this packet into <*version>. |
| 5694 | */ |
| 5695 | static inline int qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 5696 | unsigned char **buf, const unsigned char *end, |
| 5697 | int *long_header, uint32_t *version) |
| 5698 | { |
| 5699 | int ret = 0; |
| 5700 | const unsigned char byte0 = **buf; |
| 5701 | |
| 5702 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5703 | |
| 5704 | (*buf)++; |
| 5705 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 5706 | unsigned char type = |
| 5707 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 5708 | |
| 5709 | *long_header = 1; |
| 5710 | /* Version */ |
| 5711 | if (!quic_read_uint32(version, (const unsigned char **)buf, end)) { |
| 5712 | TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT); |
| 5713 | goto out; |
| 5714 | } |
| 5715 | |
Frédéric Lécaille | 21c4c9b | 2023-01-13 16:37:02 +0100 | [diff] [blame] | 5716 | if (*version != QUIC_PROTOCOL_VERSION_2) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5717 | pkt->type = type; |
| 5718 | } |
| 5719 | else { |
| 5720 | switch (type) { |
| 5721 | case 0: |
| 5722 | pkt->type = QUIC_PACKET_TYPE_RETRY; |
| 5723 | break; |
| 5724 | case 1: |
| 5725 | pkt->type = QUIC_PACKET_TYPE_INITIAL; |
| 5726 | break; |
| 5727 | case 2: |
| 5728 | pkt->type = QUIC_PACKET_TYPE_0RTT; |
| 5729 | break; |
| 5730 | case 3: |
| 5731 | pkt->type = QUIC_PACKET_TYPE_HANDSHAKE; |
| 5732 | break; |
| 5733 | } |
| 5734 | } |
| 5735 | } |
| 5736 | else { |
| 5737 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 5738 | *long_header = 0; |
| 5739 | } |
| 5740 | |
| 5741 | ret = 1; |
| 5742 | out: |
| 5743 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 5744 | return ret; |
| 5745 | } |
| 5746 | |
| 5747 | /* Return the QUIC version (quic_version struct) with <version> as version number |
| 5748 | * if supported or NULL if not. |
| 5749 | */ |
| 5750 | static inline const struct quic_version *qc_supported_version(uint32_t version) |
| 5751 | { |
| 5752 | int i; |
| 5753 | |
| 5754 | for (i = 0; i < quic_versions_nb; i++) |
| 5755 | if (quic_versions[i].num == version) |
| 5756 | return &quic_versions[i]; |
| 5757 | |
| 5758 | return NULL; |
| 5759 | } |
| 5760 | |
| 5761 | /* |
| 5762 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 5763 | * address <addr>. |
| 5764 | * Implementation of RFC9000 6. Version Negotiation |
| 5765 | * |
| 5766 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 5767 | * |
| 5768 | * Returns 0 on success else non-zero |
| 5769 | */ |
| 5770 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 5771 | struct quic_rx_packet *pkt) |
| 5772 | { |
| 5773 | char buf[256]; |
| 5774 | int ret = 0, i = 0, j; |
| 5775 | uint32_t version; |
| 5776 | const socklen_t addrlen = get_addr_len(addr); |
| 5777 | |
| 5778 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5779 | /* |
| 5780 | * header form |
| 5781 | * long header, fixed bit to 0 for Version Negotiation |
| 5782 | */ |
| 5783 | /* TODO: RAND_bytes() should be replaced? */ |
| 5784 | if (RAND_bytes((unsigned char *)buf, 1) != 1) { |
| 5785 | TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT); |
| 5786 | goto out; |
| 5787 | } |
| 5788 | |
| 5789 | buf[i++] |= '\x80'; |
| 5790 | /* null version for Version Negotiation */ |
| 5791 | buf[i++] = '\x00'; |
| 5792 | buf[i++] = '\x00'; |
| 5793 | buf[i++] = '\x00'; |
| 5794 | buf[i++] = '\x00'; |
| 5795 | |
| 5796 | /* source connection id */ |
| 5797 | buf[i++] = pkt->scid.len; |
| 5798 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5799 | i += pkt->scid.len; |
| 5800 | |
| 5801 | /* destination connection id */ |
| 5802 | buf[i++] = pkt->dcid.len; |
| 5803 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 5804 | i += pkt->dcid.len; |
| 5805 | |
| 5806 | /* supported version */ |
| 5807 | for (j = 0; j < quic_versions_nb; j++) { |
| 5808 | version = htonl(quic_versions[j].num); |
| 5809 | memcpy(&buf[i], &version, sizeof(version)); |
| 5810 | i += sizeof(version); |
| 5811 | } |
| 5812 | |
| 5813 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 5814 | goto out; |
| 5815 | |
| 5816 | ret = 1; |
| 5817 | out: |
| 5818 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5819 | return !ret; |
| 5820 | } |
| 5821 | |
| 5822 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 5823 | * from <fd> UDP socket to <dst> |
| 5824 | * Return 1 if succeeded, 0 if not. |
| 5825 | */ |
| 5826 | static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr, |
| 5827 | struct quic_rx_packet *rxpkt) |
| 5828 | { |
| 5829 | int ret = 0, pktlen, rndlen; |
| 5830 | unsigned char pkt[64]; |
| 5831 | const socklen_t addrlen = get_addr_len(dstaddr); |
| 5832 | struct proxy *prx; |
| 5833 | struct quic_counters *prx_counters; |
| 5834 | |
| 5835 | TRACE_ENTER(QUIC_EV_STATELESS_RST); |
| 5836 | |
| 5837 | prx = l->bind_conf->frontend; |
| 5838 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 5839 | /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3) |
| 5840 | * The resulting minimum size of 21 bytes does not guarantee that a Stateless |
| 5841 | * Reset is difficult to distinguish from other packets if the recipient requires |
| 5842 | * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure |
| 5843 | * that all packets it sends are at least 22 bytes longer than the minimum |
| 5844 | * connection ID length that it requests the peer to include in its packets, |
| 5845 | * adding PADDING frames as necessary. This ensures that any Stateless Reset |
| 5846 | * sent by the peer is indistinguishable from a valid packet sent to the endpoint. |
| 5847 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 5848 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 5849 | * than the packet it responds to. |
| 5850 | */ |
| 5851 | |
| 5852 | /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */ |
| 5853 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0; |
| 5854 | pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen); |
| 5855 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
| 5856 | |
| 5857 | /* Put a header of random bytes */ |
| 5858 | /* TODO: RAND_bytes() should be replaced */ |
| 5859 | if (RAND_bytes(pkt, rndlen) != 1) { |
| 5860 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST); |
| 5861 | goto leave; |
| 5862 | } |
| 5863 | |
| 5864 | /* Clear the most significant bit, and set the second one */ |
| 5865 | *pkt = (*pkt & ~0x80) | 0x40; |
| 5866 | if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN, |
| 5867 | rxpkt->dcid.data, rxpkt->dcid.len)) |
| 5868 | goto leave; |
| 5869 | |
| 5870 | if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0) |
| 5871 | goto leave; |
| 5872 | |
| 5873 | ret = 1; |
| 5874 | HA_ATOMIC_INC(&prx_counters->stateless_reset_sent); |
| 5875 | TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid); |
| 5876 | leave: |
| 5877 | TRACE_LEAVE(QUIC_EV_STATELESS_RST); |
| 5878 | return ret; |
| 5879 | } |
| 5880 | |
| 5881 | /* QUIC server only function. |
| 5882 | * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address. |
| 5883 | * This is the responsibility of the caller to check <aad> size is big enough |
| 5884 | * to contain these data. |
| 5885 | * Return the number of bytes copied to <aad>. |
| 5886 | */ |
| 5887 | static int quic_generate_retry_token_aad(unsigned char *aad, |
| 5888 | uint32_t version, |
| 5889 | const struct quic_cid *cid, |
| 5890 | const struct sockaddr_storage *addr) |
| 5891 | { |
| 5892 | unsigned char *p; |
| 5893 | |
| 5894 | p = aad; |
| 5895 | memcpy(p, &version, sizeof version); |
| 5896 | p += sizeof version; |
| 5897 | p += quic_saddr_cpy(p, addr); |
| 5898 | memcpy(p, cid->data, cid->len); |
| 5899 | p += cid->len; |
| 5900 | |
| 5901 | return p - aad; |
| 5902 | } |
| 5903 | |
| 5904 | /* QUIC server only function. |
| 5905 | * Generate the token to be used in Retry packets. The token is written to |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 5906 | * <buf> with <len> as length. <odcid> is the original destination connection |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5907 | * ID and <dcid> is our side destination connection ID (or client source |
| 5908 | * connection ID). |
| 5909 | * Returns the length of the encoded token or 0 on error. |
| 5910 | */ |
| 5911 | static int quic_generate_retry_token(unsigned char *buf, size_t len, |
| 5912 | const uint32_t version, |
| 5913 | const struct quic_cid *odcid, |
| 5914 | const struct quic_cid *dcid, |
| 5915 | struct sockaddr_storage *addr) |
| 5916 | { |
| 5917 | int ret = 0; |
| 5918 | unsigned char *p; |
| 5919 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
Amaury Denoyelle | 6c94056 | 2022-10-18 11:05:02 +0200 | [diff] [blame] | 5920 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5921 | size_t aadlen; |
| 5922 | unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN]; |
| 5923 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5924 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5925 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5926 | size_t seclen = strlen(global.cluster_secret); |
| 5927 | EVP_CIPHER_CTX *ctx = NULL; |
| 5928 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 5929 | uint32_t timestamp = now_ms; |
| 5930 | |
| 5931 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5932 | |
| 5933 | /* We copy the odcid into the token, prefixed by its one byte |
| 5934 | * length, the format token byte. It is followed by an AEAD TAG, and finally |
| 5935 | * the random bytes used to derive the secret to encrypt the token. |
| 5936 | */ |
| 5937 | if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len) |
| 5938 | goto err; |
| 5939 | |
| 5940 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
| 5941 | /* TODO: RAND_bytes() should be replaced */ |
| 5942 | if (RAND_bytes(salt, sizeof salt) != 1) { |
| 5943 | TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT); |
| 5944 | goto err; |
| 5945 | } |
| 5946 | |
| 5947 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 5948 | salt, sizeof salt, sec, seclen)) { |
| 5949 | TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT); |
| 5950 | goto err; |
| 5951 | } |
| 5952 | |
| 5953 | if (!quic_tls_tx_ctx_init(&ctx, aead, key)) { |
| 5954 | TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT); |
| 5955 | goto err; |
| 5956 | } |
| 5957 | |
| 5958 | /* Token build */ |
| 5959 | p = buf; |
| 5960 | *p++ = QUIC_TOKEN_FMT_RETRY, |
| 5961 | *p++ = odcid->len; |
| 5962 | memcpy(p, odcid->data, odcid->len); |
| 5963 | p += odcid->len; |
| 5964 | write_u32(p, htonl(timestamp)); |
| 5965 | p += sizeof timestamp; |
| 5966 | |
| 5967 | /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 5968 | if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) { |
| 5969 | TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT); |
| 5970 | goto err; |
| 5971 | } |
| 5972 | |
| 5973 | p += QUIC_TLS_TAG_LEN; |
| 5974 | memcpy(p, salt, sizeof salt); |
| 5975 | p += sizeof salt; |
| 5976 | EVP_CIPHER_CTX_free(ctx); |
| 5977 | |
| 5978 | ret = p - buf; |
| 5979 | leave: |
| 5980 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5981 | return ret; |
| 5982 | |
| 5983 | err: |
| 5984 | if (ctx) |
| 5985 | EVP_CIPHER_CTX_free(ctx); |
| 5986 | goto leave; |
| 5987 | } |
| 5988 | |
| 5989 | /* QUIC server only function. |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5990 | * |
| 5991 | * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is |
| 5992 | * the UDP datagram containing <pkt> and <l> is the listener instance on which |
| 5993 | * it was received. If the token is valid, the ODCID of <qc> QUIC connection |
| 5994 | * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed |
| 5995 | * to validate the token but it can be NULL : in this case the version will be |
| 5996 | * retrieved from the packet. |
| 5997 | * |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5998 | * Return 1 if succeeded, 0 if not. |
| 5999 | */ |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6000 | |
| 6001 | static int quic_retry_token_check(struct quic_rx_packet *pkt, |
| 6002 | struct quic_dgram *dgram, |
| 6003 | struct listener *l, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6004 | struct quic_conn *qc, |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6005 | struct quic_cid *odcid) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6006 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6007 | struct proxy *prx; |
| 6008 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6009 | int ret = 0; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6010 | unsigned char *token = pkt->token; |
| 6011 | const uint64_t tokenlen = pkt->token_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6012 | unsigned char buf[128]; |
| 6013 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
Amaury Denoyelle | 6c94056 | 2022-10-18 11:05:02 +0200 | [diff] [blame] | 6014 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6015 | size_t aadlen; |
| 6016 | const unsigned char *salt; |
| 6017 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 6018 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 6019 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 6020 | size_t seclen = strlen(global.cluster_secret); |
| 6021 | EVP_CIPHER_CTX *ctx = NULL; |
| 6022 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6023 | const struct quic_version *qv = qc ? qc->original_version : |
| 6024 | pkt->version; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6025 | |
| 6026 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6027 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6028 | /* The caller must ensure this. */ |
| 6029 | BUG_ON(!global.cluster_secret || !pkt->token_len); |
| 6030 | |
| 6031 | prx = l->bind_conf->frontend; |
| 6032 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6033 | |
| 6034 | if (*pkt->token != QUIC_TOKEN_FMT_RETRY) { |
| 6035 | /* TODO: New token check */ |
| 6036 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 6037 | goto leave; |
| 6038 | } |
| 6039 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6040 | if (sizeof buf < tokenlen) { |
| 6041 | TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc); |
| 6042 | goto err; |
| 6043 | } |
| 6044 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6045 | 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] | 6046 | salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN; |
| 6047 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 6048 | salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) { |
| 6049 | TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc); |
| 6050 | goto err; |
| 6051 | } |
| 6052 | |
| 6053 | if (!quic_tls_rx_ctx_init(&ctx, aead, key)) { |
| 6054 | TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc); |
| 6055 | goto err; |
| 6056 | } |
| 6057 | |
| 6058 | /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 6059 | if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen, |
| 6060 | ctx, aead, key, iv)) { |
| 6061 | TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc); |
| 6062 | goto err; |
| 6063 | } |
| 6064 | |
| 6065 | if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) { |
| 6066 | TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 6067 | goto err; |
| 6068 | } |
| 6069 | |
| 6070 | EVP_CIPHER_CTX_free(ctx); |
| 6071 | |
| 6072 | ret = 1; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6073 | HA_ATOMIC_INC(&prx_counters->retry_validated); |
| 6074 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6075 | leave: |
| 6076 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6077 | return ret; |
| 6078 | |
| 6079 | err: |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6080 | HA_ATOMIC_INC(&prx_counters->retry_error); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6081 | if (ctx) |
| 6082 | EVP_CIPHER_CTX_free(ctx); |
| 6083 | goto leave; |
| 6084 | } |
| 6085 | |
| 6086 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 6087 | * the Initial <pkt> packet. |
| 6088 | * |
| 6089 | * Returns 0 on success else non-zero. |
| 6090 | */ |
| 6091 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 6092 | struct quic_rx_packet *pkt, const struct quic_version *qv) |
| 6093 | { |
| 6094 | int ret = 0; |
| 6095 | unsigned char buf[128]; |
| 6096 | int i = 0, token_len; |
| 6097 | const socklen_t addrlen = get_addr_len(addr); |
| 6098 | struct quic_cid scid; |
| 6099 | |
| 6100 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 6101 | |
| 6102 | /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */ |
| 6103 | buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) | |
| 6104 | (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT); |
| 6105 | /* version */ |
| 6106 | buf[i++] = *((unsigned char *)&qv->num + 3); |
| 6107 | buf[i++] = *((unsigned char *)&qv->num + 2); |
| 6108 | buf[i++] = *((unsigned char *)&qv->num + 1); |
| 6109 | buf[i++] = *(unsigned char *)&qv->num; |
| 6110 | |
| 6111 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 6112 | buf[i++] = pkt->scid.len; |
| 6113 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 6114 | i += pkt->scid.len; |
| 6115 | |
| 6116 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 6117 | scid.len = QUIC_HAP_CID_LEN; |
| 6118 | /* TODO: RAND_bytes() should be replaced */ |
| 6119 | if (RAND_bytes(scid.data, scid.len) != 1) { |
| 6120 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT); |
| 6121 | goto out; |
| 6122 | } |
| 6123 | |
| 6124 | buf[i++] = scid.len; |
| 6125 | memcpy(&buf[i], scid.data, scid.len); |
| 6126 | i += scid.len; |
| 6127 | |
| 6128 | /* token */ |
| 6129 | if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num, |
| 6130 | &pkt->dcid, &pkt->scid, addr))) { |
| 6131 | TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT); |
| 6132 | goto out; |
| 6133 | } |
| 6134 | |
| 6135 | i += token_len; |
| 6136 | |
| 6137 | /* token integrity tag */ |
| 6138 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 6139 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 6140 | pkt->dcid.len, buf, i, qv)) { |
| 6141 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 6142 | goto out; |
| 6143 | } |
| 6144 | |
| 6145 | i += QUIC_TLS_TAG_LEN; |
| 6146 | |
| 6147 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) { |
| 6148 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 6149 | goto out; |
| 6150 | } |
| 6151 | |
| 6152 | ret = 1; |
| 6153 | out: |
| 6154 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 6155 | return !ret; |
| 6156 | } |
| 6157 | |
| 6158 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 6159 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 6160 | * concatenated to the <pkt> DCID field. |
| 6161 | * |
| 6162 | * Returns the instance or NULL if not found. |
| 6163 | */ |
| 6164 | static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt, |
| 6165 | struct listener *l, |
| 6166 | struct sockaddr_storage *saddr) |
| 6167 | { |
| 6168 | struct quic_conn *qc = NULL; |
| 6169 | struct ebmb_node *node; |
| 6170 | struct quic_connection_id *id; |
| 6171 | /* set if the quic_conn is found in the second DCID tree */ |
| 6172 | |
| 6173 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 6174 | |
| 6175 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 6176 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 6177 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 6178 | /* DCIDs of first packets coming from multiple clients may have |
| 6179 | * the same values. Let's distinguish them by concatenating the |
| 6180 | * socket addresses. |
| 6181 | */ |
| 6182 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 6183 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
| 6184 | pkt->dcid.len + pkt->dcid.addrlen); |
| 6185 | if (node) { |
| 6186 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 6187 | goto end; |
| 6188 | } |
| 6189 | } |
| 6190 | |
| 6191 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 6192 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 6193 | * used. |
| 6194 | */ |
| 6195 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len); |
| 6196 | if (!node) |
| 6197 | goto end; |
| 6198 | |
| 6199 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 6200 | qc = id->qc; |
| 6201 | |
| 6202 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 6203 | * If already done, this is a noop. |
| 6204 | */ |
| 6205 | if (qc) |
| 6206 | ebmb_delete(&qc->odcid_node); |
| 6207 | |
| 6208 | end: |
| 6209 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 6210 | return qc; |
| 6211 | } |
| 6212 | |
| 6213 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 6214 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 6215 | * parameters of this session. |
| 6216 | * This is the responsibility of the caller to check the validity of all the |
| 6217 | * pointers passed as parameter to this function. |
| 6218 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 6219 | * CO_ER_SSL_NO_MEM. |
| 6220 | */ |
| 6221 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 6222 | unsigned char *params, size_t params_len) |
| 6223 | { |
| 6224 | int retry, ret = -1; |
| 6225 | |
| 6226 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 6227 | |
| 6228 | retry = 1; |
| 6229 | retry: |
| 6230 | *ssl = SSL_new(ssl_ctx); |
| 6231 | if (!*ssl) { |
| 6232 | if (!retry--) |
| 6233 | goto err; |
| 6234 | |
| 6235 | pool_gc(NULL); |
| 6236 | goto retry; |
| 6237 | } |
| 6238 | |
| 6239 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 6240 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) { |
| 6241 | SSL_free(*ssl); |
| 6242 | *ssl = NULL; |
| 6243 | if (!retry--) |
| 6244 | goto err; |
| 6245 | |
| 6246 | pool_gc(NULL); |
| 6247 | goto retry; |
| 6248 | } |
| 6249 | |
| 6250 | ret = 0; |
| 6251 | leave: |
| 6252 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 6253 | return ret; |
| 6254 | |
| 6255 | err: |
| 6256 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 6257 | goto leave; |
| 6258 | } |
| 6259 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6260 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 6261 | * used to process <qc> received packets. The allocated context is stored in |
| 6262 | * <qc.xprt_ctx>. |
| 6263 | * |
| 6264 | * Returns 0 on success else non-zero. |
| 6265 | */ |
| 6266 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 6267 | { |
| 6268 | int ret = 0; |
| 6269 | struct bind_conf *bc = qc->li->bind_conf; |
| 6270 | struct ssl_sock_ctx *ctx = NULL; |
| 6271 | |
| 6272 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 6273 | |
| 6274 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 6275 | if (!ctx) { |
| 6276 | TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT); |
| 6277 | goto err; |
| 6278 | } |
| 6279 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6280 | ctx->subs = NULL; |
| 6281 | ctx->xprt_ctx = NULL; |
| 6282 | ctx->qc = qc; |
| 6283 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6284 | if (qc_is_listener(qc)) { |
| 6285 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 6286 | qc->enc_params, qc->enc_params_len) == -1) { |
| 6287 | goto err; |
| 6288 | } |
| 6289 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6290 | /* Enabling 0-RTT */ |
| 6291 | if (bc->ssl_conf.early_data) |
| 6292 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 6293 | #endif |
| 6294 | |
| 6295 | SSL_set_accept_state(ctx->ssl); |
| 6296 | } |
| 6297 | |
| 6298 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 6299 | |
| 6300 | /* Store the allocated context in <qc>. */ |
| 6301 | qc->xprt_ctx = ctx; |
| 6302 | |
| 6303 | ret = 1; |
| 6304 | leave: |
| 6305 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 6306 | return !ret; |
| 6307 | |
| 6308 | err: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6309 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 6310 | goto leave; |
| 6311 | } |
| 6312 | |
| 6313 | /* Check that all the bytes between <buf> included and <end> address |
| 6314 | * excluded are null. This is the responsibility of the caller to |
| 6315 | * check that there is at least one byte between <buf> end <end>. |
| 6316 | * Return 1 if this all the bytes are null, 0 if not. |
| 6317 | */ |
| 6318 | static inline int quic_padding_check(const unsigned char *buf, |
| 6319 | const unsigned char *end) |
| 6320 | { |
| 6321 | while (buf < end && !*buf) |
| 6322 | buf++; |
| 6323 | |
| 6324 | return buf == end; |
| 6325 | } |
| 6326 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6327 | /* Find the associated connection to the packet <pkt> or create a new one if |
| 6328 | * this is an Initial packet. <dgram> is the datagram containing the packet and |
| 6329 | * <l> is the listener instance on which it was received. |
| 6330 | * |
| 6331 | * Returns the quic-conn instance or NULL. |
| 6332 | */ |
| 6333 | static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt, |
| 6334 | struct quic_dgram *dgram, |
| 6335 | struct listener *l) |
| 6336 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6337 | struct quic_cid token_odcid = { .len = 0 }; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6338 | struct quic_conn *qc = NULL; |
| 6339 | struct proxy *prx; |
| 6340 | struct quic_counters *prx_counters; |
| 6341 | |
| 6342 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 6343 | |
| 6344 | prx = l->bind_conf->frontend; |
| 6345 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6346 | |
| 6347 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
| 6348 | |
| 6349 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 6350 | BUG_ON(!pkt->version); /* This must not happen. */ |
| 6351 | |
| 6352 | if (global.cluster_secret && pkt->token_len) { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6353 | if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid)) |
| 6354 | goto err; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6355 | } |
| 6356 | |
| 6357 | if (!qc) { |
| 6358 | int ipv4; |
| 6359 | |
| 6360 | if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) && |
| 6361 | HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) { |
| 6362 | TRACE_PROTO("Initial without token, sending retry", |
| 6363 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6364 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) { |
| 6365 | TRACE_ERROR("Error during Retry generation", |
| 6366 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6367 | goto out; |
| 6368 | } |
| 6369 | |
| 6370 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 6371 | goto out; |
| 6372 | } |
| 6373 | |
| 6374 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 6375 | * When an Initial packet is sent by a client that has not previously |
| 6376 | * received an Initial or Retry packet from the server, the client |
| 6377 | * populates the Destination Connection ID field with an unpredictable |
| 6378 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 6379 | */ |
| 6380 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 6381 | TRACE_PROTO("dropped packet", |
| 6382 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6383 | goto err; |
| 6384 | } |
| 6385 | |
| 6386 | pkt->saddr = dgram->saddr; |
| 6387 | ipv4 = dgram->saddr.ss_family == AF_INET; |
| 6388 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6389 | qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid, |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6390 | &dgram->daddr, &pkt->saddr, 1, |
| 6391 | !!pkt->token_len, l); |
| 6392 | if (qc == NULL) |
| 6393 | goto err; |
| 6394 | |
| 6395 | HA_ATOMIC_INC(&prx_counters->half_open_conn); |
| 6396 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
| 6397 | ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
| 6398 | qc->odcid.len + qc->odcid.addrlen); |
| 6399 | } |
| 6400 | } |
| 6401 | else if (!qc) { |
| 6402 | TRACE_PROTO("No connection on a non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6403 | if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt)) |
| 6404 | TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc); |
| 6405 | goto err; |
| 6406 | } |
| 6407 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6408 | out: |
| 6409 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6410 | return qc; |
| 6411 | |
| 6412 | err: |
| 6413 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
| 6414 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 6415 | return NULL; |
| 6416 | } |
| 6417 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6418 | /* Parse a QUIC packet starting at <buf>. Data won't be read after <end> even |
| 6419 | * if the packet is incomplete. This function will populate fields of <pkt> |
| 6420 | * instance, most notably its length. <dgram> is the UDP datagram which |
| 6421 | * contains the parsed packet. <l> is the listener instance on which it was |
| 6422 | * received. |
| 6423 | * |
| 6424 | * Returns 0 on success else non-zero. Packet length is guaranteed to be set to |
| 6425 | * the real packet value or to cover all data between <buf> and <end> : this is |
| 6426 | * useful to reject a whole datagram. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6427 | */ |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6428 | static int quic_rx_pkt_parse(struct quic_rx_packet *pkt, |
| 6429 | unsigned char *buf, const unsigned char *end, |
| 6430 | struct quic_dgram *dgram, struct listener *l) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6431 | { |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6432 | const unsigned char *beg = buf; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6433 | struct proxy *prx; |
| 6434 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6435 | int long_header = 0; |
Willy Tarreau | 33a6870 | 2022-11-24 09:16:41 +0100 | [diff] [blame] | 6436 | uint32_t version = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6437 | const struct quic_version *qv = NULL; |
| 6438 | |
| 6439 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 6440 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6441 | prx = l->bind_conf->frontend; |
| 6442 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6443 | /* This ist only to please to traces and distinguish the |
| 6444 | * packet with parsed packet number from others. |
| 6445 | */ |
| 6446 | pkt->pn_node.key = (uint64_t)-1; |
| 6447 | if (end <= buf) { |
| 6448 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6449 | goto drop; |
| 6450 | } |
| 6451 | |
| 6452 | /* Fixed bit */ |
| 6453 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6454 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
| 6455 | quic_padding_check(buf, end)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6456 | /* Some browsers may pad the remaining datagram space with null bytes. |
| 6457 | * That is what we called add padding out of QUIC packets. Such |
| 6458 | * datagrams must be considered as valid. But we can only consume |
| 6459 | * the remaining space. |
| 6460 | */ |
| 6461 | pkt->len = end - buf; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6462 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6463 | } |
| 6464 | |
| 6465 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6466 | goto drop; |
| 6467 | } |
| 6468 | |
| 6469 | /* Header form */ |
| 6470 | if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) { |
| 6471 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6472 | goto drop; |
| 6473 | } |
| 6474 | |
| 6475 | if (long_header) { |
| 6476 | uint64_t len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6477 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6478 | TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6479 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
| 6480 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6481 | goto drop; |
| 6482 | } |
| 6483 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6484 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6485 | * they must have the same DCID. |
| 6486 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6487 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6488 | (pkt->dcid.len != dgram->dcid_len || |
| 6489 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6490 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6491 | goto drop; |
| 6492 | } |
| 6493 | |
| 6494 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 6495 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) { |
| 6496 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6497 | goto drop; |
| 6498 | } |
| 6499 | |
| 6500 | /* RFC9000 6. Version Negotiation */ |
| 6501 | qv = qc_supported_version(version); |
| 6502 | if (!qv) { |
| 6503 | /* unsupported version, send Negotiation packet */ |
| 6504 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
| 6505 | TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6506 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6507 | } |
| 6508 | |
| 6509 | TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6510 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6511 | } |
Amaury Denoyelle | 0eae572 | 2022-10-17 18:05:18 +0200 | [diff] [blame] | 6512 | pkt->version = qv; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6513 | |
| 6514 | /* For Initial packets, and for servers (QUIC clients connections), |
| 6515 | * there is no Initial connection IDs storage. |
| 6516 | */ |
| 6517 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 6518 | uint64_t token_len; |
| 6519 | |
| 6520 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 6521 | end - buf < token_len) { |
| 6522 | TRACE_PROTO("Packet dropped", |
| 6523 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6524 | goto drop; |
| 6525 | } |
| 6526 | |
| 6527 | /* TODO Retry should be automatically activated if |
| 6528 | * suspect network usage is detected. |
| 6529 | */ |
| 6530 | if (global.cluster_secret && !token_len) { |
| 6531 | if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) { |
| 6532 | TRACE_PROTO("Initial without token, sending retry", |
Amaury Denoyelle | 90121b3 | 2022-09-27 10:35:29 +0200 | [diff] [blame] | 6533 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6534 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) { |
| 6535 | TRACE_PROTO("Error during Retry generation", |
Amaury Denoyelle | 90121b3 | 2022-09-27 10:35:29 +0200 | [diff] [blame] | 6536 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6537 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6538 | } |
| 6539 | |
| 6540 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6541 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6542 | } |
| 6543 | } |
| 6544 | else if (!global.cluster_secret && token_len) { |
| 6545 | /* Impossible case: a token was received without configured |
| 6546 | * cluster secret. |
| 6547 | */ |
| 6548 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, |
| 6549 | NULL, NULL, NULL, qv); |
| 6550 | goto drop; |
| 6551 | } |
| 6552 | |
| 6553 | pkt->token = buf; |
| 6554 | pkt->token_len = token_len; |
| 6555 | buf += pkt->token_len; |
| 6556 | } |
| 6557 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
| 6558 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
| 6559 | TRACE_PROTO("Packet dropped", |
| 6560 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6561 | goto drop; |
| 6562 | } |
| 6563 | } |
| 6564 | |
| 6565 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 6566 | end - buf < len) { |
| 6567 | TRACE_PROTO("Packet dropped", |
| 6568 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6569 | goto drop; |
| 6570 | } |
| 6571 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6572 | /* Packet Number is stored here. Packet Length totalizes the |
| 6573 | * rest of the content. |
| 6574 | */ |
| 6575 | pkt->pn_offset = buf - beg; |
| 6576 | pkt->len = pkt->pn_offset + len; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6577 | |
Frédéric Lécaille | 35218c6 | 2023-02-16 11:40:11 +0100 | [diff] [blame] | 6578 | /* RFC 9000. Initial Datagram Size |
| 6579 | * |
| 6580 | * A server MUST discard an Initial packet that is carried in a UDP datagram |
| 6581 | * with a payload that is smaller than the smallest allowed maximum datagram |
| 6582 | * size of 1200 bytes. |
| 6583 | */ |
| 6584 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 6585 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
| 6586 | TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT); |
| 6587 | HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram); |
| 6588 | goto drop; |
| 6589 | } |
| 6590 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6591 | /* Interrupt parsing after packet length retrieval : this |
| 6592 | * ensures that only the packet is dropped but not the whole |
| 6593 | * datagram. |
| 6594 | */ |
| 6595 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
| 6596 | TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT); |
| 6597 | goto drop; |
| 6598 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6599 | } |
| 6600 | else { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6601 | TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6602 | if (end - buf < QUIC_HAP_CID_LEN) { |
| 6603 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6604 | goto drop; |
| 6605 | } |
| 6606 | |
| 6607 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
| 6608 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
| 6609 | |
| 6610 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6611 | * they must have the same DCID. |
| 6612 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6613 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6614 | (pkt->dcid.len != dgram->dcid_len || |
| 6615 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6616 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6617 | goto drop; |
| 6618 | } |
| 6619 | |
| 6620 | buf += QUIC_HAP_CID_LEN; |
| 6621 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6622 | pkt->pn_offset = buf - beg; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6623 | /* A short packet is the last one of a UDP datagram. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6624 | pkt->len = end - beg; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6625 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6626 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6627 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv); |
| 6628 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6629 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6630 | drop: |
| 6631 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6632 | drop_silent: |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6633 | if (!pkt->len) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6634 | pkt->len = end - beg; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6635 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv); |
| 6636 | return -1; |
| 6637 | } |
| 6638 | |
| 6639 | /* Check if received packet <pkt> should be drop due to <qc> already in closing |
| 6640 | * state. This can be true if a CONNECTION_CLOSE has already been emitted for |
| 6641 | * this connection. |
| 6642 | * |
| 6643 | * Returns false if connection is not in closing state else true. The caller |
| 6644 | * should drop the whole datagram in the last case to not mess up <qc> |
| 6645 | * CONNECTION_CLOSE rate limit counter. |
| 6646 | */ |
| 6647 | static int qc_rx_check_closing(struct quic_conn *qc, |
| 6648 | struct quic_rx_packet *pkt) |
| 6649 | { |
| 6650 | if (!(qc->flags & QUIC_FL_CONN_CLOSING)) |
| 6651 | return 0; |
| 6652 | |
| 6653 | TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 6654 | |
| 6655 | /* Check if CONNECTION_CLOSE rate reemission is reached. */ |
| 6656 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 6657 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 6658 | qc->nb_pkt_for_cc++; |
| 6659 | qc->nb_pkt_since_cc = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6660 | } |
| 6661 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6662 | return 1; |
| 6663 | } |
| 6664 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6665 | /* React to a connection migration initiated on <qc> by a client with the new |
| 6666 | * path addresses <peer_addr>/<local_addr>. |
| 6667 | * |
| 6668 | * Returns 0 on success else non-zero. |
| 6669 | */ |
| 6670 | static int qc_handle_conn_migration(struct quic_conn *qc, |
| 6671 | const struct sockaddr_storage *peer_addr, |
| 6672 | const struct sockaddr_storage *local_addr) |
| 6673 | { |
| 6674 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6675 | |
Frédéric Lécaille | 6fc8697 | 2023-01-12 08:29:23 +0100 | [diff] [blame] | 6676 | /* RFC 9000. Connection Migration |
| 6677 | * |
| 6678 | * If the peer sent the disable_active_migration transport parameter, |
| 6679 | * an endpoint also MUST NOT send packets (including probing packets; |
| 6680 | * see Section 9.1) from a different local address to the address the peer |
| 6681 | * used during the handshake, unless the endpoint has acted on a |
| 6682 | * preferred_address transport parameter from the peer. |
| 6683 | */ |
| 6684 | if (qc->li->bind_conf->quic_params.disable_active_migration) { |
| 6685 | TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc); |
| 6686 | goto err; |
| 6687 | } |
| 6688 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6689 | /* RFC 9000 9. Connection Migration |
| 6690 | * |
Amaury Denoyelle | eb6be98 | 2022-11-21 11:14:45 +0100 | [diff] [blame] | 6691 | * The design of QUIC relies on endpoints retaining a stable address for |
| 6692 | * the duration of the handshake. An endpoint MUST NOT initiate |
| 6693 | * connection migration before the handshake is confirmed, as defined in |
| 6694 | * Section 4.1.2 of [QUIC-TLS]. |
| 6695 | */ |
| 6696 | if (qc->state < QUIC_HS_ST_COMPLETE) { |
| 6697 | TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc); |
| 6698 | goto err; |
| 6699 | } |
| 6700 | |
| 6701 | /* RFC 9000 9. Connection Migration |
| 6702 | * |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6703 | * TODO |
| 6704 | * An endpoint MUST |
| 6705 | * perform path validation (Section 8.2) if it detects any change to a |
| 6706 | * peer's address, unless it has previously validated that address. |
| 6707 | */ |
| 6708 | |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 6709 | /* Update quic-conn owned socket if in used. |
| 6710 | * TODO try to reuse it instead of closing and opening a new one. |
| 6711 | */ |
| 6712 | if (qc_test_fd(qc)) { |
| 6713 | /* TODO try to reuse socket instead of closing it and opening a new one. */ |
| 6714 | TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc); |
| 6715 | qc_release_fd(qc, 1); |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 6716 | /* TODO need to adjust <jobs> on socket allocation failure. */ |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 6717 | qc_alloc_fd(qc, local_addr, peer_addr); |
| 6718 | } |
| 6719 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6720 | qc->local_addr = *local_addr; |
| 6721 | qc->peer_addr = *peer_addr; |
| 6722 | HA_ATOMIC_INC(&qc->prx_counters->conn_migration_done); |
| 6723 | |
| 6724 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6725 | return 0; |
| 6726 | |
| 6727 | err: |
| 6728 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6729 | return 1; |
| 6730 | } |
| 6731 | |
Frédéric Lécaille | 1dbeb35 | 2023-02-07 11:40:21 +0100 | [diff] [blame] | 6732 | /* Release the memory for the RX packets which are no more referenced |
| 6733 | * and consume their payloads which have been copied to the RX buffer |
| 6734 | * for the connection. |
| 6735 | * Always succeeds. |
| 6736 | */ |
| 6737 | static inline void quic_rx_pkts_del(struct quic_conn *qc) |
| 6738 | { |
| 6739 | struct quic_rx_packet *pkt, *pktback; |
| 6740 | |
| 6741 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 6742 | 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] | 6743 | "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] | 6744 | (long long)pkt->pn_node.key, |
Frédéric Lécaille | b7a13be | 2023-02-22 17:24:23 +0100 | [diff] [blame] | 6745 | 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] | 6746 | (unsigned char *)b_head(&qc->rx.buf) - pkt->data); |
| 6747 | if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) { |
| 6748 | size_t cdata; |
| 6749 | |
| 6750 | cdata = b_contig_data(&qc->rx.buf, 0); |
| 6751 | 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] | 6752 | "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] | 6753 | if (cdata && !*b_head(&qc->rx.buf)) { |
| 6754 | /* Consume the remaining data */ |
| 6755 | b_del(&qc->rx.buf, cdata); |
| 6756 | } |
| 6757 | break; |
| 6758 | } |
| 6759 | |
| 6760 | if (pkt->refcnt) |
| 6761 | break; |
| 6762 | |
| 6763 | b_del(&qc->rx.buf, pkt->raw_len); |
| 6764 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 6765 | pool_free(pool_head_quic_rx_packet, pkt); |
| 6766 | } |
| 6767 | |
| 6768 | /* In frequent cases the buffer will be emptied at this stage. */ |
| 6769 | b_realign_if_empty(&qc->rx.buf); |
| 6770 | } |
| 6771 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6772 | /* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied |
| 6773 | * into <qc> receive buffer after header protection removal procedure. |
| 6774 | * |
| 6775 | * <dgram> must be set to the datagram which contains the QUIC packet. <beg> |
| 6776 | * must point to packet buffer first byte. |
| 6777 | * |
| 6778 | * <tasklist_head> may be non-NULL when the caller treat several datagrams for |
| 6779 | * different quic-conn. In this case, each quic-conn tasklet will be appended |
| 6780 | * to it in order to be woken up after the current task. |
| 6781 | * |
| 6782 | * The caller can safely removed the packet data. If packet refcount was not |
| 6783 | * incremented by this function, it means that the connection did not handled |
| 6784 | * it and it should be freed by the caller. |
| 6785 | */ |
| 6786 | static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt, |
| 6787 | struct quic_dgram *dgram, unsigned char *beg, |
| 6788 | struct list **tasklist_head) |
| 6789 | { |
| 6790 | const struct quic_version *qv = pkt->version; |
| 6791 | struct quic_enc_level *qel = NULL; |
| 6792 | size_t b_cspace; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6793 | |
Amaury Denoyelle | 3f474e6 | 2022-11-24 17:15:08 +0100 | [diff] [blame] | 6794 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 6795 | |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6796 | if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6797 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
| 6798 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 6799 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | a65b71f | 2023-03-03 10:16:32 +0100 | [diff] [blame^] | 6800 | TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached", |
| 6801 | QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6802 | /* Reset the anti-amplification bit. It will be set again |
| 6803 | * when sending the next packet if reached again. |
| 6804 | */ |
| 6805 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | a65b71f | 2023-03-03 10:16:32 +0100 | [diff] [blame^] | 6806 | qc_set_timer(qc); |
| 6807 | if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 6808 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6809 | } |
| 6810 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6811 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 6812 | TRACE_PROTO("Connection error", |
| 6813 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6814 | goto out; |
| 6815 | } |
| 6816 | |
| 6817 | pkt->raw_len = pkt->len; |
| 6818 | quic_rx_pkts_del(qc); |
| 6819 | b_cspace = b_contig_space(&qc->rx.buf); |
| 6820 | if (b_cspace < pkt->len) { |
Frédéric Lécaille | 1dbeb35 | 2023-02-07 11:40:21 +0100 | [diff] [blame] | 6821 | 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] | 6822 | "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6823 | /* Do not consume buf if space not at the end. */ |
| 6824 | if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) { |
| 6825 | TRACE_PROTO("Packet dropped", |
| 6826 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6827 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6828 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6829 | } |
| 6830 | |
| 6831 | /* Let us consume the remaining contiguous space. */ |
| 6832 | if (b_cspace) { |
| 6833 | b_putchr(&qc->rx.buf, 0x00); |
| 6834 | b_cspace--; |
| 6835 | } |
| 6836 | b_add(&qc->rx.buf, b_cspace); |
| 6837 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 6838 | TRACE_PROTO("Too big packet", |
| 6839 | QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6840 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6841 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6842 | } |
| 6843 | } |
| 6844 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6845 | if (!qc_try_rm_hp(qc, pkt, beg, &qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6846 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6847 | goto drop; |
| 6848 | } |
| 6849 | |
| 6850 | TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 6851 | if (pkt->aad_len) |
| 6852 | qc_pkt_insert(qc, pkt, qel); |
| 6853 | out: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 6854 | *tasklist_head = tasklet_wakeup_after(*tasklist_head, |
| 6855 | qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6856 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6857 | drop_silent: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6858 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6859 | return; |
| 6860 | |
| 6861 | drop: |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6862 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6863 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
| 6864 | } |
| 6865 | |
| 6866 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 6867 | * Return 1 if enough room to build this header, 0 if not. |
| 6868 | */ |
| 6869 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 6870 | int type, size_t pn_len, |
| 6871 | struct quic_conn *qc, const struct quic_version *ver) |
| 6872 | { |
| 6873 | int ret = 0; |
| 6874 | |
| 6875 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6876 | |
| 6877 | if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) { |
| 6878 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6879 | goto leave; |
| 6880 | } |
| 6881 | |
| 6882 | type = quic_pkt_type(type, ver->num); |
| 6883 | /* #0 byte flags */ |
| 6884 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 6885 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 6886 | /* Version */ |
| 6887 | quic_write_uint32(buf, end, ver->num); |
| 6888 | *(*buf)++ = qc->dcid.len; |
| 6889 | /* Destination connection ID */ |
| 6890 | if (qc->dcid.len) { |
| 6891 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6892 | *buf += qc->dcid.len; |
| 6893 | } |
| 6894 | /* Source connection ID */ |
| 6895 | *(*buf)++ = qc->scid.len; |
| 6896 | if (qc->scid.len) { |
| 6897 | memcpy(*buf, qc->scid.data, qc->scid.len); |
| 6898 | *buf += qc->scid.len; |
| 6899 | } |
| 6900 | |
| 6901 | ret = 1; |
| 6902 | leave: |
| 6903 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6904 | return ret; |
| 6905 | } |
| 6906 | |
| 6907 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 6908 | * Return 1 if enough room to build this header, 0 if not. |
| 6909 | */ |
| 6910 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 6911 | size_t pn_len, struct quic_conn *qc, |
| 6912 | unsigned char tls_flags) |
| 6913 | { |
| 6914 | int ret = 0; |
| 6915 | |
| 6916 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6917 | |
| 6918 | if (end - *buf < 1 + qc->dcid.len) { |
| 6919 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6920 | goto leave; |
| 6921 | } |
| 6922 | |
| 6923 | /* #0 byte flags */ |
| 6924 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 6925 | ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1); |
| 6926 | /* Destination connection ID */ |
| 6927 | if (qc->dcid.len) { |
| 6928 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6929 | *buf += qc->dcid.len; |
| 6930 | } |
| 6931 | |
| 6932 | ret = 1; |
| 6933 | leave: |
| 6934 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6935 | return ret; |
| 6936 | } |
| 6937 | |
| 6938 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 6939 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 6940 | * with <aead> as AEAD cipher and <key> as secret key. |
| 6941 | * Returns 1 if succeeded or 0 if failed. |
| 6942 | */ |
| 6943 | static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf, |
| 6944 | unsigned char *pn, size_t pnlen, |
| 6945 | struct quic_tls_ctx *tls_ctx) |
| 6946 | |
| 6947 | { |
| 6948 | int i, ret = 0; |
| 6949 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 6950 | * and at most 4 bytes for the packet number |
| 6951 | */ |
| 6952 | unsigned char mask[5] = {0}; |
| 6953 | EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx; |
| 6954 | |
| 6955 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6956 | |
| 6957 | if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) { |
| 6958 | TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc); |
| 6959 | goto out; |
| 6960 | } |
| 6961 | |
| 6962 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 6963 | for (i = 0; i < pnlen; i++) |
| 6964 | pn[i] ^= mask[i + 1]; |
| 6965 | |
| 6966 | ret = 1; |
| 6967 | out: |
| 6968 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6969 | return ret; |
| 6970 | } |
| 6971 | |
| 6972 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 6973 | * ACK ranges if needed to a value below <limit> in bytes. |
| 6974 | * Return 1 if succeeded, 0 if not. |
| 6975 | */ |
| 6976 | static int quic_ack_frm_reduce_sz(struct quic_conn *qc, |
| 6977 | struct quic_frame *ack_frm, size_t limit) |
| 6978 | { |
| 6979 | size_t room, ack_delay_sz; |
| 6980 | int ret = 0; |
| 6981 | |
| 6982 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6983 | |
| 6984 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 6985 | /* A frame is made of 1 byte for the frame type. */ |
| 6986 | room = limit - ack_delay_sz - 1; |
| 6987 | if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room)) |
| 6988 | goto leave; |
| 6989 | |
| 6990 | ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz; |
| 6991 | leave: |
| 6992 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6993 | return ret; |
| 6994 | } |
| 6995 | |
| 6996 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 6997 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 6998 | * with <room> as available room, and <*len> the packet Length field initialized |
| 6999 | * with the number of bytes already present in this buffer which must be taken |
| 7000 | * into an account for the Length packet field value. <headlen> is the number of |
| 7001 | * bytes already present in this packet before building frames. |
| 7002 | * |
| 7003 | * Update consequently <*len> to reflect the size of these frames built |
| 7004 | * by this function. Also attach these frames to <l> frame list. |
| 7005 | * Return 1 if at least one ack-eleciting frame could be built, 0 if not. |
| 7006 | */ |
| 7007 | static inline int qc_build_frms(struct list *outlist, struct list *inlist, |
| 7008 | size_t room, size_t *len, size_t headlen, |
| 7009 | struct quic_enc_level *qel, |
| 7010 | struct quic_conn *qc) |
| 7011 | { |
| 7012 | int ret; |
| 7013 | struct quic_frame *cf, *cfbak; |
| 7014 | |
| 7015 | TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc); |
| 7016 | |
| 7017 | ret = 0; |
| 7018 | if (*len > room) |
| 7019 | goto leave; |
| 7020 | |
| 7021 | /* If we are not probing we must take into an account the congestion |
| 7022 | * control window. |
| 7023 | */ |
| 7024 | if (!qel->pktns->tx.pto_probe) { |
| 7025 | size_t remain = quic_path_prep_data(qc->path); |
| 7026 | |
| 7027 | if (headlen > remain) |
| 7028 | goto leave; |
| 7029 | |
| 7030 | room = QUIC_MIN(room, remain - headlen); |
| 7031 | } |
| 7032 | |
| 7033 | TRACE_PROTO("************** frames build (headlen)", |
| 7034 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
| 7035 | |
| 7036 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 7037 | * returned by this function only if at least one frame could be built |
| 7038 | * in the switch/case block. |
| 7039 | */ |
| 7040 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
| 7041 | /* header length, data length, frame length. */ |
| 7042 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
| 7043 | |
| 7044 | if (!room) |
| 7045 | break; |
| 7046 | |
| 7047 | switch (cf->type) { |
| 7048 | case QUIC_FT_CRYPTO: |
| 7049 | TRACE_DEVEL(" New CRYPTO frame build (room, len)", |
| 7050 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
| 7051 | /* Compute the length of this CRYPTO frame header */ |
| 7052 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 7053 | /* Compute the data length of this CRyPTO frame. */ |
| 7054 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 7055 | TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)", |
| 7056 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen); |
| 7057 | if (!dlen) |
| 7058 | continue; |
| 7059 | |
| 7060 | /* CRYPTO frame length. */ |
| 7061 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 7062 | TRACE_DEVEL(" CRYPTO frame length (flen)", |
| 7063 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 7064 | /* Add the CRYPTO data length and its encoded length to the packet |
| 7065 | * length and the length of this length. |
| 7066 | */ |
| 7067 | *len += flen; |
| 7068 | room -= flen; |
| 7069 | if (dlen == cf->crypto.len) { |
| 7070 | /* <cf> CRYPTO data have been consumed. */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7071 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7072 | LIST_APPEND(outlist, &cf->list); |
| 7073 | } |
| 7074 | else { |
| 7075 | struct quic_frame *new_cf; |
| 7076 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 7077 | new_cf = qc_frm_alloc(QUIC_FT_CRYPTO); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7078 | if (!new_cf) { |
| 7079 | TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
| 7080 | continue; |
| 7081 | } |
| 7082 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7083 | new_cf->crypto.len = dlen; |
| 7084 | new_cf->crypto.offset = cf->crypto.offset; |
| 7085 | new_cf->crypto.qel = qel; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 7086 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7087 | if (cf->origin) { |
| 7088 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 7089 | /* This <cf> frame was duplicated */ |
| 7090 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 7091 | new_cf->origin = cf->origin; |
Frédéric Lécaille | dd41946 | 2023-01-26 15:07:39 +0100 | [diff] [blame] | 7092 | /* Detach the remaining CRYPTO frame from its original frame */ |
| 7093 | LIST_DEL_INIT(&cf->ref); |
| 7094 | cf->origin = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7095 | } |
| 7096 | LIST_APPEND(outlist, &new_cf->list); |
| 7097 | /* Consume <dlen> bytes of the current frame. */ |
| 7098 | cf->crypto.len -= dlen; |
| 7099 | cf->crypto.offset += dlen; |
| 7100 | } |
| 7101 | break; |
| 7102 | |
| 7103 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Amaury Denoyelle | c8a0efb | 2023-02-22 10:44:27 +0100 | [diff] [blame] | 7104 | if (cf->stream.dup) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7105 | struct eb64_node *node = NULL; |
| 7106 | struct qc_stream_desc *stream_desc = NULL; |
| 7107 | struct quic_stream *strm = &cf->stream; |
| 7108 | |
| 7109 | /* As this frame has been already lost, ensure the stream is always |
| 7110 | * available or the range of this frame is not consumed before |
| 7111 | * resending it. |
| 7112 | */ |
| 7113 | node = eb64_lookup(&qc->streams_by_id, strm->id); |
| 7114 | if (!node) { |
| 7115 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7116 | qc_frm_free(&cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7117 | continue; |
| 7118 | } |
| 7119 | |
| 7120 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 7121 | if (strm->offset.key + strm->len <= stream_desc->ack_offset) { |
| 7122 | TRACE_DEVEL("ignored frame frame in already acked range", |
| 7123 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7124 | qc_frm_free(&cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7125 | continue; |
| 7126 | } |
| 7127 | else if (strm->offset.key < stream_desc->ack_offset) { |
| 7128 | strm->offset.key = stream_desc->ack_offset; |
| 7129 | TRACE_DEVEL("updated partially acked frame", |
| 7130 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
| 7131 | } |
| 7132 | } |
| 7133 | /* Note that these frames are accepted in short packets only without |
| 7134 | * "Length" packet field. Here, <*len> is used only to compute the |
| 7135 | * sum of the lengths of the already built frames for this packet. |
| 7136 | * |
| 7137 | * Compute the length of this STREAM frame "header" made a all the field |
| 7138 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 7139 | */ |
| 7140 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
| 7141 | ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0); |
| 7142 | /* Compute the data length of this STREAM frame. */ |
| 7143 | avail_room = room - hlen - *len; |
| 7144 | if ((ssize_t)avail_room <= 0) |
| 7145 | continue; |
| 7146 | |
| 7147 | TRACE_DEVEL(" New STREAM frame build (room, len)", |
| 7148 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 7149 | |
| 7150 | /* hlen contains STREAM id and offset. Ensure there is |
| 7151 | * enough room for length field. |
| 7152 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7153 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 7154 | dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz), |
| 7155 | cf->stream.len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7156 | dlen_sz = quic_int_getsize(dlen); |
| 7157 | flen = hlen + dlen_sz + dlen; |
| 7158 | } |
| 7159 | else { |
| 7160 | dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len); |
| 7161 | flen = hlen + dlen; |
| 7162 | } |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 7163 | |
| 7164 | if (cf->stream.len && !dlen) { |
| 7165 | /* Only a small gap is left on buffer, not |
| 7166 | * enough to encode the STREAM data length. |
| 7167 | */ |
| 7168 | continue; |
| 7169 | } |
| 7170 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7171 | TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)", |
| 7172 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen); |
| 7173 | TRACE_DEVEL(" STREAM frame length (flen)", |
| 7174 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 7175 | /* Add the STREAM data length and its encoded length to the packet |
| 7176 | * length and the length of this length. |
| 7177 | */ |
| 7178 | *len += flen; |
| 7179 | room -= flen; |
| 7180 | if (dlen == cf->stream.len) { |
| 7181 | /* <cf> STREAM data have been consumed. */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7182 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7183 | LIST_APPEND(outlist, &cf->list); |
| 7184 | |
| 7185 | /* Do not notify MUX on retransmission. */ |
| 7186 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 7187 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 7188 | cf->stream.len, |
| 7189 | cf->stream.offset.key); |
| 7190 | } |
| 7191 | } |
| 7192 | else { |
| 7193 | struct quic_frame *new_cf; |
| 7194 | struct buffer cf_buf; |
| 7195 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 7196 | new_cf = qc_frm_alloc(cf->type); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7197 | if (!new_cf) { |
| 7198 | TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
| 7199 | continue; |
| 7200 | } |
| 7201 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7202 | new_cf->stream.stream = cf->stream.stream; |
| 7203 | new_cf->stream.buf = cf->stream.buf; |
| 7204 | new_cf->stream.id = cf->stream.id; |
Amaury Denoyelle | 1dac018 | 2023-02-02 16:45:07 +0100 | [diff] [blame] | 7205 | new_cf->stream.offset = cf->stream.offset; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7206 | new_cf->stream.len = dlen; |
| 7207 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 7208 | /* FIN bit reset */ |
| 7209 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 7210 | new_cf->stream.data = cf->stream.data; |
Amaury Denoyelle | c8a0efb | 2023-02-22 10:44:27 +0100 | [diff] [blame] | 7211 | new_cf->stream.dup = cf->stream.dup; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 7212 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7213 | if (cf->origin) { |
| 7214 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 7215 | /* This <cf> frame was duplicated */ |
| 7216 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 7217 | new_cf->origin = cf->origin; |
Frédéric Lécaille | dd41946 | 2023-01-26 15:07:39 +0100 | [diff] [blame] | 7218 | /* Detach this STREAM frame from its origin */ |
| 7219 | LIST_DEL_INIT(&cf->ref); |
| 7220 | cf->origin = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7221 | } |
| 7222 | LIST_APPEND(outlist, &new_cf->list); |
| 7223 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 7224 | /* Consume <dlen> bytes of the current frame. */ |
| 7225 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 7226 | b_size(cf->stream.buf), |
| 7227 | (char *)cf->stream.data - b_orig(cf->stream.buf), 0); |
| 7228 | cf->stream.len -= dlen; |
| 7229 | cf->stream.offset.key += dlen; |
| 7230 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
| 7231 | |
| 7232 | /* Do not notify MUX on retransmission. */ |
| 7233 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 7234 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 7235 | new_cf->stream.len, |
| 7236 | new_cf->stream.offset.key); |
| 7237 | } |
| 7238 | } |
| 7239 | |
| 7240 | /* TODO the MUX is notified about the frame sending via |
| 7241 | * previous qcc_streams_sent_done call. However, the |
| 7242 | * sending can fail later, for example if the sendto |
| 7243 | * system call returns an error. As the MUX has been |
| 7244 | * notified, the transport layer is responsible to |
| 7245 | * bufferize and resent the announced data later. |
| 7246 | */ |
| 7247 | |
| 7248 | break; |
| 7249 | |
| 7250 | default: |
| 7251 | flen = qc_frm_len(cf); |
| 7252 | BUG_ON(!flen); |
| 7253 | if (flen > room) |
| 7254 | continue; |
| 7255 | |
| 7256 | *len += flen; |
| 7257 | room -= flen; |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7258 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7259 | LIST_APPEND(outlist, &cf->list); |
| 7260 | break; |
| 7261 | } |
| 7262 | |
| 7263 | /* Successful status as soon as a frame could be built */ |
| 7264 | ret = 1; |
| 7265 | } |
| 7266 | |
| 7267 | leave: |
| 7268 | TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc); |
| 7269 | return ret; |
| 7270 | } |
| 7271 | |
| 7272 | /* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out> |
| 7273 | * is used as return parameter and should be zero'ed by the caller. |
| 7274 | */ |
| 7275 | static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel, |
| 7276 | struct quic_frame *out) |
| 7277 | { |
| 7278 | /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels |
| 7279 | * |
| 7280 | * A CONNECTION_CLOSE frame should be sent in several packets with |
| 7281 | * different encryption levels depending on the client context. This is |
| 7282 | * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for |
| 7283 | * more details on how to implement it. |
| 7284 | */ |
| 7285 | TRACE_ENTER(QUIC_EV_CONN_BFRM, qc); |
| 7286 | |
| 7287 | |
| 7288 | if (qc->err.app) { |
| 7289 | if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 7290 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 7291 | /* RFC 9000 10.2.3. Immediate Close during the Handshake |
| 7292 | * |
| 7293 | * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake |
| 7294 | * packet could expose application state or be used to alter application |
| 7295 | * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a |
| 7296 | * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or |
| 7297 | * Handshake packets. Otherwise, information about the application |
| 7298 | * state might be revealed. Endpoints MUST clear the value of the |
| 7299 | * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when |
| 7300 | * converting to a CONNECTION_CLOSE of type 0x1c. |
| 7301 | */ |
| 7302 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 7303 | out->connection_close.error_code = QC_ERR_APPLICATION_ERROR; |
| 7304 | out->connection_close.reason_phrase_len = 0; |
| 7305 | } |
| 7306 | else { |
| 7307 | out->type = QUIC_FT_CONNECTION_CLOSE_APP; |
| 7308 | out->connection_close.error_code = qc->err.code; |
| 7309 | } |
| 7310 | } |
| 7311 | else { |
| 7312 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 7313 | out->connection_close.error_code = qc->err.code; |
| 7314 | } |
| 7315 | TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc); |
| 7316 | |
| 7317 | } |
| 7318 | |
| 7319 | /* This function builds a clear packet from <pkt> information (its type) |
| 7320 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 7321 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 7322 | * filling the buffer with as much frames as possible from <frms> list of |
| 7323 | * prebuilt frames. |
| 7324 | * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are |
| 7325 | * reserved so that to ensure there is enough room to build this AEAD TAG after |
| 7326 | * having returned from this function. |
| 7327 | * This function also updates the value of <buf_pn> pointer to point to the packet |
| 7328 | * number field in this packet. <pn_len> will also have the packet number |
| 7329 | * length as value. |
| 7330 | * |
| 7331 | * Return 1 if succeeded (enough room to buile this packet), O if not. |
| 7332 | */ |
| 7333 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 7334 | size_t dglen, struct quic_tx_packet *pkt, |
| 7335 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
| 7336 | int force_ack, int padding, int cc, int probe, |
| 7337 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 7338 | const struct quic_version *ver, struct list *frms) |
| 7339 | { |
| 7340 | unsigned char *beg, *payload; |
| 7341 | size_t len, len_sz, len_frms, padding_len; |
| 7342 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 7343 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
| 7344 | struct quic_frame cc_frm = { }; |
| 7345 | size_t ack_frm_len, head_len; |
| 7346 | int64_t rx_largest_acked_pn; |
| 7347 | int add_ping_frm; |
| 7348 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 7349 | struct quic_frame *cf; |
| 7350 | int must_ack, ret = 0; |
| 7351 | int nb_aepkts_since_last_ack; |
| 7352 | |
| 7353 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 7354 | |
| 7355 | /* Length field value with CRYPTO frames if present. */ |
| 7356 | len_frms = 0; |
| 7357 | beg = pos; |
| 7358 | /* When not probing, and no immediate close is required, reduce the size of this |
| 7359 | * buffer to respect the congestion controller window. |
| 7360 | * This size will be limited if we have ack-eliciting frames to send from <frms>. |
| 7361 | */ |
| 7362 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
| 7363 | size_t path_room; |
| 7364 | |
| 7365 | path_room = quic_path_prep_data(qc->path); |
| 7366 | if (end - beg > path_room) |
| 7367 | end = beg + path_room; |
| 7368 | } |
| 7369 | |
| 7370 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 7371 | * length field if any. |
| 7372 | */ |
| 7373 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 7374 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 7375 | goto no_room; |
| 7376 | |
| 7377 | end -= QUIC_TLS_TAG_LEN; |
| 7378 | rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn; |
| 7379 | /* packet number length */ |
| 7380 | *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn); |
| 7381 | /* Build the header */ |
| 7382 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
| 7383 | !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) || |
| 7384 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
| 7385 | !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver))) |
| 7386 | goto no_room; |
| 7387 | |
| 7388 | /* Encode the token length (0) for an Initial packet. */ |
| 7389 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
| 7390 | *pos++ = 0; |
| 7391 | head_len = pos - beg; |
| 7392 | /* Build an ACK frame if required. */ |
| 7393 | ack_frm_len = 0; |
| 7394 | nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack; |
| 7395 | must_ack = !qel->pktns->tx.pto_probe && |
| 7396 | (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 7397 | (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK))); |
| 7398 | if (must_ack) { |
| 7399 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
| 7400 | BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root)); |
| 7401 | ack_frm.tx_ack.arngs = arngs; |
| 7402 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 7403 | qel->pktns->tx.ack_delay = |
| 7404 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 7405 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 7406 | } |
| 7407 | ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay; |
| 7408 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 7409 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 7410 | * that from here, we do not know if we will have to send a PING frame. |
| 7411 | * This will be decided after having computed the ack-eliciting frames |
| 7412 | * to be added to this packet. |
| 7413 | */ |
| 7414 | ack_frm_len = quic_ack_frm_reduce_sz(qc, &ack_frm, end - 1 - *pn_len - pos); |
| 7415 | if (!ack_frm_len) |
| 7416 | goto no_room; |
| 7417 | } |
| 7418 | |
| 7419 | /* Length field value without the ack-eliciting frames. */ |
| 7420 | len = ack_frm_len + *pn_len; |
| 7421 | len_frms = 0; |
| 7422 | if (!cc && !LIST_ISEMPTY(frms)) { |
| 7423 | ssize_t room = end - pos; |
| 7424 | |
| 7425 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 7426 | /* Initialize the length of the frames built below to <len>. |
| 7427 | * If any frame could be successfully built by qc_build_frms(), |
| 7428 | * we will have len_frms > len. |
| 7429 | */ |
| 7430 | len_frms = len; |
| 7431 | if (!qc_build_frms(&frm_list, frms, |
| 7432 | end - pos, &len_frms, pos - beg, qel, qc)) { |
| 7433 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
| 7434 | qc, NULL, NULL, &room); |
| 7435 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 7436 | goto no_room; |
| 7437 | } |
| 7438 | } |
| 7439 | |
| 7440 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 7441 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 7442 | * for the encryption tag. It must be taken into an account for the length |
| 7443 | * of this packet. |
| 7444 | */ |
| 7445 | if (len_frms) |
| 7446 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 7447 | else |
| 7448 | len += QUIC_TLS_TAG_LEN; |
| 7449 | /* CONNECTION_CLOSE frame */ |
| 7450 | if (cc) { |
| 7451 | qc_build_cc_frm(qc, qel, &cc_frm); |
| 7452 | len += qc_frm_len(&cc_frm); |
| 7453 | } |
| 7454 | add_ping_frm = 0; |
| 7455 | padding_len = 0; |
| 7456 | len_sz = quic_int_getsize(len); |
| 7457 | /* Add this packet size to <dglen> */ |
| 7458 | dglen += head_len + len_sz + len; |
| 7459 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 7460 | /* This is a maximum padding size */ |
| 7461 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 7462 | /* The length field value is of this packet is <len> + <padding_len> |
| 7463 | * the size of which may be greater than the initial computed size |
| 7464 | * <len_sz>. So, let's deduce the difference between these to packet |
| 7465 | * sizes from <padding_len>. |
| 7466 | */ |
| 7467 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 7468 | len += padding_len; |
| 7469 | } |
Frédéric Lécaille | 5faf577 | 2023-02-16 17:30:53 +0100 | [diff] [blame] | 7470 | else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) { |
| 7471 | len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms; |
| 7472 | } |
| 7473 | else if (LIST_ISEMPTY(&frm_list)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7474 | if (qel->pktns->tx.pto_probe) { |
| 7475 | /* If we cannot send a frame, we send a PING frame. */ |
| 7476 | add_ping_frm = 1; |
| 7477 | len += 1; |
| 7478 | } |
| 7479 | /* If there is no frame at all to follow, add at least a PADDING frame. */ |
| 7480 | if (!ack_frm_len && !cc) |
| 7481 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 7482 | } |
| 7483 | |
| 7484 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 7485 | goto no_room; |
| 7486 | |
| 7487 | /* Packet number field address. */ |
| 7488 | *buf_pn = pos; |
| 7489 | |
| 7490 | /* Packet number encoding. */ |
| 7491 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 7492 | goto no_room; |
| 7493 | |
| 7494 | /* payload building (ack-eliciting or not frames) */ |
| 7495 | payload = pos; |
| 7496 | if (ack_frm_len) { |
| 7497 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 7498 | goto no_room; |
| 7499 | |
| 7500 | pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns); |
| 7501 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
| 7502 | } |
| 7503 | |
| 7504 | /* Ack-eliciting frames */ |
| 7505 | if (!LIST_ISEMPTY(&frm_list)) { |
| 7506 | struct quic_frame *tmp_cf; |
| 7507 | list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) { |
| 7508 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
| 7509 | ssize_t room = end - pos; |
| 7510 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
| 7511 | qc, NULL, NULL, &room); |
| 7512 | /* Note that <cf> was added from <frms> to <frm_list> list by |
| 7513 | * qc_build_frms(). |
| 7514 | */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7515 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7516 | LIST_INSERT(frms, &cf->list); |
| 7517 | continue; |
| 7518 | } |
| 7519 | |
| 7520 | quic_tx_packet_refinc(pkt); |
| 7521 | cf->pkt = pkt; |
| 7522 | } |
| 7523 | } |
| 7524 | |
| 7525 | /* Build a PING frame if needed. */ |
| 7526 | if (add_ping_frm) { |
| 7527 | frm.type = QUIC_FT_PING; |
| 7528 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 7529 | goto no_room; |
| 7530 | } |
| 7531 | |
| 7532 | /* Build a CONNECTION_CLOSE frame if needed. */ |
| 7533 | if (cc) { |
| 7534 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 7535 | goto no_room; |
| 7536 | |
| 7537 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 7538 | } |
| 7539 | |
| 7540 | /* Build a PADDING frame if needed. */ |
| 7541 | if (padding_len) { |
| 7542 | frm.type = QUIC_FT_PADDING; |
| 7543 | frm.padding.len = padding_len; |
| 7544 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 7545 | goto no_room; |
| 7546 | } |
| 7547 | |
| 7548 | if (pos == payload) { |
| 7549 | /* No payload was built because of congestion control */ |
| 7550 | TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc); |
| 7551 | goto no_room; |
| 7552 | } |
| 7553 | |
| 7554 | /* If this packet is ack-eliciting and we are probing let's |
| 7555 | * decrement the PTO probe counter. |
| 7556 | */ |
| 7557 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 7558 | qel->pktns->tx.pto_probe) |
| 7559 | qel->pktns->tx.pto_probe--; |
| 7560 | |
| 7561 | pkt->len = pos - beg; |
| 7562 | LIST_SPLICE(&pkt->frms, &frm_list); |
| 7563 | |
| 7564 | ret = 1; |
| 7565 | TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt); |
| 7566 | leave: |
| 7567 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 7568 | return ret; |
| 7569 | |
| 7570 | no_room: |
| 7571 | /* Replace the pre-built frames which could not be add to this packet */ |
| 7572 | LIST_SPLICE(frms, &frm_list); |
| 7573 | TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 7574 | goto leave; |
| 7575 | } |
| 7576 | |
| 7577 | static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type) |
| 7578 | { |
| 7579 | pkt->type = type; |
| 7580 | pkt->len = 0; |
| 7581 | pkt->in_flight_len = 0; |
| 7582 | pkt->pn_node.key = (uint64_t)-1; |
| 7583 | LIST_INIT(&pkt->frms); |
| 7584 | pkt->time_sent = TICK_ETERNITY; |
| 7585 | pkt->next = NULL; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 7586 | pkt->prev = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7587 | pkt->largest_acked_pn = -1; |
| 7588 | pkt->flags = 0; |
| 7589 | pkt->refcnt = 0; |
| 7590 | } |
| 7591 | |
| 7592 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 7593 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 7594 | * of prebuilt frames. |
| 7595 | * |
| 7596 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 7597 | * -1 if there was not enough room to build a packet. |
| 7598 | * XXX NOTE XXX |
| 7599 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 7600 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 7601 | * control window limitation. |
| 7602 | */ |
| 7603 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 7604 | const unsigned char *buf_end, |
| 7605 | struct quic_enc_level *qel, |
| 7606 | struct quic_tls_ctx *tls_ctx, struct list *frms, |
| 7607 | struct quic_conn *qc, const struct quic_version *ver, |
| 7608 | size_t dglen, int pkt_type, int force_ack, |
| 7609 | int padding, int probe, int cc, int *err) |
| 7610 | { |
| 7611 | struct quic_tx_packet *ret_pkt = NULL; |
| 7612 | /* The pointer to the packet number field. */ |
| 7613 | unsigned char *buf_pn; |
| 7614 | unsigned char *beg, *end, *payload; |
| 7615 | int64_t pn; |
| 7616 | size_t pn_len, payload_len, aad_len; |
| 7617 | struct quic_tx_packet *pkt; |
| 7618 | |
| 7619 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel); |
| 7620 | *err = 0; |
| 7621 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 7622 | if (!pkt) { |
| 7623 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc); |
| 7624 | *err = -2; |
| 7625 | goto err; |
| 7626 | } |
| 7627 | |
| 7628 | quic_tx_packet_init(pkt, pkt_type); |
| 7629 | beg = *pos; |
| 7630 | pn_len = 0; |
| 7631 | buf_pn = NULL; |
| 7632 | |
| 7633 | pn = qel->pktns->tx.next_pn + 1; |
| 7634 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
| 7635 | force_ack, padding, cc, probe, qel, qc, ver, frms)) { |
| 7636 | // trace already emitted by function above |
| 7637 | *err = -1; |
| 7638 | goto err; |
| 7639 | } |
| 7640 | |
| 7641 | end = beg + pkt->len; |
| 7642 | payload = buf_pn + pn_len; |
| 7643 | payload_len = end - payload; |
| 7644 | aad_len = payload - beg; |
| 7645 | |
| 7646 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) { |
| 7647 | // trace already emitted by function above |
| 7648 | *err = -2; |
| 7649 | goto err; |
| 7650 | } |
| 7651 | |
| 7652 | end += QUIC_TLS_TAG_LEN; |
| 7653 | pkt->len += QUIC_TLS_TAG_LEN; |
| 7654 | if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) { |
| 7655 | // trace already emitted by function above |
| 7656 | *err = -2; |
| 7657 | goto err; |
| 7658 | } |
| 7659 | |
| 7660 | /* Consume a packet number */ |
| 7661 | qel->pktns->tx.next_pn++; |
| 7662 | qc->tx.prep_bytes += pkt->len; |
| 7663 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) { |
| 7664 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 7665 | TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc); |
| 7666 | } |
| 7667 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 7668 | *pos = end; |
| 7669 | /* Attach the built packet to its tree. */ |
| 7670 | pkt->pn_node.key = pn; |
| 7671 | /* Set the packet in fligth length for in flight packet only. */ |
| 7672 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
| 7673 | pkt->in_flight_len = pkt->len; |
| 7674 | qc->path->prep_in_flight += pkt->len; |
| 7675 | } |
| 7676 | /* Always reset this flags */ |
| 7677 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 7678 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 7679 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 7680 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 7681 | } |
| 7682 | |
| 7683 | pkt->pktns = qel->pktns; |
| 7684 | |
| 7685 | ret_pkt = pkt; |
| 7686 | leave: |
| 7687 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt); |
| 7688 | return ret_pkt; |
| 7689 | |
| 7690 | err: |
| 7691 | /* TODO: what about the frames which have been built |
| 7692 | * for this packet. |
| 7693 | */ |
| 7694 | free_quic_tx_packet(qc, pkt); |
| 7695 | goto leave; |
| 7696 | } |
| 7697 | |
| 7698 | |
| 7699 | static void __quic_conn_init(void) |
| 7700 | { |
| 7701 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 7702 | } |
| 7703 | INITCALL0(STG_REGISTER, __quic_conn_init); |
| 7704 | |
| 7705 | static void __quic_conn_deinit(void) |
| 7706 | { |
| 7707 | BIO_meth_free(ha_quic_meth); |
| 7708 | } |
| 7709 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
| 7710 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7711 | /* Handle a new <dgram> received. Parse each QUIC packets and copied their |
| 7712 | * content to a quic-conn instance. The datagram content can be released after |
| 7713 | * this function. |
| 7714 | * |
| 7715 | * If datagram has been received on a quic-conn owned FD, <from_qc> must be set |
| 7716 | * to the connection instance. <li> is the attached listener. The caller is |
| 7717 | * responsible to ensure that the first packet is destined to this connection |
| 7718 | * by comparing CIDs. |
| 7719 | * |
| 7720 | * If datagram has been received on a receiver FD, <from_qc> will be NULL. This |
| 7721 | * function will thus retrieve the connection from the CID tree or allocate a |
| 7722 | * new one if possible. <li> is the listener attached to the receiver. |
| 7723 | * |
| 7724 | * Returns 0 on success else non-zero. If an error happens, some packets from |
| 7725 | * the datagram may not have been parsed. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7726 | */ |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7727 | int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc, |
| 7728 | struct listener *li) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7729 | { |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7730 | struct quic_rx_packet *pkt; |
| 7731 | struct quic_conn *qc = NULL; |
| 7732 | unsigned char *pos, *end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7733 | struct list *tasklist_head = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7734 | |
| 7735 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 7736 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7737 | pos = dgram->buf; |
| 7738 | end = pos + dgram->len; |
| 7739 | do { |
| 7740 | /* TODO replace zalloc -> alloc. */ |
| 7741 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 7742 | if (!pkt) { |
| 7743 | TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT); |
| 7744 | goto err; |
| 7745 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7746 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7747 | pkt->version = NULL; |
| 7748 | pkt->pn_offset = 0; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7749 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7750 | /* Set flag if pkt is the first one in dgram. */ |
| 7751 | if (pos == dgram->buf) |
| 7752 | pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7753 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7754 | LIST_INIT(&pkt->qc_rx_pkt_list); |
| 7755 | pkt->time_received = now_ms; |
| 7756 | quic_rx_packet_refinc(pkt); |
| 7757 | if (quic_rx_pkt_parse(pkt, pos, end, dgram, li)) |
| 7758 | goto next; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7759 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7760 | /* Search quic-conn instance for first packet of the datagram. |
| 7761 | * quic_rx_packet_parse() is responsible to discard packets |
| 7762 | * with different DCID as the first one in the same datagram. |
| 7763 | */ |
| 7764 | if (!qc) { |
| 7765 | qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li); |
| 7766 | /* qc is NULL if receiving a non Initial packet for an |
| 7767 | * unknown connection. |
| 7768 | */ |
| 7769 | if (!qc) { |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7770 | /* Skip the entire datagram. */ |
| 7771 | pkt->len = end - pos; |
| 7772 | goto next; |
| 7773 | } |
| 7774 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7775 | dgram->qc = qc; |
| 7776 | } |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7777 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7778 | if (qc_rx_check_closing(qc, pkt)) { |
| 7779 | /* Skip the entire datagram. */ |
| 7780 | pkt->len = end - pos; |
| 7781 | goto next; |
| 7782 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7783 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7784 | /* Detect QUIC connection migration. */ |
Frédéric Lécaille | f676954 | 2023-01-12 10:36:26 +0100 | [diff] [blame] | 7785 | if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) { |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7786 | if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) { |
| 7787 | /* Skip the entire datagram. */ |
| 7788 | TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc); |
| 7789 | pkt->len = end - pos; |
| 7790 | goto next; |
| 7791 | } |
| 7792 | } |
| 7793 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7794 | qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7795 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7796 | next: |
| 7797 | pos += pkt->len; |
| 7798 | quic_rx_packet_refdec(pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7799 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7800 | /* Free rejected packets */ |
| 7801 | if (!pkt->refcnt) { |
| 7802 | BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list)); |
| 7803 | pool_free(pool_head_quic_rx_packet, pkt); |
| 7804 | } |
| 7805 | } while (pos < end); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7806 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7807 | /* Increasing the received bytes counter by the UDP datagram length |
| 7808 | * if this datagram could be associated to a connection. |
| 7809 | */ |
| 7810 | if (dgram->qc) |
| 7811 | dgram->qc->rx.bytes += dgram->len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7812 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7813 | /* This must never happen. */ |
| 7814 | BUG_ON(pos > end); |
| 7815 | BUG_ON(pos < end || pos > dgram->buf + dgram->len); |
| 7816 | /* Mark this datagram as consumed */ |
| 7817 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7818 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7819 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 7820 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7821 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7822 | err: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7823 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7824 | return -1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7825 | } |
| 7826 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 7827 | /* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local |
| 7828 | * CIDs. This can be used to determine if a datagram is addressed to the right |
| 7829 | * connection instance. |
| 7830 | * |
| 7831 | * Returns a boolean value. |
| 7832 | */ |
| 7833 | int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len) |
| 7834 | { |
| 7835 | struct ebmb_node *node; |
| 7836 | struct quic_connection_id *id; |
| 7837 | |
| 7838 | /* For ODCID, address is concatenated to it after qc.odcid.len so this |
| 7839 | * comparison is safe. |
| 7840 | */ |
| 7841 | if ((qc->scid.len == dcid_len && |
| 7842 | memcmp(qc->scid.data, dcid, dcid_len) == 0) || |
| 7843 | (qc->odcid.len == dcid_len && |
Frédéric Lécaille | 07846cb | 2023-02-13 16:14:24 +0100 | [diff] [blame] | 7844 | memcmp(qc->odcid.data, dcid, dcid_len) == 0)) { |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 7845 | return 1; |
| 7846 | } |
| 7847 | |
| 7848 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, dcid, dcid_len); |
| 7849 | if (node) { |
| 7850 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 7851 | if (qc == id->qc) |
| 7852 | return 1; |
| 7853 | } |
| 7854 | |
| 7855 | return 0; |
| 7856 | } |
| 7857 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7858 | /* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 7859 | * Returns 1 if succeeded, 0 if not. |
| 7860 | */ |
| 7861 | int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 7862 | unsigned char **dcid, size_t *dcid_len) |
| 7863 | { |
| 7864 | int ret = 0, long_header; |
| 7865 | size_t minlen, skip; |
| 7866 | |
| 7867 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 7868 | |
| 7869 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
| 7870 | TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT); |
| 7871 | goto err; |
| 7872 | } |
| 7873 | |
| 7874 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
| 7875 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 7876 | QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN; |
| 7877 | skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF; |
| 7878 | if (end - buf < minlen) |
| 7879 | goto err; |
| 7880 | |
| 7881 | buf += skip; |
| 7882 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 7883 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 7884 | goto err; |
| 7885 | |
| 7886 | *dcid = buf; |
| 7887 | |
| 7888 | ret = 1; |
| 7889 | leave: |
| 7890 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 7891 | return ret; |
| 7892 | |
| 7893 | err: |
| 7894 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT); |
| 7895 | goto leave; |
| 7896 | } |
| 7897 | |
| 7898 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 7899 | void qc_notify_close(struct quic_conn *qc) |
| 7900 | { |
| 7901 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 7902 | |
| 7903 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 7904 | goto leave; |
| 7905 | |
| 7906 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 7907 | /* wake up the MUX */ |
| 7908 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) { |
| 7909 | TRACE_STATE("connection closure notidfied to mux", |
| 7910 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7911 | qc->conn->mux->wake(qc->conn); |
| 7912 | } |
| 7913 | else |
| 7914 | TRACE_STATE("connection closure not notidfied to mux", |
| 7915 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7916 | leave: |
| 7917 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 7918 | } |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7919 | |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 7920 | /* Wake-up upper layer if waiting for send to be ready. |
| 7921 | * |
| 7922 | * Returns 1 if upper layer has been woken up else 0. |
| 7923 | */ |
| 7924 | int qc_notify_send(struct quic_conn *qc) |
| 7925 | { |
| 7926 | if (qc->subs && qc->subs->events & SUB_RETRY_SEND) { |
Amaury Denoyelle | caa1654 | 2023-02-28 15:11:26 +0100 | [diff] [blame] | 7927 | if (quic_path_prep_data(qc->path) && |
| 7928 | (!qc_test_fd(qc) || !fd_send_active(qc->fd))) { |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 7929 | tasklet_wakeup(qc->subs->tasklet); |
| 7930 | qc->subs->events &= ~SUB_RETRY_SEND; |
| 7931 | if (!qc->subs->events) |
| 7932 | qc->subs = NULL; |
| 7933 | |
| 7934 | return 1; |
| 7935 | } |
| 7936 | } |
| 7937 | |
| 7938 | return 0; |
| 7939 | } |
| 7940 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7941 | |
| 7942 | /* appctx context used by "show quic" command */ |
| 7943 | struct show_quic_ctx { |
| 7944 | unsigned int epoch; |
| 7945 | struct bref bref; /* back-reference to the quic-conn being dumped */ |
| 7946 | unsigned int thr; |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 7947 | int flags; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7948 | }; |
| 7949 | |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 7950 | #define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */ |
| 7951 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7952 | static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private) |
| 7953 | { |
| 7954 | struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 7955 | |
| 7956 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 7957 | return 1; |
| 7958 | |
| 7959 | ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1); |
| 7960 | ctx->thr = 0; |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 7961 | ctx->flags = 0; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7962 | |
Amaury Denoyelle | 10a46de | 2023-02-09 18:18:45 +0100 | [diff] [blame] | 7963 | if (*args[2] && strcmp(args[2], "all") == 0) |
| 7964 | ctx->flags |= QC_CLI_FL_SHOW_ALL; |
| 7965 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7966 | LIST_INIT(&ctx->bref.users); |
| 7967 | |
| 7968 | return 0; |
| 7969 | } |
| 7970 | |
| 7971 | static int cli_io_handler_dump_quic(struct appctx *appctx) |
| 7972 | { |
| 7973 | struct show_quic_ctx *ctx = appctx->svcctx; |
| 7974 | struct stconn *sc = appctx_sc(appctx); |
| 7975 | struct quic_conn *qc; |
Amaury Denoyelle | 1b0fc43 | 2023-02-01 17:05:10 +0100 | [diff] [blame] | 7976 | struct quic_enc_level *qel; |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 7977 | struct eb64_node *node; |
| 7978 | struct qc_stream_desc *stream; |
Amaury Denoyelle | b89c0e2 | 2023-02-01 17:04:26 +0100 | [diff] [blame] | 7979 | char bufaddr[INET6_ADDRSTRLEN], bufport[6]; |
Amaury Denoyelle | 58d9d5d | 2023-02-01 11:54:43 +0100 | [diff] [blame] | 7980 | int expire; |
| 7981 | unsigned char cid_len; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7982 | |
| 7983 | thread_isolate(); |
| 7984 | |
| 7985 | if (ctx->thr >= global.nbthread) |
| 7986 | goto done; |
| 7987 | |
| 7988 | if (unlikely(sc_ic(sc)->flags & CF_SHUTW)) { |
| 7989 | /* If we're forced to shut down, we might have to remove our |
| 7990 | * reference to the last stream being dumped. |
| 7991 | */ |
| 7992 | if (!LIST_ISEMPTY(&ctx->bref.users)) |
| 7993 | LIST_DEL_INIT(&ctx->bref.users); |
| 7994 | goto done; |
| 7995 | } |
| 7996 | |
| 7997 | chunk_reset(&trash); |
| 7998 | |
| 7999 | if (!LIST_ISEMPTY(&ctx->bref.users)) { |
| 8000 | /* Remove show_quic_ctx from previous quic_conn instance. */ |
| 8001 | LIST_DEL_INIT(&ctx->bref.users); |
| 8002 | } |
| 8003 | else if (!ctx->bref.ref) { |
| 8004 | /* First invocation. */ |
| 8005 | ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n; |
| 8006 | } |
| 8007 | |
| 8008 | while (1) { |
| 8009 | int done = 0; |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 8010 | int i; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8011 | |
| 8012 | if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) { |
| 8013 | done = 1; |
| 8014 | } |
| 8015 | else { |
| 8016 | qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx); |
| 8017 | if ((int)(qc->qc_epoch - ctx->epoch) > 0) |
| 8018 | done = 1; |
| 8019 | } |
| 8020 | |
| 8021 | if (done) { |
| 8022 | ++ctx->thr; |
| 8023 | if (ctx->thr >= global.nbthread) |
| 8024 | break; |
| 8025 | ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n; |
| 8026 | continue; |
| 8027 | } |
| 8028 | |
Amaury Denoyelle | 10a46de | 2023-02-09 18:18:45 +0100 | [diff] [blame] | 8029 | if (!(ctx->flags & QC_CLI_FL_SHOW_ALL) && |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 8030 | qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) { |
| 8031 | ctx->bref.ref = qc->el_th_ctx.n; |
| 8032 | continue; |
| 8033 | } |
| 8034 | |
Amaury Denoyelle | 58d9d5d | 2023-02-01 11:54:43 +0100 | [diff] [blame] | 8035 | /* CIDs */ |
| 8036 | chunk_appendf(&trash, "* %p[%02u]: scid=", qc, qc->tid); |
| 8037 | for (cid_len = 0; cid_len < qc->scid.len; ++cid_len) |
| 8038 | chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]); |
| 8039 | while (cid_len++ < 20) |
| 8040 | chunk_appendf(&trash, ".."); |
| 8041 | |
| 8042 | chunk_appendf(&trash, " dcid="); |
| 8043 | for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len) |
| 8044 | chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]); |
| 8045 | while (cid_len++ < 20) |
| 8046 | chunk_appendf(&trash, ".."); |
| 8047 | |
| 8048 | chunk_appendf(&trash, "\n"); |
| 8049 | |
| 8050 | /* Connection state */ |
| 8051 | if (qc->flags & QUIC_FL_CONN_CLOSING) |
| 8052 | chunk_appendf(&trash, " st=closing "); |
| 8053 | else if (qc->flags & QUIC_FL_CONN_DRAINING) |
| 8054 | chunk_appendf(&trash, " st=draining "); |
| 8055 | else if (qc->state < QUIC_HS_ST_CONFIRMED) |
| 8056 | chunk_appendf(&trash, " st=handshake "); |
| 8057 | else |
| 8058 | chunk_appendf(&trash, " st=opened "); |
| 8059 | |
| 8060 | if (qc->mux_state == QC_MUX_NULL) |
| 8061 | chunk_appendf(&trash, "mux=null "); |
| 8062 | else if (qc->mux_state == QC_MUX_READY) |
| 8063 | chunk_appendf(&trash, "mux=ready "); |
| 8064 | else |
| 8065 | chunk_appendf(&trash, "mux=released "); |
| 8066 | |
| 8067 | expire = qc->idle_timer_task->expire; |
| 8068 | chunk_appendf(&trash, "expire=%02ds ", |
| 8069 | expire > now_ms ? (expire - now_ms) / 1000 : 0); |
| 8070 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8071 | chunk_appendf(&trash, "\n"); |
| 8072 | |
Amaury Denoyelle | b89c0e2 | 2023-02-01 17:04:26 +0100 | [diff] [blame] | 8073 | /* Socket */ |
| 8074 | chunk_appendf(&trash, " fd=%d", qc->fd); |
| 8075 | if (qc->local_addr.ss_family == AF_INET || |
| 8076 | qc->local_addr.ss_family == AF_INET6) { |
| 8077 | addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr)); |
| 8078 | port_to_str(&qc->local_addr, bufport, sizeof(bufport)); |
| 8079 | chunk_appendf(&trash, " from=%s:%s", bufaddr, bufport); |
| 8080 | |
| 8081 | addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr)); |
| 8082 | port_to_str(&qc->peer_addr, bufport, sizeof(bufport)); |
| 8083 | chunk_appendf(&trash, " to=%s:%s", bufaddr, bufport); |
| 8084 | } |
| 8085 | |
| 8086 | chunk_appendf(&trash, "\n"); |
| 8087 | |
Amaury Denoyelle | 1b0fc43 | 2023-02-01 17:05:10 +0100 | [diff] [blame] | 8088 | /* Encryption levels */ |
| 8089 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 8090 | chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu", |
| 8091 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 8092 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 8093 | chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n", |
| 8094 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 8095 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
| 8096 | chunk_appendf(&trash, " [0-rtt] rx.ackrng=%-6zu tx.inflight=%-6zu", |
| 8097 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 8098 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 8099 | chunk_appendf(&trash, " [1-rtt] rx.ackrng=%-6zu tx.inflight=%-6zu", |
| 8100 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 8101 | |
| 8102 | chunk_appendf(&trash, "\n"); |
| 8103 | |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 8104 | /* Streams */ |
| 8105 | node = eb64_first(&qc->streams_by_id); |
| 8106 | i = 0; |
| 8107 | while (node) { |
| 8108 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 8109 | node = eb64_next(node); |
| 8110 | |
Amaury Denoyelle | a9de25a | 2023-02-10 09:25:22 +0100 | [diff] [blame] | 8111 | chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key); |
| 8112 | chunk_appendf(&trash, " off=%-8llu ack=%-8llu", |
| 8113 | (unsigned long long)stream->buf_offset, |
| 8114 | (unsigned long long)stream->ack_offset); |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 8115 | |
| 8116 | if (!(++i % 3)) { |
| 8117 | chunk_appendf(&trash, "\n"); |
| 8118 | i = 0; |
| 8119 | } |
| 8120 | } |
| 8121 | |
| 8122 | chunk_appendf(&trash, "\n"); |
| 8123 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8124 | if (applet_putchk(appctx, &trash) == -1) { |
| 8125 | /* Register show_quic_ctx to quic_conn instance. */ |
| 8126 | LIST_APPEND(&qc->back_refs, &ctx->bref.users); |
| 8127 | goto full; |
| 8128 | } |
| 8129 | |
| 8130 | ctx->bref.ref = qc->el_th_ctx.n; |
| 8131 | } |
| 8132 | |
| 8133 | done: |
| 8134 | thread_release(); |
| 8135 | return 1; |
| 8136 | |
| 8137 | full: |
| 8138 | thread_release(); |
| 8139 | return 0; |
| 8140 | } |
| 8141 | |
| 8142 | static void cli_release_show_quic(struct appctx *appctx) |
| 8143 | { |
| 8144 | struct show_quic_ctx *ctx = appctx->svcctx; |
| 8145 | |
| 8146 | if (ctx->thr < global.nbthread) { |
| 8147 | thread_isolate(); |
| 8148 | if (!LIST_ISEMPTY(&ctx->bref.users)) |
| 8149 | LIST_DEL_INIT(&ctx->bref.users); |
| 8150 | thread_release(); |
| 8151 | } |
| 8152 | } |
| 8153 | |
| 8154 | static struct cli_kw_list cli_kws = {{ }, { |
| 8155 | { { "show", "quic", NULL }, "show quic : 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] | 8156 | {{},} |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8157 | }}; |
| 8158 | |
| 8159 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 8160 | |
| 8161 | static void init_quic() |
| 8162 | { |
| 8163 | int thr; |
| 8164 | |
| 8165 | for (thr = 0; thr < MAX_THREADS; ++thr) |
| 8166 | LIST_INIT(&ha_thread_ctx[thr].quic_conns); |
| 8167 | } |
| 8168 | INITCALL0(STG_INIT, init_quic); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8169 | |
| 8170 | /* |
| 8171 | * Local variables: |
| 8172 | * c-indent-level: 8 |
| 8173 | * c-basic-offset: 8 |
| 8174 | * End: |
| 8175 | */ |