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); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3032 | } |
| 3033 | else { |
| 3034 | TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc); |
Amaury Denoyelle | 38836b6 | 2023-02-07 14:24:54 +0100 | [diff] [blame] | 3035 | if (qc->app_ops == &h3_ops) { |
Amaury Denoyelle | 156a89a | 2023-02-20 10:32:16 +0100 | [diff] [blame] | 3036 | if (!qc_h3_request_reject(qc, stream->id)) { |
| 3037 | TRACE_ERROR("error on request rejection", QUIC_EV_CONN_PRSHPKT, qc); |
| 3038 | /* This packet will not be acknowledged */ |
| 3039 | goto leave; |
| 3040 | } |
| 3041 | } |
| 3042 | else { |
| 3043 | /* This packet will not be acknowledged */ |
| 3044 | goto leave; |
Frédéric Lécaille | d18025e | 2023-01-20 15:33:50 +0100 | [diff] [blame] | 3045 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3046 | } |
Amaury Denoyelle | 315a4f6 | 2023-03-06 09:10:53 +0100 | [diff] [blame] | 3047 | |
| 3048 | break; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3049 | } |
| 3050 | |
Amaury Denoyelle | 2216b08 | 2023-02-02 14:59:36 +0100 | [diff] [blame] | 3051 | if (!qc_handle_strm_frm(pkt, stream, qc, fin)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3052 | TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 3053 | goto leave; |
| 3054 | } |
| 3055 | |
| 3056 | break; |
| 3057 | } |
| 3058 | case QUIC_FT_MAX_DATA: |
| 3059 | if (qc->mux_state == QC_MUX_READY) { |
| 3060 | struct quic_max_data *data = &frm.max_data; |
| 3061 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 3062 | } |
| 3063 | break; |
| 3064 | case QUIC_FT_MAX_STREAM_DATA: |
| 3065 | if (qc->mux_state == QC_MUX_READY) { |
| 3066 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 3067 | if (qcc_recv_max_stream_data(qc->qcc, data->id, |
| 3068 | data->max_stream_data)) { |
| 3069 | TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 3070 | goto leave; |
| 3071 | } |
| 3072 | } |
| 3073 | break; |
| 3074 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 3075 | case QUIC_FT_MAX_STREAMS_UNI: |
| 3076 | break; |
| 3077 | case QUIC_FT_DATA_BLOCKED: |
| 3078 | HA_ATOMIC_INC(&qc->prx_counters->data_blocked); |
| 3079 | break; |
| 3080 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 3081 | HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked); |
| 3082 | break; |
| 3083 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 3084 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi); |
| 3085 | break; |
| 3086 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 3087 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni); |
| 3088 | break; |
| 3089 | case QUIC_FT_NEW_CONNECTION_ID: |
| 3090 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 3091 | /* XXX TO DO XXX */ |
| 3092 | break; |
| 3093 | case QUIC_FT_CONNECTION_CLOSE: |
| 3094 | case QUIC_FT_CONNECTION_CLOSE_APP: |
| 3095 | /* Increment the error counters */ |
| 3096 | qc_cc_err_count_inc(qc, &frm); |
| 3097 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
| 3098 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 3099 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 3100 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 3101 | } |
| 3102 | TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 3103 | /* RFC 9000 10.2. Immediate Close: |
| 3104 | * The closing and draining connection states exist to ensure |
| 3105 | * that connections close cleanly and that delayed or reordered |
| 3106 | * packets are properly discarded. These states SHOULD persist |
| 3107 | * for at least three times the current PTO interval... |
| 3108 | * |
| 3109 | * Rearm the idle timeout only one time when entering draining |
| 3110 | * state. |
| 3111 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3112 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
Amaury Denoyelle | 77ed631 | 2023-02-01 09:28:55 +0100 | [diff] [blame] | 3113 | qc_idle_timer_do_rearm(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3114 | qc_notify_close(qc); |
| 3115 | } |
| 3116 | break; |
| 3117 | case QUIC_FT_HANDSHAKE_DONE: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3118 | if (qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3119 | TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame", |
| 3120 | QUIC_EV_CONN_PRSHPKT, qc); |
| 3121 | goto leave; |
| 3122 | } |
| 3123 | |
| 3124 | qc->state = QUIC_HS_ST_CONFIRMED; |
| 3125 | break; |
| 3126 | default: |
| 3127 | TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc); |
| 3128 | goto leave; |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | /* Flag this packet number space as having received a packet. */ |
| 3133 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
| 3134 | |
| 3135 | if (fast_retrans) { |
| 3136 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3137 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3138 | |
| 3139 | TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc); |
| 3140 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 3141 | qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP; |
| 3142 | } |
| 3143 | |
| 3144 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 3145 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 3146 | * be discarded. |
| 3147 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3148 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3149 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
| 3150 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 3151 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3152 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3153 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 3154 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3155 | qc_set_timer(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3156 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3157 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 3158 | } |
| 3159 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 3160 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3161 | } |
| 3162 | } |
| 3163 | |
| 3164 | ret = 1; |
| 3165 | leave: |
| 3166 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 3167 | return ret; |
| 3168 | } |
| 3169 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3170 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3171 | /* Allocate Tx buffer from <qc> quic-conn if needed. |
| 3172 | * |
| 3173 | * Returns allocated buffer or NULL on error. |
| 3174 | */ |
| 3175 | static struct buffer *qc_txb_alloc(struct quic_conn *qc) |
| 3176 | { |
| 3177 | struct buffer *buf = &qc->tx.buf; |
| 3178 | if (!b_alloc(buf)) |
| 3179 | return NULL; |
| 3180 | |
| 3181 | return buf; |
| 3182 | } |
| 3183 | |
| 3184 | /* Free Tx buffer from <qc> if it is empty. */ |
| 3185 | static void qc_txb_release(struct quic_conn *qc) |
| 3186 | { |
| 3187 | struct buffer *buf = &qc->tx.buf; |
| 3188 | |
| 3189 | /* For the moment sending function is responsible to purge the buffer |
| 3190 | * entirely. It may change in the future but this requires to be able |
| 3191 | * to reuse old data. |
Frédéric Lécaille | bbf86be | 2023-02-20 09:28:58 +0100 | [diff] [blame] | 3192 | * For the momemt we do not care to leave data in the buffer for |
| 3193 | * a connection which is supposed to be killed asap. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3194 | */ |
| 3195 | BUG_ON_HOT(buf && b_data(buf)); |
| 3196 | |
| 3197 | if (!b_data(buf)) { |
| 3198 | b_free(buf); |
| 3199 | offer_buffers(NULL, 1); |
| 3200 | } |
| 3201 | } |
| 3202 | |
| 3203 | /* Commit a datagram payload written into <buf> of length <length>. <first_pkt> |
| 3204 | * must contains the address of the first packet stored in the payload. |
| 3205 | * |
| 3206 | * Caller is responsible that there is enough space in the buffer. |
| 3207 | */ |
| 3208 | static void qc_txb_store(struct buffer *buf, uint16_t length, |
| 3209 | struct quic_tx_packet *first_pkt) |
| 3210 | { |
| 3211 | const size_t hdlen = sizeof(uint16_t) + sizeof(void *); |
| 3212 | BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */ |
| 3213 | |
| 3214 | write_u16(b_tail(buf), length); |
| 3215 | write_ptr(b_tail(buf) + sizeof(length), first_pkt); |
| 3216 | b_add(buf, hdlen + length); |
| 3217 | } |
| 3218 | |
| 3219 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 3220 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 3221 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 3222 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 3223 | * <force_ack> may be set to 1 if you want to force an ack. |
| 3224 | */ |
| 3225 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 3226 | struct quic_enc_level *qel, int cc, int probe, int force_ack) |
| 3227 | { |
| 3228 | unsigned int must_ack = force_ack || |
| 3229 | (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)); |
| 3230 | |
| 3231 | /* Do not build any more packet if the TX secrets are not available or |
| 3232 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 3233 | * and if there is no more packets to send upon PTO expiration |
| 3234 | * and if there is no more ack-eliciting frames to send or in flight |
| 3235 | * congestion control limit is reached for prepared data |
| 3236 | */ |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 3237 | if (!quic_tls_has_tx_sec(qel) || |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3238 | (!cc && !probe && !must_ack && |
| 3239 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 3240 | return 0; |
| 3241 | } |
| 3242 | |
| 3243 | return 1; |
| 3244 | } |
| 3245 | |
| 3246 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3247 | * <frms>. Each packet is stored in a distinct datagram written to <buf>. |
| 3248 | * |
| 3249 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3250 | * the address of the packet contained in the datagram. |
| 3251 | * |
| 3252 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3253 | * -1 if something wrong happened. |
| 3254 | */ |
| 3255 | static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3256 | struct list *frms) |
| 3257 | { |
| 3258 | int ret = -1; |
| 3259 | struct quic_enc_level *qel; |
| 3260 | unsigned char *end, *pos; |
| 3261 | struct quic_tx_packet *pkt; |
| 3262 | size_t total; |
| 3263 | /* Each datagram is prepended with its length followed by the address |
| 3264 | * of the first packet in the datagram. |
| 3265 | */ |
| 3266 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt); |
| 3267 | |
| 3268 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3269 | |
| 3270 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3271 | total = 0; |
| 3272 | pos = (unsigned char *)b_tail(buf); |
| 3273 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) { |
| 3274 | int err, probe, cc; |
| 3275 | |
| 3276 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
| 3277 | probe = 0; |
| 3278 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3279 | /* We do not probe if an immediate close was asked */ |
| 3280 | if (!cc) |
| 3281 | probe = qel->pktns->tx.pto_probe; |
| 3282 | |
| 3283 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0)) |
| 3284 | break; |
| 3285 | |
| 3286 | /* Leave room for the datagram header */ |
| 3287 | pos += dg_headlen; |
| 3288 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3289 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3290 | } |
| 3291 | else { |
| 3292 | end = pos + qc->path->mtu; |
| 3293 | } |
| 3294 | |
| 3295 | pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0, |
| 3296 | QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err); |
| 3297 | switch (err) { |
| 3298 | case -2: |
| 3299 | // trace already emitted by function above |
| 3300 | goto leave; |
| 3301 | case -1: |
| 3302 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 3303 | * MTU, we are here because of the congestion control window. There is |
| 3304 | * no need to try to reuse this buffer. |
| 3305 | */ |
| 3306 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
| 3307 | goto out; |
| 3308 | default: |
| 3309 | break; |
| 3310 | } |
| 3311 | |
| 3312 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 3313 | BUG_ON(!pkt); |
| 3314 | |
| 3315 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3316 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3317 | |
| 3318 | total += pkt->len; |
| 3319 | |
| 3320 | /* Write datagram header. */ |
| 3321 | qc_txb_store(buf, pkt->len, pkt); |
| 3322 | } |
| 3323 | |
| 3324 | out: |
| 3325 | ret = total; |
| 3326 | leave: |
| 3327 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3328 | return ret; |
| 3329 | } |
| 3330 | |
| 3331 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3332 | * <frms>. Several packets can be regrouped in a single datagram. The result is |
| 3333 | * written into <buf>. |
| 3334 | * |
| 3335 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3336 | * the address of first packet in the datagram. |
| 3337 | * |
| 3338 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3339 | * -1 if something wrong happened. |
| 3340 | */ |
| 3341 | static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3342 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3343 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3344 | { |
| 3345 | struct quic_enc_level *qel; |
| 3346 | unsigned char *end, *pos; |
| 3347 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 3348 | /* length of datagrams */ |
| 3349 | uint16_t dglen; |
| 3350 | size_t total; |
| 3351 | int ret = -1, padding; |
| 3352 | /* Each datagram is prepended with its length followed by the address |
| 3353 | * of the first packet in the datagram. |
| 3354 | */ |
| 3355 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt); |
| 3356 | struct list *frms; |
| 3357 | |
| 3358 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3359 | |
| 3360 | /* Currently qc_prep_pkts() does not handle buffer wrapping so the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 3361 | * caller must ensure that buf is reset. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3362 | */ |
| 3363 | BUG_ON_HOT(buf->head || buf->data); |
| 3364 | |
| 3365 | total = 0; |
| 3366 | qel = &qc->els[tel]; |
| 3367 | frms = tel_frms; |
| 3368 | dglen = 0; |
| 3369 | padding = 0; |
| 3370 | pos = (unsigned char *)b_head(buf); |
| 3371 | first_pkt = prv_pkt = NULL; |
| 3372 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
| 3373 | int err, probe, cc; |
| 3374 | enum quic_pkt_type pkt_type; |
| 3375 | struct quic_tls_ctx *tls_ctx; |
| 3376 | const struct quic_version *ver; |
| 3377 | int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 3378 | (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3379 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3380 | |
| 3381 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
| 3382 | probe = 0; |
| 3383 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3384 | /* We do not probe if an immediate close was asked */ |
| 3385 | if (!cc) |
| 3386 | probe = qel->pktns->tx.pto_probe; |
| 3387 | |
| 3388 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) { |
| 3389 | if (prv_pkt) |
| 3390 | qc_txb_store(buf, dglen, first_pkt); |
| 3391 | /* Let's select the next encryption level */ |
| 3392 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 3393 | tel = next_tel; |
| 3394 | frms = next_tel_frms; |
| 3395 | qel = &qc->els[tel]; |
| 3396 | /* Build a new datagram */ |
| 3397 | prv_pkt = NULL; |
| 3398 | TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc); |
| 3399 | continue; |
| 3400 | } |
| 3401 | break; |
| 3402 | } |
| 3403 | |
| 3404 | pkt_type = quic_tls_level_pkt_type(tel); |
| 3405 | if (!prv_pkt) { |
| 3406 | /* Leave room for the datagram header */ |
| 3407 | pos += dg_headlen; |
| 3408 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3409 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3410 | } |
| 3411 | else { |
| 3412 | end = pos + qc->path->mtu; |
| 3413 | } |
| 3414 | } |
| 3415 | |
Frédéric Lécaille | 69e7118 | 2023-02-20 14:39:41 +0100 | [diff] [blame] | 3416 | /* RFC 9000 14.1 Initial datagram size |
| 3417 | * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting |
| 3418 | * Initial packets to at least the smallest allowed maximum datagram size of |
| 3419 | * 1200 bytes. |
| 3420 | * |
| 3421 | * Ensure that no ack-eliciting packets are sent into too small datagrams |
| 3422 | */ |
| 3423 | if (pkt_type == QUIC_PACKET_TYPE_INITIAL && !LIST_ISEMPTY(tel_frms)) { |
| 3424 | if (end - pos < QUIC_INITIAL_PACKET_MINLEN) { |
Frédéric Lécaille | d30a04a | 2023-02-21 16:44:05 +0100 | [diff] [blame] | 3425 | 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] | 3426 | QUIC_EV_CONN_PHPKTS, qc); |
| 3427 | goto out; |
| 3428 | } |
| 3429 | |
| 3430 | /* Pad this Initial packet if there is no ack-eliciting frames to send from |
| 3431 | * the next packet number space. |
| 3432 | */ |
Frédéric Lécaille | ec93721 | 2023-03-03 17:34:41 +0100 | [diff] [blame] | 3433 | if (!next_tel_frms || LIST_ISEMPTY(next_tel_frms)) |
Frédéric Lécaille | 69e7118 | 2023-02-20 14:39:41 +0100 | [diff] [blame] | 3434 | padding = 1; |
| 3435 | } |
| 3436 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3437 | if (qc->negotiated_version) { |
| 3438 | ver = qc->negotiated_version; |
| 3439 | if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 3440 | tls_ctx = &qc->negotiated_ictx; |
| 3441 | else |
| 3442 | tls_ctx = &qel->tls_ctx; |
| 3443 | } |
| 3444 | else { |
| 3445 | ver = qc->original_version; |
| 3446 | tls_ctx = &qel->tls_ctx; |
| 3447 | } |
| 3448 | |
| 3449 | cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms, |
| 3450 | qc, ver, dglen, pkt_type, |
| 3451 | force_ack, padding, probe, cc, &err); |
| 3452 | switch (err) { |
| 3453 | case -2: |
| 3454 | // trace already emitted by function above |
| 3455 | goto leave; |
| 3456 | case -1: |
| 3457 | /* If there was already a correct packet present, set the |
| 3458 | * current datagram as prepared into <cbuf>. |
| 3459 | */ |
| 3460 | if (prv_pkt) |
| 3461 | qc_txb_store(buf, dglen, first_pkt); |
| 3462 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
| 3463 | goto out; |
| 3464 | default: |
| 3465 | break; |
| 3466 | } |
| 3467 | |
| 3468 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 3469 | BUG_ON(!cur_pkt); |
| 3470 | |
| 3471 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3472 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3473 | |
| 3474 | total += cur_pkt->len; |
| 3475 | /* keep trace of the first packet in the datagram */ |
| 3476 | if (!first_pkt) |
| 3477 | first_pkt = cur_pkt; |
Frédéric Lécaille | 74b5f7b | 2022-11-20 18:35:35 +0100 | [diff] [blame] | 3478 | /* Attach the current one to the previous one and vice versa */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3479 | if (prv_pkt) { |
| 3480 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 3481 | cur_pkt->prev = prv_pkt; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3482 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 3483 | } |
| 3484 | /* Let's say we have to build a new dgram */ |
| 3485 | prv_pkt = NULL; |
| 3486 | dglen += cur_pkt->len; |
| 3487 | /* Client: discard the Initial encryption keys as soon as |
| 3488 | * a handshake packet could be built. |
| 3489 | */ |
| 3490 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
| 3491 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 3492 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3493 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3494 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 3495 | qc_set_timer(qc); |
| 3496 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3497 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 3498 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
| 3499 | } |
| 3500 | /* If the data for the current encryption level have all been sent, |
| 3501 | * select the next level. |
| 3502 | */ |
| 3503 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
| 3504 | next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) { |
| 3505 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 3506 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 3507 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3508 | tel = next_tel; |
| 3509 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 3510 | frms = &qc->els[tel].pktns->tx.frms; |
| 3511 | else |
| 3512 | frms = next_tel_frms; |
| 3513 | qel = &qc->els[tel]; |
| 3514 | if (!LIST_ISEMPTY(frms)) { |
| 3515 | /* If there is data for the next level, do not |
| 3516 | * consume a datagram. |
| 3517 | */ |
| 3518 | prv_pkt = cur_pkt; |
| 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | /* If we have to build a new datagram, set the current datagram as |
| 3523 | * prepared into <cbuf>. |
| 3524 | */ |
| 3525 | if (!prv_pkt) { |
| 3526 | qc_txb_store(buf, dglen, first_pkt); |
| 3527 | first_pkt = NULL; |
| 3528 | dglen = 0; |
| 3529 | padding = 0; |
| 3530 | } |
| 3531 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
| 3532 | (!qc_is_listener(qc) || |
| 3533 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3534 | padding = 1; |
| 3535 | } |
| 3536 | } |
| 3537 | |
| 3538 | out: |
| 3539 | ret = total; |
| 3540 | leave: |
| 3541 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3542 | return ret; |
| 3543 | } |
| 3544 | |
| 3545 | /* Send datagrams stored in <buf>. |
| 3546 | * |
Amaury Denoyelle | 1febc2d | 2023-02-23 11:18:38 +0100 | [diff] [blame] | 3547 | * This function returns 1 for success. On error, there is several behavior |
| 3548 | * depending on underlying sendto() error : |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 3549 | * - for an unrecoverable error, 0 is returned and connection is killed. |
| 3550 | * - a transient error is handled differently if connection has its owned |
| 3551 | * socket. If this is the case, 0 is returned and socket is subscribed on the |
| 3552 | * 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] | 3553 | * Remaining data are purged from the buffer and will eventually be detected |
| 3554 | * as lost which gives the opportunity to retry sending. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3555 | */ |
| 3556 | int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx) |
| 3557 | { |
Frédéric Lécaille | a2c62c3 | 2023-02-10 14:13:43 +0100 | [diff] [blame] | 3558 | int ret = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3559 | struct quic_conn *qc; |
| 3560 | char skip_sendto = 0; |
| 3561 | |
| 3562 | qc = ctx->qc; |
| 3563 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
| 3564 | while (b_contig_data(buf, 0)) { |
| 3565 | unsigned char *pos; |
| 3566 | struct buffer tmpbuf = { }; |
| 3567 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3568 | uint16_t dglen; |
| 3569 | size_t headlen = sizeof dglen + sizeof first_pkt; |
| 3570 | unsigned int time_sent; |
| 3571 | |
| 3572 | pos = (unsigned char *)b_head(buf); |
| 3573 | dglen = read_u16(pos); |
| 3574 | BUG_ON_HOT(!dglen); /* this should not happen */ |
| 3575 | |
| 3576 | pos += sizeof dglen; |
| 3577 | first_pkt = read_ptr(pos); |
| 3578 | pos += sizeof first_pkt; |
| 3579 | tmpbuf.area = (char *)pos; |
| 3580 | tmpbuf.size = tmpbuf.data = dglen; |
| 3581 | |
| 3582 | TRACE_DATA("send dgram", QUIC_EV_CONN_SPPKTS, qc); |
| 3583 | /* If sendto is on error just skip the call to it for the rest |
| 3584 | * of the loop but continue to purge the buffer. Data will be |
| 3585 | * transmitted when QUIC packets are detected as lost on our |
| 3586 | * side. |
| 3587 | * |
| 3588 | * TODO use fd-monitoring to detect when send operation can be |
| 3589 | * retry. This should improve the bandwidth without relying on |
| 3590 | * retransmission timer. However, it requires a major rework on |
| 3591 | * quic-conn fd management. |
| 3592 | */ |
| 3593 | if (!skip_sendto) { |
Amaury Denoyelle | 1febc2d | 2023-02-23 11:18:38 +0100 | [diff] [blame] | 3594 | int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0); |
| 3595 | if (ret < 0) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 3596 | TRACE_ERROR("sendto fatal error", QUIC_EV_CONN_SPPKTS, qc); |
Amaury Denoyelle | 1febc2d | 2023-02-23 11:18:38 +0100 | [diff] [blame] | 3597 | qc_kill_conn(qc); |
| 3598 | b_del(buf, buf->data); |
| 3599 | goto leave; |
| 3600 | } |
| 3601 | else if (!ret) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 3602 | /* Connection owned socket : poller will wake us up when transient error is cleared. */ |
| 3603 | if (qc_test_fd(qc)) { |
| 3604 | TRACE_ERROR("sendto error, subscribe to poller", QUIC_EV_CONN_SPPKTS, qc); |
| 3605 | goto leave; |
| 3606 | } |
| 3607 | |
| 3608 | /* No connection owned-socket : rely on retransmission to retry sending. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3609 | skip_sendto = 1; |
| 3610 | TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc); |
| 3611 | } |
| 3612 | } |
| 3613 | |
| 3614 | b_del(buf, dglen + headlen); |
| 3615 | qc->tx.bytes += tmpbuf.data; |
| 3616 | time_sent = now_ms; |
| 3617 | |
| 3618 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
Frédéric Lécaille | ceb88b8 | 2023-02-20 14:43:55 +0100 | [diff] [blame] | 3619 | /* RFC 9000 14.1 Initial datagram size |
| 3620 | * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting |
| 3621 | * Initial packets to at least the smallest allowed maximum datagram size of |
| 3622 | * 1200 bytes. |
| 3623 | */ |
| 3624 | BUG_ON_HOT(pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 3625 | (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) && |
| 3626 | dglen < QUIC_INITIAL_PACKET_MINLEN); |
| 3627 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3628 | pkt->time_sent = time_sent; |
| 3629 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3630 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
| 3631 | qc->path->ifae_pkts++; |
| 3632 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3633 | qc_idle_timer_rearm(qc, 0); |
| 3634 | } |
| 3635 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3636 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3637 | qc->flags |= QUIC_FL_CONN_CLOSING; |
| 3638 | qc_notify_close(qc); |
| 3639 | |
| 3640 | /* RFC 9000 10.2. Immediate Close: |
| 3641 | * The closing and draining connection states exist to ensure |
| 3642 | * that connections close cleanly and that delayed or reordered |
| 3643 | * packets are properly discarded. These states SHOULD persist |
| 3644 | * for at least three times the current PTO interval... |
| 3645 | * |
| 3646 | * Rearm the idle timeout only one time when entering closing |
| 3647 | * state. |
| 3648 | */ |
| 3649 | qc_idle_timer_do_rearm(qc); |
| 3650 | if (qc->timer_task) { |
| 3651 | task_destroy(qc->timer_task); |
| 3652 | qc->timer_task = NULL; |
| 3653 | } |
| 3654 | } |
| 3655 | qc->path->in_flight += pkt->in_flight_len; |
| 3656 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3657 | if (pkt->in_flight_len) |
| 3658 | qc_set_timer(qc); |
| 3659 | TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 3660 | next_pkt = pkt->next; |
| 3661 | quic_tx_packet_refinc(pkt); |
| 3662 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
| 3663 | } |
| 3664 | } |
| 3665 | |
Frédéric Lécaille | a2c62c3 | 2023-02-10 14:13:43 +0100 | [diff] [blame] | 3666 | ret = 1; |
| 3667 | leave: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3668 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 3669 | |
Frédéric Lécaille | a2c62c3 | 2023-02-10 14:13:43 +0100 | [diff] [blame] | 3670 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | /* Copy into <buf> buffer a stateless reset token depending on the |
| 3674 | * <salt> salt input. This is the cluster secret which will be derived |
| 3675 | * as HKDF input secret to generate this token. |
| 3676 | * Return 1 if succeeded, 0 if not. |
| 3677 | */ |
| 3678 | static int quic_stateless_reset_token_cpy(struct quic_conn *qc, |
| 3679 | unsigned char *buf, size_t len, |
| 3680 | const unsigned char *salt, size_t saltlen) |
| 3681 | { |
| 3682 | /* Input secret */ |
| 3683 | const unsigned char *key = (const unsigned char *)global.cluster_secret; |
| 3684 | size_t keylen = strlen(global.cluster_secret); |
| 3685 | /* Info */ |
| 3686 | const unsigned char label[] = "stateless token"; |
| 3687 | size_t labellen = sizeof label - 1; |
| 3688 | int ret; |
| 3689 | |
| 3690 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3691 | |
| 3692 | ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len, |
| 3693 | key, keylen, salt, saltlen, label, labellen); |
| 3694 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3695 | return ret; |
| 3696 | } |
| 3697 | |
| 3698 | /* Initialize the stateless reset token attached to <cid> connection ID. |
| 3699 | * Returns 1 if succeeded, 0 if not. |
| 3700 | */ |
| 3701 | static int quic_stateless_reset_token_init(struct quic_conn *qc, |
| 3702 | struct quic_connection_id *quic_cid) |
| 3703 | { |
| 3704 | int ret; |
| 3705 | |
| 3706 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3707 | |
| 3708 | if (global.cluster_secret) { |
| 3709 | /* Output secret */ |
| 3710 | unsigned char *token = quic_cid->stateless_reset_token; |
| 3711 | size_t tokenlen = sizeof quic_cid->stateless_reset_token; |
| 3712 | /* Salt */ |
| 3713 | const unsigned char *cid = quic_cid->cid.data; |
| 3714 | size_t cidlen = quic_cid->cid.len; |
| 3715 | |
| 3716 | ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen); |
| 3717 | } |
| 3718 | else { |
| 3719 | /* TODO: RAND_bytes() should be replaced */ |
| 3720 | ret = RAND_bytes(quic_cid->stateless_reset_token, |
| 3721 | sizeof quic_cid->stateless_reset_token) == 1; |
| 3722 | } |
| 3723 | |
| 3724 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3725 | return ret; |
| 3726 | } |
| 3727 | |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame^] | 3728 | /* Allocate a new CID and attach it to <root> ebtree. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 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, |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame^] | 3737 | struct quic_conn *qc) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3738 | { |
| 3739 | struct quic_connection_id *cid; |
| 3740 | |
| 3741 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3742 | |
| 3743 | cid = pool_alloc(pool_head_quic_connection_id); |
| 3744 | if (!cid) { |
| 3745 | TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 3746 | goto err; |
| 3747 | } |
| 3748 | |
| 3749 | cid->cid.len = QUIC_HAP_CID_LEN; |
| 3750 | /* TODO: RAND_bytes() should be replaced */ |
| 3751 | if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) { |
| 3752 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc); |
| 3753 | goto err; |
| 3754 | } |
| 3755 | |
| 3756 | quic_pin_cid_to_tid(cid->cid.data, tid); |
| 3757 | if (quic_stateless_reset_token_init(qc, cid) != 1) { |
| 3758 | TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc); |
| 3759 | goto err; |
| 3760 | } |
| 3761 | |
| 3762 | cid->qc = qc; |
| 3763 | |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame^] | 3764 | cid->seq_num.key = qc->next_cid_seq_num++; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3765 | cid->retire_prior_to = 0; |
| 3766 | /* insert the allocated CID in the quic_conn tree */ |
| 3767 | eb64_insert(root, &cid->seq_num); |
| 3768 | |
| 3769 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3770 | return cid; |
| 3771 | |
| 3772 | err: |
| 3773 | pool_free(pool_head_quic_connection_id, cid); |
| 3774 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3775 | return NULL; |
| 3776 | } |
| 3777 | |
| 3778 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3779 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3780 | * a HANDSHAKE_DONE frame. |
| 3781 | * Return 1 if succeeded, 0 if not. |
| 3782 | */ |
| 3783 | static int quic_build_post_handshake_frames(struct quic_conn *qc) |
| 3784 | { |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame^] | 3785 | int ret = 0, max; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3786 | struct quic_enc_level *qel; |
| 3787 | struct quic_frame *frm, *frmbak; |
| 3788 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3789 | struct eb64_node *node; |
| 3790 | |
| 3791 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 3792 | |
| 3793 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3794 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
| 3795 | if (qc_is_listener(qc)) { |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 3796 | frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3797 | if (!frm) { |
| 3798 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3799 | goto leave; |
| 3800 | } |
| 3801 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3802 | LIST_APPEND(&frm_list, &frm->list); |
| 3803 | } |
| 3804 | |
| 3805 | /* Initialize <max> connection IDs minus one: there is |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame^] | 3806 | * already one connection ID used for the current connection. Also limit |
| 3807 | * the number of connection IDs sent to the peer to 4 (3 from this function |
| 3808 | * plus 1 for the current connection. |
| 3809 | * Note that active_connection_id_limit >= 2: this has been already checked |
| 3810 | * when receiving this parameter. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3811 | */ |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame^] | 3812 | max = QUIC_MIN(qc->tx.params.active_connection_id_limit - 1, (uint64_t)3); |
| 3813 | while (max--) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 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 | |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame^] | 3822 | cid = new_quic_cid(&qc->cids, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 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 | |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame^] | 3849 | /* The first CID sequence number value used to allocated CIDs by this function is 1, |
| 3850 | * 0 being the sequence number of the CID for this connection. |
| 3851 | */ |
| 3852 | node = eb64_lookup_ge(&qc->cids, 1); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3853 | while (node) { |
| 3854 | struct quic_connection_id *cid; |
| 3855 | |
| 3856 | cid = eb64_entry(node, struct quic_connection_id, seq_num); |
| 3857 | if (cid->seq_num.key >= max) |
| 3858 | break; |
| 3859 | |
| 3860 | node = eb64_next(node); |
| 3861 | ebmb_delete(&cid->node); |
| 3862 | eb64_delete(&cid->seq_num); |
| 3863 | pool_free(pool_head_quic_connection_id, cid); |
| 3864 | } |
| 3865 | goto leave; |
| 3866 | } |
| 3867 | |
| 3868 | /* Deallocate <l> list of ACK ranges. */ |
| 3869 | void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs) |
| 3870 | { |
| 3871 | struct eb64_node *n; |
| 3872 | struct quic_arng_node *ar; |
| 3873 | |
| 3874 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 3875 | |
| 3876 | n = eb64_first(&arngs->root); |
| 3877 | while (n) { |
| 3878 | struct eb64_node *next; |
| 3879 | |
| 3880 | ar = eb64_entry(n, struct quic_arng_node, first); |
| 3881 | next = eb64_next(n); |
| 3882 | eb64_delete(n); |
| 3883 | pool_free(pool_head_quic_arng, ar); |
| 3884 | n = next; |
| 3885 | } |
| 3886 | |
| 3887 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 3888 | } |
| 3889 | |
| 3890 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3891 | * descending order. |
| 3892 | */ |
| 3893 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3894 | struct quic_arng_node *q) |
| 3895 | { |
| 3896 | return p->first.key - q->last - 2; |
| 3897 | } |
| 3898 | |
| 3899 | |
| 3900 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3901 | * encoded size until it goes below <limit>. |
| 3902 | * Returns 1 if succeeded, 0 if not (no more element to remove). |
| 3903 | */ |
| 3904 | static int quic_rm_last_ack_ranges(struct quic_conn *qc, |
| 3905 | struct quic_arngs *arngs, size_t limit) |
| 3906 | { |
| 3907 | int ret = 0; |
| 3908 | struct eb64_node *last, *prev; |
| 3909 | |
| 3910 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3911 | |
| 3912 | last = eb64_last(&arngs->root); |
| 3913 | while (last && arngs->enc_sz > limit) { |
| 3914 | struct quic_arng_node *last_node, *prev_node; |
| 3915 | |
| 3916 | prev = eb64_prev(last); |
| 3917 | if (!prev) { |
| 3918 | TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc); |
| 3919 | goto out; |
| 3920 | } |
| 3921 | |
| 3922 | last_node = eb64_entry(last, struct quic_arng_node, first); |
| 3923 | prev_node = eb64_entry(prev, struct quic_arng_node, first); |
| 3924 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3925 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3926 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3927 | --arngs->sz; |
| 3928 | eb64_delete(last); |
| 3929 | pool_free(pool_head_quic_arng, last); |
| 3930 | last = prev; |
| 3931 | } |
| 3932 | |
| 3933 | ret = 1; |
| 3934 | out: |
| 3935 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3936 | return ret; |
| 3937 | } |
| 3938 | |
| 3939 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3940 | static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs) |
| 3941 | { |
| 3942 | struct eb64_node *node, *next; |
| 3943 | struct quic_arng_node *ar, *ar_next; |
| 3944 | |
| 3945 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3946 | |
| 3947 | node = eb64_last(&arngs->root); |
| 3948 | if (!node) |
| 3949 | goto leave; |
| 3950 | |
| 3951 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 3952 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3953 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3954 | |
| 3955 | while ((next = eb64_prev(node))) { |
| 3956 | ar_next = eb64_entry(next, struct quic_arng_node, first); |
| 3957 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3958 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3959 | node = next; |
| 3960 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 3961 | } |
| 3962 | |
| 3963 | leave: |
| 3964 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3965 | } |
| 3966 | |
| 3967 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3968 | * Returns the ack range node which has been inserted if succeeded, NULL if not. |
| 3969 | */ |
| 3970 | static inline |
| 3971 | struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc, |
| 3972 | struct quic_arngs *arngs, |
| 3973 | struct quic_arng *ar) |
| 3974 | { |
| 3975 | struct quic_arng_node *new_ar; |
| 3976 | |
| 3977 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3978 | |
| 3979 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3980 | if (!new_ar) { |
| 3981 | TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc); |
| 3982 | goto leave; |
| 3983 | } |
| 3984 | |
| 3985 | new_ar->first.key = ar->first; |
| 3986 | new_ar->last = ar->last; |
| 3987 | eb64_insert(&arngs->root, &new_ar->first); |
| 3988 | arngs->sz++; |
| 3989 | |
| 3990 | leave: |
| 3991 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 3992 | return new_ar; |
| 3993 | } |
| 3994 | |
| 3995 | /* Update <arngs> tree of ACK ranges with <ar> as new ACK range value. |
| 3996 | * Note that this function computes the number of bytes required to encode |
| 3997 | * this tree of ACK ranges in descending order. |
| 3998 | * |
| 3999 | * Descending order |
| 4000 | * -------------> |
| 4001 | * range1 range2 |
| 4002 | * ..........|--------|..............|--------| |
| 4003 | * ^ ^ ^ ^ |
| 4004 | * | | | | |
| 4005 | * last1 first1 last2 first2 |
| 4006 | * ..........+--------+--------------+--------+...... |
| 4007 | * diff1 gap12 diff2 |
| 4008 | * |
| 4009 | * To encode the previous list of ranges we must encode integers as follows in |
| 4010 | * descending order: |
| 4011 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
| 4012 | * with diff1 = last1 - first1 |
| 4013 | * diff2 = last2 - first2 |
| 4014 | * gap12 = first1 - last2 - 2 (>= 0) |
| 4015 | * |
| 4016 | |
| 4017 | returns 0 on error |
| 4018 | |
| 4019 | */ |
| 4020 | int quic_update_ack_ranges_list(struct quic_conn *qc, |
| 4021 | struct quic_arngs *arngs, |
| 4022 | struct quic_arng *ar) |
| 4023 | { |
| 4024 | int ret = 0; |
| 4025 | struct eb64_node *le; |
| 4026 | struct quic_arng_node *new_node; |
| 4027 | struct eb64_node *new; |
| 4028 | |
| 4029 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 4030 | |
| 4031 | new = NULL; |
| 4032 | if (eb_is_empty(&arngs->root)) { |
| 4033 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 4034 | if (new_node) |
| 4035 | ret = 1; |
| 4036 | |
| 4037 | goto leave; |
| 4038 | } |
| 4039 | |
| 4040 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 4041 | if (!le) { |
| 4042 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 4043 | if (!new_node) |
| 4044 | goto leave; |
| 4045 | |
| 4046 | new = &new_node->first; |
| 4047 | } |
| 4048 | else { |
| 4049 | struct quic_arng_node *le_ar = |
| 4050 | eb64_entry(le, struct quic_arng_node, first); |
| 4051 | |
| 4052 | /* Already existing range */ |
| 4053 | if (le_ar->last >= ar->last) { |
| 4054 | ret = 1; |
| 4055 | } |
| 4056 | else if (le_ar->last + 1 >= ar->first) { |
| 4057 | le_ar->last = ar->last; |
| 4058 | new = le; |
| 4059 | new_node = le_ar; |
| 4060 | } |
| 4061 | else { |
| 4062 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 4063 | if (!new_node) |
| 4064 | goto leave; |
| 4065 | |
| 4066 | new = &new_node->first; |
| 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | /* Verify that the new inserted node does not overlap the nodes |
| 4071 | * which follow it. |
| 4072 | */ |
| 4073 | if (new) { |
| 4074 | struct eb64_node *next; |
| 4075 | struct quic_arng_node *next_node; |
| 4076 | |
| 4077 | while ((next = eb64_next(new))) { |
| 4078 | next_node = |
| 4079 | eb64_entry(next, struct quic_arng_node, first); |
| 4080 | if (new_node->last + 1 < next_node->first.key) |
| 4081 | break; |
| 4082 | |
| 4083 | if (next_node->last > new_node->last) |
| 4084 | new_node->last = next_node->last; |
| 4085 | eb64_delete(next); |
| 4086 | pool_free(pool_head_quic_arng, next_node); |
| 4087 | /* Decrement the size of these ranges. */ |
| 4088 | arngs->sz--; |
| 4089 | } |
| 4090 | } |
| 4091 | |
| 4092 | ret = 1; |
| 4093 | leave: |
| 4094 | quic_arngs_set_enc_sz(qc, arngs); |
| 4095 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 4096 | return ret; |
| 4097 | } |
| 4098 | /* Remove the header protection of packets at <el> encryption level. |
| 4099 | * Always succeeds. |
| 4100 | */ |
| 4101 | static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el) |
| 4102 | { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4103 | struct quic_rx_packet *pqpkt, *pkttmp; |
| 4104 | struct quic_enc_level *app_qel; |
| 4105 | |
| 4106 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 4107 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4108 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
| 4109 | if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) { |
| 4110 | TRACE_DEVEL("hp not removed (handshake not completed)", |
| 4111 | QUIC_EV_CONN_ELRMHP, qc); |
| 4112 | goto out; |
| 4113 | } |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 4114 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4115 | 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] | 4116 | struct quic_tls_ctx *tls_ctx; |
| 4117 | |
| 4118 | tls_ctx = qc_select_tls_ctx(qc, el, pqpkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4119 | if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
| 4120 | pqpkt->data + pqpkt->pn_offset, pqpkt->data)) { |
| 4121 | TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc); |
| 4122 | } |
| 4123 | else { |
| 4124 | /* The AAD includes the packet number field */ |
| 4125 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 4126 | /* Store the packet into the tree of packets to decrypt. */ |
| 4127 | pqpkt->pn_node.key = pqpkt->pn; |
| 4128 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 4129 | quic_rx_packet_refinc(pqpkt); |
| 4130 | TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt); |
| 4131 | } |
| 4132 | LIST_DELETE(&pqpkt->list); |
| 4133 | quic_rx_packet_refdec(pqpkt); |
| 4134 | } |
| 4135 | |
| 4136 | out: |
| 4137 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
| 4138 | } |
| 4139 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4140 | /* Process all the CRYPTO frame at <el> encryption level. This is the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4141 | * 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] | 4142 | * stream for this level. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4143 | * Return 1 if succeeded, 0 if not. |
| 4144 | */ |
| 4145 | static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc, |
| 4146 | struct quic_enc_level *el, |
| 4147 | struct ssl_sock_ctx *ctx) |
| 4148 | { |
| 4149 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4150 | struct ncbuf *ncbuf; |
| 4151 | struct quic_cstream *cstream = el->cstream; |
| 4152 | ncb_sz_t data; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4153 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4154 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4155 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4156 | BUG_ON(!cstream); |
| 4157 | ncbuf = &cstream->rx.ncbuf; |
| 4158 | if (ncb_is_null(ncbuf)) |
| 4159 | goto done; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4160 | |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4161 | /* TODO not working if buffer is wrapping */ |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4162 | while ((data = ncb_data(ncbuf, 0))) { |
| 4163 | const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf); |
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 | if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4166 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4167 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4168 | cstream->rx.offset += data; |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4169 | TRACE_DEVEL("buffered crypto data were provided to TLS stack", |
| 4170 | QUIC_EV_CONN_PHPKTS, qc, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4171 | } |
| 4172 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4173 | done: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4174 | ret = 1; |
| 4175 | leave: |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4176 | if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) { |
| 4177 | 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] | 4178 | quic_free_ncbuf(ncbuf); |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 4179 | } |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4180 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4181 | return ret; |
| 4182 | } |
| 4183 | |
| 4184 | /* Process all the packets at <el> and <next_el> encryption level. |
| 4185 | * This is the caller responsibility to check that <cur_el> is different of <next_el> |
| 4186 | * as pointer value. |
| 4187 | * Return 1 if succeeded, 0 if not. |
| 4188 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4189 | 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] | 4190 | struct quic_enc_level *next_el) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4191 | { |
| 4192 | int ret = 0; |
| 4193 | struct eb64_node *node; |
| 4194 | int64_t largest_pn = -1; |
| 4195 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4196 | struct quic_enc_level *qel = cur_el; |
| 4197 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4198 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4199 | qel = cur_el; |
| 4200 | next_tel: |
| 4201 | if (!qel) |
| 4202 | goto out; |
| 4203 | |
| 4204 | node = eb64_first(&qel->rx.pkts); |
| 4205 | while (node) { |
| 4206 | struct quic_rx_packet *pkt; |
| 4207 | |
| 4208 | pkt = eb64_entry(node, struct quic_rx_packet, pn_node); |
| 4209 | TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT, |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4210 | qc, pkt, NULL, qc->xprt_ctx->ssl); |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 4211 | if (!qc_pkt_decrypt(qc, qel, pkt)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4212 | /* Drop the packet */ |
| 4213 | TRACE_ERROR("packet decryption failed -> dropped", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4214 | QUIC_EV_CONN_RXPKT, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4215 | } |
| 4216 | else { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4217 | if (!qc_parse_pkt_frms(qc, pkt, qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4218 | /* Drop the packet */ |
| 4219 | TRACE_ERROR("packet parsing failed -> dropped", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4220 | QUIC_EV_CONN_RXPKT, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4221 | HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing); |
| 4222 | } |
| 4223 | else { |
| 4224 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 4225 | |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 4226 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4227 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 4228 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
| 4229 | qc_idle_timer_rearm(qc, 1); |
| 4230 | } |
| 4231 | if (pkt->pn > largest_pn) { |
| 4232 | largest_pn = pkt->pn; |
| 4233 | largest_pn_time_received = pkt->time_received; |
| 4234 | } |
| 4235 | /* Update the list of ranges to acknowledge. */ |
| 4236 | if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar)) |
| 4237 | TRACE_ERROR("Could not update ack range list", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4238 | QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4239 | } |
| 4240 | } |
| 4241 | node = eb64_next(node); |
| 4242 | eb64_delete(&pkt->pn_node); |
| 4243 | quic_rx_packet_refdec(pkt); |
| 4244 | } |
| 4245 | |
| 4246 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 4247 | /* Update the largest packet number. */ |
| 4248 | qel->pktns->rx.largest_pn = largest_pn; |
| 4249 | /* Update the largest acknowledged packet timestamps */ |
| 4250 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 4251 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 4252 | } |
| 4253 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 4254 | 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] | 4255 | // trace already emitted by function above |
| 4256 | goto leave; |
| 4257 | } |
| 4258 | |
| 4259 | if (qel == cur_el) { |
| 4260 | BUG_ON(qel == next_el); |
| 4261 | qel = next_el; |
| 4262 | largest_pn = -1; |
| 4263 | goto next_tel; |
| 4264 | } |
| 4265 | |
| 4266 | out: |
| 4267 | ret = 1; |
| 4268 | leave: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4269 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4270 | return ret; |
| 4271 | } |
| 4272 | |
| 4273 | /* Check if it's possible to remove header protection for packets related to |
| 4274 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 4275 | * |
| 4276 | * Return true if the operation is possible else false. |
| 4277 | */ |
| 4278 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4279 | { |
| 4280 | int ret = 0; |
| 4281 | enum quic_tls_enc_level tel; |
| 4282 | |
| 4283 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
| 4284 | |
| 4285 | if (!qel) |
| 4286 | goto cant_rm_hp; |
| 4287 | |
| 4288 | tel = ssl_to_quic_enc_level(qel->level); |
| 4289 | |
| 4290 | /* check if tls secrets are available */ |
| 4291 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 4292 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
| 4293 | goto cant_rm_hp; |
| 4294 | } |
| 4295 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4296 | if (!quic_tls_has_rx_sec(qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4297 | TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc); |
| 4298 | goto cant_rm_hp; |
| 4299 | } |
| 4300 | |
Frédéric Lécaille | 8417beb | 2023-02-01 10:31:35 +0100 | [diff] [blame] | 4301 | if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) { |
| 4302 | TRACE_DEVEL("handshake not complete", QUIC_EV_CONN_TRMHP, qc); |
| 4303 | goto cant_rm_hp; |
| 4304 | } |
| 4305 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4306 | /* check if the connection layer is ready before using app level */ |
| 4307 | if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) && |
| 4308 | qc->mux_state == QC_MUX_NULL) { |
| 4309 | TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc); |
| 4310 | goto cant_rm_hp; |
| 4311 | } |
| 4312 | |
| 4313 | ret = 1; |
| 4314 | cant_rm_hp: |
| 4315 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc); |
| 4316 | return ret; |
| 4317 | } |
| 4318 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 4319 | /* Flush txbuf for <qc> connection. This must be called prior to a packet |
| 4320 | * preparation when txbuf contains older data. A send will be conducted for |
| 4321 | * these data. |
| 4322 | * |
| 4323 | * Returns 1 on success : buffer is empty and can be use for packet |
| 4324 | * preparation. On error 0 is returned. |
| 4325 | */ |
| 4326 | static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf) |
| 4327 | { |
| 4328 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4329 | |
| 4330 | /* This operation can only be conducted if txbuf is not empty. This |
| 4331 | * case only happens for connection with their owned socket due to an |
| 4332 | * older transient sendto() error. |
| 4333 | */ |
| 4334 | BUG_ON(!qc_test_fd(qc)); |
| 4335 | |
| 4336 | if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) { |
| 4337 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 4338 | qc_txb_release(qc); |
| 4339 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc); |
| 4340 | return 0; |
| 4341 | } |
| 4342 | |
| 4343 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4344 | return 1; |
| 4345 | } |
| 4346 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4347 | /* Try to send application frames from list <frms> on connection <qc>. |
| 4348 | * |
| 4349 | * Use qc_send_app_probing wrapper when probing with old data. |
| 4350 | * |
| 4351 | * Returns 1 on success. Some data might not have been sent due to congestion, |
| 4352 | * in this case they are left in <frms> input list. The caller may subscribe on |
| 4353 | * quic-conn to retry later. |
| 4354 | * |
| 4355 | * Returns 0 on critical error. |
| 4356 | * TODO review and classify more distinctly transient from definitive errors to |
| 4357 | * allow callers to properly handle it. |
| 4358 | */ |
| 4359 | static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms) |
| 4360 | { |
| 4361 | int status = 0; |
| 4362 | struct buffer *buf; |
| 4363 | |
| 4364 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4365 | |
| 4366 | buf = qc_txb_alloc(qc); |
| 4367 | if (!buf) { |
| 4368 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4369 | goto err; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4370 | } |
| 4371 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 4372 | if (b_data(buf) && !qc_purge_txbuf(qc, buf)) |
| 4373 | goto err; |
| 4374 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4375 | /* Prepare and send packets until we could not further prepare packets. */ |
| 4376 | while (1) { |
| 4377 | int ret; |
| 4378 | /* Currently buf cannot be non-empty at this stage. Even if a |
| 4379 | * previous sendto() has failed it is emptied to simulate |
| 4380 | * packet emission and rely on QUIC lost detection to try to |
| 4381 | * emit it. |
| 4382 | */ |
| 4383 | BUG_ON_HOT(b_data(buf)); |
| 4384 | b_reset(buf); |
| 4385 | |
| 4386 | ret = qc_prep_app_pkts(qc, buf, frms); |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4387 | if (ret == -1) { |
| 4388 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4389 | goto err; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4390 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4391 | |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4392 | if (!ret) |
| 4393 | break; |
| 4394 | |
| 4395 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 4396 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 4397 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4398 | goto err; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4399 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4400 | } |
| 4401 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4402 | status = 1; |
| 4403 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4404 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4405 | return status; |
| 4406 | |
| 4407 | err: |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4408 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc); |
| 4409 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | /* Try to send application frames from list <frms> on connection <qc>. Use this |
| 4413 | * function when probing is required. |
| 4414 | * |
| 4415 | * Returns the result from qc_send_app_pkts function. |
| 4416 | */ |
| 4417 | static forceinline int qc_send_app_probing(struct quic_conn *qc, |
| 4418 | struct list *frms) |
| 4419 | { |
| 4420 | int ret; |
| 4421 | |
| 4422 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4423 | |
| 4424 | TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc); |
| 4425 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4426 | ret = qc_send_app_pkts(qc, frms); |
| 4427 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4428 | |
| 4429 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4430 | return ret; |
| 4431 | } |
| 4432 | |
| 4433 | /* Try to send application frames from list <frms> on connection <qc>. This |
| 4434 | * function is provided for MUX upper layer usage only. |
| 4435 | * |
| 4436 | * Returns the result from qc_send_app_pkts function. |
| 4437 | */ |
| 4438 | int qc_send_mux(struct quic_conn *qc, struct list *frms) |
| 4439 | { |
| 4440 | int ret; |
| 4441 | |
| 4442 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4443 | BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */ |
| 4444 | |
| 4445 | TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc); |
| 4446 | qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4447 | ret = qc_send_app_pkts(qc, frms); |
| 4448 | qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4449 | |
| 4450 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4451 | return ret; |
| 4452 | } |
| 4453 | |
| 4454 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 4455 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
| 4456 | * QUIC connection. <old_data> is used as boolean to send data already sent but |
| 4457 | * not already acknowledged (in flight). |
| 4458 | * Returns 1 if succeeded, 0 if not. |
| 4459 | */ |
| 4460 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 4461 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 4462 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 4463 | { |
| 4464 | int ret, status = 0; |
| 4465 | struct buffer *buf = qc_txb_alloc(qc); |
| 4466 | |
| 4467 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4468 | |
| 4469 | if (!buf) { |
| 4470 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 4471 | goto leave; |
| 4472 | } |
| 4473 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 4474 | if (b_data(buf) && !qc_purge_txbuf(qc, buf)) |
| 4475 | goto out; |
| 4476 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4477 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4478 | * sendto() has failed it is emptied to simulate packet emission and |
| 4479 | * rely on QUIC lost detection to try to emit it. |
| 4480 | */ |
| 4481 | BUG_ON_HOT(b_data(buf)); |
| 4482 | b_reset(buf); |
| 4483 | |
| 4484 | if (old_data) { |
| 4485 | TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc); |
| 4486 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4487 | } |
| 4488 | |
| 4489 | 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] | 4490 | if (ret == -1) { |
| 4491 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4492 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4493 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4494 | |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4495 | if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 4496 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 4497 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4498 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4499 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4500 | |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4501 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4502 | status = 1; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4503 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4504 | out: |
| 4505 | TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc); |
| 4506 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4507 | leave: |
| 4508 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4509 | return status; |
| 4510 | } |
| 4511 | |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4512 | /* Retransmit up to two datagrams depending on packet number space. |
| 4513 | * Return 0 when failed, 0 if not. |
| 4514 | */ |
| 4515 | static int qc_dgrams_retransmit(struct quic_conn *qc) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4516 | { |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4517 | int ret = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4518 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4519 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4520 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4521 | |
| 4522 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4523 | |
| 4524 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4525 | int i; |
| 4526 | |
| 4527 | for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) { |
| 4528 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 4529 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4530 | |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4531 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
| 4532 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 4533 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 4534 | if (!LIST_ISEMPTY(&ifrms)) { |
| 4535 | iqel->pktns->tx.pto_probe = 1; |
| 4536 | if (!LIST_ISEMPTY(&hfrms)) |
| 4537 | hqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4538 | if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 4539 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms)) |
| 4540 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4541 | /* Put back unsent frames in their packet number spaces */ |
| 4542 | LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms); |
| 4543 | LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms); |
| 4544 | } |
Frédéric Lécaille | ec93721 | 2023-03-03 17:34:41 +0100 | [diff] [blame] | 4545 | else { |
| 4546 | if (!(qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
| 4547 | iqel->pktns->tx.pto_probe = 1; |
| 4548 | if (!qc_send_hdshk_pkts(qc, 0, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 4549 | QUIC_TLS_ENC_LEVEL_NONE, NULL)) |
| 4550 | goto leave; |
| 4551 | } |
| 4552 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4553 | } |
| 4554 | TRACE_STATE("no more need to probe Initial packet number space", |
| 4555 | QUIC_EV_CONN_TXPKT, qc); |
| 4556 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 4557 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4558 | } |
| 4559 | else { |
| 4560 | int i; |
| 4561 | |
| 4562 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4563 | hqel->pktns->tx.pto_probe = 0; |
| 4564 | 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] | 4565 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4566 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4567 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
| 4568 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4569 | if (!LIST_ISEMPTY(&frms1)) { |
| 4570 | hqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4571 | if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 4572 | QUIC_TLS_ENC_LEVEL_NONE, NULL)) |
| 4573 | goto leave; |
| 4574 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4575 | /* Put back unsent frames into their packet number spaces */ |
| 4576 | LIST_SPLICE(&hqel->pktns->tx.frms, &frms1); |
| 4577 | } |
| 4578 | } |
| 4579 | TRACE_STATE("no more need to probe Handshake packet number space", |
| 4580 | QUIC_EV_CONN_TXPKT, qc); |
| 4581 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4582 | } |
| 4583 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4584 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 4585 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4586 | |
| 4587 | aqel->pktns->tx.pto_probe = 0; |
| 4588 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 4589 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4590 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 4591 | if (!LIST_ISEMPTY(&frms1)) { |
| 4592 | aqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4593 | if (!qc_send_app_probing(qc, &frms1)) |
| 4594 | goto leave; |
| 4595 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4596 | /* Put back unsent frames into their packet number spaces */ |
| 4597 | LIST_SPLICE(&aqel->pktns->tx.frms, &frms1); |
| 4598 | } |
| 4599 | if (!LIST_ISEMPTY(&frms2)) { |
| 4600 | aqel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4601 | if (!qc_send_app_probing(qc, &frms2)) |
| 4602 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4603 | /* Put back unsent frames into their packet number spaces */ |
| 4604 | LIST_SPLICE(&aqel->pktns->tx.frms, &frms2); |
| 4605 | } |
| 4606 | TRACE_STATE("no more need to probe 01RTT packet number space", |
| 4607 | QUIC_EV_CONN_TXPKT, qc); |
| 4608 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4609 | } |
| 4610 | } |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4611 | |
| 4612 | ret = 1; |
| 4613 | leave: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4614 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4615 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4616 | } |
| 4617 | |
| 4618 | /* QUIC connection packet handler task (post handshake) */ |
| 4619 | struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 4620 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4621 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4622 | struct quic_enc_level *qel; |
| 4623 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4624 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4625 | |
| 4626 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 4627 | TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state); |
| 4628 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 4629 | if (qc_test_fd(qc)) |
| 4630 | qc_rcv_buf(qc); |
| 4631 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4632 | /* Retranmissions */ |
| 4633 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 4634 | TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
| 4635 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4636 | if (!qc_dgrams_retransmit(qc)) |
| 4637 | goto out; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4638 | } |
| 4639 | |
| 4640 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 4641 | qc_rm_hp_pkts(qc, qel); |
| 4642 | |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 4643 | if (!qc_treat_rx_pkts(qc, qel, NULL)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4644 | TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4645 | goto out; |
| 4646 | } |
| 4647 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 4648 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 4649 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc); |
| 4650 | goto out; |
| 4651 | } |
| 4652 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4653 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4654 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) { |
| 4655 | TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc); |
| 4656 | goto out; |
| 4657 | } |
| 4658 | |
| 4659 | /* XXX TODO: how to limit the list frames to send */ |
| 4660 | if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) { |
| 4661 | TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4662 | goto out; |
| 4663 | } |
| 4664 | |
| 4665 | out: |
| 4666 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 4667 | return t; |
| 4668 | } |
| 4669 | |
| 4670 | /* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */ |
| 4671 | static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4672 | { |
| 4673 | return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) || |
| 4674 | (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) || |
| 4675 | qel->pktns->tx.pto_probe || |
| 4676 | !LIST_ISEMPTY(&qel->pktns->tx.frms); |
| 4677 | } |
| 4678 | |
| 4679 | /* QUIC connection packet handler task. */ |
| 4680 | struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state) |
| 4681 | { |
| 4682 | int ret, ssl_err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4683 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4684 | enum quic_tls_enc_level tel, next_tel; |
| 4685 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4686 | /* Early-data encryption level */ |
| 4687 | struct quic_enc_level *eqel; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4688 | struct buffer *buf = NULL; |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 4689 | int st, zero_rtt; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4690 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4691 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4692 | eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4693 | st = qc->state; |
| 4694 | TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st); |
| 4695 | |
| 4696 | /* Retranmissions */ |
| 4697 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 4698 | TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
| 4699 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
Frédéric Lécaille | e1738df | 2023-02-10 14:46:39 +0100 | [diff] [blame] | 4700 | if (!qc_dgrams_retransmit(qc)) |
| 4701 | goto out; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4702 | } |
| 4703 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4704 | ssl_err = SSL_ERROR_NONE; |
| 4705 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4706 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4707 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 4708 | |
| 4709 | if (qc_test_fd(qc)) |
| 4710 | qc_rcv_buf(qc); |
| 4711 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4712 | if (st >= QUIC_HS_ST_COMPLETE && |
| 4713 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 4714 | TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 4715 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 4716 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 4717 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 4718 | } |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4719 | 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] | 4720 | goto out; |
| 4721 | |
| 4722 | qel = &qc->els[tel]; |
| 4723 | next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel]; |
| 4724 | |
| 4725 | next_level: |
| 4726 | /* Treat packets waiting for header packet protection decryption */ |
| 4727 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 4728 | qc_rm_hp_pkts(qc, qel); |
| 4729 | |
Frédéric Lécaille | b3562a3 | 2023-02-25 11:27:34 +0100 | [diff] [blame] | 4730 | if (!qc_treat_rx_pkts(qc, qel, next_qel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4731 | goto out; |
| 4732 | |
Frédéric Lécaille | 0aa7995 | 2023-02-03 16:15:08 +0100 | [diff] [blame] | 4733 | if (qc->flags & QUIC_FL_CONN_TO_KILL) { |
| 4734 | TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc); |
| 4735 | goto out; |
| 4736 | } |
| 4737 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4738 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4739 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 4740 | goto out; |
| 4741 | |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4742 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4743 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4744 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
| 4745 | if (next_qel && next_qel == eqel && zero_rtt) { |
| 4746 | TRACE_DEVEL("select 0RTT as next encryption level", |
| 4747 | QUIC_EV_CONN_PHPKTS, qc); |
| 4748 | qel = next_qel; |
| 4749 | next_qel = NULL; |
| 4750 | goto next_level; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | st = qc->state; |
| 4754 | if (st >= QUIC_HS_ST_COMPLETE) { |
| 4755 | if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) && |
| 4756 | !quic_build_post_handshake_frames(qc)) |
| 4757 | goto out; |
| 4758 | |
| 4759 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags & |
| 4760 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 4761 | /* Discard the Handshake keys. */ |
| 4762 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4763 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 4764 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 4765 | qc_set_timer(qc); |
| 4766 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4767 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
| 4768 | } |
| 4769 | |
| 4770 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 4771 | /* There may be remaining handshake to build (acks) */ |
| 4772 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 4773 | } |
| 4774 | } |
| 4775 | |
| 4776 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 4777 | * be considered. |
| 4778 | */ |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4779 | if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4780 | goto out; |
| 4781 | |
| 4782 | if (!qc_need_sending(qc, qel) && |
| 4783 | (!next_qel || !qc_need_sending(qc, next_qel))) { |
| 4784 | goto skip_send; |
| 4785 | } |
| 4786 | |
| 4787 | buf = qc_txb_alloc(qc); |
| 4788 | if (!buf) |
| 4789 | goto out; |
| 4790 | |
Amaury Denoyelle | 147862d | 2023-02-28 15:10:00 +0100 | [diff] [blame] | 4791 | if (b_data(buf) && !qc_purge_txbuf(qc, buf)) |
| 4792 | goto skip_send; |
| 4793 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4794 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4795 | * sendto() has failed it is emptied to simulate packet emission and |
| 4796 | * rely on QUIC lost detection to try to emit it. |
| 4797 | */ |
| 4798 | BUG_ON_HOT(b_data(buf)); |
| 4799 | b_reset(buf); |
| 4800 | |
| 4801 | ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms, |
| 4802 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4803 | if (ret == -1) { |
| 4804 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4805 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4806 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4807 | |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4808 | if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) { |
Amaury Denoyelle | e1a0ee3 | 2023-02-28 15:11:09 +0100 | [diff] [blame] | 4809 | if (qc->flags & QUIC_FL_CONN_TO_KILL) |
| 4810 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4811 | goto out; |
Amaury Denoyelle | 3733386 | 2023-02-28 11:53:48 +0100 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | qc_txb_release(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4815 | |
| 4816 | skip_send: |
| 4817 | /* Check if there is something to do for the next level. |
| 4818 | */ |
| 4819 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4820 | quic_tls_has_rx_sec(next_qel) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4821 | (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) { |
| 4822 | qel = next_qel; |
| 4823 | next_qel = NULL; |
| 4824 | goto next_level; |
| 4825 | } |
| 4826 | |
| 4827 | out: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4828 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err); |
| 4829 | return t; |
| 4830 | } |
| 4831 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4832 | /* Release the memory allocated for <cs> CRYPTO stream */ |
| 4833 | void quic_cstream_free(struct quic_cstream *cs) |
| 4834 | { |
| 4835 | if (!cs) { |
| 4836 | /* This is the case for ORTT encryption level */ |
| 4837 | return; |
| 4838 | } |
| 4839 | |
Amaury Denoyelle | bc174b2 | 2022-11-17 10:12:52 +0100 | [diff] [blame] | 4840 | quic_free_ncbuf(&cs->rx.ncbuf); |
| 4841 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4842 | qc_stream_desc_release(cs->desc); |
| 4843 | pool_free(pool_head_quic_cstream, cs); |
| 4844 | } |
| 4845 | |
| 4846 | /* Allocate a new QUIC stream for <qc>. |
| 4847 | * Return it if succeeded, NULL if not. |
| 4848 | */ |
| 4849 | struct quic_cstream *quic_cstream_new(struct quic_conn *qc) |
| 4850 | { |
| 4851 | struct quic_cstream *cs, *ret_cs = NULL; |
| 4852 | |
| 4853 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 4854 | cs = pool_alloc(pool_head_quic_cstream); |
| 4855 | if (!cs) { |
| 4856 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 4857 | goto leave; |
| 4858 | } |
| 4859 | |
| 4860 | cs->rx.offset = 0; |
| 4861 | cs->rx.ncbuf = NCBUF_NULL; |
| 4862 | cs->rx.offset = 0; |
| 4863 | |
| 4864 | cs->tx.offset = 0; |
| 4865 | cs->tx.sent_offset = 0; |
| 4866 | cs->tx.buf = BUF_NULL; |
| 4867 | cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc); |
| 4868 | if (!cs->desc) { |
| 4869 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 4870 | goto err; |
| 4871 | } |
| 4872 | |
| 4873 | ret_cs = cs; |
| 4874 | leave: |
| 4875 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 4876 | return ret_cs; |
| 4877 | |
| 4878 | err: |
| 4879 | pool_free(pool_head_quic_cstream, cs); |
| 4880 | goto leave; |
| 4881 | } |
| 4882 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4883 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
| 4884 | static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4885 | { |
| 4886 | int i; |
| 4887 | |
| 4888 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4889 | |
| 4890 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4891 | if (qel->tx.crypto.bufs[i]) { |
| 4892 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4893 | qel->tx.crypto.bufs[i] = NULL; |
| 4894 | } |
| 4895 | } |
| 4896 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4897 | quic_cstream_free(qel->cstream); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4898 | |
| 4899 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4900 | } |
| 4901 | |
| 4902 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
| 4903 | * connection allocating everything needed. |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4904 | * |
| 4905 | * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use |
| 4906 | * quic_conn_enc_level_uninit() to cleanup partially allocated content. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4907 | */ |
| 4908 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4909 | enum quic_tls_enc_level level) |
| 4910 | { |
| 4911 | int ret = 0; |
| 4912 | struct quic_enc_level *qel; |
| 4913 | |
| 4914 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4915 | |
| 4916 | qel = &qc->els[level]; |
| 4917 | qel->level = quic_to_ssl_enc_level(level); |
| 4918 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4919 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4920 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 4921 | qel->tls_ctx.flags = 0; |
| 4922 | |
| 4923 | qel->rx.pkts = EB_ROOT; |
| 4924 | LIST_INIT(&qel->rx.pqpkts); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4925 | |
| 4926 | /* Allocate only one buffer. */ |
| 4927 | /* TODO: use a pool */ |
| 4928 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4929 | if (!qel->tx.crypto.bufs) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4930 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4931 | |
| 4932 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4933 | if (!qel->tx.crypto.bufs[0]) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4934 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4935 | |
| 4936 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4937 | qel->tx.crypto.nb_buf = 1; |
| 4938 | |
| 4939 | qel->tx.crypto.sz = 0; |
| 4940 | qel->tx.crypto.offset = 0; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4941 | /* No CRYPTO data for early data TLS encryption level */ |
| 4942 | if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA) |
| 4943 | qel->cstream = NULL; |
| 4944 | else { |
| 4945 | qel->cstream = quic_cstream_new(qc); |
| 4946 | if (!qel->cstream) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4947 | goto leave; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4948 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4949 | |
| 4950 | ret = 1; |
| 4951 | leave: |
| 4952 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4953 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4954 | } |
| 4955 | |
Frédéric Lécaille | 7c6d8f8 | 2023-02-14 16:00:18 +0100 | [diff] [blame] | 4956 | /* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not. |
| 4957 | * This is not the case if the remote peer address is not validated and if |
| 4958 | * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes. |
| 4959 | */ |
| 4960 | static int qc_may_probe_ipktns(struct quic_conn *qc) |
| 4961 | { |
| 4962 | return quic_peer_validated_addr(qc) || |
| 4963 | (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN; |
| 4964 | } |
| 4965 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4966 | /* Callback called upon loss detection and PTO timer expirations. */ |
| 4967 | struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state) |
| 4968 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4969 | struct quic_conn *qc = ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4970 | struct quic_pktns *pktns; |
| 4971 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4972 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
| 4973 | NULL, NULL, &qc->path->ifae_pkts); |
| 4974 | task->expire = TICK_ETERNITY; |
| 4975 | pktns = quic_loss_pktns(qc); |
| 4976 | if (tick_isset(pktns->tx.loss_time)) { |
| 4977 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4978 | |
| 4979 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 4980 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4981 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | e4abb1f | 2023-01-27 17:54:15 +0100 | [diff] [blame] | 4982 | if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms)) |
| 4983 | qc_set_timer(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4984 | goto out; |
| 4985 | } |
| 4986 | |
| 4987 | if (qc->path->in_flight) { |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 4988 | pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL); |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 4989 | if (!qc_notify_send(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4990 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
Frédéric Lécaille | 7c6d8f8 | 2023-02-14 16:00:18 +0100 | [diff] [blame] | 4991 | if (qc_may_probe_ipktns(qc)) { |
| 4992 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4993 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4994 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4995 | } |
| 4996 | else { |
| 4997 | TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4998 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4999 | 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] | 5000 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5001 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 5002 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 5003 | } |
| 5004 | } |
| 5005 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) { |
| 5006 | 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] | 5007 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 5008 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 5009 | 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] | 5010 | if (qc_may_probe_ipktns(qc)) { |
| 5011 | qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 5012 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 5013 | } |
| 5014 | else { |
| 5015 | TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 5016 | } |
Frédéric Lécaille | 37ed4a3 | 2023-01-31 17:32:06 +0100 | [diff] [blame] | 5017 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5018 | } |
| 5019 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) { |
| 5020 | 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] | 5021 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 5022 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5023 | } |
| 5024 | } |
| 5025 | } |
| 5026 | else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) { |
| 5027 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 5028 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 5029 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 5030 | if (quic_tls_has_tx_sec(hel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5031 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 5032 | if (quic_tls_has_tx_sec(iel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5033 | iel->pktns->tx.pto_probe = 1; |
| 5034 | } |
| 5035 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5036 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5037 | qc->path->loss.pto_count++; |
| 5038 | |
| 5039 | out: |
| 5040 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
| 5041 | |
| 5042 | return task; |
| 5043 | } |
| 5044 | |
| 5045 | /* Parse the Retry token from buffer <token> with <end> a pointer to |
| 5046 | * one byte past the end of this buffer. This will extract the ODCID |
| 5047 | * which will be stored into <odcid> |
| 5048 | * |
| 5049 | * Returns 0 on success else non-zero. |
| 5050 | */ |
| 5051 | static int parse_retry_token(struct quic_conn *qc, |
| 5052 | const unsigned char *token, const unsigned char *end, |
| 5053 | struct quic_cid *odcid) |
| 5054 | { |
| 5055 | int ret = 0; |
| 5056 | uint64_t odcid_len; |
| 5057 | uint32_t timestamp; |
| 5058 | |
| 5059 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 5060 | |
| 5061 | if (!quic_dec_int(&odcid_len, &token, end)) { |
| 5062 | TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc); |
| 5063 | goto leave; |
| 5064 | } |
| 5065 | |
| 5066 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 5067 | * When an Initial packet is sent by a client that has not previously |
| 5068 | * received an Initial or Retry packet from the server, the client |
| 5069 | * populates the Destination Connection ID field with an unpredictable |
| 5070 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 5071 | */ |
| 5072 | if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) { |
| 5073 | TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 5074 | goto leave; |
| 5075 | } |
| 5076 | |
| 5077 | if (end - token < odcid_len + sizeof timestamp) { |
| 5078 | TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 5079 | goto leave; |
| 5080 | } |
| 5081 | |
| 5082 | timestamp = ntohl(read_u32(token + odcid_len)); |
| 5083 | if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) { |
| 5084 | TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc); |
| 5085 | goto leave; |
| 5086 | } |
| 5087 | |
| 5088 | ret = 1; |
| 5089 | memcpy(odcid->data, token, odcid_len); |
| 5090 | odcid->len = odcid_len; |
| 5091 | leave: |
| 5092 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 5093 | return !ret; |
| 5094 | } |
| 5095 | |
| 5096 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 5097 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 5098 | * for QUIC servers (or haproxy listeners). |
| 5099 | * <dcid> is the destination connection ID, <scid> is the source connection ID, |
| 5100 | * <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] | 5101 | * length. Endpoints addresses are specified via <local_addr> and <peer_addr>. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5102 | * Returns the connection if succeeded, NULL if not. |
| 5103 | */ |
| 5104 | static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4, |
| 5105 | struct quic_cid *dcid, struct quic_cid *scid, |
| 5106 | const struct quic_cid *token_odcid, |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 5107 | struct sockaddr_storage *local_addr, |
| 5108 | struct sockaddr_storage *peer_addr, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5109 | int server, int token, void *owner) |
| 5110 | { |
| 5111 | int i; |
| 5112 | struct quic_conn *qc; |
| 5113 | /* Initial CID. */ |
| 5114 | struct quic_connection_id *icid; |
| 5115 | char *buf_area = NULL; |
| 5116 | struct listener *l = NULL; |
| 5117 | struct quic_cc_algo *cc_algo = NULL; |
| 5118 | struct quic_tls_ctx *ictx; |
| 5119 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5120 | /* TODO replace pool_zalloc by pool_alloc(). This requires special care |
| 5121 | * to properly initialized internal quic_conn members to safely use |
| 5122 | * quic_conn_release() on alloc failure. |
| 5123 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5124 | qc = pool_zalloc(pool_head_quic_conn); |
| 5125 | if (!qc) { |
| 5126 | TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 5127 | goto err; |
| 5128 | } |
| 5129 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5130 | /* Initialize in priority qc members required for a safe dealloc. */ |
| 5131 | |
| 5132 | /* required to use MTLIST_IN_LIST */ |
| 5133 | MT_LIST_INIT(&qc->accept_list); |
| 5134 | |
| 5135 | LIST_INIT(&qc->rx.pkt_list); |
| 5136 | |
Amaury Denoyelle | 4244833 | 2022-12-12 11:24:05 +0100 | [diff] [blame] | 5137 | qc_init_fd(qc); |
| 5138 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5139 | LIST_INIT(&qc->back_refs); |
| 5140 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5141 | /* Now proceeds to allocation of qc members. */ |
| 5142 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5143 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 5144 | if (!buf_area) { |
| 5145 | TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc); |
| 5146 | goto err; |
| 5147 | } |
| 5148 | |
| 5149 | qc->cids = EB_ROOT; |
| 5150 | /* QUIC Server (or listener). */ |
| 5151 | if (server) { |
| 5152 | struct proxy *prx; |
| 5153 | |
| 5154 | l = owner; |
| 5155 | prx = l->bind_conf->frontend; |
| 5156 | cc_algo = l->bind_conf->quic_cc_algo; |
| 5157 | |
| 5158 | qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 5159 | &quic_stats_module); |
| 5160 | qc->flags |= QUIC_FL_CONN_LISTENER; |
| 5161 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
| 5162 | /* Copy the initial DCID with the address. */ |
| 5163 | qc->odcid.len = dcid->len; |
| 5164 | qc->odcid.addrlen = dcid->addrlen; |
| 5165 | memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen); |
| 5166 | |
| 5167 | /* copy the packet SCID to reuse it as DCID for sending */ |
| 5168 | if (scid->len) |
| 5169 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 5170 | qc->dcid.len = scid->len; |
| 5171 | qc->tx.buf = BUF_NULL; |
| 5172 | qc->li = l; |
| 5173 | } |
| 5174 | /* QUIC Client (outgoing connection to servers) */ |
| 5175 | else { |
| 5176 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
| 5177 | if (dcid->len) |
| 5178 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 5179 | qc->dcid.len = dcid->len; |
| 5180 | } |
| 5181 | qc->mux_state = QC_MUX_NULL; |
| 5182 | qc->err = quic_err_transport(QC_ERR_NO_ERROR); |
| 5183 | |
Frédéric Lécaille | b4c5471 | 2023-03-06 14:07:59 +0100 | [diff] [blame^] | 5184 | /* Initialize the next CID sequence number to be used for this connection. */ |
| 5185 | qc->next_cid_seq_num = 0; |
| 5186 | /* Insert the CID for this connection with 0 as sequence number. */ |
| 5187 | icid = new_quic_cid(&qc->cids, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5188 | if (!icid) { |
| 5189 | TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc); |
| 5190 | goto err; |
| 5191 | } |
| 5192 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5193 | if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) && |
| 5194 | is_addr(local_addr)) { |
| 5195 | TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc); |
| 5196 | qc_alloc_fd(qc, local_addr, peer_addr); |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 5197 | |
| 5198 | /* haproxy soft-stop is supported only for QUIC connections |
| 5199 | * with their owned socket. |
| 5200 | */ |
| 5201 | if (qc_test_fd(qc)) |
| 5202 | _HA_ATOMIC_INC(&jobs); |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5203 | } |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5204 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5205 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 5206 | if (server) |
| 5207 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
| 5208 | |
| 5209 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 5210 | qc->scid = icid->cid; |
| 5211 | |
| 5212 | /* Packet number spaces initialization. */ |
| 5213 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 5214 | quic_pktns_init(&qc->pktns[i]); |
| 5215 | /* QUIC encryption level context initialization. */ |
| 5216 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 5217 | if (!quic_conn_enc_level_init(qc, i)) { |
| 5218 | TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc); |
| 5219 | goto err; |
| 5220 | } |
| 5221 | /* Initialize the packet number space. */ |
| 5222 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 5223 | } |
| 5224 | |
| 5225 | qc->original_version = qv; |
| 5226 | qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ? |
| 5227 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 5228 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
| 5229 | /* TX part. */ |
| 5230 | LIST_INIT(&qc->tx.frms_to_send); |
| 5231 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 5232 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 5233 | qc->tx.bytes = 0; |
| 5234 | qc->tx.buf = BUF_NULL; |
| 5235 | /* RX part. */ |
| 5236 | qc->rx.bytes = 0; |
| 5237 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 5238 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 5239 | qc->rx.strms[i].nb_streams = 0; |
| 5240 | |
| 5241 | qc->nb_pkt_for_cc = 1; |
| 5242 | qc->nb_pkt_since_cc = 0; |
| 5243 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5244 | if (!quic_tls_ku_init(qc)) { |
| 5245 | TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc); |
| 5246 | goto err; |
| 5247 | } |
| 5248 | |
| 5249 | /* XXX TO DO: Only one path at this time. */ |
| 5250 | qc->path = &qc->paths[0]; |
| 5251 | quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc); |
| 5252 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5253 | qc->streams_by_id = EB_ROOT_UNIQUE; |
| 5254 | qc->stream_buf_count = 0; |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 5255 | memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr)); |
| 5256 | memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5257 | |
| 5258 | if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params, |
| 5259 | icid->stateless_reset_token, |
| 5260 | dcid->data, dcid->len, |
| 5261 | qc->scid.data, qc->scid.len, token_odcid)) |
| 5262 | goto err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5263 | |
| 5264 | qc->wait_event.tasklet = tasklet_new(); |
| 5265 | if (!qc->wait_event.tasklet) { |
| 5266 | TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT); |
| 5267 | goto err; |
| 5268 | } |
| 5269 | qc->wait_event.tasklet->process = quic_conn_io_cb; |
| 5270 | qc->wait_event.tasklet->context = qc; |
| 5271 | qc->wait_event.events = 0; |
| 5272 | /* Set tasklet tid based on the SCID selected by us for this |
| 5273 | * connection. The upper layer will also be binded on the same thread. |
| 5274 | */ |
Willy Tarreau | eed7826 | 2022-12-21 09:09:19 +0100 | [diff] [blame] | 5275 | qc->tid = quic_get_cid_tid(qc->scid.data, &l->rx); |
Willy Tarreau | f5a0c8a | 2022-10-13 16:14:11 +0200 | [diff] [blame] | 5276 | qc->wait_event.tasklet->tid = qc->tid; |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 5277 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5278 | |
| 5279 | if (qc_conn_alloc_ssl_ctx(qc) || |
| 5280 | !quic_conn_init_timer(qc) || |
| 5281 | !quic_conn_init_idle_timer_task(qc)) |
| 5282 | goto err; |
| 5283 | |
| 5284 | ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx; |
| 5285 | if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1)) |
| 5286 | goto err; |
| 5287 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5288 | LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx); |
| 5289 | qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch); |
| 5290 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5291 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
| 5292 | |
| 5293 | return qc; |
| 5294 | |
| 5295 | err: |
| 5296 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5297 | if (qc) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5298 | qc->rx.buf.area = NULL; |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5299 | quic_conn_release(qc); |
| 5300 | } |
| 5301 | TRACE_LEAVE(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5302 | return NULL; |
| 5303 | } |
| 5304 | |
| 5305 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 5306 | * The connection tasklet is killed. |
| 5307 | * |
| 5308 | * This function must only be called by the thread responsible of the quic_conn |
| 5309 | * tasklet. |
| 5310 | */ |
| 5311 | void quic_conn_release(struct quic_conn *qc) |
| 5312 | { |
| 5313 | int i; |
| 5314 | struct ssl_sock_ctx *conn_ctx; |
| 5315 | struct eb64_node *node; |
| 5316 | struct quic_tls_ctx *app_tls_ctx; |
| 5317 | struct quic_rx_packet *pkt, *pktback; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5318 | struct bref *bref, *back; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5319 | |
| 5320 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 5321 | |
| 5322 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 5323 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 5324 | |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 5325 | if (qc_test_fd(qc)) |
| 5326 | _HA_ATOMIC_DEC(&jobs); |
| 5327 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5328 | /* Close quic-conn socket fd. */ |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 5329 | qc_release_fd(qc, 0); |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 5330 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5331 | /* in the unlikely (but possible) case the connection was just added to |
| 5332 | * the accept_list we must delete it from there. |
| 5333 | */ |
| 5334 | MT_LIST_DELETE(&qc->accept_list); |
| 5335 | |
| 5336 | /* free remaining stream descriptors */ |
| 5337 | node = eb64_first(&qc->streams_by_id); |
| 5338 | while (node) { |
| 5339 | struct qc_stream_desc *stream; |
| 5340 | |
| 5341 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 5342 | node = eb64_next(node); |
| 5343 | |
| 5344 | /* all streams attached to the quic-conn are released, so |
| 5345 | * qc_stream_desc_free will liberate the stream instance. |
| 5346 | */ |
| 5347 | BUG_ON(!stream->release); |
| 5348 | qc_stream_desc_free(stream, 1); |
| 5349 | } |
| 5350 | |
| 5351 | /* Purge Rx packet list. */ |
| 5352 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 5353 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 5354 | pool_free(pool_head_quic_rx_packet, pkt); |
| 5355 | } |
| 5356 | |
| 5357 | if (qc->idle_timer_task) { |
| 5358 | task_destroy(qc->idle_timer_task); |
| 5359 | qc->idle_timer_task = NULL; |
| 5360 | } |
| 5361 | |
| 5362 | if (qc->timer_task) { |
| 5363 | task_destroy(qc->timer_task); |
| 5364 | qc->timer_task = NULL; |
| 5365 | } |
| 5366 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5367 | if (qc->wait_event.tasklet) |
| 5368 | tasklet_free(qc->wait_event.tasklet); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5369 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5370 | /* remove the connection from receiver cids trees */ |
| 5371 | ebmb_delete(&qc->odcid_node); |
| 5372 | ebmb_delete(&qc->scid_node); |
| 5373 | free_quic_conn_cids(qc); |
| 5374 | |
| 5375 | conn_ctx = qc->xprt_ctx; |
| 5376 | if (conn_ctx) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5377 | SSL_free(conn_ctx->ssl); |
| 5378 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
| 5379 | } |
| 5380 | |
| 5381 | quic_tls_ku_free(qc); |
| 5382 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 5383 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
| 5384 | quic_conn_enc_level_uninit(qc, &qc->els[i]); |
| 5385 | } |
| 5386 | quic_tls_ctx_secs_free(&qc->negotiated_ictx); |
| 5387 | |
| 5388 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 5389 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 5390 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
| 5391 | |
| 5392 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 5393 | quic_pktns_tx_pkts_release(&qc->pktns[i], qc); |
| 5394 | quic_free_arngs(qc, &qc->pktns[i].rx.arngs); |
| 5395 | } |
| 5396 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 5397 | /* Detach CLI context watchers currently dumping this connection. |
| 5398 | * Reattach them to the next quic_conn instance. |
| 5399 | */ |
| 5400 | list_for_each_entry_safe(bref, back, &qc->back_refs, users) { |
| 5401 | /* Remove watcher from this quic_conn instance. */ |
| 5402 | LIST_DEL_INIT(&bref->users); |
| 5403 | |
| 5404 | /* Attach it to next instance unless it was the last list element. */ |
| 5405 | if (qc->el_th_ctx.n != &th_ctx->quic_conns) { |
| 5406 | struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx, |
| 5407 | struct quic_conn *, |
| 5408 | el_th_ctx); |
| 5409 | LIST_APPEND(&next->back_refs, &bref->users); |
| 5410 | } |
| 5411 | bref->ref = qc->el_th_ctx.n; |
| 5412 | __ha_barrier_store(); |
| 5413 | } |
| 5414 | /* Remove quic_conn from global ha_thread_ctx list. */ |
| 5415 | LIST_DELETE(&qc->el_th_ctx); |
| 5416 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5417 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 5418 | pool_free(pool_head_quic_conn, qc); |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 5419 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5420 | TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc); |
| 5421 | |
| 5422 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 5423 | } |
| 5424 | |
| 5425 | /* Initialize the timer task of <qc> QUIC connection. |
| 5426 | * Returns 1 if succeeded, 0 if not. |
| 5427 | */ |
| 5428 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 5429 | { |
| 5430 | int ret = 0; |
| 5431 | /* Attach this task to the same thread ID used for the connection */ |
| 5432 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5433 | |
| 5434 | qc->timer_task = task_new_on(qc->tid); |
| 5435 | if (!qc->timer_task) { |
| 5436 | TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 5437 | goto leave; |
| 5438 | } |
| 5439 | |
| 5440 | qc->timer = TICK_ETERNITY; |
| 5441 | qc->timer_task->process = qc_process_timer; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5442 | qc->timer_task->context = qc; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5443 | |
| 5444 | ret = 1; |
| 5445 | leave: |
| 5446 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5447 | return ret; |
| 5448 | } |
| 5449 | |
| 5450 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 5451 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 5452 | { |
| 5453 | unsigned int expire; |
| 5454 | |
Amaury Denoyelle | 77ed631 | 2023-02-01 09:28:55 +0100 | [diff] [blame] | 5455 | if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) { |
| 5456 | TRACE_STATE("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5457 | task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG); |
| 5458 | } |
| 5459 | else { |
| 5460 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 5461 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 5462 | task_queue(qc->idle_timer_task); |
| 5463 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5464 | } |
| 5465 | |
| 5466 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 5467 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 5468 | */ |
| 5469 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 5470 | { |
| 5471 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5472 | |
| 5473 | if (read) { |
| 5474 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 5475 | } |
| 5476 | else { |
| 5477 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 5478 | } |
| 5479 | qc_idle_timer_do_rearm(qc); |
| 5480 | |
| 5481 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5482 | } |
| 5483 | |
| 5484 | /* The task handling the idle timeout */ |
| 5485 | struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 5486 | { |
| 5487 | struct quic_conn *qc = ctx; |
| 5488 | struct quic_counters *prx_counters = qc->prx_counters; |
| 5489 | unsigned int qc_flags = qc->flags; |
| 5490 | |
| 5491 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5492 | |
| 5493 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 5494 | * might free the quic-conn too early via quic_close(). |
| 5495 | */ |
| 5496 | qc_notify_close(qc); |
| 5497 | |
| 5498 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 5499 | * responsible to call quic_close to release it. |
| 5500 | */ |
| 5501 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 5502 | if (qc->mux_state != QC_MUX_READY) |
| 5503 | quic_conn_release(qc); |
| 5504 | |
| 5505 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 5506 | * least clean some parts of it such as the tasklet. |
| 5507 | */ |
| 5508 | |
| 5509 | if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 5510 | qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 5511 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
| 5512 | HA_ATOMIC_DEC(&prx_counters->half_open_conn); |
| 5513 | } |
| 5514 | |
| 5515 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5516 | return NULL; |
| 5517 | } |
| 5518 | |
| 5519 | /* Initialize the idle timeout task for <qc>. |
| 5520 | * Returns 1 if succeeded, 0 if not. |
| 5521 | */ |
| 5522 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 5523 | { |
| 5524 | int ret = 0; |
| 5525 | |
| 5526 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5527 | |
| 5528 | qc->idle_timer_task = task_new_here(); |
| 5529 | if (!qc->idle_timer_task) { |
| 5530 | TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 5531 | goto leave; |
| 5532 | } |
| 5533 | |
| 5534 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 5535 | qc->idle_timer_task->context = qc; |
| 5536 | qc_idle_timer_rearm(qc, 1); |
| 5537 | task_queue(qc->idle_timer_task); |
| 5538 | |
| 5539 | ret = 1; |
| 5540 | leave: |
| 5541 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5542 | return ret; |
| 5543 | } |
| 5544 | |
| 5545 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 5546 | * past one byte of this buffer. |
| 5547 | */ |
| 5548 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 5549 | struct quic_rx_packet *pkt) |
| 5550 | { |
| 5551 | int ret = 0; |
| 5552 | unsigned char dcid_len, scid_len; |
| 5553 | |
| 5554 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5555 | |
| 5556 | if (end == *buf) { |
| 5557 | TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT); |
| 5558 | goto leave; |
| 5559 | } |
| 5560 | |
| 5561 | /* Destination Connection ID Length */ |
| 5562 | dcid_len = *(*buf)++; |
| 5563 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 5564 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) { |
| 5565 | TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT); |
| 5566 | goto leave; |
| 5567 | } |
| 5568 | |
| 5569 | if (dcid_len) { |
| 5570 | /* Check that the length of this received DCID matches the CID lengths |
| 5571 | * of our implementation for non Initials packets only. |
| 5572 | */ |
| 5573 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 5574 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
| 5575 | dcid_len != QUIC_HAP_CID_LEN) { |
| 5576 | TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT); |
| 5577 | goto leave; |
| 5578 | } |
| 5579 | |
| 5580 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 5581 | } |
| 5582 | |
| 5583 | pkt->dcid.len = dcid_len; |
| 5584 | *buf += dcid_len; |
| 5585 | |
| 5586 | /* Source Connection ID Length */ |
| 5587 | scid_len = *(*buf)++; |
| 5588 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) { |
| 5589 | TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT); |
| 5590 | goto leave; |
| 5591 | } |
| 5592 | |
| 5593 | if (scid_len) |
| 5594 | memcpy(pkt->scid.data, *buf, scid_len); |
| 5595 | pkt->scid.len = scid_len; |
| 5596 | *buf += scid_len; |
| 5597 | |
| 5598 | ret = 1; |
| 5599 | leave: |
| 5600 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 5601 | return ret; |
| 5602 | } |
| 5603 | |
| 5604 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 5605 | static void qc_pkt_insert(struct quic_conn *qc, |
| 5606 | struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 5607 | { |
| 5608 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 5609 | |
| 5610 | pkt->pn_node.key = pkt->pn; |
| 5611 | quic_rx_packet_refinc(pkt); |
| 5612 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 5613 | |
| 5614 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 5615 | } |
| 5616 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5617 | /* Try to remove the header protection of <pkt> QUIC packet with <beg> the |
| 5618 | * address of the packet first byte, using the keys from encryption level <el>. |
| 5619 | * |
| 5620 | * If header protection has been successfully removed, packet data are copied |
| 5621 | * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also |
| 5622 | * proceeded, and the packet is inserted into <qc> protected packets tree. In |
| 5623 | * both cases, packet can now be considered handled by the <qc> connection. |
| 5624 | * |
| 5625 | * If header protection cannot be removed due to <el> secrets already |
| 5626 | * discarded, no operation is conducted. |
| 5627 | * |
| 5628 | * Returns 1 on success : packet data is now handled by the connection. On |
| 5629 | * error 0 is returned : packet should be dropped by the caller. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5630 | */ |
| 5631 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 5632 | struct quic_rx_packet *pkt, |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5633 | unsigned char *beg, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5634 | struct quic_enc_level **el) |
| 5635 | { |
| 5636 | int ret = 0; |
| 5637 | unsigned char *pn = NULL; /* Packet number field */ |
| 5638 | enum quic_tls_enc_level tel; |
| 5639 | struct quic_enc_level *qel; |
| 5640 | /* Only for traces. */ |
| 5641 | struct quic_rx_packet *qpkt_trace; |
| 5642 | |
| 5643 | qpkt_trace = NULL; |
| 5644 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5645 | BUG_ON(!pkt->pn_offset); |
| 5646 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5647 | /* The packet number is here. This is also the start minus |
| 5648 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 5649 | * protection. |
| 5650 | */ |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5651 | pn = beg + pkt->pn_offset; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5652 | |
| 5653 | tel = quic_packet_type_enc_level(pkt->type); |
| 5654 | qel = &qc->els[tel]; |
| 5655 | |
| 5656 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 5657 | struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt); |
| 5658 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5659 | /* Note that the following function enables us to unprotect the packet |
| 5660 | * number and its length subsequently used to decrypt the entire |
| 5661 | * packets. |
| 5662 | */ |
Frédéric Lécaille | 7202778 | 2023-02-22 16:20:09 +0100 | [diff] [blame] | 5663 | if (!qc_do_rm_hp(qc, pkt, tls_ctx, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5664 | qel->pktns->rx.largest_pn, pn, beg)) { |
| 5665 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
| 5666 | goto out; |
| 5667 | } |
| 5668 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5669 | /* The AAD includes the packet number field. */ |
| 5670 | pkt->aad_len = pkt->pn_offset + pkt->pnl; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5671 | if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) { |
| 5672 | TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc); |
| 5673 | goto out; |
| 5674 | } |
| 5675 | |
| 5676 | qpkt_trace = pkt; |
| 5677 | } |
| 5678 | else { |
| 5679 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 5680 | /* If the packet number space has been discarded, this packet |
| 5681 | * will be not parsed. |
| 5682 | */ |
| 5683 | TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt); |
| 5684 | goto out; |
| 5685 | } |
| 5686 | |
| 5687 | TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5688 | LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 5689 | quic_rx_packet_refinc(pkt); |
| 5690 | } |
| 5691 | |
| 5692 | *el = qel; |
| 5693 | /* No reference counter incrementation here!!! */ |
| 5694 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 5695 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 5696 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 5697 | b_add(&qc->rx.buf, pkt->len); |
| 5698 | |
| 5699 | ret = 1; |
| 5700 | out: |
| 5701 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
| 5702 | return ret; |
| 5703 | } |
| 5704 | |
| 5705 | /* Parse the header form from <byte0> first byte of <pkt> packet to set its type. |
| 5706 | * Also set <*long_header> to 1 if this form is long, 0 if not and the version |
| 5707 | * of this packet into <*version>. |
| 5708 | */ |
| 5709 | static inline int qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 5710 | unsigned char **buf, const unsigned char *end, |
| 5711 | int *long_header, uint32_t *version) |
| 5712 | { |
| 5713 | int ret = 0; |
| 5714 | const unsigned char byte0 = **buf; |
| 5715 | |
| 5716 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5717 | |
| 5718 | (*buf)++; |
| 5719 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 5720 | unsigned char type = |
| 5721 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 5722 | |
| 5723 | *long_header = 1; |
| 5724 | /* Version */ |
| 5725 | if (!quic_read_uint32(version, (const unsigned char **)buf, end)) { |
| 5726 | TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT); |
| 5727 | goto out; |
| 5728 | } |
| 5729 | |
Frédéric Lécaille | 21c4c9b | 2023-01-13 16:37:02 +0100 | [diff] [blame] | 5730 | if (*version != QUIC_PROTOCOL_VERSION_2) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5731 | pkt->type = type; |
| 5732 | } |
| 5733 | else { |
| 5734 | switch (type) { |
| 5735 | case 0: |
| 5736 | pkt->type = QUIC_PACKET_TYPE_RETRY; |
| 5737 | break; |
| 5738 | case 1: |
| 5739 | pkt->type = QUIC_PACKET_TYPE_INITIAL; |
| 5740 | break; |
| 5741 | case 2: |
| 5742 | pkt->type = QUIC_PACKET_TYPE_0RTT; |
| 5743 | break; |
| 5744 | case 3: |
| 5745 | pkt->type = QUIC_PACKET_TYPE_HANDSHAKE; |
| 5746 | break; |
| 5747 | } |
| 5748 | } |
| 5749 | } |
| 5750 | else { |
| 5751 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 5752 | *long_header = 0; |
| 5753 | } |
| 5754 | |
| 5755 | ret = 1; |
| 5756 | out: |
| 5757 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 5758 | return ret; |
| 5759 | } |
| 5760 | |
| 5761 | /* Return the QUIC version (quic_version struct) with <version> as version number |
| 5762 | * if supported or NULL if not. |
| 5763 | */ |
| 5764 | static inline const struct quic_version *qc_supported_version(uint32_t version) |
| 5765 | { |
| 5766 | int i; |
| 5767 | |
| 5768 | for (i = 0; i < quic_versions_nb; i++) |
| 5769 | if (quic_versions[i].num == version) |
| 5770 | return &quic_versions[i]; |
| 5771 | |
| 5772 | return NULL; |
| 5773 | } |
| 5774 | |
| 5775 | /* |
| 5776 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 5777 | * address <addr>. |
| 5778 | * Implementation of RFC9000 6. Version Negotiation |
| 5779 | * |
| 5780 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 5781 | * |
| 5782 | * Returns 0 on success else non-zero |
| 5783 | */ |
| 5784 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 5785 | struct quic_rx_packet *pkt) |
| 5786 | { |
| 5787 | char buf[256]; |
| 5788 | int ret = 0, i = 0, j; |
| 5789 | uint32_t version; |
| 5790 | const socklen_t addrlen = get_addr_len(addr); |
| 5791 | |
| 5792 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5793 | /* |
| 5794 | * header form |
| 5795 | * long header, fixed bit to 0 for Version Negotiation |
| 5796 | */ |
| 5797 | /* TODO: RAND_bytes() should be replaced? */ |
| 5798 | if (RAND_bytes((unsigned char *)buf, 1) != 1) { |
| 5799 | TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT); |
| 5800 | goto out; |
| 5801 | } |
| 5802 | |
| 5803 | buf[i++] |= '\x80'; |
| 5804 | /* null version for Version Negotiation */ |
| 5805 | buf[i++] = '\x00'; |
| 5806 | buf[i++] = '\x00'; |
| 5807 | buf[i++] = '\x00'; |
| 5808 | buf[i++] = '\x00'; |
| 5809 | |
| 5810 | /* source connection id */ |
| 5811 | buf[i++] = pkt->scid.len; |
| 5812 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5813 | i += pkt->scid.len; |
| 5814 | |
| 5815 | /* destination connection id */ |
| 5816 | buf[i++] = pkt->dcid.len; |
| 5817 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 5818 | i += pkt->dcid.len; |
| 5819 | |
| 5820 | /* supported version */ |
| 5821 | for (j = 0; j < quic_versions_nb; j++) { |
| 5822 | version = htonl(quic_versions[j].num); |
| 5823 | memcpy(&buf[i], &version, sizeof(version)); |
| 5824 | i += sizeof(version); |
| 5825 | } |
| 5826 | |
| 5827 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 5828 | goto out; |
| 5829 | |
| 5830 | ret = 1; |
| 5831 | out: |
| 5832 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5833 | return !ret; |
| 5834 | } |
| 5835 | |
| 5836 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 5837 | * from <fd> UDP socket to <dst> |
| 5838 | * Return 1 if succeeded, 0 if not. |
| 5839 | */ |
| 5840 | static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr, |
| 5841 | struct quic_rx_packet *rxpkt) |
| 5842 | { |
| 5843 | int ret = 0, pktlen, rndlen; |
| 5844 | unsigned char pkt[64]; |
| 5845 | const socklen_t addrlen = get_addr_len(dstaddr); |
| 5846 | struct proxy *prx; |
| 5847 | struct quic_counters *prx_counters; |
| 5848 | |
| 5849 | TRACE_ENTER(QUIC_EV_STATELESS_RST); |
| 5850 | |
| 5851 | prx = l->bind_conf->frontend; |
| 5852 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 5853 | /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3) |
| 5854 | * The resulting minimum size of 21 bytes does not guarantee that a Stateless |
| 5855 | * Reset is difficult to distinguish from other packets if the recipient requires |
| 5856 | * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure |
| 5857 | * that all packets it sends are at least 22 bytes longer than the minimum |
| 5858 | * connection ID length that it requests the peer to include in its packets, |
| 5859 | * adding PADDING frames as necessary. This ensures that any Stateless Reset |
| 5860 | * sent by the peer is indistinguishable from a valid packet sent to the endpoint. |
| 5861 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 5862 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 5863 | * than the packet it responds to. |
| 5864 | */ |
| 5865 | |
| 5866 | /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */ |
| 5867 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0; |
| 5868 | pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen); |
| 5869 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
| 5870 | |
| 5871 | /* Put a header of random bytes */ |
| 5872 | /* TODO: RAND_bytes() should be replaced */ |
| 5873 | if (RAND_bytes(pkt, rndlen) != 1) { |
| 5874 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST); |
| 5875 | goto leave; |
| 5876 | } |
| 5877 | |
| 5878 | /* Clear the most significant bit, and set the second one */ |
| 5879 | *pkt = (*pkt & ~0x80) | 0x40; |
| 5880 | if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN, |
| 5881 | rxpkt->dcid.data, rxpkt->dcid.len)) |
| 5882 | goto leave; |
| 5883 | |
| 5884 | if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0) |
| 5885 | goto leave; |
| 5886 | |
| 5887 | ret = 1; |
| 5888 | HA_ATOMIC_INC(&prx_counters->stateless_reset_sent); |
| 5889 | TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid); |
| 5890 | leave: |
| 5891 | TRACE_LEAVE(QUIC_EV_STATELESS_RST); |
| 5892 | return ret; |
| 5893 | } |
| 5894 | |
| 5895 | /* QUIC server only function. |
| 5896 | * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address. |
| 5897 | * This is the responsibility of the caller to check <aad> size is big enough |
| 5898 | * to contain these data. |
| 5899 | * Return the number of bytes copied to <aad>. |
| 5900 | */ |
| 5901 | static int quic_generate_retry_token_aad(unsigned char *aad, |
| 5902 | uint32_t version, |
| 5903 | const struct quic_cid *cid, |
| 5904 | const struct sockaddr_storage *addr) |
| 5905 | { |
| 5906 | unsigned char *p; |
| 5907 | |
| 5908 | p = aad; |
| 5909 | memcpy(p, &version, sizeof version); |
| 5910 | p += sizeof version; |
| 5911 | p += quic_saddr_cpy(p, addr); |
| 5912 | memcpy(p, cid->data, cid->len); |
| 5913 | p += cid->len; |
| 5914 | |
| 5915 | return p - aad; |
| 5916 | } |
| 5917 | |
| 5918 | /* QUIC server only function. |
| 5919 | * 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] | 5920 | * <buf> with <len> as length. <odcid> is the original destination connection |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5921 | * ID and <dcid> is our side destination connection ID (or client source |
| 5922 | * connection ID). |
| 5923 | * Returns the length of the encoded token or 0 on error. |
| 5924 | */ |
| 5925 | static int quic_generate_retry_token(unsigned char *buf, size_t len, |
| 5926 | const uint32_t version, |
| 5927 | const struct quic_cid *odcid, |
| 5928 | const struct quic_cid *dcid, |
| 5929 | struct sockaddr_storage *addr) |
| 5930 | { |
| 5931 | int ret = 0; |
| 5932 | unsigned char *p; |
| 5933 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
Amaury Denoyelle | 6c94056 | 2022-10-18 11:05:02 +0200 | [diff] [blame] | 5934 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5935 | size_t aadlen; |
| 5936 | unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN]; |
| 5937 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5938 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5939 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5940 | size_t seclen = strlen(global.cluster_secret); |
| 5941 | EVP_CIPHER_CTX *ctx = NULL; |
| 5942 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 5943 | uint32_t timestamp = now_ms; |
| 5944 | |
| 5945 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5946 | |
| 5947 | /* We copy the odcid into the token, prefixed by its one byte |
| 5948 | * length, the format token byte. It is followed by an AEAD TAG, and finally |
| 5949 | * the random bytes used to derive the secret to encrypt the token. |
| 5950 | */ |
| 5951 | if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len) |
| 5952 | goto err; |
| 5953 | |
| 5954 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
| 5955 | /* TODO: RAND_bytes() should be replaced */ |
| 5956 | if (RAND_bytes(salt, sizeof salt) != 1) { |
| 5957 | TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT); |
| 5958 | goto err; |
| 5959 | } |
| 5960 | |
| 5961 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 5962 | salt, sizeof salt, sec, seclen)) { |
| 5963 | TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT); |
| 5964 | goto err; |
| 5965 | } |
| 5966 | |
| 5967 | if (!quic_tls_tx_ctx_init(&ctx, aead, key)) { |
| 5968 | TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT); |
| 5969 | goto err; |
| 5970 | } |
| 5971 | |
| 5972 | /* Token build */ |
| 5973 | p = buf; |
| 5974 | *p++ = QUIC_TOKEN_FMT_RETRY, |
| 5975 | *p++ = odcid->len; |
| 5976 | memcpy(p, odcid->data, odcid->len); |
| 5977 | p += odcid->len; |
| 5978 | write_u32(p, htonl(timestamp)); |
| 5979 | p += sizeof timestamp; |
| 5980 | |
| 5981 | /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 5982 | if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) { |
| 5983 | TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT); |
| 5984 | goto err; |
| 5985 | } |
| 5986 | |
| 5987 | p += QUIC_TLS_TAG_LEN; |
| 5988 | memcpy(p, salt, sizeof salt); |
| 5989 | p += sizeof salt; |
| 5990 | EVP_CIPHER_CTX_free(ctx); |
| 5991 | |
| 5992 | ret = p - buf; |
| 5993 | leave: |
| 5994 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5995 | return ret; |
| 5996 | |
| 5997 | err: |
| 5998 | if (ctx) |
| 5999 | EVP_CIPHER_CTX_free(ctx); |
| 6000 | goto leave; |
| 6001 | } |
| 6002 | |
| 6003 | /* QUIC server only function. |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6004 | * |
| 6005 | * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is |
| 6006 | * the UDP datagram containing <pkt> and <l> is the listener instance on which |
| 6007 | * it was received. If the token is valid, the ODCID of <qc> QUIC connection |
| 6008 | * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed |
| 6009 | * to validate the token but it can be NULL : in this case the version will be |
| 6010 | * retrieved from the packet. |
| 6011 | * |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6012 | * Return 1 if succeeded, 0 if not. |
| 6013 | */ |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6014 | |
| 6015 | static int quic_retry_token_check(struct quic_rx_packet *pkt, |
| 6016 | struct quic_dgram *dgram, |
| 6017 | struct listener *l, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6018 | struct quic_conn *qc, |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6019 | struct quic_cid *odcid) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6020 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6021 | struct proxy *prx; |
| 6022 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6023 | int ret = 0; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6024 | unsigned char *token = pkt->token; |
| 6025 | const uint64_t tokenlen = pkt->token_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6026 | unsigned char buf[128]; |
| 6027 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
Amaury Denoyelle | 6c94056 | 2022-10-18 11:05:02 +0200 | [diff] [blame] | 6028 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6029 | size_t aadlen; |
| 6030 | const unsigned char *salt; |
| 6031 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 6032 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 6033 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 6034 | size_t seclen = strlen(global.cluster_secret); |
| 6035 | EVP_CIPHER_CTX *ctx = NULL; |
| 6036 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6037 | const struct quic_version *qv = qc ? qc->original_version : |
| 6038 | pkt->version; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6039 | |
| 6040 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6041 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6042 | /* The caller must ensure this. */ |
| 6043 | BUG_ON(!global.cluster_secret || !pkt->token_len); |
| 6044 | |
| 6045 | prx = l->bind_conf->frontend; |
| 6046 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6047 | |
| 6048 | if (*pkt->token != QUIC_TOKEN_FMT_RETRY) { |
| 6049 | /* TODO: New token check */ |
| 6050 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 6051 | goto leave; |
| 6052 | } |
| 6053 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6054 | if (sizeof buf < tokenlen) { |
| 6055 | TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc); |
| 6056 | goto err; |
| 6057 | } |
| 6058 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6059 | 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] | 6060 | salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN; |
| 6061 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 6062 | salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) { |
| 6063 | TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc); |
| 6064 | goto err; |
| 6065 | } |
| 6066 | |
| 6067 | if (!quic_tls_rx_ctx_init(&ctx, aead, key)) { |
| 6068 | TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc); |
| 6069 | goto err; |
| 6070 | } |
| 6071 | |
| 6072 | /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 6073 | if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen, |
| 6074 | ctx, aead, key, iv)) { |
| 6075 | TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc); |
| 6076 | goto err; |
| 6077 | } |
| 6078 | |
| 6079 | if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) { |
| 6080 | TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 6081 | goto err; |
| 6082 | } |
| 6083 | |
| 6084 | EVP_CIPHER_CTX_free(ctx); |
| 6085 | |
| 6086 | ret = 1; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6087 | HA_ATOMIC_INC(&prx_counters->retry_validated); |
| 6088 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6089 | leave: |
| 6090 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6091 | return ret; |
| 6092 | |
| 6093 | err: |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6094 | HA_ATOMIC_INC(&prx_counters->retry_error); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6095 | if (ctx) |
| 6096 | EVP_CIPHER_CTX_free(ctx); |
| 6097 | goto leave; |
| 6098 | } |
| 6099 | |
| 6100 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 6101 | * the Initial <pkt> packet. |
| 6102 | * |
| 6103 | * Returns 0 on success else non-zero. |
| 6104 | */ |
| 6105 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 6106 | struct quic_rx_packet *pkt, const struct quic_version *qv) |
| 6107 | { |
| 6108 | int ret = 0; |
| 6109 | unsigned char buf[128]; |
| 6110 | int i = 0, token_len; |
| 6111 | const socklen_t addrlen = get_addr_len(addr); |
| 6112 | struct quic_cid scid; |
| 6113 | |
| 6114 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 6115 | |
| 6116 | /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */ |
| 6117 | buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) | |
| 6118 | (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT); |
| 6119 | /* version */ |
| 6120 | buf[i++] = *((unsigned char *)&qv->num + 3); |
| 6121 | buf[i++] = *((unsigned char *)&qv->num + 2); |
| 6122 | buf[i++] = *((unsigned char *)&qv->num + 1); |
| 6123 | buf[i++] = *(unsigned char *)&qv->num; |
| 6124 | |
| 6125 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 6126 | buf[i++] = pkt->scid.len; |
| 6127 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 6128 | i += pkt->scid.len; |
| 6129 | |
| 6130 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 6131 | scid.len = QUIC_HAP_CID_LEN; |
| 6132 | /* TODO: RAND_bytes() should be replaced */ |
| 6133 | if (RAND_bytes(scid.data, scid.len) != 1) { |
| 6134 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT); |
| 6135 | goto out; |
| 6136 | } |
| 6137 | |
| 6138 | buf[i++] = scid.len; |
| 6139 | memcpy(&buf[i], scid.data, scid.len); |
| 6140 | i += scid.len; |
| 6141 | |
| 6142 | /* token */ |
| 6143 | if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num, |
| 6144 | &pkt->dcid, &pkt->scid, addr))) { |
| 6145 | TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT); |
| 6146 | goto out; |
| 6147 | } |
| 6148 | |
| 6149 | i += token_len; |
| 6150 | |
| 6151 | /* token integrity tag */ |
| 6152 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 6153 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 6154 | pkt->dcid.len, buf, i, qv)) { |
| 6155 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 6156 | goto out; |
| 6157 | } |
| 6158 | |
| 6159 | i += QUIC_TLS_TAG_LEN; |
| 6160 | |
| 6161 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) { |
| 6162 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 6163 | goto out; |
| 6164 | } |
| 6165 | |
| 6166 | ret = 1; |
| 6167 | out: |
| 6168 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 6169 | return !ret; |
| 6170 | } |
| 6171 | |
| 6172 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 6173 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 6174 | * concatenated to the <pkt> DCID field. |
| 6175 | * |
| 6176 | * Returns the instance or NULL if not found. |
| 6177 | */ |
| 6178 | static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt, |
| 6179 | struct listener *l, |
| 6180 | struct sockaddr_storage *saddr) |
| 6181 | { |
| 6182 | struct quic_conn *qc = NULL; |
| 6183 | struct ebmb_node *node; |
| 6184 | struct quic_connection_id *id; |
| 6185 | /* set if the quic_conn is found in the second DCID tree */ |
| 6186 | |
| 6187 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 6188 | |
| 6189 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 6190 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 6191 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 6192 | /* DCIDs of first packets coming from multiple clients may have |
| 6193 | * the same values. Let's distinguish them by concatenating the |
| 6194 | * socket addresses. |
| 6195 | */ |
| 6196 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 6197 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
| 6198 | pkt->dcid.len + pkt->dcid.addrlen); |
| 6199 | if (node) { |
| 6200 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 6201 | goto end; |
| 6202 | } |
| 6203 | } |
| 6204 | |
| 6205 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 6206 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 6207 | * used. |
| 6208 | */ |
| 6209 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len); |
| 6210 | if (!node) |
| 6211 | goto end; |
| 6212 | |
| 6213 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 6214 | qc = id->qc; |
| 6215 | |
| 6216 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 6217 | * If already done, this is a noop. |
| 6218 | */ |
| 6219 | if (qc) |
| 6220 | ebmb_delete(&qc->odcid_node); |
| 6221 | |
| 6222 | end: |
| 6223 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 6224 | return qc; |
| 6225 | } |
| 6226 | |
| 6227 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 6228 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 6229 | * parameters of this session. |
| 6230 | * This is the responsibility of the caller to check the validity of all the |
| 6231 | * pointers passed as parameter to this function. |
| 6232 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 6233 | * CO_ER_SSL_NO_MEM. |
| 6234 | */ |
| 6235 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 6236 | unsigned char *params, size_t params_len) |
| 6237 | { |
| 6238 | int retry, ret = -1; |
| 6239 | |
| 6240 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 6241 | |
| 6242 | retry = 1; |
| 6243 | retry: |
| 6244 | *ssl = SSL_new(ssl_ctx); |
| 6245 | if (!*ssl) { |
| 6246 | if (!retry--) |
| 6247 | goto err; |
| 6248 | |
| 6249 | pool_gc(NULL); |
| 6250 | goto retry; |
| 6251 | } |
| 6252 | |
| 6253 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 6254 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) { |
| 6255 | SSL_free(*ssl); |
| 6256 | *ssl = NULL; |
| 6257 | if (!retry--) |
| 6258 | goto err; |
| 6259 | |
| 6260 | pool_gc(NULL); |
| 6261 | goto retry; |
| 6262 | } |
| 6263 | |
| 6264 | ret = 0; |
| 6265 | leave: |
| 6266 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 6267 | return ret; |
| 6268 | |
| 6269 | err: |
| 6270 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 6271 | goto leave; |
| 6272 | } |
| 6273 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6274 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 6275 | * used to process <qc> received packets. The allocated context is stored in |
| 6276 | * <qc.xprt_ctx>. |
| 6277 | * |
| 6278 | * Returns 0 on success else non-zero. |
| 6279 | */ |
| 6280 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 6281 | { |
| 6282 | int ret = 0; |
| 6283 | struct bind_conf *bc = qc->li->bind_conf; |
| 6284 | struct ssl_sock_ctx *ctx = NULL; |
| 6285 | |
| 6286 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 6287 | |
| 6288 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 6289 | if (!ctx) { |
| 6290 | TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT); |
| 6291 | goto err; |
| 6292 | } |
| 6293 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6294 | ctx->subs = NULL; |
| 6295 | ctx->xprt_ctx = NULL; |
| 6296 | ctx->qc = qc; |
| 6297 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6298 | if (qc_is_listener(qc)) { |
| 6299 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 6300 | qc->enc_params, qc->enc_params_len) == -1) { |
| 6301 | goto err; |
| 6302 | } |
| 6303 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 6304 | /* Enabling 0-RTT */ |
| 6305 | if (bc->ssl_conf.early_data) |
| 6306 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 6307 | #endif |
| 6308 | |
| 6309 | SSL_set_accept_state(ctx->ssl); |
| 6310 | } |
| 6311 | |
| 6312 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 6313 | |
| 6314 | /* Store the allocated context in <qc>. */ |
| 6315 | qc->xprt_ctx = ctx; |
| 6316 | |
| 6317 | ret = 1; |
| 6318 | leave: |
| 6319 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 6320 | return !ret; |
| 6321 | |
| 6322 | err: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6323 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 6324 | goto leave; |
| 6325 | } |
| 6326 | |
| 6327 | /* Check that all the bytes between <buf> included and <end> address |
| 6328 | * excluded are null. This is the responsibility of the caller to |
| 6329 | * check that there is at least one byte between <buf> end <end>. |
| 6330 | * Return 1 if this all the bytes are null, 0 if not. |
| 6331 | */ |
| 6332 | static inline int quic_padding_check(const unsigned char *buf, |
| 6333 | const unsigned char *end) |
| 6334 | { |
| 6335 | while (buf < end && !*buf) |
| 6336 | buf++; |
| 6337 | |
| 6338 | return buf == end; |
| 6339 | } |
| 6340 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6341 | /* Find the associated connection to the packet <pkt> or create a new one if |
| 6342 | * this is an Initial packet. <dgram> is the datagram containing the packet and |
| 6343 | * <l> is the listener instance on which it was received. |
| 6344 | * |
| 6345 | * Returns the quic-conn instance or NULL. |
| 6346 | */ |
| 6347 | static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt, |
| 6348 | struct quic_dgram *dgram, |
| 6349 | struct listener *l) |
| 6350 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6351 | struct quic_cid token_odcid = { .len = 0 }; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6352 | struct quic_conn *qc = NULL; |
| 6353 | struct proxy *prx; |
| 6354 | struct quic_counters *prx_counters; |
| 6355 | |
| 6356 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 6357 | |
| 6358 | prx = l->bind_conf->frontend; |
| 6359 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6360 | |
| 6361 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
| 6362 | |
| 6363 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 6364 | BUG_ON(!pkt->version); /* This must not happen. */ |
| 6365 | |
| 6366 | if (global.cluster_secret && pkt->token_len) { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6367 | if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid)) |
| 6368 | goto err; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6369 | } |
| 6370 | |
| 6371 | if (!qc) { |
| 6372 | int ipv4; |
| 6373 | |
| 6374 | if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) && |
| 6375 | HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) { |
| 6376 | TRACE_PROTO("Initial without token, sending retry", |
| 6377 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6378 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) { |
| 6379 | TRACE_ERROR("Error during Retry generation", |
| 6380 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6381 | goto out; |
| 6382 | } |
| 6383 | |
| 6384 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 6385 | goto out; |
| 6386 | } |
| 6387 | |
| 6388 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 6389 | * When an Initial packet is sent by a client that has not previously |
| 6390 | * received an Initial or Retry packet from the server, the client |
| 6391 | * populates the Destination Connection ID field with an unpredictable |
| 6392 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 6393 | */ |
| 6394 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 6395 | TRACE_PROTO("dropped packet", |
| 6396 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6397 | goto err; |
| 6398 | } |
| 6399 | |
| 6400 | pkt->saddr = dgram->saddr; |
| 6401 | ipv4 = dgram->saddr.ss_family == AF_INET; |
| 6402 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6403 | 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] | 6404 | &dgram->daddr, &pkt->saddr, 1, |
| 6405 | !!pkt->token_len, l); |
| 6406 | if (qc == NULL) |
| 6407 | goto err; |
| 6408 | |
| 6409 | HA_ATOMIC_INC(&prx_counters->half_open_conn); |
| 6410 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
| 6411 | ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
| 6412 | qc->odcid.len + qc->odcid.addrlen); |
| 6413 | } |
| 6414 | } |
| 6415 | else if (!qc) { |
| 6416 | TRACE_PROTO("No connection on a non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6417 | if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt)) |
| 6418 | TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc); |
| 6419 | goto err; |
| 6420 | } |
| 6421 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6422 | out: |
| 6423 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6424 | return qc; |
| 6425 | |
| 6426 | err: |
| 6427 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
| 6428 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 6429 | return NULL; |
| 6430 | } |
| 6431 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6432 | /* Parse a QUIC packet starting at <buf>. Data won't be read after <end> even |
| 6433 | * if the packet is incomplete. This function will populate fields of <pkt> |
| 6434 | * instance, most notably its length. <dgram> is the UDP datagram which |
| 6435 | * contains the parsed packet. <l> is the listener instance on which it was |
| 6436 | * received. |
| 6437 | * |
| 6438 | * Returns 0 on success else non-zero. Packet length is guaranteed to be set to |
| 6439 | * the real packet value or to cover all data between <buf> and <end> : this is |
| 6440 | * useful to reject a whole datagram. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6441 | */ |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6442 | static int quic_rx_pkt_parse(struct quic_rx_packet *pkt, |
| 6443 | unsigned char *buf, const unsigned char *end, |
| 6444 | struct quic_dgram *dgram, struct listener *l) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6445 | { |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6446 | const unsigned char *beg = buf; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6447 | struct proxy *prx; |
| 6448 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6449 | int long_header = 0; |
Willy Tarreau | 33a6870 | 2022-11-24 09:16:41 +0100 | [diff] [blame] | 6450 | uint32_t version = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6451 | const struct quic_version *qv = NULL; |
| 6452 | |
| 6453 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 6454 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6455 | prx = l->bind_conf->frontend; |
| 6456 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6457 | /* This ist only to please to traces and distinguish the |
| 6458 | * packet with parsed packet number from others. |
| 6459 | */ |
| 6460 | pkt->pn_node.key = (uint64_t)-1; |
| 6461 | if (end <= buf) { |
| 6462 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6463 | goto drop; |
| 6464 | } |
| 6465 | |
| 6466 | /* Fixed bit */ |
| 6467 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6468 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
| 6469 | quic_padding_check(buf, end)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6470 | /* Some browsers may pad the remaining datagram space with null bytes. |
| 6471 | * That is what we called add padding out of QUIC packets. Such |
| 6472 | * datagrams must be considered as valid. But we can only consume |
| 6473 | * the remaining space. |
| 6474 | */ |
| 6475 | pkt->len = end - buf; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6476 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6477 | } |
| 6478 | |
| 6479 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6480 | goto drop; |
| 6481 | } |
| 6482 | |
| 6483 | /* Header form */ |
| 6484 | if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) { |
| 6485 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6486 | goto drop; |
| 6487 | } |
| 6488 | |
| 6489 | if (long_header) { |
| 6490 | uint64_t len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6491 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6492 | TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6493 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
| 6494 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6495 | goto drop; |
| 6496 | } |
| 6497 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6498 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6499 | * they must have the same DCID. |
| 6500 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6501 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6502 | (pkt->dcid.len != dgram->dcid_len || |
| 6503 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6504 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6505 | goto drop; |
| 6506 | } |
| 6507 | |
| 6508 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 6509 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) { |
| 6510 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6511 | goto drop; |
| 6512 | } |
| 6513 | |
| 6514 | /* RFC9000 6. Version Negotiation */ |
| 6515 | qv = qc_supported_version(version); |
| 6516 | if (!qv) { |
| 6517 | /* unsupported version, send Negotiation packet */ |
| 6518 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
| 6519 | TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6520 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6521 | } |
| 6522 | |
| 6523 | TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6524 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6525 | } |
Amaury Denoyelle | 0eae572 | 2022-10-17 18:05:18 +0200 | [diff] [blame] | 6526 | pkt->version = qv; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6527 | |
| 6528 | /* For Initial packets, and for servers (QUIC clients connections), |
| 6529 | * there is no Initial connection IDs storage. |
| 6530 | */ |
| 6531 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 6532 | uint64_t token_len; |
| 6533 | |
| 6534 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 6535 | end - buf < token_len) { |
| 6536 | TRACE_PROTO("Packet dropped", |
| 6537 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6538 | goto drop; |
| 6539 | } |
| 6540 | |
| 6541 | /* TODO Retry should be automatically activated if |
| 6542 | * suspect network usage is detected. |
| 6543 | */ |
| 6544 | if (global.cluster_secret && !token_len) { |
| 6545 | if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) { |
| 6546 | TRACE_PROTO("Initial without token, sending retry", |
Amaury Denoyelle | 90121b3 | 2022-09-27 10:35:29 +0200 | [diff] [blame] | 6547 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6548 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) { |
| 6549 | TRACE_PROTO("Error during Retry generation", |
Amaury Denoyelle | 90121b3 | 2022-09-27 10:35:29 +0200 | [diff] [blame] | 6550 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6551 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6552 | } |
| 6553 | |
| 6554 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6555 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6556 | } |
| 6557 | } |
| 6558 | else if (!global.cluster_secret && token_len) { |
| 6559 | /* Impossible case: a token was received without configured |
| 6560 | * cluster secret. |
| 6561 | */ |
| 6562 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, |
| 6563 | NULL, NULL, NULL, qv); |
| 6564 | goto drop; |
| 6565 | } |
| 6566 | |
| 6567 | pkt->token = buf; |
| 6568 | pkt->token_len = token_len; |
| 6569 | buf += pkt->token_len; |
| 6570 | } |
| 6571 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
| 6572 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
| 6573 | TRACE_PROTO("Packet dropped", |
| 6574 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6575 | goto drop; |
| 6576 | } |
| 6577 | } |
| 6578 | |
| 6579 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 6580 | end - buf < len) { |
| 6581 | TRACE_PROTO("Packet dropped", |
| 6582 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6583 | goto drop; |
| 6584 | } |
| 6585 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6586 | /* Packet Number is stored here. Packet Length totalizes the |
| 6587 | * rest of the content. |
| 6588 | */ |
| 6589 | pkt->pn_offset = buf - beg; |
| 6590 | pkt->len = pkt->pn_offset + len; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6591 | |
Frédéric Lécaille | 35218c6 | 2023-02-16 11:40:11 +0100 | [diff] [blame] | 6592 | /* RFC 9000. Initial Datagram Size |
| 6593 | * |
| 6594 | * A server MUST discard an Initial packet that is carried in a UDP datagram |
| 6595 | * with a payload that is smaller than the smallest allowed maximum datagram |
| 6596 | * size of 1200 bytes. |
| 6597 | */ |
| 6598 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 6599 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
| 6600 | TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT); |
| 6601 | HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram); |
| 6602 | goto drop; |
| 6603 | } |
| 6604 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6605 | /* Interrupt parsing after packet length retrieval : this |
| 6606 | * ensures that only the packet is dropped but not the whole |
| 6607 | * datagram. |
| 6608 | */ |
| 6609 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
| 6610 | TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT); |
| 6611 | goto drop; |
| 6612 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6613 | } |
| 6614 | else { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6615 | TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6616 | if (end - buf < QUIC_HAP_CID_LEN) { |
| 6617 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6618 | goto drop; |
| 6619 | } |
| 6620 | |
| 6621 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
| 6622 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
| 6623 | |
| 6624 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6625 | * they must have the same DCID. |
| 6626 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6627 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6628 | (pkt->dcid.len != dgram->dcid_len || |
| 6629 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6630 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6631 | goto drop; |
| 6632 | } |
| 6633 | |
| 6634 | buf += QUIC_HAP_CID_LEN; |
| 6635 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6636 | pkt->pn_offset = buf - beg; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6637 | /* A short packet is the last one of a UDP datagram. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6638 | pkt->len = end - beg; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6639 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6640 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6641 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv); |
| 6642 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6643 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6644 | drop: |
| 6645 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6646 | drop_silent: |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6647 | if (!pkt->len) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6648 | pkt->len = end - beg; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6649 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv); |
| 6650 | return -1; |
| 6651 | } |
| 6652 | |
| 6653 | /* Check if received packet <pkt> should be drop due to <qc> already in closing |
| 6654 | * state. This can be true if a CONNECTION_CLOSE has already been emitted for |
| 6655 | * this connection. |
| 6656 | * |
| 6657 | * Returns false if connection is not in closing state else true. The caller |
| 6658 | * should drop the whole datagram in the last case to not mess up <qc> |
| 6659 | * CONNECTION_CLOSE rate limit counter. |
| 6660 | */ |
| 6661 | static int qc_rx_check_closing(struct quic_conn *qc, |
| 6662 | struct quic_rx_packet *pkt) |
| 6663 | { |
| 6664 | if (!(qc->flags & QUIC_FL_CONN_CLOSING)) |
| 6665 | return 0; |
| 6666 | |
| 6667 | TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 6668 | |
| 6669 | /* Check if CONNECTION_CLOSE rate reemission is reached. */ |
| 6670 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 6671 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 6672 | qc->nb_pkt_for_cc++; |
| 6673 | qc->nb_pkt_since_cc = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6674 | } |
| 6675 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6676 | return 1; |
| 6677 | } |
| 6678 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6679 | /* React to a connection migration initiated on <qc> by a client with the new |
| 6680 | * path addresses <peer_addr>/<local_addr>. |
| 6681 | * |
| 6682 | * Returns 0 on success else non-zero. |
| 6683 | */ |
| 6684 | static int qc_handle_conn_migration(struct quic_conn *qc, |
| 6685 | const struct sockaddr_storage *peer_addr, |
| 6686 | const struct sockaddr_storage *local_addr) |
| 6687 | { |
| 6688 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6689 | |
Frédéric Lécaille | 6fc8697 | 2023-01-12 08:29:23 +0100 | [diff] [blame] | 6690 | /* RFC 9000. Connection Migration |
| 6691 | * |
| 6692 | * If the peer sent the disable_active_migration transport parameter, |
| 6693 | * an endpoint also MUST NOT send packets (including probing packets; |
| 6694 | * see Section 9.1) from a different local address to the address the peer |
| 6695 | * used during the handshake, unless the endpoint has acted on a |
| 6696 | * preferred_address transport parameter from the peer. |
| 6697 | */ |
| 6698 | if (qc->li->bind_conf->quic_params.disable_active_migration) { |
| 6699 | TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc); |
| 6700 | goto err; |
| 6701 | } |
| 6702 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6703 | /* RFC 9000 9. Connection Migration |
| 6704 | * |
Amaury Denoyelle | eb6be98 | 2022-11-21 11:14:45 +0100 | [diff] [blame] | 6705 | * The design of QUIC relies on endpoints retaining a stable address for |
| 6706 | * the duration of the handshake. An endpoint MUST NOT initiate |
| 6707 | * connection migration before the handshake is confirmed, as defined in |
| 6708 | * Section 4.1.2 of [QUIC-TLS]. |
| 6709 | */ |
| 6710 | if (qc->state < QUIC_HS_ST_COMPLETE) { |
| 6711 | TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc); |
| 6712 | goto err; |
| 6713 | } |
| 6714 | |
| 6715 | /* RFC 9000 9. Connection Migration |
| 6716 | * |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6717 | * TODO |
| 6718 | * An endpoint MUST |
| 6719 | * perform path validation (Section 8.2) if it detects any change to a |
| 6720 | * peer's address, unless it has previously validated that address. |
| 6721 | */ |
| 6722 | |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 6723 | /* Update quic-conn owned socket if in used. |
| 6724 | * TODO try to reuse it instead of closing and opening a new one. |
| 6725 | */ |
| 6726 | if (qc_test_fd(qc)) { |
| 6727 | /* TODO try to reuse socket instead of closing it and opening a new one. */ |
| 6728 | TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc); |
| 6729 | qc_release_fd(qc, 1); |
Amaury Denoyelle | fb37557 | 2023-02-01 09:28:32 +0100 | [diff] [blame] | 6730 | /* TODO need to adjust <jobs> on socket allocation failure. */ |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 6731 | qc_alloc_fd(qc, local_addr, peer_addr); |
| 6732 | } |
| 6733 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6734 | qc->local_addr = *local_addr; |
| 6735 | qc->peer_addr = *peer_addr; |
| 6736 | HA_ATOMIC_INC(&qc->prx_counters->conn_migration_done); |
| 6737 | |
| 6738 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6739 | return 0; |
| 6740 | |
| 6741 | err: |
| 6742 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6743 | return 1; |
| 6744 | } |
| 6745 | |
Frédéric Lécaille | 1dbeb35 | 2023-02-07 11:40:21 +0100 | [diff] [blame] | 6746 | /* Release the memory for the RX packets which are no more referenced |
| 6747 | * and consume their payloads which have been copied to the RX buffer |
| 6748 | * for the connection. |
| 6749 | * Always succeeds. |
| 6750 | */ |
| 6751 | static inline void quic_rx_pkts_del(struct quic_conn *qc) |
| 6752 | { |
| 6753 | struct quic_rx_packet *pkt, *pktback; |
| 6754 | |
| 6755 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 6756 | 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] | 6757 | "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] | 6758 | (long long)pkt->pn_node.key, |
Frédéric Lécaille | b7a13be | 2023-02-22 17:24:23 +0100 | [diff] [blame] | 6759 | 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] | 6760 | (unsigned char *)b_head(&qc->rx.buf) - pkt->data); |
| 6761 | if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) { |
| 6762 | size_t cdata; |
| 6763 | |
| 6764 | cdata = b_contig_data(&qc->rx.buf, 0); |
| 6765 | 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] | 6766 | "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] | 6767 | if (cdata && !*b_head(&qc->rx.buf)) { |
| 6768 | /* Consume the remaining data */ |
| 6769 | b_del(&qc->rx.buf, cdata); |
| 6770 | } |
| 6771 | break; |
| 6772 | } |
| 6773 | |
| 6774 | if (pkt->refcnt) |
| 6775 | break; |
| 6776 | |
| 6777 | b_del(&qc->rx.buf, pkt->raw_len); |
| 6778 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 6779 | pool_free(pool_head_quic_rx_packet, pkt); |
| 6780 | } |
| 6781 | |
| 6782 | /* In frequent cases the buffer will be emptied at this stage. */ |
| 6783 | b_realign_if_empty(&qc->rx.buf); |
| 6784 | } |
| 6785 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6786 | /* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied |
| 6787 | * into <qc> receive buffer after header protection removal procedure. |
| 6788 | * |
| 6789 | * <dgram> must be set to the datagram which contains the QUIC packet. <beg> |
| 6790 | * must point to packet buffer first byte. |
| 6791 | * |
| 6792 | * <tasklist_head> may be non-NULL when the caller treat several datagrams for |
| 6793 | * different quic-conn. In this case, each quic-conn tasklet will be appended |
| 6794 | * to it in order to be woken up after the current task. |
| 6795 | * |
| 6796 | * The caller can safely removed the packet data. If packet refcount was not |
| 6797 | * incremented by this function, it means that the connection did not handled |
| 6798 | * it and it should be freed by the caller. |
| 6799 | */ |
| 6800 | static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt, |
| 6801 | struct quic_dgram *dgram, unsigned char *beg, |
| 6802 | struct list **tasklist_head) |
| 6803 | { |
| 6804 | const struct quic_version *qv = pkt->version; |
| 6805 | struct quic_enc_level *qel = NULL; |
| 6806 | size_t b_cspace; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6807 | |
Amaury Denoyelle | 3f474e6 | 2022-11-24 17:15:08 +0100 | [diff] [blame] | 6808 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 6809 | |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6810 | if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6811 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
| 6812 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 6813 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Frédéric Lécaille | a65b71f | 2023-03-03 10:16:32 +0100 | [diff] [blame] | 6814 | TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached", |
| 6815 | QUIC_EV_CONN_LPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6816 | /* Reset the anti-amplification bit. It will be set again |
| 6817 | * when sending the next packet if reached again. |
| 6818 | */ |
| 6819 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
Frédéric Lécaille | a65b71f | 2023-03-03 10:16:32 +0100 | [diff] [blame] | 6820 | qc_set_timer(qc); |
| 6821 | if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 6822 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6823 | } |
| 6824 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6825 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 6826 | TRACE_PROTO("Connection error", |
| 6827 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6828 | goto out; |
| 6829 | } |
| 6830 | |
| 6831 | pkt->raw_len = pkt->len; |
| 6832 | quic_rx_pkts_del(qc); |
| 6833 | b_cspace = b_contig_space(&qc->rx.buf); |
| 6834 | if (b_cspace < pkt->len) { |
Frédéric Lécaille | 1dbeb35 | 2023-02-07 11:40:21 +0100 | [diff] [blame] | 6835 | 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] | 6836 | "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6837 | /* Do not consume buf if space not at the end. */ |
| 6838 | if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) { |
| 6839 | TRACE_PROTO("Packet dropped", |
| 6840 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6841 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6842 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6843 | } |
| 6844 | |
| 6845 | /* Let us consume the remaining contiguous space. */ |
| 6846 | if (b_cspace) { |
| 6847 | b_putchr(&qc->rx.buf, 0x00); |
| 6848 | b_cspace--; |
| 6849 | } |
| 6850 | b_add(&qc->rx.buf, b_cspace); |
| 6851 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 6852 | TRACE_PROTO("Too big packet", |
| 6853 | QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6854 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6855 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6856 | } |
| 6857 | } |
| 6858 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6859 | if (!qc_try_rm_hp(qc, pkt, beg, &qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6860 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6861 | goto drop; |
| 6862 | } |
| 6863 | |
| 6864 | TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 6865 | if (pkt->aad_len) |
| 6866 | qc_pkt_insert(qc, pkt, qel); |
| 6867 | out: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 6868 | *tasklist_head = tasklet_wakeup_after(*tasklist_head, |
| 6869 | qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6870 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6871 | drop_silent: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6872 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6873 | return; |
| 6874 | |
| 6875 | drop: |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6876 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6877 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
| 6878 | } |
| 6879 | |
| 6880 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 6881 | * Return 1 if enough room to build this header, 0 if not. |
| 6882 | */ |
| 6883 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 6884 | int type, size_t pn_len, |
| 6885 | struct quic_conn *qc, const struct quic_version *ver) |
| 6886 | { |
| 6887 | int ret = 0; |
| 6888 | |
| 6889 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6890 | |
| 6891 | if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) { |
| 6892 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6893 | goto leave; |
| 6894 | } |
| 6895 | |
| 6896 | type = quic_pkt_type(type, ver->num); |
| 6897 | /* #0 byte flags */ |
| 6898 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 6899 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 6900 | /* Version */ |
| 6901 | quic_write_uint32(buf, end, ver->num); |
| 6902 | *(*buf)++ = qc->dcid.len; |
| 6903 | /* Destination connection ID */ |
| 6904 | if (qc->dcid.len) { |
| 6905 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6906 | *buf += qc->dcid.len; |
| 6907 | } |
| 6908 | /* Source connection ID */ |
| 6909 | *(*buf)++ = qc->scid.len; |
| 6910 | if (qc->scid.len) { |
| 6911 | memcpy(*buf, qc->scid.data, qc->scid.len); |
| 6912 | *buf += qc->scid.len; |
| 6913 | } |
| 6914 | |
| 6915 | ret = 1; |
| 6916 | leave: |
| 6917 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6918 | return ret; |
| 6919 | } |
| 6920 | |
| 6921 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 6922 | * Return 1 if enough room to build this header, 0 if not. |
| 6923 | */ |
| 6924 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 6925 | size_t pn_len, struct quic_conn *qc, |
| 6926 | unsigned char tls_flags) |
| 6927 | { |
| 6928 | int ret = 0; |
| 6929 | |
| 6930 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6931 | |
| 6932 | if (end - *buf < 1 + qc->dcid.len) { |
| 6933 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6934 | goto leave; |
| 6935 | } |
| 6936 | |
| 6937 | /* #0 byte flags */ |
| 6938 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 6939 | ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1); |
| 6940 | /* Destination connection ID */ |
| 6941 | if (qc->dcid.len) { |
| 6942 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6943 | *buf += qc->dcid.len; |
| 6944 | } |
| 6945 | |
| 6946 | ret = 1; |
| 6947 | leave: |
| 6948 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6949 | return ret; |
| 6950 | } |
| 6951 | |
| 6952 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 6953 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 6954 | * with <aead> as AEAD cipher and <key> as secret key. |
| 6955 | * Returns 1 if succeeded or 0 if failed. |
| 6956 | */ |
| 6957 | static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf, |
| 6958 | unsigned char *pn, size_t pnlen, |
| 6959 | struct quic_tls_ctx *tls_ctx) |
| 6960 | |
| 6961 | { |
| 6962 | int i, ret = 0; |
| 6963 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 6964 | * and at most 4 bytes for the packet number |
| 6965 | */ |
| 6966 | unsigned char mask[5] = {0}; |
| 6967 | EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx; |
| 6968 | |
| 6969 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6970 | |
| 6971 | if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) { |
| 6972 | TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc); |
| 6973 | goto out; |
| 6974 | } |
| 6975 | |
| 6976 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 6977 | for (i = 0; i < pnlen; i++) |
| 6978 | pn[i] ^= mask[i + 1]; |
| 6979 | |
| 6980 | ret = 1; |
| 6981 | out: |
| 6982 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6983 | return ret; |
| 6984 | } |
| 6985 | |
| 6986 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 6987 | * ACK ranges if needed to a value below <limit> in bytes. |
| 6988 | * Return 1 if succeeded, 0 if not. |
| 6989 | */ |
| 6990 | static int quic_ack_frm_reduce_sz(struct quic_conn *qc, |
| 6991 | struct quic_frame *ack_frm, size_t limit) |
| 6992 | { |
| 6993 | size_t room, ack_delay_sz; |
| 6994 | int ret = 0; |
| 6995 | |
| 6996 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6997 | |
| 6998 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 6999 | /* A frame is made of 1 byte for the frame type. */ |
| 7000 | room = limit - ack_delay_sz - 1; |
| 7001 | if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room)) |
| 7002 | goto leave; |
| 7003 | |
| 7004 | ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz; |
| 7005 | leave: |
| 7006 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 7007 | return ret; |
| 7008 | } |
| 7009 | |
| 7010 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 7011 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 7012 | * with <room> as available room, and <*len> the packet Length field initialized |
| 7013 | * with the number of bytes already present in this buffer which must be taken |
| 7014 | * into an account for the Length packet field value. <headlen> is the number of |
| 7015 | * bytes already present in this packet before building frames. |
| 7016 | * |
| 7017 | * Update consequently <*len> to reflect the size of these frames built |
| 7018 | * by this function. Also attach these frames to <l> frame list. |
| 7019 | * Return 1 if at least one ack-eleciting frame could be built, 0 if not. |
| 7020 | */ |
| 7021 | static inline int qc_build_frms(struct list *outlist, struct list *inlist, |
| 7022 | size_t room, size_t *len, size_t headlen, |
| 7023 | struct quic_enc_level *qel, |
| 7024 | struct quic_conn *qc) |
| 7025 | { |
| 7026 | int ret; |
| 7027 | struct quic_frame *cf, *cfbak; |
| 7028 | |
| 7029 | TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc); |
| 7030 | |
| 7031 | ret = 0; |
| 7032 | if (*len > room) |
| 7033 | goto leave; |
| 7034 | |
| 7035 | /* If we are not probing we must take into an account the congestion |
| 7036 | * control window. |
| 7037 | */ |
| 7038 | if (!qel->pktns->tx.pto_probe) { |
| 7039 | size_t remain = quic_path_prep_data(qc->path); |
| 7040 | |
| 7041 | if (headlen > remain) |
| 7042 | goto leave; |
| 7043 | |
| 7044 | room = QUIC_MIN(room, remain - headlen); |
| 7045 | } |
| 7046 | |
| 7047 | TRACE_PROTO("************** frames build (headlen)", |
| 7048 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
| 7049 | |
| 7050 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 7051 | * returned by this function only if at least one frame could be built |
| 7052 | * in the switch/case block. |
| 7053 | */ |
| 7054 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
| 7055 | /* header length, data length, frame length. */ |
| 7056 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
| 7057 | |
| 7058 | if (!room) |
| 7059 | break; |
| 7060 | |
| 7061 | switch (cf->type) { |
| 7062 | case QUIC_FT_CRYPTO: |
| 7063 | TRACE_DEVEL(" New CRYPTO frame build (room, len)", |
| 7064 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
| 7065 | /* Compute the length of this CRYPTO frame header */ |
| 7066 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 7067 | /* Compute the data length of this CRyPTO frame. */ |
| 7068 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 7069 | TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)", |
| 7070 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen); |
| 7071 | if (!dlen) |
| 7072 | continue; |
| 7073 | |
| 7074 | /* CRYPTO frame length. */ |
| 7075 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 7076 | TRACE_DEVEL(" CRYPTO frame length (flen)", |
| 7077 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 7078 | /* Add the CRYPTO data length and its encoded length to the packet |
| 7079 | * length and the length of this length. |
| 7080 | */ |
| 7081 | *len += flen; |
| 7082 | room -= flen; |
| 7083 | if (dlen == cf->crypto.len) { |
| 7084 | /* <cf> CRYPTO data have been consumed. */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7085 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7086 | LIST_APPEND(outlist, &cf->list); |
| 7087 | } |
| 7088 | else { |
| 7089 | struct quic_frame *new_cf; |
| 7090 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 7091 | new_cf = qc_frm_alloc(QUIC_FT_CRYPTO); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7092 | if (!new_cf) { |
| 7093 | TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
| 7094 | continue; |
| 7095 | } |
| 7096 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7097 | new_cf->crypto.len = dlen; |
| 7098 | new_cf->crypto.offset = cf->crypto.offset; |
| 7099 | new_cf->crypto.qel = qel; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 7100 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7101 | if (cf->origin) { |
| 7102 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 7103 | /* This <cf> frame was duplicated */ |
| 7104 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 7105 | new_cf->origin = cf->origin; |
Frédéric Lécaille | dd41946 | 2023-01-26 15:07:39 +0100 | [diff] [blame] | 7106 | /* Detach the remaining CRYPTO frame from its original frame */ |
| 7107 | LIST_DEL_INIT(&cf->ref); |
| 7108 | cf->origin = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7109 | } |
| 7110 | LIST_APPEND(outlist, &new_cf->list); |
| 7111 | /* Consume <dlen> bytes of the current frame. */ |
| 7112 | cf->crypto.len -= dlen; |
| 7113 | cf->crypto.offset += dlen; |
| 7114 | } |
| 7115 | break; |
| 7116 | |
| 7117 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Amaury Denoyelle | c8a0efb | 2023-02-22 10:44:27 +0100 | [diff] [blame] | 7118 | if (cf->stream.dup) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7119 | struct eb64_node *node = NULL; |
| 7120 | struct qc_stream_desc *stream_desc = NULL; |
| 7121 | struct quic_stream *strm = &cf->stream; |
| 7122 | |
| 7123 | /* As this frame has been already lost, ensure the stream is always |
| 7124 | * available or the range of this frame is not consumed before |
| 7125 | * resending it. |
| 7126 | */ |
| 7127 | node = eb64_lookup(&qc->streams_by_id, strm->id); |
| 7128 | if (!node) { |
| 7129 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7130 | qc_frm_free(&cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7131 | continue; |
| 7132 | } |
| 7133 | |
| 7134 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 7135 | if (strm->offset.key + strm->len <= stream_desc->ack_offset) { |
| 7136 | TRACE_DEVEL("ignored frame frame in already acked range", |
| 7137 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7138 | qc_frm_free(&cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7139 | continue; |
| 7140 | } |
| 7141 | else if (strm->offset.key < stream_desc->ack_offset) { |
| 7142 | strm->offset.key = stream_desc->ack_offset; |
| 7143 | TRACE_DEVEL("updated partially acked frame", |
| 7144 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
| 7145 | } |
| 7146 | } |
| 7147 | /* Note that these frames are accepted in short packets only without |
| 7148 | * "Length" packet field. Here, <*len> is used only to compute the |
| 7149 | * sum of the lengths of the already built frames for this packet. |
| 7150 | * |
| 7151 | * Compute the length of this STREAM frame "header" made a all the field |
| 7152 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 7153 | */ |
| 7154 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
| 7155 | ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0); |
| 7156 | /* Compute the data length of this STREAM frame. */ |
| 7157 | avail_room = room - hlen - *len; |
| 7158 | if ((ssize_t)avail_room <= 0) |
| 7159 | continue; |
| 7160 | |
| 7161 | TRACE_DEVEL(" New STREAM frame build (room, len)", |
| 7162 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 7163 | |
| 7164 | /* hlen contains STREAM id and offset. Ensure there is |
| 7165 | * enough room for length field. |
| 7166 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7167 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 7168 | dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz), |
| 7169 | cf->stream.len); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7170 | dlen_sz = quic_int_getsize(dlen); |
| 7171 | flen = hlen + dlen_sz + dlen; |
| 7172 | } |
| 7173 | else { |
| 7174 | dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len); |
| 7175 | flen = hlen + dlen; |
| 7176 | } |
Amaury Denoyelle | f2f08f8 | 2023-02-03 18:39:06 +0100 | [diff] [blame] | 7177 | |
| 7178 | if (cf->stream.len && !dlen) { |
| 7179 | /* Only a small gap is left on buffer, not |
| 7180 | * enough to encode the STREAM data length. |
| 7181 | */ |
| 7182 | continue; |
| 7183 | } |
| 7184 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7185 | TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)", |
| 7186 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen); |
| 7187 | TRACE_DEVEL(" STREAM frame length (flen)", |
| 7188 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 7189 | /* Add the STREAM data length and its encoded length to the packet |
| 7190 | * length and the length of this length. |
| 7191 | */ |
| 7192 | *len += flen; |
| 7193 | room -= flen; |
| 7194 | if (dlen == cf->stream.len) { |
| 7195 | /* <cf> STREAM data have been consumed. */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7196 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7197 | LIST_APPEND(outlist, &cf->list); |
| 7198 | |
| 7199 | /* Do not notify MUX on retransmission. */ |
| 7200 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 7201 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 7202 | cf->stream.len, |
| 7203 | cf->stream.offset.key); |
| 7204 | } |
| 7205 | } |
| 7206 | else { |
| 7207 | struct quic_frame *new_cf; |
| 7208 | struct buffer cf_buf; |
| 7209 | |
Amaury Denoyelle | 40c24f1 | 2023-01-27 17:47:49 +0100 | [diff] [blame] | 7210 | new_cf = qc_frm_alloc(cf->type); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7211 | if (!new_cf) { |
| 7212 | TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
| 7213 | continue; |
| 7214 | } |
| 7215 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7216 | new_cf->stream.stream = cf->stream.stream; |
| 7217 | new_cf->stream.buf = cf->stream.buf; |
| 7218 | new_cf->stream.id = cf->stream.id; |
Amaury Denoyelle | 1dac018 | 2023-02-02 16:45:07 +0100 | [diff] [blame] | 7219 | new_cf->stream.offset = cf->stream.offset; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7220 | new_cf->stream.len = dlen; |
| 7221 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 7222 | /* FIN bit reset */ |
| 7223 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 7224 | new_cf->stream.data = cf->stream.data; |
Amaury Denoyelle | c8a0efb | 2023-02-22 10:44:27 +0100 | [diff] [blame] | 7225 | new_cf->stream.dup = cf->stream.dup; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 7226 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7227 | if (cf->origin) { |
| 7228 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 7229 | /* This <cf> frame was duplicated */ |
| 7230 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 7231 | new_cf->origin = cf->origin; |
Frédéric Lécaille | dd41946 | 2023-01-26 15:07:39 +0100 | [diff] [blame] | 7232 | /* Detach this STREAM frame from its origin */ |
| 7233 | LIST_DEL_INIT(&cf->ref); |
| 7234 | cf->origin = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7235 | } |
| 7236 | LIST_APPEND(outlist, &new_cf->list); |
| 7237 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 7238 | /* Consume <dlen> bytes of the current frame. */ |
| 7239 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 7240 | b_size(cf->stream.buf), |
| 7241 | (char *)cf->stream.data - b_orig(cf->stream.buf), 0); |
| 7242 | cf->stream.len -= dlen; |
| 7243 | cf->stream.offset.key += dlen; |
| 7244 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
| 7245 | |
| 7246 | /* Do not notify MUX on retransmission. */ |
| 7247 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 7248 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 7249 | new_cf->stream.len, |
| 7250 | new_cf->stream.offset.key); |
| 7251 | } |
| 7252 | } |
| 7253 | |
| 7254 | /* TODO the MUX is notified about the frame sending via |
| 7255 | * previous qcc_streams_sent_done call. However, the |
| 7256 | * sending can fail later, for example if the sendto |
| 7257 | * system call returns an error. As the MUX has been |
| 7258 | * notified, the transport layer is responsible to |
| 7259 | * bufferize and resent the announced data later. |
| 7260 | */ |
| 7261 | |
| 7262 | break; |
| 7263 | |
| 7264 | default: |
| 7265 | flen = qc_frm_len(cf); |
| 7266 | BUG_ON(!flen); |
| 7267 | if (flen > room) |
| 7268 | continue; |
| 7269 | |
| 7270 | *len += flen; |
| 7271 | room -= flen; |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7272 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7273 | LIST_APPEND(outlist, &cf->list); |
| 7274 | break; |
| 7275 | } |
| 7276 | |
| 7277 | /* Successful status as soon as a frame could be built */ |
| 7278 | ret = 1; |
| 7279 | } |
| 7280 | |
| 7281 | leave: |
| 7282 | TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc); |
| 7283 | return ret; |
| 7284 | } |
| 7285 | |
| 7286 | /* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out> |
| 7287 | * is used as return parameter and should be zero'ed by the caller. |
| 7288 | */ |
| 7289 | static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel, |
| 7290 | struct quic_frame *out) |
| 7291 | { |
| 7292 | /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels |
| 7293 | * |
| 7294 | * A CONNECTION_CLOSE frame should be sent in several packets with |
| 7295 | * different encryption levels depending on the client context. This is |
| 7296 | * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for |
| 7297 | * more details on how to implement it. |
| 7298 | */ |
| 7299 | TRACE_ENTER(QUIC_EV_CONN_BFRM, qc); |
| 7300 | |
| 7301 | |
| 7302 | if (qc->err.app) { |
| 7303 | if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 7304 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 7305 | /* RFC 9000 10.2.3. Immediate Close during the Handshake |
| 7306 | * |
| 7307 | * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake |
| 7308 | * packet could expose application state or be used to alter application |
| 7309 | * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a |
| 7310 | * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or |
| 7311 | * Handshake packets. Otherwise, information about the application |
| 7312 | * state might be revealed. Endpoints MUST clear the value of the |
| 7313 | * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when |
| 7314 | * converting to a CONNECTION_CLOSE of type 0x1c. |
| 7315 | */ |
| 7316 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 7317 | out->connection_close.error_code = QC_ERR_APPLICATION_ERROR; |
| 7318 | out->connection_close.reason_phrase_len = 0; |
| 7319 | } |
| 7320 | else { |
| 7321 | out->type = QUIC_FT_CONNECTION_CLOSE_APP; |
| 7322 | out->connection_close.error_code = qc->err.code; |
| 7323 | } |
| 7324 | } |
| 7325 | else { |
| 7326 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 7327 | out->connection_close.error_code = qc->err.code; |
| 7328 | } |
| 7329 | TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc); |
| 7330 | |
| 7331 | } |
| 7332 | |
| 7333 | /* This function builds a clear packet from <pkt> information (its type) |
| 7334 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 7335 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 7336 | * filling the buffer with as much frames as possible from <frms> list of |
| 7337 | * prebuilt frames. |
| 7338 | * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are |
| 7339 | * reserved so that to ensure there is enough room to build this AEAD TAG after |
| 7340 | * having returned from this function. |
| 7341 | * This function also updates the value of <buf_pn> pointer to point to the packet |
| 7342 | * number field in this packet. <pn_len> will also have the packet number |
| 7343 | * length as value. |
| 7344 | * |
| 7345 | * Return 1 if succeeded (enough room to buile this packet), O if not. |
| 7346 | */ |
| 7347 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 7348 | size_t dglen, struct quic_tx_packet *pkt, |
| 7349 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
| 7350 | int force_ack, int padding, int cc, int probe, |
| 7351 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 7352 | const struct quic_version *ver, struct list *frms) |
| 7353 | { |
| 7354 | unsigned char *beg, *payload; |
| 7355 | size_t len, len_sz, len_frms, padding_len; |
| 7356 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 7357 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
| 7358 | struct quic_frame cc_frm = { }; |
| 7359 | size_t ack_frm_len, head_len; |
| 7360 | int64_t rx_largest_acked_pn; |
| 7361 | int add_ping_frm; |
| 7362 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 7363 | struct quic_frame *cf; |
| 7364 | int must_ack, ret = 0; |
| 7365 | int nb_aepkts_since_last_ack; |
| 7366 | |
| 7367 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 7368 | |
| 7369 | /* Length field value with CRYPTO frames if present. */ |
| 7370 | len_frms = 0; |
| 7371 | beg = pos; |
| 7372 | /* When not probing, and no immediate close is required, reduce the size of this |
| 7373 | * buffer to respect the congestion controller window. |
| 7374 | * This size will be limited if we have ack-eliciting frames to send from <frms>. |
| 7375 | */ |
| 7376 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
| 7377 | size_t path_room; |
| 7378 | |
| 7379 | path_room = quic_path_prep_data(qc->path); |
| 7380 | if (end - beg > path_room) |
| 7381 | end = beg + path_room; |
| 7382 | } |
| 7383 | |
| 7384 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 7385 | * length field if any. |
| 7386 | */ |
| 7387 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 7388 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 7389 | goto no_room; |
| 7390 | |
| 7391 | end -= QUIC_TLS_TAG_LEN; |
| 7392 | rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn; |
| 7393 | /* packet number length */ |
| 7394 | *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn); |
| 7395 | /* Build the header */ |
| 7396 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
| 7397 | !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) || |
| 7398 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
| 7399 | !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver))) |
| 7400 | goto no_room; |
| 7401 | |
| 7402 | /* Encode the token length (0) for an Initial packet. */ |
| 7403 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
| 7404 | *pos++ = 0; |
| 7405 | head_len = pos - beg; |
| 7406 | /* Build an ACK frame if required. */ |
| 7407 | ack_frm_len = 0; |
| 7408 | nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack; |
| 7409 | must_ack = !qel->pktns->tx.pto_probe && |
| 7410 | (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 7411 | (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK))); |
| 7412 | if (must_ack) { |
| 7413 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
| 7414 | BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root)); |
| 7415 | ack_frm.tx_ack.arngs = arngs; |
| 7416 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 7417 | qel->pktns->tx.ack_delay = |
| 7418 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 7419 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 7420 | } |
| 7421 | ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay; |
| 7422 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 7423 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 7424 | * that from here, we do not know if we will have to send a PING frame. |
| 7425 | * This will be decided after having computed the ack-eliciting frames |
| 7426 | * to be added to this packet. |
| 7427 | */ |
| 7428 | ack_frm_len = quic_ack_frm_reduce_sz(qc, &ack_frm, end - 1 - *pn_len - pos); |
| 7429 | if (!ack_frm_len) |
| 7430 | goto no_room; |
| 7431 | } |
| 7432 | |
| 7433 | /* Length field value without the ack-eliciting frames. */ |
| 7434 | len = ack_frm_len + *pn_len; |
| 7435 | len_frms = 0; |
| 7436 | if (!cc && !LIST_ISEMPTY(frms)) { |
| 7437 | ssize_t room = end - pos; |
| 7438 | |
| 7439 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 7440 | /* Initialize the length of the frames built below to <len>. |
| 7441 | * If any frame could be successfully built by qc_build_frms(), |
| 7442 | * we will have len_frms > len. |
| 7443 | */ |
| 7444 | len_frms = len; |
| 7445 | if (!qc_build_frms(&frm_list, frms, |
| 7446 | end - pos, &len_frms, pos - beg, qel, qc)) { |
| 7447 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
| 7448 | qc, NULL, NULL, &room); |
| 7449 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 7450 | goto no_room; |
| 7451 | } |
| 7452 | } |
| 7453 | |
| 7454 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 7455 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 7456 | * for the encryption tag. It must be taken into an account for the length |
| 7457 | * of this packet. |
| 7458 | */ |
| 7459 | if (len_frms) |
| 7460 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 7461 | else |
| 7462 | len += QUIC_TLS_TAG_LEN; |
| 7463 | /* CONNECTION_CLOSE frame */ |
| 7464 | if (cc) { |
| 7465 | qc_build_cc_frm(qc, qel, &cc_frm); |
| 7466 | len += qc_frm_len(&cc_frm); |
| 7467 | } |
| 7468 | add_ping_frm = 0; |
| 7469 | padding_len = 0; |
| 7470 | len_sz = quic_int_getsize(len); |
| 7471 | /* Add this packet size to <dglen> */ |
| 7472 | dglen += head_len + len_sz + len; |
Frédéric Lécaille | ec93721 | 2023-03-03 17:34:41 +0100 | [diff] [blame] | 7473 | /* Note that <padding> is true only when building an Handshake packet |
| 7474 | * coalesced to an Initial packet. |
| 7475 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7476 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 7477 | /* This is a maximum padding size */ |
| 7478 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 7479 | /* The length field value is of this packet is <len> + <padding_len> |
| 7480 | * the size of which may be greater than the initial computed size |
| 7481 | * <len_sz>. So, let's deduce the difference between these to packet |
| 7482 | * sizes from <padding_len>. |
| 7483 | */ |
| 7484 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 7485 | len += padding_len; |
| 7486 | } |
Frédéric Lécaille | 5faf577 | 2023-02-16 17:30:53 +0100 | [diff] [blame] | 7487 | else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) { |
| 7488 | len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms; |
| 7489 | } |
| 7490 | else if (LIST_ISEMPTY(&frm_list)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7491 | if (qel->pktns->tx.pto_probe) { |
| 7492 | /* If we cannot send a frame, we send a PING frame. */ |
| 7493 | add_ping_frm = 1; |
| 7494 | len += 1; |
Frédéric Lécaille | ec93721 | 2023-03-03 17:34:41 +0100 | [diff] [blame] | 7495 | dglen += 1; |
| 7496 | /* Note that only we are in the case where this Initial packet |
| 7497 | * is not coalesced to an Handshake packet. We must directly |
| 7498 | * pad the datragram. |
| 7499 | */ |
| 7500 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 7501 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 7502 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 7503 | len += padding_len; |
| 7504 | } |
| 7505 | } |
| 7506 | else { |
| 7507 | /* If there is no frame at all to follow, add at least a PADDING frame. */ |
| 7508 | if (!ack_frm_len && !cc) |
| 7509 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7510 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7511 | } |
| 7512 | |
| 7513 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 7514 | goto no_room; |
| 7515 | |
| 7516 | /* Packet number field address. */ |
| 7517 | *buf_pn = pos; |
| 7518 | |
| 7519 | /* Packet number encoding. */ |
| 7520 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 7521 | goto no_room; |
| 7522 | |
| 7523 | /* payload building (ack-eliciting or not frames) */ |
| 7524 | payload = pos; |
| 7525 | if (ack_frm_len) { |
| 7526 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 7527 | goto no_room; |
| 7528 | |
| 7529 | pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns); |
| 7530 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
| 7531 | } |
| 7532 | |
| 7533 | /* Ack-eliciting frames */ |
| 7534 | if (!LIST_ISEMPTY(&frm_list)) { |
| 7535 | struct quic_frame *tmp_cf; |
| 7536 | list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) { |
| 7537 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
| 7538 | ssize_t room = end - pos; |
| 7539 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
| 7540 | qc, NULL, NULL, &room); |
| 7541 | /* Note that <cf> was added from <frms> to <frm_list> list by |
| 7542 | * qc_build_frms(). |
| 7543 | */ |
Amaury Denoyelle | 57b3eaa | 2023-02-02 16:12:09 +0100 | [diff] [blame] | 7544 | LIST_DEL_INIT(&cf->list); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7545 | LIST_INSERT(frms, &cf->list); |
| 7546 | continue; |
| 7547 | } |
| 7548 | |
| 7549 | quic_tx_packet_refinc(pkt); |
| 7550 | cf->pkt = pkt; |
| 7551 | } |
| 7552 | } |
| 7553 | |
| 7554 | /* Build a PING frame if needed. */ |
| 7555 | if (add_ping_frm) { |
| 7556 | frm.type = QUIC_FT_PING; |
| 7557 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 7558 | goto no_room; |
| 7559 | } |
| 7560 | |
| 7561 | /* Build a CONNECTION_CLOSE frame if needed. */ |
| 7562 | if (cc) { |
| 7563 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 7564 | goto no_room; |
| 7565 | |
| 7566 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 7567 | } |
| 7568 | |
| 7569 | /* Build a PADDING frame if needed. */ |
| 7570 | if (padding_len) { |
| 7571 | frm.type = QUIC_FT_PADDING; |
| 7572 | frm.padding.len = padding_len; |
| 7573 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 7574 | goto no_room; |
| 7575 | } |
| 7576 | |
| 7577 | if (pos == payload) { |
| 7578 | /* No payload was built because of congestion control */ |
| 7579 | TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc); |
| 7580 | goto no_room; |
| 7581 | } |
| 7582 | |
| 7583 | /* If this packet is ack-eliciting and we are probing let's |
| 7584 | * decrement the PTO probe counter. |
| 7585 | */ |
| 7586 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 7587 | qel->pktns->tx.pto_probe) |
| 7588 | qel->pktns->tx.pto_probe--; |
| 7589 | |
| 7590 | pkt->len = pos - beg; |
| 7591 | LIST_SPLICE(&pkt->frms, &frm_list); |
| 7592 | |
| 7593 | ret = 1; |
| 7594 | TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt); |
| 7595 | leave: |
| 7596 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 7597 | return ret; |
| 7598 | |
| 7599 | no_room: |
| 7600 | /* Replace the pre-built frames which could not be add to this packet */ |
| 7601 | LIST_SPLICE(frms, &frm_list); |
| 7602 | TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 7603 | goto leave; |
| 7604 | } |
| 7605 | |
| 7606 | static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type) |
| 7607 | { |
| 7608 | pkt->type = type; |
| 7609 | pkt->len = 0; |
| 7610 | pkt->in_flight_len = 0; |
| 7611 | pkt->pn_node.key = (uint64_t)-1; |
| 7612 | LIST_INIT(&pkt->frms); |
| 7613 | pkt->time_sent = TICK_ETERNITY; |
| 7614 | pkt->next = NULL; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 7615 | pkt->prev = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7616 | pkt->largest_acked_pn = -1; |
| 7617 | pkt->flags = 0; |
| 7618 | pkt->refcnt = 0; |
| 7619 | } |
| 7620 | |
| 7621 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 7622 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 7623 | * of prebuilt frames. |
| 7624 | * |
| 7625 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 7626 | * -1 if there was not enough room to build a packet. |
| 7627 | * XXX NOTE XXX |
| 7628 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 7629 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 7630 | * control window limitation. |
| 7631 | */ |
| 7632 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 7633 | const unsigned char *buf_end, |
| 7634 | struct quic_enc_level *qel, |
| 7635 | struct quic_tls_ctx *tls_ctx, struct list *frms, |
| 7636 | struct quic_conn *qc, const struct quic_version *ver, |
| 7637 | size_t dglen, int pkt_type, int force_ack, |
| 7638 | int padding, int probe, int cc, int *err) |
| 7639 | { |
| 7640 | struct quic_tx_packet *ret_pkt = NULL; |
| 7641 | /* The pointer to the packet number field. */ |
| 7642 | unsigned char *buf_pn; |
| 7643 | unsigned char *beg, *end, *payload; |
| 7644 | int64_t pn; |
| 7645 | size_t pn_len, payload_len, aad_len; |
| 7646 | struct quic_tx_packet *pkt; |
| 7647 | |
| 7648 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel); |
| 7649 | *err = 0; |
| 7650 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 7651 | if (!pkt) { |
| 7652 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc); |
| 7653 | *err = -2; |
| 7654 | goto err; |
| 7655 | } |
| 7656 | |
| 7657 | quic_tx_packet_init(pkt, pkt_type); |
| 7658 | beg = *pos; |
| 7659 | pn_len = 0; |
| 7660 | buf_pn = NULL; |
| 7661 | |
| 7662 | pn = qel->pktns->tx.next_pn + 1; |
| 7663 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
| 7664 | force_ack, padding, cc, probe, qel, qc, ver, frms)) { |
| 7665 | // trace already emitted by function above |
| 7666 | *err = -1; |
| 7667 | goto err; |
| 7668 | } |
| 7669 | |
| 7670 | end = beg + pkt->len; |
| 7671 | payload = buf_pn + pn_len; |
| 7672 | payload_len = end - payload; |
| 7673 | aad_len = payload - beg; |
| 7674 | |
| 7675 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) { |
| 7676 | // trace already emitted by function above |
| 7677 | *err = -2; |
| 7678 | goto err; |
| 7679 | } |
| 7680 | |
| 7681 | end += QUIC_TLS_TAG_LEN; |
| 7682 | pkt->len += QUIC_TLS_TAG_LEN; |
| 7683 | if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) { |
| 7684 | // trace already emitted by function above |
| 7685 | *err = -2; |
| 7686 | goto err; |
| 7687 | } |
| 7688 | |
| 7689 | /* Consume a packet number */ |
| 7690 | qel->pktns->tx.next_pn++; |
| 7691 | qc->tx.prep_bytes += pkt->len; |
| 7692 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) { |
| 7693 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 7694 | TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc); |
| 7695 | } |
| 7696 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 7697 | *pos = end; |
| 7698 | /* Attach the built packet to its tree. */ |
| 7699 | pkt->pn_node.key = pn; |
| 7700 | /* Set the packet in fligth length for in flight packet only. */ |
| 7701 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
| 7702 | pkt->in_flight_len = pkt->len; |
| 7703 | qc->path->prep_in_flight += pkt->len; |
| 7704 | } |
| 7705 | /* Always reset this flags */ |
| 7706 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 7707 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 7708 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 7709 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 7710 | } |
| 7711 | |
| 7712 | pkt->pktns = qel->pktns; |
| 7713 | |
| 7714 | ret_pkt = pkt; |
| 7715 | leave: |
| 7716 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt); |
| 7717 | return ret_pkt; |
| 7718 | |
| 7719 | err: |
| 7720 | /* TODO: what about the frames which have been built |
| 7721 | * for this packet. |
| 7722 | */ |
| 7723 | free_quic_tx_packet(qc, pkt); |
| 7724 | goto leave; |
| 7725 | } |
| 7726 | |
| 7727 | |
| 7728 | static void __quic_conn_init(void) |
| 7729 | { |
| 7730 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 7731 | } |
| 7732 | INITCALL0(STG_REGISTER, __quic_conn_init); |
| 7733 | |
| 7734 | static void __quic_conn_deinit(void) |
| 7735 | { |
| 7736 | BIO_meth_free(ha_quic_meth); |
| 7737 | } |
| 7738 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
| 7739 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7740 | /* Handle a new <dgram> received. Parse each QUIC packets and copied their |
| 7741 | * content to a quic-conn instance. The datagram content can be released after |
| 7742 | * this function. |
| 7743 | * |
| 7744 | * If datagram has been received on a quic-conn owned FD, <from_qc> must be set |
| 7745 | * to the connection instance. <li> is the attached listener. The caller is |
| 7746 | * responsible to ensure that the first packet is destined to this connection |
| 7747 | * by comparing CIDs. |
| 7748 | * |
| 7749 | * If datagram has been received on a receiver FD, <from_qc> will be NULL. This |
| 7750 | * function will thus retrieve the connection from the CID tree or allocate a |
| 7751 | * new one if possible. <li> is the listener attached to the receiver. |
| 7752 | * |
| 7753 | * Returns 0 on success else non-zero. If an error happens, some packets from |
| 7754 | * the datagram may not have been parsed. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7755 | */ |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7756 | int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc, |
| 7757 | struct listener *li) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7758 | { |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7759 | struct quic_rx_packet *pkt; |
| 7760 | struct quic_conn *qc = NULL; |
| 7761 | unsigned char *pos, *end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7762 | struct list *tasklist_head = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7763 | |
| 7764 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 7765 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7766 | pos = dgram->buf; |
| 7767 | end = pos + dgram->len; |
| 7768 | do { |
| 7769 | /* TODO replace zalloc -> alloc. */ |
| 7770 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 7771 | if (!pkt) { |
| 7772 | TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT); |
| 7773 | goto err; |
| 7774 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7775 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7776 | pkt->version = NULL; |
| 7777 | pkt->pn_offset = 0; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7778 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7779 | /* Set flag if pkt is the first one in dgram. */ |
| 7780 | if (pos == dgram->buf) |
| 7781 | pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7782 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7783 | LIST_INIT(&pkt->qc_rx_pkt_list); |
| 7784 | pkt->time_received = now_ms; |
| 7785 | quic_rx_packet_refinc(pkt); |
| 7786 | if (quic_rx_pkt_parse(pkt, pos, end, dgram, li)) |
| 7787 | goto next; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7788 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7789 | /* Search quic-conn instance for first packet of the datagram. |
| 7790 | * quic_rx_packet_parse() is responsible to discard packets |
| 7791 | * with different DCID as the first one in the same datagram. |
| 7792 | */ |
| 7793 | if (!qc) { |
| 7794 | qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li); |
| 7795 | /* qc is NULL if receiving a non Initial packet for an |
| 7796 | * unknown connection. |
| 7797 | */ |
| 7798 | if (!qc) { |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7799 | /* Skip the entire datagram. */ |
| 7800 | pkt->len = end - pos; |
| 7801 | goto next; |
| 7802 | } |
| 7803 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7804 | dgram->qc = qc; |
| 7805 | } |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7806 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7807 | if (qc_rx_check_closing(qc, pkt)) { |
| 7808 | /* Skip the entire datagram. */ |
| 7809 | pkt->len = end - pos; |
| 7810 | goto next; |
| 7811 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7812 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7813 | /* Detect QUIC connection migration. */ |
Frédéric Lécaille | f676954 | 2023-01-12 10:36:26 +0100 | [diff] [blame] | 7814 | if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) { |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7815 | if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) { |
| 7816 | /* Skip the entire datagram. */ |
| 7817 | TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc); |
| 7818 | pkt->len = end - pos; |
| 7819 | goto next; |
| 7820 | } |
| 7821 | } |
| 7822 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7823 | qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7824 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7825 | next: |
| 7826 | pos += pkt->len; |
| 7827 | quic_rx_packet_refdec(pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7828 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7829 | /* Free rejected packets */ |
| 7830 | if (!pkt->refcnt) { |
| 7831 | BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list)); |
| 7832 | pool_free(pool_head_quic_rx_packet, pkt); |
| 7833 | } |
| 7834 | } while (pos < end); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7835 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7836 | /* Increasing the received bytes counter by the UDP datagram length |
| 7837 | * if this datagram could be associated to a connection. |
| 7838 | */ |
| 7839 | if (dgram->qc) |
| 7840 | dgram->qc->rx.bytes += dgram->len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7841 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7842 | /* This must never happen. */ |
| 7843 | BUG_ON(pos > end); |
| 7844 | BUG_ON(pos < end || pos > dgram->buf + dgram->len); |
| 7845 | /* Mark this datagram as consumed */ |
| 7846 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7847 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7848 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 7849 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7850 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7851 | err: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7852 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7853 | return -1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7854 | } |
| 7855 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 7856 | /* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local |
| 7857 | * CIDs. This can be used to determine if a datagram is addressed to the right |
| 7858 | * connection instance. |
| 7859 | * |
| 7860 | * Returns a boolean value. |
| 7861 | */ |
| 7862 | int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len) |
| 7863 | { |
| 7864 | struct ebmb_node *node; |
| 7865 | struct quic_connection_id *id; |
| 7866 | |
| 7867 | /* For ODCID, address is concatenated to it after qc.odcid.len so this |
| 7868 | * comparison is safe. |
| 7869 | */ |
| 7870 | if ((qc->scid.len == dcid_len && |
| 7871 | memcmp(qc->scid.data, dcid, dcid_len) == 0) || |
| 7872 | (qc->odcid.len == dcid_len && |
Frédéric Lécaille | 07846cb | 2023-02-13 16:14:24 +0100 | [diff] [blame] | 7873 | memcmp(qc->odcid.data, dcid, dcid_len) == 0)) { |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 7874 | return 1; |
| 7875 | } |
| 7876 | |
| 7877 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, dcid, dcid_len); |
| 7878 | if (node) { |
| 7879 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 7880 | if (qc == id->qc) |
| 7881 | return 1; |
| 7882 | } |
| 7883 | |
| 7884 | return 0; |
| 7885 | } |
| 7886 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7887 | /* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 7888 | * Returns 1 if succeeded, 0 if not. |
| 7889 | */ |
| 7890 | int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 7891 | unsigned char **dcid, size_t *dcid_len) |
| 7892 | { |
| 7893 | int ret = 0, long_header; |
| 7894 | size_t minlen, skip; |
| 7895 | |
| 7896 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 7897 | |
| 7898 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
| 7899 | TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT); |
| 7900 | goto err; |
| 7901 | } |
| 7902 | |
| 7903 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
| 7904 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 7905 | QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN; |
| 7906 | skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF; |
| 7907 | if (end - buf < minlen) |
| 7908 | goto err; |
| 7909 | |
| 7910 | buf += skip; |
| 7911 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 7912 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 7913 | goto err; |
| 7914 | |
| 7915 | *dcid = buf; |
| 7916 | |
| 7917 | ret = 1; |
| 7918 | leave: |
| 7919 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 7920 | return ret; |
| 7921 | |
| 7922 | err: |
| 7923 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT); |
| 7924 | goto leave; |
| 7925 | } |
| 7926 | |
| 7927 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 7928 | void qc_notify_close(struct quic_conn *qc) |
| 7929 | { |
| 7930 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 7931 | |
| 7932 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 7933 | goto leave; |
| 7934 | |
| 7935 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 7936 | /* wake up the MUX */ |
| 7937 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) { |
| 7938 | TRACE_STATE("connection closure notidfied to mux", |
| 7939 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7940 | qc->conn->mux->wake(qc->conn); |
| 7941 | } |
| 7942 | else |
| 7943 | TRACE_STATE("connection closure not notidfied to mux", |
| 7944 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7945 | leave: |
| 7946 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 7947 | } |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7948 | |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 7949 | /* Wake-up upper layer if waiting for send to be ready. |
| 7950 | * |
| 7951 | * Returns 1 if upper layer has been woken up else 0. |
| 7952 | */ |
| 7953 | int qc_notify_send(struct quic_conn *qc) |
| 7954 | { |
| 7955 | if (qc->subs && qc->subs->events & SUB_RETRY_SEND) { |
Amaury Denoyelle | caa1654 | 2023-02-28 15:11:26 +0100 | [diff] [blame] | 7956 | if (quic_path_prep_data(qc->path) && |
| 7957 | (!qc_test_fd(qc) || !fd_send_active(qc->fd))) { |
Amaury Denoyelle | e0fe118 | 2023-02-28 15:08:59 +0100 | [diff] [blame] | 7958 | tasklet_wakeup(qc->subs->tasklet); |
| 7959 | qc->subs->events &= ~SUB_RETRY_SEND; |
| 7960 | if (!qc->subs->events) |
| 7961 | qc->subs = NULL; |
| 7962 | |
| 7963 | return 1; |
| 7964 | } |
| 7965 | } |
| 7966 | |
| 7967 | return 0; |
| 7968 | } |
| 7969 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7970 | |
| 7971 | /* appctx context used by "show quic" command */ |
| 7972 | struct show_quic_ctx { |
| 7973 | unsigned int epoch; |
| 7974 | struct bref bref; /* back-reference to the quic-conn being dumped */ |
| 7975 | unsigned int thr; |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 7976 | int flags; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7977 | }; |
| 7978 | |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 7979 | #define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */ |
| 7980 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7981 | static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private) |
| 7982 | { |
| 7983 | struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 7984 | |
| 7985 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 7986 | return 1; |
| 7987 | |
| 7988 | ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1); |
| 7989 | ctx->thr = 0; |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 7990 | ctx->flags = 0; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7991 | |
Amaury Denoyelle | 10a46de | 2023-02-09 18:18:45 +0100 | [diff] [blame] | 7992 | if (*args[2] && strcmp(args[2], "all") == 0) |
| 7993 | ctx->flags |= QC_CLI_FL_SHOW_ALL; |
| 7994 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 7995 | LIST_INIT(&ctx->bref.users); |
| 7996 | |
| 7997 | return 0; |
| 7998 | } |
| 7999 | |
| 8000 | static int cli_io_handler_dump_quic(struct appctx *appctx) |
| 8001 | { |
| 8002 | struct show_quic_ctx *ctx = appctx->svcctx; |
| 8003 | struct stconn *sc = appctx_sc(appctx); |
| 8004 | struct quic_conn *qc; |
Amaury Denoyelle | 1b0fc43 | 2023-02-01 17:05:10 +0100 | [diff] [blame] | 8005 | struct quic_enc_level *qel; |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 8006 | struct eb64_node *node; |
| 8007 | struct qc_stream_desc *stream; |
Amaury Denoyelle | b89c0e2 | 2023-02-01 17:04:26 +0100 | [diff] [blame] | 8008 | char bufaddr[INET6_ADDRSTRLEN], bufport[6]; |
Amaury Denoyelle | 58d9d5d | 2023-02-01 11:54:43 +0100 | [diff] [blame] | 8009 | int expire; |
| 8010 | unsigned char cid_len; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8011 | |
| 8012 | thread_isolate(); |
| 8013 | |
| 8014 | if (ctx->thr >= global.nbthread) |
| 8015 | goto done; |
| 8016 | |
| 8017 | if (unlikely(sc_ic(sc)->flags & CF_SHUTW)) { |
| 8018 | /* If we're forced to shut down, we might have to remove our |
| 8019 | * reference to the last stream being dumped. |
| 8020 | */ |
| 8021 | if (!LIST_ISEMPTY(&ctx->bref.users)) |
| 8022 | LIST_DEL_INIT(&ctx->bref.users); |
| 8023 | goto done; |
| 8024 | } |
| 8025 | |
| 8026 | chunk_reset(&trash); |
| 8027 | |
| 8028 | if (!LIST_ISEMPTY(&ctx->bref.users)) { |
| 8029 | /* Remove show_quic_ctx from previous quic_conn instance. */ |
| 8030 | LIST_DEL_INIT(&ctx->bref.users); |
| 8031 | } |
| 8032 | else if (!ctx->bref.ref) { |
| 8033 | /* First invocation. */ |
| 8034 | ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n; |
| 8035 | } |
| 8036 | |
| 8037 | while (1) { |
| 8038 | int done = 0; |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 8039 | int i; |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8040 | |
| 8041 | if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) { |
| 8042 | done = 1; |
| 8043 | } |
| 8044 | else { |
| 8045 | qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx); |
| 8046 | if ((int)(qc->qc_epoch - ctx->epoch) > 0) |
| 8047 | done = 1; |
| 8048 | } |
| 8049 | |
| 8050 | if (done) { |
| 8051 | ++ctx->thr; |
| 8052 | if (ctx->thr >= global.nbthread) |
| 8053 | break; |
| 8054 | ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n; |
| 8055 | continue; |
| 8056 | } |
| 8057 | |
Amaury Denoyelle | 10a46de | 2023-02-09 18:18:45 +0100 | [diff] [blame] | 8058 | if (!(ctx->flags & QC_CLI_FL_SHOW_ALL) && |
Amaury Denoyelle | 3f9758e | 2023-02-01 17:31:02 +0100 | [diff] [blame] | 8059 | qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) { |
| 8060 | ctx->bref.ref = qc->el_th_ctx.n; |
| 8061 | continue; |
| 8062 | } |
| 8063 | |
Amaury Denoyelle | 58d9d5d | 2023-02-01 11:54:43 +0100 | [diff] [blame] | 8064 | /* CIDs */ |
| 8065 | chunk_appendf(&trash, "* %p[%02u]: scid=", qc, qc->tid); |
| 8066 | for (cid_len = 0; cid_len < qc->scid.len; ++cid_len) |
| 8067 | chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]); |
| 8068 | while (cid_len++ < 20) |
| 8069 | chunk_appendf(&trash, ".."); |
| 8070 | |
| 8071 | chunk_appendf(&trash, " dcid="); |
| 8072 | for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len) |
| 8073 | chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]); |
| 8074 | while (cid_len++ < 20) |
| 8075 | chunk_appendf(&trash, ".."); |
| 8076 | |
| 8077 | chunk_appendf(&trash, "\n"); |
| 8078 | |
| 8079 | /* Connection state */ |
| 8080 | if (qc->flags & QUIC_FL_CONN_CLOSING) |
| 8081 | chunk_appendf(&trash, " st=closing "); |
| 8082 | else if (qc->flags & QUIC_FL_CONN_DRAINING) |
| 8083 | chunk_appendf(&trash, " st=draining "); |
| 8084 | else if (qc->state < QUIC_HS_ST_CONFIRMED) |
| 8085 | chunk_appendf(&trash, " st=handshake "); |
| 8086 | else |
| 8087 | chunk_appendf(&trash, " st=opened "); |
| 8088 | |
| 8089 | if (qc->mux_state == QC_MUX_NULL) |
| 8090 | chunk_appendf(&trash, "mux=null "); |
| 8091 | else if (qc->mux_state == QC_MUX_READY) |
| 8092 | chunk_appendf(&trash, "mux=ready "); |
| 8093 | else |
| 8094 | chunk_appendf(&trash, "mux=released "); |
| 8095 | |
| 8096 | expire = qc->idle_timer_task->expire; |
| 8097 | chunk_appendf(&trash, "expire=%02ds ", |
| 8098 | expire > now_ms ? (expire - now_ms) / 1000 : 0); |
| 8099 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8100 | chunk_appendf(&trash, "\n"); |
| 8101 | |
Amaury Denoyelle | b89c0e2 | 2023-02-01 17:04:26 +0100 | [diff] [blame] | 8102 | /* Socket */ |
| 8103 | chunk_appendf(&trash, " fd=%d", qc->fd); |
| 8104 | if (qc->local_addr.ss_family == AF_INET || |
| 8105 | qc->local_addr.ss_family == AF_INET6) { |
| 8106 | addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr)); |
| 8107 | port_to_str(&qc->local_addr, bufport, sizeof(bufport)); |
| 8108 | chunk_appendf(&trash, " from=%s:%s", bufaddr, bufport); |
| 8109 | |
| 8110 | addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr)); |
| 8111 | port_to_str(&qc->peer_addr, bufport, sizeof(bufport)); |
| 8112 | chunk_appendf(&trash, " to=%s:%s", bufaddr, bufport); |
| 8113 | } |
| 8114 | |
| 8115 | chunk_appendf(&trash, "\n"); |
| 8116 | |
Amaury Denoyelle | 1b0fc43 | 2023-02-01 17:05:10 +0100 | [diff] [blame] | 8117 | /* Encryption levels */ |
| 8118 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 8119 | chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu", |
| 8120 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 8121 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 8122 | chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n", |
| 8123 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 8124 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
| 8125 | chunk_appendf(&trash, " [0-rtt] rx.ackrng=%-6zu tx.inflight=%-6zu", |
| 8126 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 8127 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 8128 | chunk_appendf(&trash, " [1-rtt] rx.ackrng=%-6zu tx.inflight=%-6zu", |
| 8129 | qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight); |
| 8130 | |
| 8131 | chunk_appendf(&trash, "\n"); |
| 8132 | |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 8133 | /* Streams */ |
| 8134 | node = eb64_first(&qc->streams_by_id); |
| 8135 | i = 0; |
| 8136 | while (node) { |
| 8137 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 8138 | node = eb64_next(node); |
| 8139 | |
Amaury Denoyelle | a9de25a | 2023-02-10 09:25:22 +0100 | [diff] [blame] | 8140 | chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key); |
| 8141 | chunk_appendf(&trash, " off=%-8llu ack=%-8llu", |
| 8142 | (unsigned long long)stream->buf_offset, |
| 8143 | (unsigned long long)stream->ack_offset); |
Amaury Denoyelle | 2eda63b | 2023-02-01 17:05:36 +0100 | [diff] [blame] | 8144 | |
| 8145 | if (!(++i % 3)) { |
| 8146 | chunk_appendf(&trash, "\n"); |
| 8147 | i = 0; |
| 8148 | } |
| 8149 | } |
| 8150 | |
| 8151 | chunk_appendf(&trash, "\n"); |
| 8152 | |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8153 | if (applet_putchk(appctx, &trash) == -1) { |
| 8154 | /* Register show_quic_ctx to quic_conn instance. */ |
| 8155 | LIST_APPEND(&qc->back_refs, &ctx->bref.users); |
| 8156 | goto full; |
| 8157 | } |
| 8158 | |
| 8159 | ctx->bref.ref = qc->el_th_ctx.n; |
| 8160 | } |
| 8161 | |
| 8162 | done: |
| 8163 | thread_release(); |
| 8164 | return 1; |
| 8165 | |
| 8166 | full: |
| 8167 | thread_release(); |
| 8168 | return 0; |
| 8169 | } |
| 8170 | |
| 8171 | static void cli_release_show_quic(struct appctx *appctx) |
| 8172 | { |
| 8173 | struct show_quic_ctx *ctx = appctx->svcctx; |
| 8174 | |
| 8175 | if (ctx->thr < global.nbthread) { |
| 8176 | thread_isolate(); |
| 8177 | if (!LIST_ISEMPTY(&ctx->bref.users)) |
| 8178 | LIST_DEL_INIT(&ctx->bref.users); |
| 8179 | thread_release(); |
| 8180 | } |
| 8181 | } |
| 8182 | |
| 8183 | static struct cli_kw_list cli_kws = {{ }, { |
| 8184 | { { "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] | 8185 | {{},} |
Amaury Denoyelle | 15c7470 | 2023-02-01 10:18:26 +0100 | [diff] [blame] | 8186 | }}; |
| 8187 | |
| 8188 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 8189 | |
| 8190 | static void init_quic() |
| 8191 | { |
| 8192 | int thr; |
| 8193 | |
| 8194 | for (thr = 0; thr < MAX_THREADS; ++thr) |
| 8195 | LIST_INIT(&ha_thread_ctx[thr].quic_conns); |
| 8196 | } |
| 8197 | INITCALL0(STG_INIT, init_quic); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 8198 | |
| 8199 | /* |
| 8200 | * Local variables: |
| 8201 | * c-indent-level: 8 |
| 8202 | * c-basic-offset: 8 |
| 8203 | * End: |
| 8204 | */ |