Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QUIC protocol implementation. Lower layer with internal features implemented |
| 3 | * here such as QUIC encryption, idle timeout, acknowledgement and |
| 4 | * retransmission. |
| 5 | * |
| 6 | * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <haproxy/quic_conn.h> |
| 16 | |
| 17 | #define _GNU_SOURCE |
| 18 | #include <errno.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | #include <netinet/tcp.h> |
| 27 | |
| 28 | #include <import/ebmbtree.h> |
| 29 | |
| 30 | #include <haproxy/buf-t.h> |
| 31 | #include <haproxy/compat.h> |
| 32 | #include <haproxy/api.h> |
| 33 | #include <haproxy/debug.h> |
| 34 | #include <haproxy/tools.h> |
| 35 | #include <haproxy/ticks.h> |
| 36 | |
| 37 | #include <haproxy/connection.h> |
| 38 | #include <haproxy/fd.h> |
| 39 | #include <haproxy/freq_ctr.h> |
| 40 | #include <haproxy/global.h> |
| 41 | #include <haproxy/h3.h> |
| 42 | #include <haproxy/hq_interop.h> |
| 43 | #include <haproxy/log.h> |
| 44 | #include <haproxy/mux_quic.h> |
| 45 | #include <haproxy/ncbuf.h> |
| 46 | #include <haproxy/pipe.h> |
| 47 | #include <haproxy/proxy.h> |
| 48 | #include <haproxy/quic_cc.h> |
| 49 | #include <haproxy/quic_frame.h> |
| 50 | #include <haproxy/quic_enc.h> |
| 51 | #include <haproxy/quic_loss.h> |
| 52 | #include <haproxy/quic_sock.h> |
| 53 | #include <haproxy/quic_stats.h> |
| 54 | #include <haproxy/quic_stream.h> |
| 55 | #include <haproxy/quic_tp.h> |
| 56 | #include <haproxy/cbuf.h> |
| 57 | #include <haproxy/proto_quic.h> |
| 58 | #include <haproxy/quic_tls.h> |
| 59 | #include <haproxy/ssl_sock.h> |
| 60 | #include <haproxy/task.h> |
| 61 | #include <haproxy/trace.h> |
| 62 | |
| 63 | /* list of supported QUIC versions by this implementation */ |
| 64 | const struct quic_version quic_versions[] = { |
| 65 | { |
| 66 | .num = QUIC_PROTOCOL_VERSION_DRAFT_29, |
| 67 | .initial_salt = initial_salt_draft_29, |
| 68 | .initial_salt_len = sizeof initial_salt_draft_29, |
| 69 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
| 70 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
| 71 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
| 72 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
| 73 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
| 74 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
| 75 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
| 76 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
| 77 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT, |
| 78 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT, |
| 79 | }, |
| 80 | { |
| 81 | .num = QUIC_PROTOCOL_VERSION_1, |
| 82 | .initial_salt = initial_salt_v1, |
| 83 | .initial_salt_len = sizeof initial_salt_v1, |
| 84 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1, |
| 85 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1, |
| 86 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1, |
| 87 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1, |
| 88 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1, |
| 89 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1, |
| 90 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1, |
| 91 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1, |
| 92 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1, |
| 93 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1, |
| 94 | }, |
| 95 | { |
| 96 | .num = QUIC_PROTOCOL_VERSION_2_DRAFT, |
| 97 | .initial_salt = initial_salt_v2_draft, |
| 98 | .initial_salt_len = sizeof initial_salt_v2_draft, |
| 99 | .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2, |
| 100 | .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1, |
| 101 | .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2, |
| 102 | .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1, |
| 103 | .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2, |
| 104 | .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1, |
| 105 | .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2, |
| 106 | .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V2) - 1, |
| 107 | .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2_DRAFT, |
| 108 | .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2_DRAFT, |
| 109 | }, |
| 110 | }; |
| 111 | |
| 112 | /* The total number of supported versions */ |
| 113 | const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions; |
| 114 | /* Listener only preferred version */ |
| 115 | const struct quic_version *preferred_version; |
| 116 | |
| 117 | /* trace source and events */ |
| 118 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 119 | const struct trace_source *src, |
| 120 | const struct ist where, const struct ist func, |
| 121 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 122 | |
| 123 | static const struct trace_event quic_trace_events[] = { |
| 124 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 125 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 126 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 127 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 128 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 129 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 130 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
| 131 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
| 132 | { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" }, |
| 133 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 134 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 135 | { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" }, |
| 136 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 137 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 138 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 139 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 140 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 141 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 142 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 143 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 144 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 145 | { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" }, |
| 146 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 147 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 148 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 149 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 150 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
| 151 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 152 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 153 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 154 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 155 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 156 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
| 157 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
| 158 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 159 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
| 160 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 161 | { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." }, |
| 162 | { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."}, |
| 163 | { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"}, |
| 164 | { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"}, |
| 165 | { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"}, |
| 166 | { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"}, |
| 167 | { .mask = QUIC_EV_CONN_SUB, .name = "xprt_sub", .desc = "RX/TX subcription or unsubscription to QUIC xprt"}, |
Amaury Denoyelle | 5b41486 | 2022-10-24 17:40:37 +0200 | [diff] [blame] | 168 | { .mask = QUIC_EV_CONN_RCV, .name = "conn_recv", .desc = "RX on connection" }, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 169 | { /* end */ } |
| 170 | }; |
| 171 | |
| 172 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 173 | /* arg1 */ { /* already used by the connection */ }, |
| 174 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 175 | /* arg3 */ { }, |
| 176 | /* arg4 */ { } |
| 177 | }; |
| 178 | |
| 179 | static const struct name_desc quic_trace_decoding[] = { |
| 180 | #define QUIC_VERB_CLEAN 1 |
| 181 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 182 | { /* end */ } |
| 183 | }; |
| 184 | |
| 185 | |
| 186 | struct trace_source trace_quic = { |
| 187 | .name = IST("quic"), |
| 188 | .desc = "QUIC xprt", |
| 189 | .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */ |
| 190 | .default_cb = quic_trace, |
| 191 | .known_events = quic_trace_events, |
| 192 | .lockon_args = quic_trace_lockon_args, |
| 193 | .decoding = quic_trace_decoding, |
| 194 | .report_events = ~0, /* report everything by default */ |
| 195 | }; |
| 196 | |
| 197 | #define TRACE_SOURCE &trace_quic |
| 198 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 199 | |
| 200 | static BIO_METHOD *ha_quic_meth; |
| 201 | |
| 202 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ); |
| 203 | DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ); |
| 204 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
| 205 | "quic_conn_ctx", sizeof(struct ssl_sock_ctx)); |
| 206 | DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn)); |
| 207 | DECLARE_POOL(pool_head_quic_connection_id, |
| 208 | "quic_connnection_id", sizeof(struct quic_connection_id)); |
| 209 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
| 210 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet)); |
| 211 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet)); |
| 212 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm)); |
| 213 | DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf", sizeof(struct quic_crypto_buf)); |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 214 | DECLARE_STATIC_POOL(pool_head_quic_cstream, "quic_cstream", sizeof(struct quic_cstream)); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 215 | DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame)); |
| 216 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node)); |
| 217 | |
| 218 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
| 219 | struct quic_enc_level *qel, struct quic_tls_ctx *ctx, |
| 220 | struct list *frms, struct quic_conn *qc, |
| 221 | const struct quic_version *ver, size_t dglen, int pkt_type, |
| 222 | int force_ack, int padding, int probe, int cc, int *err); |
| 223 | struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state); |
| 224 | static void qc_idle_timer_do_rearm(struct quic_conn *qc); |
| 225 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read); |
| 226 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc); |
| 227 | static int quic_conn_init_timer(struct quic_conn *qc); |
| 228 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc); |
| 229 | |
| 230 | /* Only for debug purpose */ |
| 231 | struct enc_debug_info { |
| 232 | unsigned char *payload; |
| 233 | size_t payload_len; |
| 234 | unsigned char *aad; |
| 235 | size_t aad_len; |
| 236 | uint64_t pn; |
| 237 | }; |
| 238 | |
| 239 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 240 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 241 | unsigned char *payload, size_t payload_len, |
| 242 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 243 | { |
| 244 | edi->payload = payload; |
| 245 | edi->payload_len = payload_len; |
| 246 | edi->aad = aad; |
| 247 | edi->aad_len = aad_len; |
| 248 | edi->pn = pn; |
| 249 | } |
| 250 | |
| 251 | /* Trace callback for QUIC. |
| 252 | * These traces always expect that arg1, if non-null, is of type connection. |
| 253 | */ |
| 254 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 255 | const struct ist where, const struct ist func, |
| 256 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 257 | { |
| 258 | const struct quic_conn *qc = a1; |
| 259 | |
| 260 | if (qc) { |
| 261 | const struct quic_tls_ctx *tls_ctx; |
| 262 | |
| 263 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
| 264 | if (mask & QUIC_EV_CONN_INIT) { |
| 265 | chunk_appendf(&trace_buf, "\n odcid"); |
| 266 | quic_cid_dump(&trace_buf, &qc->odcid); |
| 267 | chunk_appendf(&trace_buf, "\n dcid"); |
| 268 | quic_cid_dump(&trace_buf, &qc->dcid); |
| 269 | chunk_appendf(&trace_buf, "\n scid"); |
| 270 | quic_cid_dump(&trace_buf, &qc->scid); |
| 271 | } |
| 272 | |
| 273 | if (mask & QUIC_EV_TRANSP_PARAMS) { |
| 274 | const struct quic_transport_params *p = a2; |
| 275 | quic_transport_params_dump(&trace_buf, qc, p); |
| 276 | } |
| 277 | |
| 278 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 279 | const enum ssl_encryption_level_t *level = a2; |
| 280 | const size_t *len = a3; |
| 281 | |
| 282 | if (level) { |
| 283 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 284 | |
| 285 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 286 | } |
| 287 | if (len) |
| 288 | chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len); |
| 289 | } |
| 290 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 291 | /* Initial read & write secrets. */ |
| 292 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 293 | const unsigned char *rx_sec = a2; |
| 294 | const unsigned char *tx_sec = a3; |
| 295 | |
| 296 | tls_ctx = &qc->els[level].tls_ctx; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 297 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 298 | if (rx_sec) |
| 299 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 300 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
| 301 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 302 | if (tx_sec) |
| 303 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 304 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 305 | } |
| 306 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 307 | const enum ssl_encryption_level_t *level = a2; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 308 | |
| 309 | if (level) { |
| 310 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 311 | |
| 312 | 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] | 313 | if (quic_tls_has_rx_sec(&qc->els[lvl])) { |
| 314 | tls_ctx = &qc->els[lvl].tls_ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 315 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 316 | } |
| 317 | else |
| 318 | chunk_appendf(&trace_buf, " (none)"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 323 | const enum ssl_encryption_level_t *level = a2; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 324 | |
| 325 | if (level) { |
| 326 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 327 | |
| 328 | 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] | 329 | if (quic_tls_has_tx_sec(&qc->els[lvl])) { |
| 330 | tls_ctx = &qc->els[lvl].tls_ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 331 | quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 332 | } |
| 333 | else |
| 334 | chunk_appendf(&trace_buf, " (none)"); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | } |
| 338 | |
| 339 | if (mask & QUIC_EV_CONN_FRMLIST) { |
| 340 | const struct list *l = a2; |
| 341 | |
| 342 | if (l) { |
| 343 | const struct quic_frame *frm; |
| 344 | list_for_each_entry(frm, l, list) { |
| 345 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 346 | chunk_frm_appendf(&trace_buf, frm); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) { |
| 352 | const struct quic_tx_packet *pkt = a2; |
| 353 | const struct quic_enc_level *qel = a3; |
| 354 | const ssize_t *room = a4; |
| 355 | |
| 356 | if (qel) { |
| 357 | const struct quic_pktns *pktns = qel->pktns; |
| 358 | chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu " |
| 359 | "if=%llu pp=%u", |
| 360 | quic_enc_level_char_from_qel(qel, qc), |
| 361 | (unsigned long long)qc->path->cwnd, |
| 362 | (unsigned long long)qc->path->prep_in_flight, |
| 363 | (unsigned long long)qc->path->in_flight, |
| 364 | (unsigned long long)pktns->tx.in_flight, |
| 365 | pktns->tx.pto_probe); |
| 366 | } |
| 367 | if (pkt) { |
| 368 | const struct quic_frame *frm; |
| 369 | if (pkt->pn_node.key != (uint64_t)-1) |
| 370 | chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key); |
| 371 | list_for_each_entry(frm, &pkt->frms, list) { |
| 372 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 373 | chunk_frm_appendf(&trace_buf, frm); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | if (room) { |
| 378 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 379 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 380 | (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | if (mask & QUIC_EV_CONN_IO_CB) { |
| 385 | const enum quic_handshake_state *state = a2; |
| 386 | const int *err = a3; |
| 387 | |
| 388 | if (state) |
| 389 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 390 | if (err) |
| 391 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 392 | } |
| 393 | |
| 394 | if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) { |
| 395 | const struct quic_rx_packet *pkt = a2; |
| 396 | const unsigned long *pktlen = a3; |
| 397 | const SSL *ssl = a4; |
| 398 | |
| 399 | if (pkt) { |
| 400 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 401 | if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data) |
| 402 | chunk_appendf(&trace_buf, " kp=%d", |
| 403 | !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT)); |
| 404 | chunk_appendf(&trace_buf, " el=%c", |
| 405 | quic_packet_type_enc_level_char(pkt->type)); |
| 406 | if (pkt->pnl) |
| 407 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 408 | (unsigned long long)pkt->pn); |
| 409 | if (pkt->token_len) |
| 410 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 411 | (unsigned long long)pkt->token_len); |
| 412 | if (pkt->aad_len) |
| 413 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 414 | (unsigned long long)pkt->aad_len); |
| 415 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 416 | pkt->flags, (unsigned long long)pkt->len); |
| 417 | } |
| 418 | if (pktlen) |
| 419 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 420 | if (ssl) { |
| 421 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 422 | chunk_appendf(&trace_buf, " el=%c", |
| 423 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 428 | const struct quic_rx_packet *pkt = a2; |
| 429 | const struct quic_rx_crypto_frm *cf = a3; |
| 430 | const SSL *ssl = a4; |
| 431 | |
| 432 | if (pkt) |
| 433 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 434 | quic_packet_type_enc_level_char(pkt->type), |
| 435 | (unsigned long long)pkt->pn); |
| 436 | if (cf) |
| 437 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 438 | (unsigned long long)cf->offset_node.key, |
| 439 | (unsigned long long)cf->len); |
| 440 | if (ssl) { |
| 441 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 442 | chunk_appendf(&trace_buf, " rel=%c", |
| 443 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 444 | } |
| 445 | |
| 446 | if (qc->err.code) |
| 447 | chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code); |
| 448 | } |
| 449 | |
| 450 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 451 | const struct quic_frame *frm = a2; |
| 452 | |
| 453 | if (frm) |
| 454 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 455 | } |
| 456 | |
| 457 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 458 | const struct quic_enc_level *qel = a2; |
| 459 | |
| 460 | if (qel) { |
| 461 | const struct quic_pktns *pktns = qel->pktns; |
| 462 | chunk_appendf(&trace_buf, |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 463 | " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u off=%llu", |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 464 | quic_enc_level_char_from_qel(qel, qc), |
| 465 | quic_hdshk_state_str(qc->state), |
| 466 | !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED), |
| 467 | (unsigned long long)qc->path->cwnd, |
| 468 | (unsigned long long)qc->path->prep_in_flight, |
| 469 | (unsigned long long)qc->path->in_flight, |
| 470 | (unsigned long long)pktns->tx.in_flight, |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 471 | pktns->tx.pto_probe, |
| 472 | qel->cstream ? (unsigned long long)qel->cstream->rx.offset : 0); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 477 | const struct enc_debug_info *edi = a2; |
| 478 | |
| 479 | if (edi) |
| 480 | chunk_appendf(&trace_buf, |
| 481 | " payload=@%p payload_len=%llu" |
| 482 | " aad=@%p aad_len=%llu pn=%llu", |
| 483 | edi->payload, (unsigned long long)edi->payload_len, |
| 484 | edi->aad, (unsigned long long)edi->aad_len, |
| 485 | (unsigned long long)edi->pn); |
| 486 | } |
| 487 | |
| 488 | if (mask & QUIC_EV_CONN_RMHP) { |
| 489 | const struct quic_rx_packet *pkt = a2; |
| 490 | |
| 491 | if (pkt) { |
| 492 | const int *ret = a3; |
| 493 | |
| 494 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 495 | if (ret && *ret) |
| 496 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 497 | pkt->pnl, (unsigned long long)pkt->pn); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
| 502 | const struct quic_frame *frm = a2; |
| 503 | const unsigned long *val1 = a3; |
| 504 | const unsigned long *val2 = a4; |
| 505 | |
| 506 | if (frm) { |
| 507 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 508 | chunk_frm_appendf(&trace_buf, frm); |
| 509 | } |
| 510 | if (val1) |
| 511 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 512 | if (val2) |
| 513 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 514 | } |
| 515 | |
| 516 | if (mask & QUIC_EV_CONN_ACKSTRM) { |
| 517 | const struct quic_stream *s = a2; |
| 518 | const struct qc_stream_desc *stream = a3; |
| 519 | |
| 520 | if (s) |
| 521 | chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len); |
| 522 | if (stream) |
| 523 | chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset); |
| 524 | } |
| 525 | |
| 526 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 527 | const unsigned int *rtt_sample = a2; |
| 528 | const unsigned int *ack_delay = a3; |
| 529 | const struct quic_loss *ql = a4; |
| 530 | |
| 531 | if (rtt_sample) |
| 532 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 533 | if (ack_delay) |
| 534 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 535 | if (ql) |
| 536 | chunk_appendf(&trace_buf, |
| 537 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 538 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 539 | } |
| 540 | if (mask & QUIC_EV_CONN_CC) { |
| 541 | const struct quic_cc_event *ev = a2; |
| 542 | const struct quic_cc *cc = a3; |
| 543 | |
| 544 | if (a2) |
| 545 | quic_cc_event_trace(&trace_buf, ev); |
| 546 | if (a3) |
| 547 | quic_cc_state_trace(&trace_buf, cc); |
| 548 | } |
| 549 | |
| 550 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 551 | const struct quic_pktns *pktns = a2; |
| 552 | const struct list *lost_pkts = a3; |
| 553 | |
| 554 | if (pktns) { |
| 555 | chunk_appendf(&trace_buf, " pktns=%s", |
| 556 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 557 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 558 | if (pktns->tx.loss_time) |
| 559 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 560 | TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time))); |
| 561 | } |
| 562 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 563 | struct quic_tx_packet *pkt; |
| 564 | |
| 565 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 566 | list_for_each_entry(pkt, lost_pkts, list) |
| 567 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) { |
| 572 | const struct quic_pktns *pktns = a2; |
| 573 | const int *duration = a3; |
| 574 | const uint64_t *ifae_pkts = a4; |
| 575 | |
| 576 | if (ifae_pkts) |
| 577 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 578 | (unsigned long long)*ifae_pkts); |
| 579 | if (pktns) { |
| 580 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
| 581 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 582 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 583 | pktns->tx.pto_probe); |
| 584 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 585 | if (pktns->tx.in_flight) |
| 586 | chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight); |
| 587 | if (pktns->tx.loss_time) |
| 588 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 589 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 590 | } |
| 591 | if (mask & QUIC_EV_CONN_SPTO) { |
| 592 | if (pktns->tx.time_of_last_eliciting) |
| 593 | chunk_appendf(&trace_buf, " tole=%dms", |
| 594 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 595 | if (duration) |
| 596 | chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration)); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) { |
| 601 | chunk_appendf(&trace_buf, |
| 602 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 607 | const struct quic_tx_packet *pkt = a2; |
| 608 | |
| 609 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 610 | (unsigned long long)qc->path->cwnd, |
| 611 | (unsigned long long)qc->path->prep_in_flight, |
| 612 | (unsigned long long)qc->path->in_flight); |
| 613 | if (pkt) { |
| 614 | const struct quic_frame *frm; |
| 615 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
| 616 | (unsigned long)pkt->pn_node.key, |
| 617 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 618 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 619 | (unsigned long long)pkt->in_flight_len); |
| 620 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 621 | (unsigned long long)qc->rx.bytes, |
| 622 | (unsigned long long)qc->tx.bytes); |
| 623 | list_for_each_entry(frm, &pkt->frms, list) { |
| 624 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 625 | chunk_frm_appendf(&trace_buf, frm); |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 631 | const uint8_t *alert = a2; |
| 632 | const enum ssl_encryption_level_t *level = a3; |
| 633 | |
| 634 | if (alert) |
| 635 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 636 | if (level) |
| 637 | chunk_appendf(&trace_buf, " el=%c", |
| 638 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 639 | } |
| 640 | |
| 641 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 642 | const size_t *sz1 = a2; |
| 643 | const size_t *sz2 = a3; |
| 644 | const size_t *sz3 = a4; |
| 645 | |
| 646 | if (sz1) |
| 647 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 648 | if (sz2) |
| 649 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 650 | if (sz3) |
| 651 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 652 | } |
| 653 | |
| 654 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 655 | const struct quic_frame *frm = a2; |
| 656 | |
| 657 | if (frm) { |
| 658 | chunk_appendf(&trace_buf, " frm@%p", frm); |
| 659 | chunk_frm_appendf(&trace_buf, frm); |
| 660 | } |
| 661 | } |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 662 | |
| 663 | if (mask & QUIC_EV_CONN_ELEVELSEL) { |
| 664 | const enum quic_handshake_state *state = a2; |
| 665 | const enum quic_tls_enc_level *level = a3; |
| 666 | const enum quic_tls_enc_level *next_level = a4; |
| 667 | |
| 668 | if (state) |
| 669 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(qc->state)); |
| 670 | if (level) |
| 671 | chunk_appendf(&trace_buf, " level=%c", quic_enc_level_char(*level)); |
| 672 | if (next_level) |
| 673 | chunk_appendf(&trace_buf, " next_level=%c", quic_enc_level_char(*next_level)); |
| 674 | |
| 675 | } |
Amaury Denoyelle | 5b41486 | 2022-10-24 17:40:37 +0200 | [diff] [blame] | 676 | |
| 677 | if (mask & QUIC_EV_CONN_RCV) { |
| 678 | const struct quic_dgram *dgram = a2; |
| 679 | |
| 680 | if (dgram) |
| 681 | chunk_appendf(&trace_buf, " dgram.len=%zu", dgram->len); |
| 682 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 683 | } |
| 684 | if (mask & QUIC_EV_CONN_LPKT) { |
| 685 | const struct quic_rx_packet *pkt = a2; |
| 686 | const uint64_t *len = a3; |
| 687 | const struct quic_version *ver = a4; |
| 688 | |
| 689 | if (pkt) { |
| 690 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 691 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 692 | if (pkt->pn_node.key != (uint64_t)-1) |
| 693 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 694 | } |
| 695 | |
| 696 | if (len) |
| 697 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
| 698 | |
| 699 | if (ver) |
| 700 | chunk_appendf(&trace_buf, " ver=0x%08x", ver->num); |
| 701 | } |
| 702 | |
| 703 | if (mask & QUIC_EV_STATELESS_RST) { |
| 704 | const struct quic_cid *cid = a2; |
| 705 | |
| 706 | if (cid) |
| 707 | quic_cid_dump(&trace_buf, cid); |
| 708 | } |
| 709 | |
| 710 | } |
| 711 | |
| 712 | /* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */ |
| 713 | static inline int quic_peer_validated_addr(struct quic_conn *qc) |
| 714 | { |
| 715 | struct quic_pktns *hdshk_pktns, *app_pktns; |
| 716 | |
| 717 | if (!qc_is_listener(qc)) |
| 718 | return 1; |
| 719 | |
| 720 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 721 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 722 | if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 723 | (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) || |
| 724 | qc->state >= QUIC_HS_ST_COMPLETE) |
| 725 | return 1; |
| 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 731 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 732 | */ |
| 733 | static inline void qc_set_timer(struct quic_conn *qc) |
| 734 | { |
| 735 | struct quic_pktns *pktns; |
| 736 | unsigned int pto; |
| 737 | int handshake_complete; |
| 738 | |
| 739 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
| 740 | NULL, NULL, &qc->path->ifae_pkts); |
| 741 | |
| 742 | pktns = quic_loss_pktns(qc); |
| 743 | if (tick_isset(pktns->tx.loss_time)) { |
| 744 | qc->timer = pktns->tx.loss_time; |
| 745 | goto out; |
| 746 | } |
| 747 | |
| 748 | /* anti-amplification: the timer must be |
| 749 | * cancelled for a server which reached the anti-amplification limit. |
| 750 | */ |
| 751 | if (!quic_peer_validated_addr(qc) && |
| 752 | (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
| 753 | TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc); |
| 754 | qc->timer = TICK_ETERNITY; |
| 755 | goto out; |
| 756 | } |
| 757 | |
| 758 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
| 759 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
| 760 | /* Timer cancellation. */ |
| 761 | qc->timer = TICK_ETERNITY; |
| 762 | goto out; |
| 763 | } |
| 764 | |
| 765 | handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE; |
| 766 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
| 767 | if (tick_isset(pto)) |
| 768 | qc->timer = pto; |
| 769 | out: |
| 770 | if (qc->timer_task && qc->timer != TICK_ETERNITY) { |
| 771 | if (tick_is_expired(qc->timer, now_ms)) { |
| 772 | TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc); |
| 773 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 774 | } |
| 775 | else { |
| 776 | TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc); |
| 777 | task_schedule(qc->timer_task, qc->timer); |
| 778 | } |
| 779 | } |
| 780 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
| 781 | } |
| 782 | |
| 783 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 784 | * connection. |
| 785 | * Return 1 if succeeded, 0 if not. |
| 786 | */ |
| 787 | static int quic_tls_key_update(struct quic_conn *qc) |
| 788 | { |
| 789 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 790 | struct quic_tls_secrets *rx, *tx; |
| 791 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 792 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 793 | const struct quic_version *ver = |
| 794 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
| 795 | int ret = 0; |
| 796 | |
| 797 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
| 798 | |
| 799 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 800 | rx = &tls_ctx->rx; |
| 801 | tx = &tls_ctx->tx; |
| 802 | nxt_rx = &qc->ku.nxt_rx; |
| 803 | nxt_tx = &qc->ku.nxt_tx; |
| 804 | |
| 805 | /* Prepare new RX secrets */ |
| 806 | if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen, |
| 807 | rx->secret, rx->secretlen)) { |
| 808 | TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
| 809 | goto leave; |
| 810 | } |
| 811 | |
| 812 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver, |
| 813 | nxt_rx->key, nxt_rx->keylen, |
| 814 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 815 | nxt_rx->secret, nxt_rx->secretlen)) { |
| 816 | TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 817 | goto leave; |
| 818 | } |
| 819 | |
| 820 | /* Prepare new TX secrets */ |
| 821 | if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen, |
| 822 | tx->secret, tx->secretlen)) { |
| 823 | TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
| 824 | goto leave; |
| 825 | } |
| 826 | |
| 827 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver, |
| 828 | nxt_tx->key, nxt_tx->keylen, |
| 829 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 830 | nxt_tx->secret, nxt_tx->secretlen)) { |
| 831 | TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 832 | goto leave; |
| 833 | } |
| 834 | |
| 835 | if (nxt_rx->ctx) { |
| 836 | EVP_CIPHER_CTX_free(nxt_rx->ctx); |
| 837 | nxt_rx->ctx = NULL; |
| 838 | } |
| 839 | |
| 840 | if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) { |
| 841 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 842 | goto leave; |
| 843 | } |
| 844 | |
| 845 | if (nxt_tx->ctx) { |
| 846 | EVP_CIPHER_CTX_free(nxt_tx->ctx); |
| 847 | nxt_tx->ctx = NULL; |
| 848 | } |
| 849 | |
| 850 | if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) { |
| 851 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 852 | goto leave; |
| 853 | } |
| 854 | |
| 855 | ret = 1; |
| 856 | leave: |
| 857 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc); |
| 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 862 | * Must be used after having updated them. |
| 863 | * Always succeeds. |
| 864 | */ |
| 865 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 866 | { |
| 867 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 868 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 869 | EVP_CIPHER_CTX *curr_ctx; |
| 870 | |
| 871 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 872 | |
| 873 | /* Rotate the RX secrets */ |
| 874 | curr_ctx = tls_ctx->rx.ctx; |
| 875 | curr_secret = tls_ctx->rx.secret; |
| 876 | curr_iv = tls_ctx->rx.iv; |
| 877 | curr_key = tls_ctx->rx.key; |
| 878 | |
| 879 | tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx; |
| 880 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 881 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 882 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 883 | |
| 884 | qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx; |
| 885 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 886 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 887 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 888 | |
| 889 | qc->ku.prv_rx.ctx = curr_ctx; |
| 890 | qc->ku.prv_rx.secret = curr_secret; |
| 891 | qc->ku.prv_rx.iv = curr_iv; |
| 892 | qc->ku.prv_rx.key = curr_key; |
| 893 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 894 | |
| 895 | /* Update the TX secrets */ |
| 896 | curr_ctx = tls_ctx->tx.ctx; |
| 897 | curr_secret = tls_ctx->tx.secret; |
| 898 | curr_iv = tls_ctx->tx.iv; |
| 899 | curr_key = tls_ctx->tx.key; |
| 900 | |
| 901 | tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx; |
| 902 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 903 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 904 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 905 | |
| 906 | qc->ku.nxt_tx.ctx = curr_ctx; |
| 907 | qc->ku.nxt_tx.secret = curr_secret; |
| 908 | qc->ku.nxt_tx.iv = curr_iv; |
| 909 | qc->ku.nxt_tx.key = curr_key; |
| 910 | |
| 911 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 912 | } |
| 913 | |
| 914 | /* returns 0 on error, 1 on success */ |
| 915 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 916 | const uint8_t *read_secret, |
| 917 | const uint8_t *write_secret, size_t secret_len) |
| 918 | { |
| 919 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 920 | struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 921 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 922 | struct quic_tls_secrets *rx = NULL, *tx = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 923 | const struct quic_version *ver = |
| 924 | qc->negotiated_version ? qc->negotiated_version : qc->original_version; |
| 925 | int ret = 0; |
| 926 | |
| 927 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
| 928 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
| 929 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 930 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 931 | goto out; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 932 | } |
| 933 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 934 | if (!read_secret) |
| 935 | goto write; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 936 | |
| 937 | rx = &tls_ctx->rx; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 938 | if (!quic_tls_secrets_keys_alloc(rx)) { |
| 939 | TRACE_ERROR("RX keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 940 | goto leave; |
| 941 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 942 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 943 | rx->aead = tls_aead(cipher); |
| 944 | rx->md = tls_md(cipher); |
| 945 | rx->hp = tls_hp(cipher); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 946 | |
| 947 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen, |
| 948 | rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key, |
| 949 | read_secret, secret_len)) { |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 950 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 951 | goto leave; |
| 952 | } |
| 953 | |
| 954 | if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) { |
| 955 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 956 | goto leave; |
| 957 | } |
| 958 | |
| 959 | if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) { |
| 960 | TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 961 | goto leave; |
| 962 | } |
| 963 | |
| 964 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 965 | * listener. Note that a listener derives only RX secrets for this |
| 966 | * level. |
| 967 | */ |
| 968 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) { |
| 969 | TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc); |
| 970 | quic_accept_push_qc(qc); |
| 971 | } |
| 972 | |
| 973 | write: |
| 974 | |
| 975 | if (!write_secret) |
| 976 | goto out; |
| 977 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 978 | tx = &tls_ctx->tx; |
| 979 | if (!quic_tls_secrets_keys_alloc(tx)) { |
| 980 | TRACE_ERROR("TX keys allocation failed", QUIC_EV_CONN_RWSEC, qc); |
| 981 | goto leave; |
| 982 | } |
| 983 | |
| 984 | tx->aead = tls_aead(cipher); |
| 985 | tx->md = tls_md(cipher); |
| 986 | tx->hp = tls_hp(cipher); |
| 987 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 988 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen, |
| 989 | tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key, |
| 990 | write_secret, secret_len)) { |
| 991 | TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
| 992 | goto leave; |
| 993 | } |
| 994 | |
| 995 | if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) { |
| 996 | TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc); |
| 997 | goto leave; |
| 998 | } |
| 999 | |
| 1000 | if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) { |
| 1001 | TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc); |
| 1002 | goto leave; |
| 1003 | } |
| 1004 | |
| 1005 | if (level == ssl_encryption_application) { |
| 1006 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 1007 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 1008 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 1009 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1010 | if (rx) { |
| 1011 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
| 1012 | TRACE_ERROR("Could not allocate RX Application secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 1013 | goto leave; |
| 1014 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1015 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1016 | memcpy(rx->secret, read_secret, secret_len); |
| 1017 | rx->secretlen = secret_len; |
| 1018 | } |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1019 | |
| 1020 | if (tx) { |
| 1021 | if (!(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
| 1022 | TRACE_ERROR("Could not allocate TX Application secrete keys", QUIC_EV_CONN_RWSEC, qc); |
| 1023 | goto leave; |
| 1024 | } |
| 1025 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1026 | memcpy(tx->secret, write_secret, secret_len); |
| 1027 | tx->secretlen = secret_len; |
| 1028 | } |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 1029 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1030 | /* Initialize all the secret keys lengths */ |
| 1031 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | out: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1035 | ret = 1; |
| 1036 | leave: |
| 1037 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
| 1038 | return ret; |
| 1039 | } |
| 1040 | |
| 1041 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 1042 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 1043 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 1044 | * It fails (returns 0) only if it could not managed to allocate enough CRYPTO |
| 1045 | * buffers to store all the data. |
| 1046 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 1047 | */ |
| 1048 | static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel, |
| 1049 | const unsigned char *data, size_t len) |
| 1050 | { |
| 1051 | struct quic_crypto_buf **qcb; |
| 1052 | /* The remaining byte to store in CRYPTO buffers. */ |
| 1053 | size_t cf_offset, cf_len, *nb_buf; |
| 1054 | unsigned char *pos; |
| 1055 | int ret = 0; |
| 1056 | |
| 1057 | nb_buf = &qel->tx.crypto.nb_buf; |
| 1058 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 1059 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 1060 | cf_len = len; |
| 1061 | |
| 1062 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1063 | |
| 1064 | while (len) { |
| 1065 | size_t to_copy, room; |
| 1066 | |
| 1067 | pos = (*qcb)->data + (*qcb)->sz; |
| 1068 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 1069 | to_copy = len > room ? room : len; |
| 1070 | if (to_copy) { |
| 1071 | memcpy(pos, data, to_copy); |
| 1072 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 1073 | qel->tx.crypto.sz += to_copy; |
| 1074 | (*qcb)->sz += to_copy; |
| 1075 | len -= to_copy; |
| 1076 | data += to_copy; |
| 1077 | } |
| 1078 | else { |
| 1079 | struct quic_crypto_buf **tmp; |
| 1080 | |
| 1081 | // FIXME: realloc! |
| 1082 | tmp = realloc(qel->tx.crypto.bufs, |
| 1083 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 1084 | if (tmp) { |
| 1085 | qel->tx.crypto.bufs = tmp; |
| 1086 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 1087 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 1088 | if (!*qcb) { |
| 1089 | TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc); |
| 1090 | goto leave; |
| 1091 | } |
| 1092 | |
| 1093 | (*qcb)->sz = 0; |
| 1094 | ++*nb_buf; |
| 1095 | } |
| 1096 | else { |
| 1097 | break; |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 1103 | * have been buffered. |
| 1104 | */ |
| 1105 | if (!len) { |
| 1106 | struct quic_frame *frm; |
| 1107 | struct quic_frame *found = NULL; |
| 1108 | |
| 1109 | /* There is at most one CRYPTO frame in this packet number |
| 1110 | * space. Let's look for it. |
| 1111 | */ |
| 1112 | list_for_each_entry(frm, &qel->pktns->tx.frms, list) { |
| 1113 | if (frm->type != QUIC_FT_CRYPTO) |
| 1114 | continue; |
| 1115 | |
| 1116 | /* Found */ |
| 1117 | found = frm; |
| 1118 | break; |
| 1119 | } |
| 1120 | |
| 1121 | if (found) { |
| 1122 | found->crypto.len += cf_len; |
| 1123 | } |
| 1124 | else { |
| 1125 | frm = pool_zalloc(pool_head_quic_frame); |
| 1126 | if (!frm) { |
| 1127 | TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc); |
| 1128 | goto leave; |
| 1129 | } |
| 1130 | |
| 1131 | LIST_INIT(&frm->reflist); |
| 1132 | frm->type = QUIC_FT_CRYPTO; |
| 1133 | frm->crypto.offset = cf_offset; |
| 1134 | frm->crypto.len = cf_len; |
| 1135 | frm->crypto.qel = qel; |
| 1136 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
| 1137 | } |
| 1138 | } |
| 1139 | ret = len == 0; |
| 1140 | leave: |
| 1141 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1142 | return ret; |
| 1143 | } |
| 1144 | |
| 1145 | /* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive |
| 1146 | * activity for <qc> will be interrupted. |
| 1147 | */ |
| 1148 | void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err) |
| 1149 | { |
| 1150 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1151 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) |
| 1152 | goto leave; |
| 1153 | |
| 1154 | TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc); |
| 1155 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 1156 | qc->err.code = err.code; |
| 1157 | qc->err.app = err.app; |
| 1158 | leave: |
| 1159 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 1160 | } |
| 1161 | |
| 1162 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1163 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1164 | { |
| 1165 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1166 | |
| 1167 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 1168 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 1169 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
| 1170 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 1171 | } |
| 1172 | quic_set_connection_close(qc, quic_err_tls(alert)); |
| 1173 | qc->flags |= QUIC_FL_CONN_TLS_ALERT; |
| 1174 | TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc); |
| 1175 | |
| 1176 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
| 1177 | } |
| 1178 | |
| 1179 | /* Set the application for <qc> QUIC connection. |
| 1180 | * Return 1 if succeeded, 0 if not. |
| 1181 | */ |
| 1182 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1183 | { |
| 1184 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1185 | qc->app_ops = &h3_ops; |
| 1186 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1187 | qc->app_ops = &hq_interop_ops; |
| 1188 | else |
| 1189 | return 0; |
| 1190 | |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
| 1194 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1195 | * wants to provide the QUIC layer with CRYPTO data. |
| 1196 | * Returns 1 if succeeded, 0 if not. |
| 1197 | */ |
| 1198 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1199 | const uint8_t *data, size_t len) |
| 1200 | { |
| 1201 | struct quic_conn *qc; |
| 1202 | enum quic_tls_enc_level tel; |
| 1203 | struct quic_enc_level *qel; |
| 1204 | int ret = 0; |
| 1205 | |
| 1206 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1207 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1208 | |
| 1209 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 1210 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
| 1211 | goto out; |
| 1212 | } |
| 1213 | |
| 1214 | tel = ssl_to_quic_enc_level(level); |
| 1215 | if (tel == -1) { |
| 1216 | TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc); |
| 1217 | goto leave; |
| 1218 | } |
| 1219 | |
| 1220 | qel = &qc->els[tel]; |
| 1221 | if (!quic_crypto_data_cpy(qc, qel, data, len)) { |
| 1222 | TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc); |
| 1223 | goto leave; |
| 1224 | } |
| 1225 | |
| 1226 | TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
| 1227 | qc, &level, &len); |
| 1228 | out: |
| 1229 | ret = 1; |
| 1230 | leave: |
| 1231 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
| 1232 | return ret; |
| 1233 | } |
| 1234 | |
| 1235 | int ha_quic_flush_flight(SSL *ssl) |
| 1236 | { |
| 1237 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1238 | |
| 1239 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1240 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
| 1241 | |
| 1242 | return 1; |
| 1243 | } |
| 1244 | |
| 1245 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1246 | { |
| 1247 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1248 | |
| 1249 | TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc); |
| 1250 | |
| 1251 | TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1252 | |
| 1253 | quic_set_tls_alert(qc, alert); |
| 1254 | TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc); |
| 1255 | return 1; |
| 1256 | } |
| 1257 | |
| 1258 | /* QUIC TLS methods */ |
| 1259 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1260 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1261 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1262 | .flush_flight = ha_quic_flush_flight, |
| 1263 | .send_alert = ha_quic_send_alert, |
| 1264 | }; |
| 1265 | |
| 1266 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1267 | * Returns an error count. |
| 1268 | */ |
| 1269 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1270 | { |
| 1271 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1272 | int cfgerr = 0; |
| 1273 | |
| 1274 | long options = |
| 1275 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1276 | SSL_OP_SINGLE_ECDH_USE | |
| 1277 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1278 | SSL_CTX *ctx; |
| 1279 | |
| 1280 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1281 | bind_conf->initial_ctx = ctx; |
| 1282 | |
| 1283 | SSL_CTX_set_options(ctx, options); |
| 1284 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1285 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1286 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1287 | |
| 1288 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1289 | # if defined(HAVE_SSL_CLIENT_HELLO_CB) |
| 1290 | # if defined(SSL_OP_NO_ANTI_REPLAY) |
| 1291 | if (bind_conf->ssl_conf.early_data) { |
| 1292 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
| 1293 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
| 1294 | } |
| 1295 | # endif /* !SSL_OP_NO_ANTI_REPLAY */ |
| 1296 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1297 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1298 | # else /* ! HAVE_SSL_CLIENT_HELLO_CB */ |
| 1299 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1300 | # endif |
| 1301 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1302 | #endif |
| 1303 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1304 | |
| 1305 | return cfgerr; |
| 1306 | } |
| 1307 | |
| 1308 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1309 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1310 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1311 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1312 | */ |
| 1313 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1314 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1315 | { |
| 1316 | uint64_t expected_pn = largest_pn + 1; |
| 1317 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1318 | uint64_t pn_hwin = pn_win / 2; |
| 1319 | uint64_t pn_mask = pn_win - 1; |
| 1320 | uint64_t candidate_pn; |
| 1321 | |
| 1322 | |
| 1323 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1324 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1325 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1326 | candidate_pn + pn_hwin <= expected_pn) |
| 1327 | return candidate_pn + pn_win; |
| 1328 | |
| 1329 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1330 | return candidate_pn - pn_win; |
| 1331 | |
| 1332 | return candidate_pn; |
| 1333 | } |
| 1334 | |
| 1335 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1336 | * cryptographic context. |
| 1337 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1338 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1339 | * <end> points to one byte past the end of this packet. |
| 1340 | * Returns 1 if succeeded, 0 if not. |
| 1341 | */ |
| 1342 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1343 | struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
| 1344 | int64_t largest_pn, unsigned char *pn, unsigned char *byte0) |
| 1345 | { |
| 1346 | int ret, i, pnlen; |
| 1347 | uint64_t packet_number; |
| 1348 | uint32_t truncated_pn = 0; |
| 1349 | unsigned char mask[5] = {0}; |
| 1350 | unsigned char *sample; |
| 1351 | EVP_CIPHER_CTX *cctx = NULL; |
| 1352 | |
| 1353 | TRACE_ENTER(QUIC_EV_CONN_RMHP, qc); |
| 1354 | |
| 1355 | ret = 0; |
| 1356 | |
| 1357 | /* Check there is enough data in this packet. */ |
| 1358 | if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
| 1359 | TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1360 | goto leave; |
| 1361 | } |
| 1362 | |
| 1363 | cctx = EVP_CIPHER_CTX_new(); |
| 1364 | if (!cctx) { |
| 1365 | TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1366 | goto leave; |
| 1367 | } |
| 1368 | |
| 1369 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1370 | |
| 1371 | if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) { |
| 1372 | TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt); |
| 1373 | goto leave; |
| 1374 | } |
| 1375 | |
| 1376 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1377 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1378 | for (i = 0; i < pnlen; i++) { |
| 1379 | pn[i] ^= mask[i + 1]; |
| 1380 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1381 | } |
| 1382 | |
| 1383 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1384 | /* Store remaining information for this unprotected header */ |
| 1385 | pkt->pn = packet_number; |
| 1386 | pkt->pnl = pnlen; |
| 1387 | |
| 1388 | ret = 1; |
| 1389 | leave: |
| 1390 | if (cctx) |
| 1391 | EVP_CIPHER_CTX_free(cctx); |
| 1392 | TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc); |
| 1393 | return ret; |
| 1394 | } |
| 1395 | |
| 1396 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1397 | * address, with <payload_len> as payload length, <aad> as address of |
| 1398 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1399 | * context. |
| 1400 | * Returns 1 if succeeded, 0 if not. |
| 1401 | */ |
| 1402 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1403 | unsigned char *aad, size_t aad_len, uint64_t pn, |
| 1404 | struct quic_tls_ctx *tls_ctx, struct quic_conn *qc) |
| 1405 | { |
| 1406 | int ret = 0; |
| 1407 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 1408 | unsigned char *tx_iv = tls_ctx->tx.iv; |
| 1409 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
| 1410 | struct enc_debug_info edi; |
| 1411 | |
| 1412 | TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc); |
| 1413 | |
| 1414 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
| 1415 | TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc); |
| 1416 | goto err; |
| 1417 | } |
| 1418 | |
| 1419 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1420 | tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
| 1421 | TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc); |
| 1422 | goto err; |
| 1423 | } |
| 1424 | |
| 1425 | ret = 1; |
| 1426 | leave: |
| 1427 | TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc); |
| 1428 | return ret; |
| 1429 | |
| 1430 | err: |
| 1431 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
| 1432 | goto leave; |
| 1433 | } |
| 1434 | |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1435 | /* Decrypt <pkt> packet using encryption level <qel> for <qc> connection. |
| 1436 | * Decryption is done in place in packet buffer. |
| 1437 | * |
Ilya Shipitsin | 5fa29b8 | 2022-12-07 09:46:19 +0500 | [diff] [blame] | 1438 | * Returns 1 on success else 0. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1439 | */ |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1440 | static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel, |
| 1441 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1442 | { |
| 1443 | int ret, kp_changed; |
| 1444 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 1445 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
| 1446 | EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx; |
| 1447 | unsigned char *rx_iv = tls_ctx->rx.iv; |
| 1448 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1449 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1450 | |
| 1451 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 1452 | |
| 1453 | ret = 0; |
| 1454 | kp_changed = 0; |
| 1455 | |
| 1456 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1457 | /* The two tested bits are not at the same position, |
| 1458 | * this is why they are first both inversed. |
| 1459 | */ |
| 1460 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1461 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1462 | /* The lowest packet number of a previous key phase |
| 1463 | * cannot be null if it really stores previous key phase |
| 1464 | * secrets. |
| 1465 | */ |
| 1466 | // TODO: check if BUG_ON() more suitable |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1467 | if (!qc->ku.prv_rx.pn) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1468 | TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc); |
| 1469 | goto leave; |
| 1470 | } |
| 1471 | |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1472 | rx_ctx = qc->ku.prv_rx.ctx; |
| 1473 | rx_iv = qc->ku.prv_rx.iv; |
| 1474 | rx_key = qc->ku.prv_rx.key; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1475 | } |
| 1476 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1477 | /* Next key phase */ |
| 1478 | kp_changed = 1; |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1479 | rx_ctx = qc->ku.nxt_rx.ctx; |
| 1480 | rx_iv = qc->ku.nxt_rx.iv; |
| 1481 | rx_key = qc->ku.nxt_rx.key; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) { |
| 1487 | TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1488 | goto leave; |
| 1489 | } |
| 1490 | |
| 1491 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1492 | pkt->data, pkt->aad_len, |
| 1493 | rx_ctx, tls_ctx->rx.aead, rx_key, iv); |
| 1494 | if (!ret) { |
| 1495 | TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1496 | goto leave; |
| 1497 | } |
| 1498 | |
| 1499 | /* Update the keys only if the packet decryption succeeded. */ |
| 1500 | if (kp_changed) { |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1501 | quic_tls_rotate_keys(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1502 | /* Toggle the Key Phase bit */ |
| 1503 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1504 | /* Store the lowest packet number received for the current key phase */ |
| 1505 | tls_ctx->rx.pn = pkt->pn; |
| 1506 | /* Prepare the next key update */ |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 1507 | if (!quic_tls_key_update(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1508 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc); |
| 1509 | goto leave; |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | /* Update the packet length (required to parse the frames). */ |
| 1514 | pkt->len -= QUIC_TLS_TAG_LEN; |
| 1515 | ret = 1; |
| 1516 | leave: |
| 1517 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 1518 | return ret; |
| 1519 | } |
| 1520 | |
| 1521 | |
| 1522 | /* Remove references to <frm> frame */ |
| 1523 | static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm) |
| 1524 | { |
| 1525 | struct quic_frame *f, *tmp; |
| 1526 | |
| 1527 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1528 | |
| 1529 | list_for_each_entry_safe(f, tmp, &frm->reflist, ref) { |
| 1530 | f->origin = NULL; |
| 1531 | LIST_DELETE(&f->ref); |
| 1532 | if (f->pkt) { |
| 1533 | TRACE_DEVEL("remove frame reference", |
| 1534 | QUIC_EV_CONN_PRSAFRM, qc, f, &f->pkt->pn_node.key); |
| 1535 | } |
| 1536 | else { |
| 1537 | TRACE_DEVEL("remove frame reference for unsent frame", |
| 1538 | QUIC_EV_CONN_PRSAFRM, qc, f); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1543 | } |
| 1544 | |
| 1545 | /* Release <frm> frame and mark its copies as acknowledged */ |
| 1546 | void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm) |
| 1547 | { |
| 1548 | uint64_t pn; |
| 1549 | struct quic_frame *origin, *f, *tmp; |
| 1550 | |
| 1551 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1552 | |
| 1553 | /* Identify this frame: a frame copy or one of its copies */ |
| 1554 | origin = frm->origin ? frm->origin : frm; |
| 1555 | /* Ensure the source of the copies is flagged as acked, <frm> being |
| 1556 | * possibly a copy of <origin> |
| 1557 | */ |
| 1558 | origin->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1559 | /* Mark all the copy of <origin> as acknowledged. We must |
| 1560 | * not release the packets (releasing the frames) at this time as |
| 1561 | * they are possibly also to be acknowledged alongside the |
| 1562 | * the current one. |
| 1563 | */ |
| 1564 | list_for_each_entry_safe(f, tmp, &origin->reflist, ref) { |
| 1565 | if (f->pkt) { |
| 1566 | f->flags |= QUIC_FL_TX_FRAME_ACKED; |
| 1567 | f->origin = NULL; |
| 1568 | LIST_DELETE(&f->ref); |
| 1569 | pn = f->pkt->pn_node.key; |
| 1570 | TRACE_DEVEL("mark frame as acked from packet", |
| 1571 | QUIC_EV_CONN_PRSAFRM, qc, f, &pn); |
| 1572 | } |
| 1573 | else { |
| 1574 | TRACE_DEVEL("freeing unsent frame", |
| 1575 | QUIC_EV_CONN_PRSAFRM, qc, f); |
| 1576 | LIST_DELETE(&f->ref); |
| 1577 | LIST_DELETE(&f->list); |
| 1578 | pool_free(pool_head_quic_frame, f); |
| 1579 | } |
| 1580 | } |
| 1581 | LIST_DELETE(&frm->list); |
| 1582 | pn = frm->pkt->pn_node.key; |
| 1583 | quic_tx_packet_refdec(frm->pkt); |
| 1584 | TRACE_DEVEL("freeing frame from packet", |
| 1585 | QUIC_EV_CONN_PRSAFRM, qc, frm, &pn); |
| 1586 | pool_free(pool_head_quic_frame, frm); |
| 1587 | |
| 1588 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1589 | } |
| 1590 | |
| 1591 | /* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released |
| 1592 | * and all STREAM data are acknowledged. The MUX is responsible to have set |
| 1593 | * <qc.err> before as it is reused for the CONNECTION_CLOSE frame. |
| 1594 | * |
| 1595 | * TODO this should also be called on lost packet detection |
| 1596 | */ |
| 1597 | void qc_check_close_on_released_mux(struct quic_conn *qc) |
| 1598 | { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1599 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 1600 | |
| 1601 | if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) { |
| 1602 | /* Reuse errcode which should have been previously set by the MUX on release. */ |
| 1603 | quic_set_connection_close(qc, qc->err); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 1604 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 1608 | } |
| 1609 | |
| 1610 | /* Remove from <stream> the acknowledged frames. |
| 1611 | * |
| 1612 | * Returns 1 if at least one frame was removed else 0. |
| 1613 | */ |
| 1614 | static int quic_stream_try_to_consume(struct quic_conn *qc, |
| 1615 | struct qc_stream_desc *stream) |
| 1616 | { |
| 1617 | int ret; |
| 1618 | struct eb64_node *frm_node; |
| 1619 | |
| 1620 | TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc); |
| 1621 | |
| 1622 | ret = 0; |
| 1623 | frm_node = eb64_first(&stream->acked_frms); |
| 1624 | while (frm_node) { |
| 1625 | struct quic_stream *strm; |
| 1626 | struct quic_frame *frm; |
| 1627 | size_t offset, len; |
| 1628 | |
| 1629 | strm = eb64_entry(frm_node, struct quic_stream, offset); |
| 1630 | offset = strm->offset.key; |
| 1631 | len = strm->len; |
| 1632 | |
| 1633 | if (offset > stream->ack_offset) |
| 1634 | break; |
| 1635 | |
| 1636 | if (qc_stream_desc_ack(&stream, offset, len)) { |
| 1637 | /* cf. next comment : frame may be freed at this stage. */ |
| 1638 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1639 | qc, stream ? strm : NULL, stream); |
| 1640 | ret = 1; |
| 1641 | } |
| 1642 | |
| 1643 | /* If stream is NULL after qc_stream_desc_ack(), it means frame |
| 1644 | * has been freed. with the stream frames tree. Nothing to do |
| 1645 | * anymore in here. |
| 1646 | */ |
| 1647 | if (!stream) { |
| 1648 | qc_check_close_on_released_mux(qc); |
| 1649 | ret = 1; |
| 1650 | goto leave; |
| 1651 | } |
| 1652 | |
| 1653 | frm_node = eb64_next(frm_node); |
| 1654 | eb64_delete(&strm->offset); |
| 1655 | |
| 1656 | frm = container_of(strm, struct quic_frame, stream); |
| 1657 | qc_release_frm(qc, frm); |
| 1658 | } |
| 1659 | |
| 1660 | leave: |
| 1661 | TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc); |
| 1662 | return ret; |
| 1663 | } |
| 1664 | |
| 1665 | /* Treat <frm> frame whose packet it is attached to has just been acknowledged. */ |
| 1666 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1667 | struct quic_frame *frm) |
| 1668 | { |
| 1669 | int stream_acked; |
| 1670 | |
| 1671 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1672 | |
| 1673 | stream_acked = 0; |
| 1674 | switch (frm->type) { |
| 1675 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1676 | { |
| 1677 | struct quic_stream *strm_frm = &frm->stream; |
| 1678 | struct eb64_node *node = NULL; |
| 1679 | struct qc_stream_desc *stream = NULL; |
| 1680 | const size_t offset = strm_frm->offset.key; |
| 1681 | const size_t len = strm_frm->len; |
| 1682 | |
| 1683 | /* do not use strm_frm->stream as the qc_stream_desc instance |
| 1684 | * might be freed at this stage. Use the id to do a proper |
| 1685 | * lookup. |
| 1686 | * |
| 1687 | * TODO if lookup operation impact on the perf is noticeable, |
| 1688 | * implement a refcount on qc_stream_desc instances. |
| 1689 | */ |
| 1690 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1691 | if (!node) { |
| 1692 | TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm); |
| 1693 | qc_release_frm(qc, frm); |
| 1694 | /* early return */ |
| 1695 | goto leave; |
| 1696 | } |
| 1697 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1698 | |
| 1699 | TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream); |
| 1700 | if (offset <= stream->ack_offset) { |
| 1701 | if (qc_stream_desc_ack(&stream, offset, len)) { |
| 1702 | stream_acked = 1; |
| 1703 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1704 | qc, strm_frm, stream); |
| 1705 | } |
| 1706 | |
| 1707 | if (!stream) { |
| 1708 | /* no need to continue if stream freed. */ |
| 1709 | TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc); |
| 1710 | qc_release_frm(qc, frm); |
| 1711 | qc_check_close_on_released_mux(qc); |
| 1712 | break; |
| 1713 | } |
| 1714 | |
| 1715 | TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM, |
| 1716 | qc, strm_frm, stream); |
| 1717 | qc_release_frm(qc, frm); |
| 1718 | } |
| 1719 | else { |
| 1720 | eb64_insert(&stream->acked_frms, &strm_frm->offset); |
| 1721 | } |
| 1722 | |
| 1723 | stream_acked |= quic_stream_try_to_consume(qc, stream); |
| 1724 | } |
| 1725 | break; |
| 1726 | default: |
| 1727 | qc_release_frm(qc, frm); |
| 1728 | } |
| 1729 | |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 1730 | if (stream_acked) { |
| 1731 | if (qc->subs && qc->subs->events & SUB_RETRY_SEND) { |
| 1732 | tasklet_wakeup(qc->subs->tasklet); |
| 1733 | qc->subs->events &= ~SUB_RETRY_SEND; |
| 1734 | if (!qc->subs->events) |
| 1735 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1736 | } |
| 1737 | } |
| 1738 | leave: |
| 1739 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1740 | } |
| 1741 | |
| 1742 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1743 | * deallocating them, and their TX frames. |
| 1744 | * Returns the last node reached to be used for the next range. |
| 1745 | * May be NULL if <largest> node could not be found. |
| 1746 | */ |
| 1747 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1748 | struct eb_root *pkts, |
| 1749 | unsigned int *pkt_flags, |
| 1750 | struct list *newly_acked_pkts, |
| 1751 | struct eb64_node *largest_node, |
| 1752 | uint64_t largest, uint64_t smallest) |
| 1753 | { |
| 1754 | struct eb64_node *node; |
| 1755 | struct quic_tx_packet *pkt; |
| 1756 | |
| 1757 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1758 | |
| 1759 | node = largest_node ? largest_node : eb64_lookup_le(pkts, largest); |
| 1760 | while (node && node->key >= smallest) { |
| 1761 | struct quic_frame *frm, *frmbak; |
| 1762 | |
| 1763 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 1764 | *pkt_flags |= pkt->flags; |
| 1765 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
| 1766 | TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 1767 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1768 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 1769 | /* If there are others packet in the same datagram <pkt> is attached to, |
| 1770 | * detach the previous one and the next one from <pkt>. |
| 1771 | */ |
Frédéric Lécaille | 74b5f7b | 2022-11-20 18:35:35 +0100 | [diff] [blame] | 1772 | quic_tx_packet_dgram_detach(pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 1773 | node = eb64_prev(node); |
| 1774 | eb64_delete(&pkt->pn_node); |
| 1775 | } |
| 1776 | |
| 1777 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1778 | return node; |
| 1779 | } |
| 1780 | |
| 1781 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1782 | * same order they have been sent into <pktns_frm_list>. |
| 1783 | */ |
| 1784 | static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1785 | struct quic_tx_packet *pkt, |
| 1786 | struct list *pktns_frm_list) |
| 1787 | { |
| 1788 | struct quic_frame *frm, *frmbak; |
| 1789 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1790 | struct list *pkt_frm_list = &pkt->frms; |
| 1791 | uint64_t pn = pkt->pn_node.key; |
| 1792 | |
| 1793 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1794 | |
| 1795 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1796 | /* First remove this frame from the packet it was attached to */ |
| 1797 | LIST_DELETE(&frm->list); |
| 1798 | quic_tx_packet_refdec(pkt); |
| 1799 | /* At this time, this frame is not freed but removed from its packet */ |
| 1800 | frm->pkt = NULL; |
| 1801 | /* Remove any reference to this frame */ |
| 1802 | qc_frm_unref(qc, frm); |
| 1803 | switch (frm->type) { |
| 1804 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1805 | { |
| 1806 | struct quic_stream *strm_frm = &frm->stream; |
| 1807 | struct eb64_node *node = NULL; |
| 1808 | struct qc_stream_desc *stream_desc; |
| 1809 | |
| 1810 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 1811 | if (!node) { |
| 1812 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1813 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1814 | qc, frm, &pn); |
| 1815 | pool_free(pool_head_quic_frame, frm); |
| 1816 | continue; |
| 1817 | } |
| 1818 | |
| 1819 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 1820 | /* Do not resend this frame if in the "already acked range" */ |
| 1821 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 1822 | TRACE_DEVEL("ignored frame in already acked range", |
| 1823 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1824 | pool_free(pool_head_quic_frame, frm); |
| 1825 | continue; |
| 1826 | } |
| 1827 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 1828 | strm_frm->offset.key = stream_desc->ack_offset; |
| 1829 | TRACE_DEVEL("updated partially acked frame", |
| 1830 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1831 | } |
| 1832 | break; |
| 1833 | } |
| 1834 | |
| 1835 | default: |
| 1836 | break; |
| 1837 | } |
| 1838 | |
| 1839 | /* Do not resend probing packet with old data */ |
| 1840 | if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) { |
| 1841 | TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM, |
| 1842 | qc, frm, &pn); |
| 1843 | if (frm->origin) |
| 1844 | LIST_DELETE(&frm->ref); |
| 1845 | pool_free(pool_head_quic_frame, frm); |
| 1846 | continue; |
| 1847 | } |
| 1848 | |
| 1849 | if (frm->flags & QUIC_FL_TX_FRAME_ACKED) { |
| 1850 | TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1851 | TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM, |
| 1852 | qc, frm, &pn); |
| 1853 | pool_free(pool_head_quic_frame, frm); |
| 1854 | } |
| 1855 | else { |
| 1856 | if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) { |
| 1857 | /* Mark this STREAM frame as lost. A look up their stream descriptor |
| 1858 | * will be performed to check the stream is not consumed or released. |
| 1859 | */ |
| 1860 | frm->flags |= QUIC_FL_TX_FRAME_LOST; |
| 1861 | } |
| 1862 | LIST_APPEND(&tmp, &frm->list); |
| 1863 | TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | LIST_SPLICE(pktns_frm_list, &tmp); |
| 1868 | |
| 1869 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1870 | } |
| 1871 | |
| 1872 | /* Free <pkt> TX packet and its attached frames. |
| 1873 | * This is the responsibility of the caller to remove this packet of |
| 1874 | * any data structure it was possibly attached to. |
| 1875 | */ |
| 1876 | static inline void free_quic_tx_packet(struct quic_conn *qc, |
| 1877 | struct quic_tx_packet *pkt) |
| 1878 | { |
| 1879 | struct quic_frame *frm, *frmbak; |
| 1880 | |
| 1881 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1882 | |
| 1883 | if (!pkt) |
| 1884 | goto leave; |
| 1885 | |
| 1886 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 1887 | LIST_DELETE(&frm->list); |
| 1888 | pool_free(pool_head_quic_frame, frm); |
| 1889 | } |
| 1890 | pool_free(pool_head_quic_tx_packet, pkt); |
| 1891 | |
| 1892 | leave: |
| 1893 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 1894 | } |
| 1895 | |
| 1896 | /* Free the TX packets of <pkts> list */ |
| 1897 | static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts) |
| 1898 | { |
| 1899 | struct quic_tx_packet *pkt, *tmp; |
| 1900 | |
| 1901 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 1902 | |
| 1903 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 1904 | LIST_DELETE(&pkt->list); |
| 1905 | eb64_delete(&pkt->pn_node); |
| 1906 | free_quic_tx_packet(qc, pkt); |
| 1907 | } |
| 1908 | |
| 1909 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 1910 | } |
| 1911 | |
| 1912 | /* Remove already sent ranges of acknowledged packet numbers from |
| 1913 | * <pktns> packet number space tree below <largest_acked_pn> possibly |
| 1914 | * updating the range which contains <largest_acked_pn>. |
| 1915 | * Never fails. |
| 1916 | */ |
| 1917 | static void qc_treat_ack_of_ack(struct quic_conn *qc, |
| 1918 | struct quic_pktns *pktns, |
| 1919 | int64_t largest_acked_pn) |
| 1920 | { |
| 1921 | struct eb64_node *ar, *next_ar; |
| 1922 | struct quic_arngs *arngs = &pktns->rx.arngs; |
| 1923 | |
| 1924 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1925 | |
| 1926 | ar = eb64_first(&arngs->root); |
| 1927 | while (ar) { |
| 1928 | struct quic_arng_node *ar_node; |
| 1929 | |
| 1930 | next_ar = eb64_next(ar); |
| 1931 | ar_node = eb64_entry(ar, struct quic_arng_node, first); |
| 1932 | |
| 1933 | if ((int64_t)ar_node->first.key > largest_acked_pn) { |
| 1934 | TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc); |
| 1935 | break; |
| 1936 | } |
| 1937 | |
| 1938 | if (largest_acked_pn < ar_node->last) { |
| 1939 | eb64_delete(ar); |
| 1940 | ar_node->first.key = largest_acked_pn + 1; |
| 1941 | eb64_insert(&arngs->root, ar); |
| 1942 | break; |
| 1943 | } |
| 1944 | |
| 1945 | eb64_delete(ar); |
| 1946 | pool_free(pool_head_quic_arng, ar_node); |
| 1947 | arngs->sz--; |
| 1948 | ar = next_ar; |
| 1949 | } |
| 1950 | |
| 1951 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1952 | } |
| 1953 | |
| 1954 | /* Send a packet ack event nofication for each newly acked packet of |
| 1955 | * <newly_acked_pkts> list and free them. |
| 1956 | * Always succeeds. |
| 1957 | */ |
| 1958 | static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc, |
| 1959 | struct list *newly_acked_pkts) |
| 1960 | { |
| 1961 | struct quic_tx_packet *pkt, *tmp; |
| 1962 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1963 | |
| 1964 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 1965 | |
| 1966 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1967 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
| 1968 | qc->path->prep_in_flight -= pkt->in_flight_len; |
| 1969 | qc->path->in_flight -= pkt->in_flight_len; |
| 1970 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
| 1971 | qc->path->ifae_pkts--; |
| 1972 | /* If this packet contained an ACK frame, proceed to the |
| 1973 | * acknowledging of range of acks from the largest acknowledged |
| 1974 | * packet number which was sent in an ACK frame by this packet. |
| 1975 | */ |
| 1976 | if (pkt->largest_acked_pn != -1) |
| 1977 | qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn); |
| 1978 | ev.ack.acked = pkt->in_flight_len; |
| 1979 | ev.ack.time_sent = pkt->time_sent; |
| 1980 | quic_cc_event(&qc->path->cc, &ev); |
| 1981 | LIST_DELETE(&pkt->list); |
| 1982 | eb64_delete(&pkt->pn_node); |
| 1983 | quic_tx_packet_refdec(pkt); |
| 1984 | } |
| 1985 | |
| 1986 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 1987 | |
| 1988 | } |
| 1989 | |
| 1990 | /* Release all the frames attached to <pktns> packet number space */ |
| 1991 | static inline void qc_release_pktns_frms(struct quic_conn *qc, |
| 1992 | struct quic_pktns *pktns) |
| 1993 | { |
| 1994 | struct quic_frame *frm, *frmbak; |
| 1995 | |
| 1996 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 1997 | |
| 1998 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1999 | LIST_DELETE(&frm->list); |
| 2000 | pool_free(pool_head_quic_frame, frm); |
| 2001 | } |
| 2002 | |
| 2003 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 2004 | } |
| 2005 | |
| 2006 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 2007 | * their TX frames. |
| 2008 | * Send a packet loss event to the congestion controller if |
| 2009 | * in flight packet have been lost. |
| 2010 | * Also frees the packet in <pkts> list. |
| 2011 | * Never fails. |
| 2012 | */ |
| 2013 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 2014 | struct quic_pktns *pktns, |
| 2015 | struct list *pkts, |
| 2016 | uint64_t now_us) |
| 2017 | { |
| 2018 | struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost; |
| 2019 | |
| 2020 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2021 | |
| 2022 | if (LIST_ISEMPTY(pkts)) |
| 2023 | goto leave; |
| 2024 | |
| 2025 | oldest_lost = newest_lost = NULL; |
| 2026 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 2027 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2028 | |
| 2029 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
| 2030 | qc->path->prep_in_flight -= pkt->in_flight_len; |
| 2031 | qc->path->in_flight -= pkt->in_flight_len; |
| 2032 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
| 2033 | qc->path->ifae_pkts--; |
| 2034 | /* Treat the frames of this lost packet. */ |
| 2035 | qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms); |
| 2036 | LIST_DELETE(&pkt->list); |
| 2037 | if (!oldest_lost) { |
| 2038 | oldest_lost = newest_lost = pkt; |
| 2039 | } |
| 2040 | else { |
| 2041 | if (newest_lost != oldest_lost) |
| 2042 | quic_tx_packet_refdec(newest_lost); |
| 2043 | newest_lost = pkt; |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | if (newest_lost) { |
| 2048 | /* Sent a congestion event to the controller */ |
| 2049 | struct quic_cc_event ev = { }; |
| 2050 | |
| 2051 | ev.type = QUIC_CC_EVT_LOSS; |
| 2052 | ev.loss.time_sent = newest_lost->time_sent; |
| 2053 | |
| 2054 | quic_cc_event(&qc->path->cc, &ev); |
| 2055 | } |
| 2056 | |
| 2057 | /* If an RTT have been already sampled, <rtt_min> has been set. |
| 2058 | * We must check if we are experiencing a persistent congestion. |
| 2059 | * If this is the case, the congestion controller must re-enter |
| 2060 | * slow start state. |
| 2061 | */ |
| 2062 | if (qc->path->loss.rtt_min && newest_lost != oldest_lost) { |
| 2063 | unsigned int period = newest_lost->time_sent - oldest_lost->time_sent; |
| 2064 | |
| 2065 | if (quic_loss_persistent_congestion(&qc->path->loss, period, |
| 2066 | now_ms, qc->max_ack_delay)) |
| 2067 | qc->path->cc.algo->slow_start(&qc->path->cc); |
| 2068 | } |
| 2069 | |
Amaury Denoyelle | 3a72ba2 | 2022-11-14 11:41:34 +0100 | [diff] [blame] | 2070 | /* <oldest_lost> cannot be NULL at this stage because we have ensured |
| 2071 | * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a |
| 2072 | * possible overflow on a 0 byte region with O2 optimization. |
| 2073 | */ |
| 2074 | ALREADY_CHECKED(oldest_lost); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2075 | quic_tx_packet_refdec(oldest_lost); |
| 2076 | if (newest_lost != oldest_lost) |
| 2077 | quic_tx_packet_refdec(newest_lost); |
| 2078 | |
| 2079 | leave: |
| 2080 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2081 | } |
| 2082 | |
| 2083 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 2084 | * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e. |
| 2085 | * if the largest acked packet was newly acked and if there was at least one newly |
| 2086 | * acked ack-eliciting packet. |
| 2087 | * Return 1, if succeeded, 0 if not. |
| 2088 | */ |
| 2089 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 2090 | struct quic_frame *frm, |
| 2091 | struct quic_enc_level *qel, |
| 2092 | unsigned int *rtt_sample, |
| 2093 | const unsigned char **pos, const unsigned char *end) |
| 2094 | { |
| 2095 | struct quic_ack *ack = &frm->ack; |
| 2096 | uint64_t smallest, largest; |
| 2097 | struct eb_root *pkts; |
| 2098 | struct eb64_node *largest_node; |
| 2099 | unsigned int time_sent, pkt_flags; |
| 2100 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 2101 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 2102 | int ret = 0; |
| 2103 | |
| 2104 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2105 | |
| 2106 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 2107 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
| 2108 | qc, NULL, &ack->largest_ack); |
| 2109 | goto err; |
| 2110 | } |
| 2111 | |
| 2112 | if (ack->first_ack_range > ack->largest_ack) { |
| 2113 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
| 2114 | qc, NULL, &ack->first_ack_range); |
| 2115 | goto err; |
| 2116 | } |
| 2117 | |
| 2118 | largest = ack->largest_ack; |
| 2119 | smallest = largest - ack->first_ack_range; |
| 2120 | pkts = &qel->pktns->tx.pkts; |
| 2121 | pkt_flags = 0; |
| 2122 | largest_node = NULL; |
| 2123 | time_sent = 0; |
| 2124 | |
| 2125 | if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) { |
| 2126 | largest_node = eb64_lookup(pkts, largest); |
| 2127 | if (!largest_node) { |
| 2128 | TRACE_DEVEL("Largest acked packet not found", |
| 2129 | QUIC_EV_CONN_PRSAFRM, qc); |
| 2130 | } |
| 2131 | else { |
| 2132 | time_sent = eb64_entry(largest_node, |
| 2133 | struct quic_tx_packet, pn_node)->time_sent; |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM, |
| 2138 | qc, NULL, &largest, &smallest); |
| 2139 | do { |
| 2140 | uint64_t gap, ack_range; |
| 2141 | |
| 2142 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 2143 | largest_node, largest, smallest); |
| 2144 | if (!ack->ack_range_num--) |
| 2145 | break; |
| 2146 | |
| 2147 | if (!quic_dec_int(&gap, pos, end)) { |
| 2148 | TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc); |
| 2149 | goto err; |
| 2150 | } |
| 2151 | |
| 2152 | if (smallest < gap + 2) { |
| 2153 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
| 2154 | qc, NULL, &gap, &smallest); |
| 2155 | goto err; |
| 2156 | } |
| 2157 | |
| 2158 | largest = smallest - gap - 2; |
| 2159 | if (!quic_dec_int(&ack_range, pos, end)) { |
| 2160 | TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc); |
| 2161 | goto err; |
| 2162 | } |
| 2163 | |
| 2164 | if (largest < ack_range) { |
| 2165 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
| 2166 | qc, NULL, &largest, &ack_range); |
| 2167 | goto err; |
| 2168 | } |
| 2169 | |
| 2170 | /* Do not use this node anymore. */ |
| 2171 | largest_node = NULL; |
| 2172 | /* Next range */ |
| 2173 | smallest = largest - ack_range; |
| 2174 | |
| 2175 | TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM, |
| 2176 | qc, NULL, &largest, &smallest); |
| 2177 | } while (1); |
| 2178 | |
| 2179 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2180 | *rtt_sample = tick_remain(time_sent, now_ms); |
| 2181 | qel->pktns->rx.largest_acked_pn = ack->largest_ack; |
| 2182 | } |
| 2183 | |
| 2184 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 2185 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
| 2186 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
| 2187 | qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms); |
| 2188 | } |
| 2189 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 2190 | if (quic_peer_validated_addr(qc)) |
| 2191 | qc->path->loss.pto_count = 0; |
| 2192 | qc_set_timer(qc); |
| 2193 | } |
| 2194 | |
| 2195 | ret = 1; |
| 2196 | leave: |
| 2197 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2198 | return ret; |
| 2199 | |
| 2200 | err: |
| 2201 | free_quic_tx_pkts(qc, &newly_acked_pkts); |
| 2202 | goto leave; |
| 2203 | } |
| 2204 | |
| 2205 | /* This function gives the detail of the SSL error. It is used only |
| 2206 | * if the debug mode and the verbose mode are activated. It dump all |
| 2207 | * the SSL error until the stack was empty. |
| 2208 | */ |
| 2209 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 2210 | { |
| 2211 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 2212 | while (1) { |
| 2213 | const char *func = NULL; |
| 2214 | unsigned long ret; |
| 2215 | |
| 2216 | ERR_peek_error_func(&func); |
| 2217 | ret = ERR_get_error(); |
| 2218 | if (!ret) |
| 2219 | return; |
| 2220 | |
| 2221 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 2222 | func, ERR_reason_error_string(ret)); |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 2228 | const char **str, int *len); |
| 2229 | |
| 2230 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 2231 | * from <qel> encryption level with <ctx> as QUIC connection context. |
| 2232 | * Remaining parameter are there for debugging purposes. |
| 2233 | * Return 1 if succeeded, 0 if not. |
| 2234 | */ |
| 2235 | static inline int qc_provide_cdata(struct quic_enc_level *el, |
| 2236 | struct ssl_sock_ctx *ctx, |
| 2237 | const unsigned char *data, size_t len, |
| 2238 | struct quic_rx_packet *pkt, |
| 2239 | struct quic_rx_crypto_frm *cf) |
| 2240 | { |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2241 | #ifdef DEBUG_STRICT |
| 2242 | enum ncb_ret ncb_ret; |
| 2243 | #endif |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2244 | int ssl_err, state; |
| 2245 | struct quic_conn *qc; |
| 2246 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2247 | struct ncbuf *ncbuf = &el->cstream->rx.ncbuf; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2248 | |
| 2249 | ssl_err = SSL_ERROR_NONE; |
| 2250 | qc = ctx->qc; |
| 2251 | |
| 2252 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 2253 | |
| 2254 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 2255 | TRACE_ERROR("SSL_provide_quic_data() error", |
| 2256 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
| 2257 | goto leave; |
| 2258 | } |
| 2259 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2260 | TRACE_PROTO("in order CRYPTO data", |
| 2261 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
| 2262 | |
| 2263 | state = qc->state; |
| 2264 | if (state < QUIC_HS_ST_COMPLETE) { |
| 2265 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 2266 | if (ssl_err != 1) { |
| 2267 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2268 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2269 | TRACE_PROTO("SSL handshake in progress", |
| 2270 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2271 | goto out; |
| 2272 | } |
| 2273 | |
| 2274 | /* TODO: Should close the connection asap */ |
| 2275 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2276 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2277 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2278 | HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail); |
| 2279 | } |
| 2280 | TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2281 | qc_ssl_dump_errors(ctx->conn); |
| 2282 | ERR_clear_error(); |
| 2283 | goto leave; |
| 2284 | } |
| 2285 | |
| 2286 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2287 | |
| 2288 | /* Check the alpn could be negotiated */ |
| 2289 | if (!qc->app_ops) { |
| 2290 | TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2291 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
| 2292 | goto leave; |
| 2293 | } |
| 2294 | |
| 2295 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2296 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2297 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2298 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2299 | } |
| 2300 | /* I/O callback switch */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2301 | qc->wait_event.tasklet->process = quic_conn_app_io_cb; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2302 | if (qc_is_listener(ctx->qc)) { |
| 2303 | qc->state = QUIC_HS_ST_CONFIRMED; |
| 2304 | /* The connection is ready to be accepted. */ |
| 2305 | quic_accept_push_qc(qc); |
| 2306 | } |
| 2307 | else { |
| 2308 | qc->state = QUIC_HS_ST_COMPLETE; |
| 2309 | } |
| 2310 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 2311 | /* Prepare the next key update */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2312 | if (!quic_tls_key_update(qc)) { |
| 2313 | TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc); |
| 2314 | goto leave; |
| 2315 | } |
| 2316 | } else { |
| 2317 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 2318 | if (ssl_err != 1) { |
| 2319 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 2320 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 2321 | TRACE_PROTO("SSL post handshake in progress", |
| 2322 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2323 | goto out; |
| 2324 | } |
| 2325 | |
| 2326 | TRACE_ERROR("SSL post handshake error", |
| 2327 | QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err); |
| 2328 | goto leave; |
| 2329 | } |
| 2330 | |
| 2331 | TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state); |
| 2332 | } |
| 2333 | |
| 2334 | out: |
| 2335 | ret = 1; |
| 2336 | leave: |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2337 | /* The CRYPTO data are consumed even in case of an error to release |
| 2338 | * the memory asap. |
| 2339 | */ |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2340 | if (!ncb_is_null(ncbuf)) { |
| 2341 | #ifdef DEBUG_STRICT |
| 2342 | ncb_ret = ncb_advance(ncbuf, len); |
| 2343 | /* ncb_advance() must always succeed. This is guaranteed as |
| 2344 | * this is only done inside a data block. If false, this will |
| 2345 | * lead to handshake failure with quic_enc_level offset shifted |
| 2346 | * from buffer data. |
| 2347 | */ |
| 2348 | BUG_ON(ncb_ret != NCB_RET_OK); |
| 2349 | #else |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2350 | ncb_advance(ncbuf, len); |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2351 | #endif |
| 2352 | } |
| 2353 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2354 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
| 2355 | return ret; |
| 2356 | } |
| 2357 | |
| 2358 | /* Parse a STREAM frame <strm_frm> |
| 2359 | * |
| 2360 | * Return 1 on success. On error, 0 is returned. In this case, the packet |
| 2361 | * containing the frame must not be acknowledged. |
| 2362 | */ |
| 2363 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2364 | struct quic_stream *strm_frm, |
| 2365 | struct quic_conn *qc) |
| 2366 | { |
| 2367 | int ret; |
| 2368 | |
| 2369 | /* RFC9000 13.1. Packet Processing |
| 2370 | * |
| 2371 | * A packet MUST NOT be acknowledged until packet protection has been |
| 2372 | * successfully removed and all frames contained in the packet have |
| 2373 | * been processed. For STREAM frames, this means the data has been |
| 2374 | * enqueued in preparation to be received by the application protocol, |
| 2375 | * but it does not require that data be delivered and consumed. |
| 2376 | */ |
| 2377 | TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc); |
| 2378 | |
| 2379 | ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len, |
| 2380 | strm_frm->offset.key, strm_frm->fin, |
| 2381 | (char *)strm_frm->data); |
| 2382 | |
| 2383 | /* frame rejected - packet must not be acknowledeged */ |
| 2384 | TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc); |
| 2385 | return !ret; |
| 2386 | } |
| 2387 | |
| 2388 | /* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list |
| 2389 | * for <qc> QUIC connection. |
| 2390 | * This is a best effort function which never fails even if no memory could be |
| 2391 | * allocated to duplicate these frames. |
| 2392 | */ |
| 2393 | static void qc_dup_pkt_frms(struct quic_conn *qc, |
| 2394 | struct list *pkt_frm_list, struct list *out_frm_list) |
| 2395 | { |
| 2396 | struct quic_frame *frm, *frmbak; |
| 2397 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 2398 | |
| 2399 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2400 | |
| 2401 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 2402 | struct quic_frame *dup_frm, *origin; |
| 2403 | |
| 2404 | switch (frm->type) { |
| 2405 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2406 | { |
| 2407 | struct quic_stream *strm_frm = &frm->stream; |
| 2408 | struct eb64_node *node = NULL; |
| 2409 | struct qc_stream_desc *stream_desc; |
| 2410 | |
| 2411 | node = eb64_lookup(&qc->streams_by_id, strm_frm->id); |
| 2412 | if (!node) { |
| 2413 | TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2414 | continue; |
| 2415 | } |
| 2416 | |
| 2417 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 2418 | /* Do not resend this frame if in the "already acked range" */ |
| 2419 | if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) { |
| 2420 | TRACE_DEVEL("ignored frame in already acked range", |
| 2421 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2422 | continue; |
| 2423 | } |
| 2424 | else if (strm_frm->offset.key < stream_desc->ack_offset) { |
| 2425 | strm_frm->offset.key = stream_desc->ack_offset; |
| 2426 | TRACE_DEVEL("updated partially acked frame", |
| 2427 | QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2428 | } |
| 2429 | break; |
| 2430 | } |
| 2431 | |
| 2432 | default: |
| 2433 | break; |
| 2434 | } |
| 2435 | |
| 2436 | dup_frm = pool_alloc(pool_head_quic_frame); |
| 2437 | if (!dup_frm) { |
| 2438 | TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2439 | break; |
| 2440 | } |
| 2441 | |
| 2442 | /* If <frm> is already a copy of another frame, we must take |
| 2443 | * its original frame as source for the copy. |
| 2444 | */ |
| 2445 | origin = frm->origin ? frm->origin : frm; |
| 2446 | TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin); |
| 2447 | if (origin->pkt) |
| 2448 | TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM, |
| 2449 | qc, NULL, &origin->pkt->pn_node.key); |
| 2450 | else { |
| 2451 | /* <origin> is a frame which was sent from a packet detected as lost. */ |
| 2452 | TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc); |
| 2453 | } |
| 2454 | *dup_frm = *origin; |
| 2455 | dup_frm->pkt = NULL; |
| 2456 | dup_frm->origin = origin; |
| 2457 | dup_frm->flags = 0; |
| 2458 | LIST_INIT(&dup_frm->reflist); |
| 2459 | LIST_APPEND(&origin->reflist, &dup_frm->ref); |
| 2460 | LIST_APPEND(&tmp, &dup_frm->list); |
| 2461 | } |
| 2462 | |
| 2463 | LIST_SPLICE(out_frm_list, &tmp); |
| 2464 | |
| 2465 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2466 | } |
| 2467 | |
| 2468 | /* Prepare a fast retransmission from <qel> encryption level */ |
| 2469 | static void qc_prep_fast_retrans(struct quic_conn *qc, |
| 2470 | struct quic_enc_level *qel, |
| 2471 | struct list *frms1, struct list *frms2) |
| 2472 | { |
| 2473 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
| 2474 | struct list *frms = frms1; |
| 2475 | struct eb64_node *node; |
| 2476 | struct quic_tx_packet *pkt; |
| 2477 | |
| 2478 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2479 | |
| 2480 | BUG_ON(frms1 == frms2); |
| 2481 | |
| 2482 | pkt = NULL; |
| 2483 | node = eb64_first(pkts); |
| 2484 | start: |
| 2485 | while (node) { |
| 2486 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 2487 | node = eb64_next(node); |
| 2488 | /* Skip the empty and coalesced packets */ |
| 2489 | if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED)) |
| 2490 | break; |
| 2491 | } |
| 2492 | |
| 2493 | if (!pkt) |
| 2494 | goto leave; |
| 2495 | |
| 2496 | /* When building a packet from another one, the field which may increase the |
| 2497 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2498 | */ |
| 2499 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2500 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2501 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 2502 | goto leave; |
| 2503 | } |
| 2504 | |
| 2505 | TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 2506 | qc_dup_pkt_frms(qc, &pkt->frms, frms); |
| 2507 | if (frms == frms1 && frms2) { |
| 2508 | frms = frms2; |
| 2509 | goto start; |
| 2510 | } |
| 2511 | leave: |
| 2512 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 2513 | } |
| 2514 | |
| 2515 | /* Prepare a fast retransmission during a handshake after a client |
| 2516 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2517 | * Initial packets send them coalescing with others (Handshake here). |
| 2518 | * (Listener only function). |
| 2519 | */ |
| 2520 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc, |
| 2521 | struct list *ifrms, struct list *hfrms) |
| 2522 | { |
| 2523 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2524 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2525 | |
| 2526 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2527 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2528 | struct quic_enc_level *qel = iqel; |
| 2529 | struct eb_root *pkts; |
| 2530 | struct eb64_node *node; |
| 2531 | struct quic_tx_packet *pkt; |
| 2532 | struct list *tmp = &itmp; |
| 2533 | |
| 2534 | TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc); |
| 2535 | start: |
| 2536 | pkt = NULL; |
| 2537 | pkts = &qel->pktns->tx.pkts; |
| 2538 | node = eb64_first(pkts); |
| 2539 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2540 | while (node) { |
| 2541 | pkt = eb64_entry(node, struct quic_tx_packet, pn_node); |
| 2542 | if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED)) |
| 2543 | break; |
| 2544 | node = eb64_next(node); |
| 2545 | } |
| 2546 | |
| 2547 | if (!pkt) |
| 2548 | goto end; |
| 2549 | |
| 2550 | /* When building a packet from another one, the field which may increase the |
| 2551 | * packet size is the packet number. And the maximum increase is 4 bytes. |
| 2552 | */ |
| 2553 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) && |
| 2554 | pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) { |
| 2555 | TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc); |
| 2556 | goto end; |
| 2557 | } |
| 2558 | |
| 2559 | qel->pktns->tx.pto_probe += 1; |
| 2560 | |
| 2561 | /* No risk to loop here, #packet per datagram is bounded */ |
| 2562 | requeue: |
| 2563 | TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
| 2564 | qc_dup_pkt_frms(qc, &pkt->frms, tmp); |
| 2565 | if (qel == iqel) { |
| 2566 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2567 | pkt = pkt->next; |
| 2568 | tmp = &htmp; |
| 2569 | hqel->pktns->tx.pto_probe += 1; |
| 2570 | TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc); |
| 2571 | goto requeue; |
| 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | end: |
| 2576 | LIST_SPLICE(ifrms, &itmp); |
| 2577 | LIST_SPLICE(hfrms, &htmp); |
| 2578 | |
| 2579 | TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc); |
| 2580 | } |
| 2581 | |
| 2582 | static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm) |
| 2583 | { |
| 2584 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 2585 | |
| 2586 | if (frm->type == QUIC_FT_CONNECTION_CLOSE) |
| 2587 | quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code); |
| 2588 | else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) { |
| 2589 | if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt) |
| 2590 | goto out; |
| 2591 | |
| 2592 | qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code); |
| 2593 | } |
| 2594 | |
| 2595 | out: |
| 2596 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 2597 | } |
| 2598 | |
| 2599 | /* Enqueue a STOP_SENDING frame to send into 1RTT packet number space |
| 2600 | * frame list to send. |
| 2601 | * Return 1 if succeeded, 0 if not. |
| 2602 | */ |
| 2603 | static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id) |
| 2604 | { |
| 2605 | int ret = 0; |
| 2606 | struct quic_frame *frm; |
| 2607 | struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2608 | uint64_t app_error_code; |
| 2609 | |
| 2610 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2611 | |
| 2612 | /* TODO: the mux may be released, we cannot have more |
| 2613 | * information about the application error code to send |
| 2614 | * at this time. |
| 2615 | */ |
| 2616 | app_error_code = H3_REQUEST_REJECTED; |
| 2617 | // fixme: zalloc |
| 2618 | frm = pool_zalloc(pool_head_quic_frame); |
| 2619 | if (!frm) { |
| 2620 | TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2621 | goto out; |
| 2622 | } |
| 2623 | |
| 2624 | frm->type = QUIC_FT_STOP_SENDING; |
| 2625 | frm->stop_sending.id = id; |
| 2626 | frm->stop_sending.app_error_code = app_error_code; |
| 2627 | LIST_INIT(&frm->reflist); |
| 2628 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
| 2629 | ret = 1; |
| 2630 | out: |
| 2631 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2632 | return ret; |
| 2633 | } |
| 2634 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2635 | /* Release the underlying memory use by <ncbuf> non-contiguous buffer */ |
| 2636 | static void quic_free_ncbuf(struct ncbuf *ncbuf) |
| 2637 | { |
| 2638 | struct buffer buf; |
| 2639 | |
| 2640 | if (ncb_is_null(ncbuf)) |
| 2641 | return; |
| 2642 | |
| 2643 | buf = b_make(ncbuf->area, ncbuf->size, 0, 0); |
| 2644 | b_free(&buf); |
| 2645 | offer_buffers(NULL, 1); |
| 2646 | |
| 2647 | *ncbuf = NCBUF_NULL; |
| 2648 | } |
| 2649 | |
| 2650 | /* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */ |
| 2651 | static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf) |
| 2652 | { |
| 2653 | struct buffer buf = BUF_NULL; |
| 2654 | |
| 2655 | if (!ncb_is_null(ncbuf)) |
| 2656 | return ncbuf; |
| 2657 | |
| 2658 | b_alloc(&buf); |
| 2659 | BUG_ON(b_is_null(&buf)); |
| 2660 | |
| 2661 | *ncbuf = ncb_make(buf.area, buf.size, 0); |
| 2662 | ncb_init(ncbuf, 0); |
| 2663 | |
| 2664 | return ncbuf; |
| 2665 | } |
| 2666 | |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2667 | /* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn. |
| 2668 | * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the |
| 2669 | * speed up handshake completion may be run after having received duplicated |
| 2670 | * CRYPTO data. |
| 2671 | */ |
| 2672 | static int qc_handle_crypto_frm(struct quic_conn *qc, |
| 2673 | struct quic_crypto *frm, struct quic_rx_packet *pkt, |
| 2674 | struct quic_enc_level *qel, int *fast_retrans) |
| 2675 | { |
| 2676 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2677 | enum ncb_ret ncb_ret; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2678 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2679 | struct quic_rx_crypto_frm cfdebug = { |
| 2680 | .offset_node.key = frm->offset, |
| 2681 | .len = frm->len, |
| 2682 | }; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2683 | struct quic_cstream *cstream = qel->cstream; |
| 2684 | struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2685 | |
| 2686 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2687 | if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) { |
| 2688 | TRACE_PROTO("CRYPTO data discarded", |
| 2689 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2690 | goto done; |
| 2691 | } |
| 2692 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2693 | if (unlikely(frm->offset < cstream->rx.offset)) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2694 | size_t diff; |
| 2695 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2696 | if (frm->offset + frm->len <= cstream->rx.offset) { |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2697 | /* Nothing to do */ |
| 2698 | TRACE_PROTO("Already received CRYPTO data", |
| 2699 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2700 | if (qc_is_listener(qc) && qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] && |
| 2701 | !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP)) |
| 2702 | *fast_retrans = 1; |
| 2703 | goto done; |
| 2704 | } |
| 2705 | |
| 2706 | TRACE_PROTO("Partially already received CRYPTO data", |
| 2707 | QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug); |
| 2708 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2709 | diff = cstream->rx.offset - frm->offset; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2710 | frm->len -= diff; |
| 2711 | frm->data += diff; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2712 | frm->offset = cstream->rx.offset; |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2713 | } |
| 2714 | |
Amaury Denoyelle | ff95f2d | 2022-11-18 14:50:06 +0100 | [diff] [blame] | 2715 | 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] | 2716 | if (!qc_provide_cdata(qel, qc->xprt_ctx, frm->data, frm->len, |
| 2717 | pkt, &cfdebug)) { |
| 2718 | // trace already emitted by function above |
| 2719 | goto leave; |
| 2720 | } |
| 2721 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2722 | cstream->rx.offset += frm->len; |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 2723 | 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] | 2724 | goto done; |
| 2725 | } |
| 2726 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2727 | if (!quic_get_ncbuf(ncbuf) || |
| 2728 | ncb_is_null(ncbuf)) { |
| 2729 | 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] | 2730 | goto leave; |
| 2731 | } |
| 2732 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 2733 | /* frm->offset > cstream-trx.offset */ |
| 2734 | ncb_ret = ncb_add(ncbuf, frm->offset - cstream->rx.offset, |
| 2735 | (const char *)frm->data, frm->len, NCB_ADD_COMPARE); |
| 2736 | if (ncb_ret != NCB_RET_OK) { |
| 2737 | if (ncb_ret == NCB_RET_DATA_REJ) { |
| 2738 | TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc); |
| 2739 | quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION)); |
| 2740 | } |
| 2741 | else if (ncb_ret == NCB_RET_GAP_SIZE) { |
| 2742 | TRACE_ERROR("cannot bufferize frame due to gap size limit", |
| 2743 | QUIC_EV_CONN_PRSHPKT, qc); |
| 2744 | } |
| 2745 | goto leave; |
| 2746 | } |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2747 | |
| 2748 | done: |
| 2749 | ret = 1; |
| 2750 | leave: |
| 2751 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2752 | return ret; |
| 2753 | } |
| 2754 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2755 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel> |
| 2756 | * as encryption level. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2757 | * Returns 1 if succeeded, 0 if failed. |
| 2758 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2759 | 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] | 2760 | struct quic_enc_level *qel) |
| 2761 | { |
| 2762 | struct quic_frame frm; |
| 2763 | const unsigned char *pos, *end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2764 | int fast_retrans = 0, ret = 0; |
| 2765 | |
| 2766 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
| 2767 | /* Skip the AAD */ |
| 2768 | pos = pkt->data + pkt->aad_len; |
| 2769 | end = pkt->data + pkt->len; |
| 2770 | |
| 2771 | while (pos < end) { |
| 2772 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) { |
| 2773 | // trace already emitted by function above |
| 2774 | goto leave; |
| 2775 | } |
| 2776 | |
| 2777 | TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm); |
| 2778 | switch (frm.type) { |
| 2779 | case QUIC_FT_PADDING: |
| 2780 | break; |
| 2781 | case QUIC_FT_PING: |
| 2782 | break; |
| 2783 | case QUIC_FT_ACK: |
| 2784 | { |
| 2785 | unsigned int rtt_sample; |
| 2786 | |
| 2787 | rtt_sample = 0; |
| 2788 | if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) { |
| 2789 | // trace already emitted by function above |
| 2790 | goto leave; |
| 2791 | } |
| 2792 | |
| 2793 | if (rtt_sample) { |
| 2794 | unsigned int ack_delay; |
| 2795 | |
| 2796 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
| 2797 | qc->state >= QUIC_HS_ST_CONFIRMED ? |
| 2798 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2799 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
| 2800 | quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc); |
| 2801 | } |
| 2802 | break; |
| 2803 | } |
| 2804 | case QUIC_FT_RESET_STREAM: |
| 2805 | /* TODO: handle this frame at STREAM level */ |
| 2806 | break; |
| 2807 | case QUIC_FT_STOP_SENDING: |
| 2808 | { |
| 2809 | struct quic_stop_sending *stop_sending = &frm.stop_sending; |
| 2810 | if (qc->mux_state == QC_MUX_READY) { |
| 2811 | if (qcc_recv_stop_sending(qc->qcc, stop_sending->id, |
| 2812 | stop_sending->app_error_code)) { |
| 2813 | TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2814 | goto leave; |
| 2815 | } |
| 2816 | } |
| 2817 | break; |
| 2818 | } |
| 2819 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | a20c93e | 2022-09-12 14:54:45 +0200 | [diff] [blame] | 2820 | if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2821 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2822 | break; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2823 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 2824 | { |
| 2825 | struct quic_stream *stream = &frm.stream; |
| 2826 | unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams; |
| 2827 | |
| 2828 | /* The upper layer may not be allocated. */ |
| 2829 | if (qc->mux_state != QC_MUX_READY) { |
| 2830 | if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) { |
| 2831 | TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc); |
| 2832 | break; |
| 2833 | } |
| 2834 | else { |
| 2835 | TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc); |
| 2836 | if (!qc_stop_sending_frm_enqueue(qc, stream->id)) |
| 2837 | TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc); |
| 2838 | /* This packet will not be acknowledged */ |
| 2839 | goto leave; |
| 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | if (!qc_handle_strm_frm(pkt, stream, qc)) { |
| 2844 | TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2845 | goto leave; |
| 2846 | } |
| 2847 | |
| 2848 | break; |
| 2849 | } |
| 2850 | case QUIC_FT_MAX_DATA: |
| 2851 | if (qc->mux_state == QC_MUX_READY) { |
| 2852 | struct quic_max_data *data = &frm.max_data; |
| 2853 | qcc_recv_max_data(qc->qcc, data->max_data); |
| 2854 | } |
| 2855 | break; |
| 2856 | case QUIC_FT_MAX_STREAM_DATA: |
| 2857 | if (qc->mux_state == QC_MUX_READY) { |
| 2858 | struct quic_max_stream_data *data = &frm.max_stream_data; |
| 2859 | if (qcc_recv_max_stream_data(qc->qcc, data->id, |
| 2860 | data->max_stream_data)) { |
| 2861 | TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc); |
| 2862 | goto leave; |
| 2863 | } |
| 2864 | } |
| 2865 | break; |
| 2866 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2867 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2868 | break; |
| 2869 | case QUIC_FT_DATA_BLOCKED: |
| 2870 | HA_ATOMIC_INC(&qc->prx_counters->data_blocked); |
| 2871 | break; |
| 2872 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2873 | HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked); |
| 2874 | break; |
| 2875 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2876 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi); |
| 2877 | break; |
| 2878 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2879 | HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni); |
| 2880 | break; |
| 2881 | case QUIC_FT_NEW_CONNECTION_ID: |
| 2882 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2883 | /* XXX TO DO XXX */ |
| 2884 | break; |
| 2885 | case QUIC_FT_CONNECTION_CLOSE: |
| 2886 | case QUIC_FT_CONNECTION_CLOSE_APP: |
| 2887 | /* Increment the error counters */ |
| 2888 | qc_cc_err_count_inc(qc, &frm); |
| 2889 | if (!(qc->flags & QUIC_FL_CONN_DRAINING)) { |
| 2890 | if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 2891 | qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 2892 | HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn); |
| 2893 | } |
| 2894 | TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc); |
| 2895 | /* RFC 9000 10.2. Immediate Close: |
| 2896 | * The closing and draining connection states exist to ensure |
| 2897 | * that connections close cleanly and that delayed or reordered |
| 2898 | * packets are properly discarded. These states SHOULD persist |
| 2899 | * for at least three times the current PTO interval... |
| 2900 | * |
| 2901 | * Rearm the idle timeout only one time when entering draining |
| 2902 | * state. |
| 2903 | */ |
| 2904 | qc_idle_timer_do_rearm(qc); |
| 2905 | qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 2906 | qc_notify_close(qc); |
| 2907 | } |
| 2908 | break; |
| 2909 | case QUIC_FT_HANDSHAKE_DONE: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2910 | if (qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2911 | TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame", |
| 2912 | QUIC_EV_CONN_PRSHPKT, qc); |
| 2913 | goto leave; |
| 2914 | } |
| 2915 | |
| 2916 | qc->state = QUIC_HS_ST_CONFIRMED; |
| 2917 | break; |
| 2918 | default: |
| 2919 | TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc); |
| 2920 | goto leave; |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | /* Flag this packet number space as having received a packet. */ |
| 2925 | qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED; |
| 2926 | |
| 2927 | if (fast_retrans) { |
| 2928 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2929 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2930 | |
| 2931 | TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc); |
| 2932 | qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms); |
| 2933 | qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP; |
| 2934 | } |
| 2935 | |
| 2936 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2937 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2938 | * be discarded. |
| 2939 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2940 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2941 | if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) { |
| 2942 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags & |
| 2943 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 2944 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2945 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
| 2946 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2947 | qc_set_timer(qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2948 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2949 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 2950 | } |
| 2951 | if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2952 | qc->state = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | ret = 1; |
| 2957 | leave: |
| 2958 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
| 2959 | return ret; |
| 2960 | } |
| 2961 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 2962 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 2963 | /* Allocate Tx buffer from <qc> quic-conn if needed. |
| 2964 | * |
| 2965 | * Returns allocated buffer or NULL on error. |
| 2966 | */ |
| 2967 | static struct buffer *qc_txb_alloc(struct quic_conn *qc) |
| 2968 | { |
| 2969 | struct buffer *buf = &qc->tx.buf; |
| 2970 | if (!b_alloc(buf)) |
| 2971 | return NULL; |
| 2972 | |
| 2973 | return buf; |
| 2974 | } |
| 2975 | |
| 2976 | /* Free Tx buffer from <qc> if it is empty. */ |
| 2977 | static void qc_txb_release(struct quic_conn *qc) |
| 2978 | { |
| 2979 | struct buffer *buf = &qc->tx.buf; |
| 2980 | |
| 2981 | /* For the moment sending function is responsible to purge the buffer |
| 2982 | * entirely. It may change in the future but this requires to be able |
| 2983 | * to reuse old data. |
| 2984 | */ |
| 2985 | BUG_ON_HOT(buf && b_data(buf)); |
| 2986 | |
| 2987 | if (!b_data(buf)) { |
| 2988 | b_free(buf); |
| 2989 | offer_buffers(NULL, 1); |
| 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | /* Commit a datagram payload written into <buf> of length <length>. <first_pkt> |
| 2994 | * must contains the address of the first packet stored in the payload. |
| 2995 | * |
| 2996 | * Caller is responsible that there is enough space in the buffer. |
| 2997 | */ |
| 2998 | static void qc_txb_store(struct buffer *buf, uint16_t length, |
| 2999 | struct quic_tx_packet *first_pkt) |
| 3000 | { |
| 3001 | const size_t hdlen = sizeof(uint16_t) + sizeof(void *); |
| 3002 | BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */ |
| 3003 | |
| 3004 | write_u16(b_tail(buf), length); |
| 3005 | write_ptr(b_tail(buf) + sizeof(length), first_pkt); |
| 3006 | b_add(buf, hdlen + length); |
| 3007 | } |
| 3008 | |
| 3009 | /* Returns 1 if a packet may be built for <qc> from <qel> encryption level |
| 3010 | * with <frms> as ack-eliciting frame list to send, 0 if not. |
| 3011 | * <cc> must equal to 1 if an immediate close was asked, 0 if not. |
| 3012 | * <probe> must equalt to 1 if a probing packet is required, 0 if not. |
| 3013 | * <force_ack> may be set to 1 if you want to force an ack. |
| 3014 | */ |
| 3015 | static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms, |
| 3016 | struct quic_enc_level *qel, int cc, int probe, int force_ack) |
| 3017 | { |
| 3018 | unsigned int must_ack = force_ack || |
| 3019 | (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)); |
| 3020 | |
| 3021 | /* Do not build any more packet if the TX secrets are not available or |
| 3022 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
| 3023 | * and if there is no more packets to send upon PTO expiration |
| 3024 | * and if there is no more ack-eliciting frames to send or in flight |
| 3025 | * congestion control limit is reached for prepared data |
| 3026 | */ |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 3027 | if (!quic_tls_has_tx_sec(qel) || |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3028 | (!cc && !probe && !must_ack && |
| 3029 | (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) { |
| 3030 | return 0; |
| 3031 | } |
| 3032 | |
| 3033 | return 1; |
| 3034 | } |
| 3035 | |
| 3036 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3037 | * <frms>. Each packet is stored in a distinct datagram written to <buf>. |
| 3038 | * |
| 3039 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3040 | * the address of the packet contained in the datagram. |
| 3041 | * |
| 3042 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3043 | * -1 if something wrong happened. |
| 3044 | */ |
| 3045 | static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3046 | struct list *frms) |
| 3047 | { |
| 3048 | int ret = -1; |
| 3049 | struct quic_enc_level *qel; |
| 3050 | unsigned char *end, *pos; |
| 3051 | struct quic_tx_packet *pkt; |
| 3052 | size_t total; |
| 3053 | /* Each datagram is prepended with its length followed by the address |
| 3054 | * of the first packet in the datagram. |
| 3055 | */ |
| 3056 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt); |
| 3057 | |
| 3058 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3059 | |
| 3060 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3061 | total = 0; |
| 3062 | pos = (unsigned char *)b_tail(buf); |
| 3063 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) { |
| 3064 | int err, probe, cc; |
| 3065 | |
| 3066 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
| 3067 | probe = 0; |
| 3068 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3069 | /* We do not probe if an immediate close was asked */ |
| 3070 | if (!cc) |
| 3071 | probe = qel->pktns->tx.pto_probe; |
| 3072 | |
| 3073 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0)) |
| 3074 | break; |
| 3075 | |
| 3076 | /* Leave room for the datagram header */ |
| 3077 | pos += dg_headlen; |
| 3078 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3079 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3080 | } |
| 3081 | else { |
| 3082 | end = pos + qc->path->mtu; |
| 3083 | } |
| 3084 | |
| 3085 | pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0, |
| 3086 | QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err); |
| 3087 | switch (err) { |
| 3088 | case -2: |
| 3089 | // trace already emitted by function above |
| 3090 | goto leave; |
| 3091 | case -1: |
| 3092 | /* As we provide qc_build_pkt() with an enough big buffer to fulfill an |
| 3093 | * MTU, we are here because of the congestion control window. There is |
| 3094 | * no need to try to reuse this buffer. |
| 3095 | */ |
| 3096 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
| 3097 | goto out; |
| 3098 | default: |
| 3099 | break; |
| 3100 | } |
| 3101 | |
| 3102 | /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */ |
| 3103 | BUG_ON(!pkt); |
| 3104 | |
| 3105 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3106 | pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3107 | |
| 3108 | total += pkt->len; |
| 3109 | |
| 3110 | /* Write datagram header. */ |
| 3111 | qc_txb_store(buf, pkt->len, pkt); |
| 3112 | } |
| 3113 | |
| 3114 | out: |
| 3115 | ret = total; |
| 3116 | leave: |
| 3117 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3118 | return ret; |
| 3119 | } |
| 3120 | |
| 3121 | /* Prepare as much as possible QUIC packets for sending from prebuilt frames |
| 3122 | * <frms>. Several packets can be regrouped in a single datagram. The result is |
| 3123 | * written into <buf>. |
| 3124 | * |
| 3125 | * Each datagram is prepended by a two fields header : the datagram length and |
| 3126 | * the address of first packet in the datagram. |
| 3127 | * |
| 3128 | * Returns the number of bytes prepared in packets if succeeded (may be 0), or |
| 3129 | * -1 if something wrong happened. |
| 3130 | */ |
| 3131 | static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf, |
| 3132 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 3133 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 3134 | { |
| 3135 | struct quic_enc_level *qel; |
| 3136 | unsigned char *end, *pos; |
| 3137 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 3138 | /* length of datagrams */ |
| 3139 | uint16_t dglen; |
| 3140 | size_t total; |
| 3141 | int ret = -1, padding; |
| 3142 | /* Each datagram is prepended with its length followed by the address |
| 3143 | * of the first packet in the datagram. |
| 3144 | */ |
| 3145 | const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt); |
| 3146 | struct list *frms; |
| 3147 | |
| 3148 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 3149 | |
| 3150 | /* Currently qc_prep_pkts() does not handle buffer wrapping so the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 3151 | * caller must ensure that buf is reset. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3152 | */ |
| 3153 | BUG_ON_HOT(buf->head || buf->data); |
| 3154 | |
| 3155 | total = 0; |
| 3156 | qel = &qc->els[tel]; |
| 3157 | frms = tel_frms; |
| 3158 | dglen = 0; |
| 3159 | padding = 0; |
| 3160 | pos = (unsigned char *)b_head(buf); |
| 3161 | first_pkt = prv_pkt = NULL; |
| 3162 | while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
| 3163 | int err, probe, cc; |
| 3164 | enum quic_pkt_type pkt_type; |
| 3165 | struct quic_tls_ctx *tls_ctx; |
| 3166 | const struct quic_version *ver; |
| 3167 | int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 3168 | (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3169 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3170 | |
| 3171 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
| 3172 | probe = 0; |
| 3173 | cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 3174 | /* We do not probe if an immediate close was asked */ |
| 3175 | if (!cc) |
| 3176 | probe = qel->pktns->tx.pto_probe; |
| 3177 | |
| 3178 | if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) { |
| 3179 | if (prv_pkt) |
| 3180 | qc_txb_store(buf, dglen, first_pkt); |
| 3181 | /* Let's select the next encryption level */ |
| 3182 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 3183 | tel = next_tel; |
| 3184 | frms = next_tel_frms; |
| 3185 | qel = &qc->els[tel]; |
| 3186 | /* Build a new datagram */ |
| 3187 | prv_pkt = NULL; |
| 3188 | TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc); |
| 3189 | continue; |
| 3190 | } |
| 3191 | break; |
| 3192 | } |
| 3193 | |
| 3194 | pkt_type = quic_tls_level_pkt_type(tel); |
| 3195 | if (!prv_pkt) { |
| 3196 | /* Leave room for the datagram header */ |
| 3197 | pos += dg_headlen; |
| 3198 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
| 3199 | end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 3200 | } |
| 3201 | else { |
| 3202 | end = pos + qc->path->mtu; |
| 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | if (qc->negotiated_version) { |
| 3207 | ver = qc->negotiated_version; |
| 3208 | if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 3209 | tls_ctx = &qc->negotiated_ictx; |
| 3210 | else |
| 3211 | tls_ctx = &qel->tls_ctx; |
| 3212 | } |
| 3213 | else { |
| 3214 | ver = qc->original_version; |
| 3215 | tls_ctx = &qel->tls_ctx; |
| 3216 | } |
| 3217 | |
| 3218 | cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms, |
| 3219 | qc, ver, dglen, pkt_type, |
| 3220 | force_ack, padding, probe, cc, &err); |
| 3221 | switch (err) { |
| 3222 | case -2: |
| 3223 | // trace already emitted by function above |
| 3224 | goto leave; |
| 3225 | case -1: |
| 3226 | /* If there was already a correct packet present, set the |
| 3227 | * current datagram as prepared into <cbuf>. |
| 3228 | */ |
| 3229 | if (prv_pkt) |
| 3230 | qc_txb_store(buf, dglen, first_pkt); |
| 3231 | TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc); |
| 3232 | goto out; |
| 3233 | default: |
| 3234 | break; |
| 3235 | } |
| 3236 | |
| 3237 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 3238 | BUG_ON(!cur_pkt); |
| 3239 | |
| 3240 | if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA) |
| 3241 | cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA; |
| 3242 | |
| 3243 | total += cur_pkt->len; |
| 3244 | /* keep trace of the first packet in the datagram */ |
| 3245 | if (!first_pkt) |
| 3246 | first_pkt = cur_pkt; |
Frédéric Lécaille | 74b5f7b | 2022-11-20 18:35:35 +0100 | [diff] [blame] | 3247 | /* Attach the current one to the previous one and vice versa */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3248 | if (prv_pkt) { |
| 3249 | prv_pkt->next = cur_pkt; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 3250 | cur_pkt->prev = prv_pkt; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3251 | cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED; |
| 3252 | } |
| 3253 | /* Let's say we have to build a new dgram */ |
| 3254 | prv_pkt = NULL; |
| 3255 | dglen += cur_pkt->len; |
| 3256 | /* Client: discard the Initial encryption keys as soon as |
| 3257 | * a handshake packet could be built. |
| 3258 | */ |
| 3259 | if (qc->state == QUIC_HS_ST_CLIENT_INITIAL && |
| 3260 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 3261 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3262 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3263 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 3264 | qc_set_timer(qc); |
| 3265 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 3266 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
| 3267 | qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE; |
| 3268 | } |
| 3269 | /* If the data for the current encryption level have all been sent, |
| 3270 | * select the next level. |
| 3271 | */ |
| 3272 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
| 3273 | next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) { |
| 3274 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 3275 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 3276 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3277 | tel = next_tel; |
| 3278 | if (tel == QUIC_TLS_ENC_LEVEL_APP) |
| 3279 | frms = &qc->els[tel].pktns->tx.frms; |
| 3280 | else |
| 3281 | frms = next_tel_frms; |
| 3282 | qel = &qc->els[tel]; |
| 3283 | if (!LIST_ISEMPTY(frms)) { |
| 3284 | /* If there is data for the next level, do not |
| 3285 | * consume a datagram. |
| 3286 | */ |
| 3287 | prv_pkt = cur_pkt; |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | /* If we have to build a new datagram, set the current datagram as |
| 3292 | * prepared into <cbuf>. |
| 3293 | */ |
| 3294 | if (!prv_pkt) { |
| 3295 | qc_txb_store(buf, dglen, first_pkt); |
| 3296 | first_pkt = NULL; |
| 3297 | dglen = 0; |
| 3298 | padding = 0; |
| 3299 | } |
| 3300 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
| 3301 | (!qc_is_listener(qc) || |
| 3302 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 3303 | padding = 1; |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | out: |
| 3308 | ret = total; |
| 3309 | leave: |
| 3310 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
| 3311 | return ret; |
| 3312 | } |
| 3313 | |
| 3314 | /* Send datagrams stored in <buf>. |
| 3315 | * |
| 3316 | * This function always returns 1 for success. Even if sendto() syscall failed, |
| 3317 | * buffer is drained and packets are considered as emitted. QUIC loss detection |
| 3318 | * mechanism is used as a back door way to retry sending. |
| 3319 | */ |
| 3320 | int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx) |
| 3321 | { |
| 3322 | struct quic_conn *qc; |
| 3323 | char skip_sendto = 0; |
| 3324 | |
| 3325 | qc = ctx->qc; |
| 3326 | TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc); |
| 3327 | while (b_contig_data(buf, 0)) { |
| 3328 | unsigned char *pos; |
| 3329 | struct buffer tmpbuf = { }; |
| 3330 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 3331 | uint16_t dglen; |
| 3332 | size_t headlen = sizeof dglen + sizeof first_pkt; |
| 3333 | unsigned int time_sent; |
| 3334 | |
| 3335 | pos = (unsigned char *)b_head(buf); |
| 3336 | dglen = read_u16(pos); |
| 3337 | BUG_ON_HOT(!dglen); /* this should not happen */ |
| 3338 | |
| 3339 | pos += sizeof dglen; |
| 3340 | first_pkt = read_ptr(pos); |
| 3341 | pos += sizeof first_pkt; |
| 3342 | tmpbuf.area = (char *)pos; |
| 3343 | tmpbuf.size = tmpbuf.data = dglen; |
| 3344 | |
| 3345 | TRACE_DATA("send dgram", QUIC_EV_CONN_SPPKTS, qc); |
| 3346 | /* If sendto is on error just skip the call to it for the rest |
| 3347 | * of the loop but continue to purge the buffer. Data will be |
| 3348 | * transmitted when QUIC packets are detected as lost on our |
| 3349 | * side. |
| 3350 | * |
| 3351 | * TODO use fd-monitoring to detect when send operation can be |
| 3352 | * retry. This should improve the bandwidth without relying on |
| 3353 | * retransmission timer. However, it requires a major rework on |
| 3354 | * quic-conn fd management. |
| 3355 | */ |
| 3356 | if (!skip_sendto) { |
| 3357 | if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) { |
| 3358 | skip_sendto = 1; |
| 3359 | TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc); |
| 3360 | } |
| 3361 | } |
| 3362 | |
| 3363 | b_del(buf, dglen + headlen); |
| 3364 | qc->tx.bytes += tmpbuf.data; |
| 3365 | time_sent = now_ms; |
| 3366 | |
| 3367 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 3368 | pkt->time_sent = time_sent; |
| 3369 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 3370 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
| 3371 | qc->path->ifae_pkts++; |
| 3372 | if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ) |
| 3373 | qc_idle_timer_rearm(qc, 0); |
| 3374 | } |
| 3375 | if (!(qc->flags & QUIC_FL_CONN_CLOSING) && |
| 3376 | (pkt->flags & QUIC_FL_TX_PACKET_CC)) { |
| 3377 | qc->flags |= QUIC_FL_CONN_CLOSING; |
| 3378 | qc_notify_close(qc); |
| 3379 | |
| 3380 | /* RFC 9000 10.2. Immediate Close: |
| 3381 | * The closing and draining connection states exist to ensure |
| 3382 | * that connections close cleanly and that delayed or reordered |
| 3383 | * packets are properly discarded. These states SHOULD persist |
| 3384 | * for at least three times the current PTO interval... |
| 3385 | * |
| 3386 | * Rearm the idle timeout only one time when entering closing |
| 3387 | * state. |
| 3388 | */ |
| 3389 | qc_idle_timer_do_rearm(qc); |
| 3390 | if (qc->timer_task) { |
| 3391 | task_destroy(qc->timer_task); |
| 3392 | qc->timer_task = NULL; |
| 3393 | } |
| 3394 | } |
| 3395 | qc->path->in_flight += pkt->in_flight_len; |
| 3396 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 3397 | if (pkt->in_flight_len) |
| 3398 | qc_set_timer(qc); |
| 3399 | TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
| 3400 | next_pkt = pkt->next; |
| 3401 | quic_tx_packet_refinc(pkt); |
| 3402 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc); |
| 3407 | |
| 3408 | return 1; |
| 3409 | } |
| 3410 | |
| 3411 | /* Copy into <buf> buffer a stateless reset token depending on the |
| 3412 | * <salt> salt input. This is the cluster secret which will be derived |
| 3413 | * as HKDF input secret to generate this token. |
| 3414 | * Return 1 if succeeded, 0 if not. |
| 3415 | */ |
| 3416 | static int quic_stateless_reset_token_cpy(struct quic_conn *qc, |
| 3417 | unsigned char *buf, size_t len, |
| 3418 | const unsigned char *salt, size_t saltlen) |
| 3419 | { |
| 3420 | /* Input secret */ |
| 3421 | const unsigned char *key = (const unsigned char *)global.cluster_secret; |
| 3422 | size_t keylen = strlen(global.cluster_secret); |
| 3423 | /* Info */ |
| 3424 | const unsigned char label[] = "stateless token"; |
| 3425 | size_t labellen = sizeof label - 1; |
| 3426 | int ret; |
| 3427 | |
| 3428 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3429 | |
| 3430 | ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len, |
| 3431 | key, keylen, salt, saltlen, label, labellen); |
| 3432 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3433 | return ret; |
| 3434 | } |
| 3435 | |
| 3436 | /* Initialize the stateless reset token attached to <cid> connection ID. |
| 3437 | * Returns 1 if succeeded, 0 if not. |
| 3438 | */ |
| 3439 | static int quic_stateless_reset_token_init(struct quic_conn *qc, |
| 3440 | struct quic_connection_id *quic_cid) |
| 3441 | { |
| 3442 | int ret; |
| 3443 | |
| 3444 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3445 | |
| 3446 | if (global.cluster_secret) { |
| 3447 | /* Output secret */ |
| 3448 | unsigned char *token = quic_cid->stateless_reset_token; |
| 3449 | size_t tokenlen = sizeof quic_cid->stateless_reset_token; |
| 3450 | /* Salt */ |
| 3451 | const unsigned char *cid = quic_cid->cid.data; |
| 3452 | size_t cidlen = quic_cid->cid.len; |
| 3453 | |
| 3454 | ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen); |
| 3455 | } |
| 3456 | else { |
| 3457 | /* TODO: RAND_bytes() should be replaced */ |
| 3458 | ret = RAND_bytes(quic_cid->stateless_reset_token, |
| 3459 | sizeof quic_cid->stateless_reset_token) == 1; |
| 3460 | } |
| 3461 | |
| 3462 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3463 | return ret; |
| 3464 | } |
| 3465 | |
| 3466 | /* Allocate a new CID with <seq_num> as sequence number and attach it to <root> |
| 3467 | * ebtree. |
| 3468 | * |
| 3469 | * The CID is randomly generated in part with the result altered to be |
| 3470 | * associated with the current thread ID. This means this function must only |
| 3471 | * be called by the quic_conn thread. |
| 3472 | * |
| 3473 | * Returns the new CID if succeeded, NULL if not. |
| 3474 | */ |
| 3475 | static struct quic_connection_id *new_quic_cid(struct eb_root *root, |
| 3476 | struct quic_conn *qc, |
| 3477 | int seq_num) |
| 3478 | { |
| 3479 | struct quic_connection_id *cid; |
| 3480 | |
| 3481 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3482 | |
| 3483 | cid = pool_alloc(pool_head_quic_connection_id); |
| 3484 | if (!cid) { |
| 3485 | TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 3486 | goto err; |
| 3487 | } |
| 3488 | |
| 3489 | cid->cid.len = QUIC_HAP_CID_LEN; |
| 3490 | /* TODO: RAND_bytes() should be replaced */ |
| 3491 | if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) { |
| 3492 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc); |
| 3493 | goto err; |
| 3494 | } |
| 3495 | |
| 3496 | quic_pin_cid_to_tid(cid->cid.data, tid); |
| 3497 | if (quic_stateless_reset_token_init(qc, cid) != 1) { |
| 3498 | TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc); |
| 3499 | goto err; |
| 3500 | } |
| 3501 | |
| 3502 | cid->qc = qc; |
| 3503 | |
| 3504 | cid->seq_num.key = seq_num; |
| 3505 | cid->retire_prior_to = 0; |
| 3506 | /* insert the allocated CID in the quic_conn tree */ |
| 3507 | eb64_insert(root, &cid->seq_num); |
| 3508 | |
| 3509 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3510 | return cid; |
| 3511 | |
| 3512 | err: |
| 3513 | pool_free(pool_head_quic_connection_id, cid); |
| 3514 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3515 | return NULL; |
| 3516 | } |
| 3517 | |
| 3518 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 3519 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 3520 | * a HANDSHAKE_DONE frame. |
| 3521 | * Return 1 if succeeded, 0 if not. |
| 3522 | */ |
| 3523 | static int quic_build_post_handshake_frames(struct quic_conn *qc) |
| 3524 | { |
| 3525 | int ret = 0, i, first, max; |
| 3526 | struct quic_enc_level *qel; |
| 3527 | struct quic_frame *frm, *frmbak; |
| 3528 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 3529 | struct eb64_node *node; |
| 3530 | |
| 3531 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 3532 | |
| 3533 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3534 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
| 3535 | if (qc_is_listener(qc)) { |
| 3536 | frm = pool_zalloc(pool_head_quic_frame); |
| 3537 | if (!frm) { |
| 3538 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3539 | goto leave; |
| 3540 | } |
| 3541 | |
| 3542 | LIST_INIT(&frm->reflist); |
| 3543 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
| 3544 | LIST_APPEND(&frm_list, &frm->list); |
| 3545 | } |
| 3546 | |
| 3547 | /* Initialize <max> connection IDs minus one: there is |
| 3548 | * already one connection ID used for the current connection. |
| 3549 | */ |
| 3550 | first = 1; |
| 3551 | max = qc->tx.params.active_connection_id_limit; |
| 3552 | |
| 3553 | /* TODO: check limit */ |
| 3554 | for (i = first; i < max; i++) { |
| 3555 | struct quic_connection_id *cid; |
| 3556 | |
| 3557 | frm = pool_zalloc(pool_head_quic_frame); |
| 3558 | if (!frm) { |
| 3559 | TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3560 | goto err; |
| 3561 | } |
| 3562 | |
| 3563 | LIST_INIT(&frm->reflist); |
| 3564 | cid = new_quic_cid(&qc->cids, qc, i); |
| 3565 | if (!cid) { |
| 3566 | pool_free(pool_head_quic_frame, frm); |
| 3567 | TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc); |
| 3568 | goto err; |
| 3569 | } |
| 3570 | |
| 3571 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 3572 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
| 3573 | |
| 3574 | quic_connection_id_to_frm_cpy(frm, cid); |
| 3575 | LIST_APPEND(&frm_list, &frm->list); |
| 3576 | } |
| 3577 | |
| 3578 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
| 3579 | qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT; |
| 3580 | |
| 3581 | ret = 1; |
| 3582 | leave: |
| 3583 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 3584 | return ret; |
| 3585 | |
| 3586 | err: |
| 3587 | /* free the frames */ |
| 3588 | list_for_each_entry_safe(frm, frmbak, &frm_list, list) |
| 3589 | pool_free(pool_head_quic_frame, frm); |
| 3590 | |
| 3591 | node = eb64_lookup_ge(&qc->cids, first); |
| 3592 | while (node) { |
| 3593 | struct quic_connection_id *cid; |
| 3594 | |
| 3595 | cid = eb64_entry(node, struct quic_connection_id, seq_num); |
| 3596 | if (cid->seq_num.key >= max) |
| 3597 | break; |
| 3598 | |
| 3599 | node = eb64_next(node); |
| 3600 | ebmb_delete(&cid->node); |
| 3601 | eb64_delete(&cid->seq_num); |
| 3602 | pool_free(pool_head_quic_connection_id, cid); |
| 3603 | } |
| 3604 | goto leave; |
| 3605 | } |
| 3606 | |
| 3607 | /* Deallocate <l> list of ACK ranges. */ |
| 3608 | void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs) |
| 3609 | { |
| 3610 | struct eb64_node *n; |
| 3611 | struct quic_arng_node *ar; |
| 3612 | |
| 3613 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 3614 | |
| 3615 | n = eb64_first(&arngs->root); |
| 3616 | while (n) { |
| 3617 | struct eb64_node *next; |
| 3618 | |
| 3619 | ar = eb64_entry(n, struct quic_arng_node, first); |
| 3620 | next = eb64_next(n); |
| 3621 | eb64_delete(n); |
| 3622 | pool_free(pool_head_quic_arng, ar); |
| 3623 | n = next; |
| 3624 | } |
| 3625 | |
| 3626 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 3627 | } |
| 3628 | |
| 3629 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 3630 | * descending order. |
| 3631 | */ |
| 3632 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 3633 | struct quic_arng_node *q) |
| 3634 | { |
| 3635 | return p->first.key - q->last - 2; |
| 3636 | } |
| 3637 | |
| 3638 | |
| 3639 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 3640 | * encoded size until it goes below <limit>. |
| 3641 | * Returns 1 if succeeded, 0 if not (no more element to remove). |
| 3642 | */ |
| 3643 | static int quic_rm_last_ack_ranges(struct quic_conn *qc, |
| 3644 | struct quic_arngs *arngs, size_t limit) |
| 3645 | { |
| 3646 | int ret = 0; |
| 3647 | struct eb64_node *last, *prev; |
| 3648 | |
| 3649 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3650 | |
| 3651 | last = eb64_last(&arngs->root); |
| 3652 | while (last && arngs->enc_sz > limit) { |
| 3653 | struct quic_arng_node *last_node, *prev_node; |
| 3654 | |
| 3655 | prev = eb64_prev(last); |
| 3656 | if (!prev) { |
| 3657 | TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc); |
| 3658 | goto out; |
| 3659 | } |
| 3660 | |
| 3661 | last_node = eb64_entry(last, struct quic_arng_node, first); |
| 3662 | prev_node = eb64_entry(prev, struct quic_arng_node, first); |
| 3663 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 3664 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 3665 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 3666 | --arngs->sz; |
| 3667 | eb64_delete(last); |
| 3668 | pool_free(pool_head_quic_arng, last); |
| 3669 | last = prev; |
| 3670 | } |
| 3671 | |
| 3672 | ret = 1; |
| 3673 | out: |
| 3674 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3675 | return ret; |
| 3676 | } |
| 3677 | |
| 3678 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 3679 | static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs) |
| 3680 | { |
| 3681 | struct eb64_node *node, *next; |
| 3682 | struct quic_arng_node *ar, *ar_next; |
| 3683 | |
| 3684 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 3685 | |
| 3686 | node = eb64_last(&arngs->root); |
| 3687 | if (!node) |
| 3688 | goto leave; |
| 3689 | |
| 3690 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 3691 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 3692 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 3693 | |
| 3694 | while ((next = eb64_prev(node))) { |
| 3695 | ar_next = eb64_entry(next, struct quic_arng_node, first); |
| 3696 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 3697 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 3698 | node = next; |
| 3699 | ar = eb64_entry(node, struct quic_arng_node, first); |
| 3700 | } |
| 3701 | |
| 3702 | leave: |
| 3703 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 3704 | } |
| 3705 | |
| 3706 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 3707 | * Returns the ack range node which has been inserted if succeeded, NULL if not. |
| 3708 | */ |
| 3709 | static inline |
| 3710 | struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc, |
| 3711 | struct quic_arngs *arngs, |
| 3712 | struct quic_arng *ar) |
| 3713 | { |
| 3714 | struct quic_arng_node *new_ar; |
| 3715 | |
| 3716 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3717 | |
| 3718 | new_ar = pool_alloc(pool_head_quic_arng); |
| 3719 | if (!new_ar) { |
| 3720 | TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc); |
| 3721 | goto leave; |
| 3722 | } |
| 3723 | |
| 3724 | new_ar->first.key = ar->first; |
| 3725 | new_ar->last = ar->last; |
| 3726 | eb64_insert(&arngs->root, &new_ar->first); |
| 3727 | arngs->sz++; |
| 3728 | |
| 3729 | leave: |
| 3730 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 3731 | return new_ar; |
| 3732 | } |
| 3733 | |
| 3734 | /* Update <arngs> tree of ACK ranges with <ar> as new ACK range value. |
| 3735 | * Note that this function computes the number of bytes required to encode |
| 3736 | * this tree of ACK ranges in descending order. |
| 3737 | * |
| 3738 | * Descending order |
| 3739 | * -------------> |
| 3740 | * range1 range2 |
| 3741 | * ..........|--------|..............|--------| |
| 3742 | * ^ ^ ^ ^ |
| 3743 | * | | | | |
| 3744 | * last1 first1 last2 first2 |
| 3745 | * ..........+--------+--------------+--------+...... |
| 3746 | * diff1 gap12 diff2 |
| 3747 | * |
| 3748 | * To encode the previous list of ranges we must encode integers as follows in |
| 3749 | * descending order: |
| 3750 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
| 3751 | * with diff1 = last1 - first1 |
| 3752 | * diff2 = last2 - first2 |
| 3753 | * gap12 = first1 - last2 - 2 (>= 0) |
| 3754 | * |
| 3755 | |
| 3756 | returns 0 on error |
| 3757 | |
| 3758 | */ |
| 3759 | int quic_update_ack_ranges_list(struct quic_conn *qc, |
| 3760 | struct quic_arngs *arngs, |
| 3761 | struct quic_arng *ar) |
| 3762 | { |
| 3763 | int ret = 0; |
| 3764 | struct eb64_node *le; |
| 3765 | struct quic_arng_node *new_node; |
| 3766 | struct eb64_node *new; |
| 3767 | |
| 3768 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 3769 | |
| 3770 | new = NULL; |
| 3771 | if (eb_is_empty(&arngs->root)) { |
| 3772 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 3773 | if (new_node) |
| 3774 | ret = 1; |
| 3775 | |
| 3776 | goto leave; |
| 3777 | } |
| 3778 | |
| 3779 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 3780 | if (!le) { |
| 3781 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 3782 | if (!new_node) |
| 3783 | goto leave; |
| 3784 | |
| 3785 | new = &new_node->first; |
| 3786 | } |
| 3787 | else { |
| 3788 | struct quic_arng_node *le_ar = |
| 3789 | eb64_entry(le, struct quic_arng_node, first); |
| 3790 | |
| 3791 | /* Already existing range */ |
| 3792 | if (le_ar->last >= ar->last) { |
| 3793 | ret = 1; |
| 3794 | } |
| 3795 | else if (le_ar->last + 1 >= ar->first) { |
| 3796 | le_ar->last = ar->last; |
| 3797 | new = le; |
| 3798 | new_node = le_ar; |
| 3799 | } |
| 3800 | else { |
| 3801 | new_node = quic_insert_new_range(qc, arngs, ar); |
| 3802 | if (!new_node) |
| 3803 | goto leave; |
| 3804 | |
| 3805 | new = &new_node->first; |
| 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | /* Verify that the new inserted node does not overlap the nodes |
| 3810 | * which follow it. |
| 3811 | */ |
| 3812 | if (new) { |
| 3813 | struct eb64_node *next; |
| 3814 | struct quic_arng_node *next_node; |
| 3815 | |
| 3816 | while ((next = eb64_next(new))) { |
| 3817 | next_node = |
| 3818 | eb64_entry(next, struct quic_arng_node, first); |
| 3819 | if (new_node->last + 1 < next_node->first.key) |
| 3820 | break; |
| 3821 | |
| 3822 | if (next_node->last > new_node->last) |
| 3823 | new_node->last = next_node->last; |
| 3824 | eb64_delete(next); |
| 3825 | pool_free(pool_head_quic_arng, next_node); |
| 3826 | /* Decrement the size of these ranges. */ |
| 3827 | arngs->sz--; |
| 3828 | } |
| 3829 | } |
| 3830 | |
| 3831 | ret = 1; |
| 3832 | leave: |
| 3833 | quic_arngs_set_enc_sz(qc, arngs); |
| 3834 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 3835 | return ret; |
| 3836 | } |
| 3837 | /* Remove the header protection of packets at <el> encryption level. |
| 3838 | * Always succeeds. |
| 3839 | */ |
| 3840 | static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el) |
| 3841 | { |
| 3842 | struct quic_tls_ctx *tls_ctx; |
| 3843 | struct quic_rx_packet *pqpkt, *pkttmp; |
| 3844 | struct quic_enc_level *app_qel; |
| 3845 | |
| 3846 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3847 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 3848 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
| 3849 | if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) { |
| 3850 | TRACE_DEVEL("hp not removed (handshake not completed)", |
| 3851 | QUIC_EV_CONN_ELRMHP, qc); |
| 3852 | goto out; |
| 3853 | } |
| 3854 | tls_ctx = &el->tls_ctx; |
| 3855 | list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) { |
| 3856 | if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
| 3857 | pqpkt->data + pqpkt->pn_offset, pqpkt->data)) { |
| 3858 | TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc); |
| 3859 | } |
| 3860 | else { |
| 3861 | /* The AAD includes the packet number field */ |
| 3862 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3863 | /* Store the packet into the tree of packets to decrypt. */ |
| 3864 | pqpkt->pn_node.key = pqpkt->pn; |
| 3865 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3866 | quic_rx_packet_refinc(pqpkt); |
| 3867 | TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt); |
| 3868 | } |
| 3869 | LIST_DELETE(&pqpkt->list); |
| 3870 | quic_rx_packet_refdec(pqpkt); |
| 3871 | } |
| 3872 | |
| 3873 | out: |
| 3874 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
| 3875 | } |
| 3876 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3877 | /* Process all the CRYPTO frame at <el> encryption level. This is the |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 3878 | * 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] | 3879 | * stream for this level. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3880 | * Return 1 if succeeded, 0 if not. |
| 3881 | */ |
| 3882 | static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc, |
| 3883 | struct quic_enc_level *el, |
| 3884 | struct ssl_sock_ctx *ctx) |
| 3885 | { |
| 3886 | int ret = 0; |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3887 | struct ncbuf *ncbuf; |
| 3888 | struct quic_cstream *cstream = el->cstream; |
| 3889 | ncb_sz_t data; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3890 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3891 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3892 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3893 | BUG_ON(!cstream); |
| 3894 | ncbuf = &cstream->rx.ncbuf; |
| 3895 | if (ncb_is_null(ncbuf)) |
| 3896 | goto done; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3897 | |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 3898 | /* TODO not working if buffer is wrapping */ |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3899 | while ((data = ncb_data(ncbuf, 0))) { |
| 3900 | const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3901 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3902 | if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3903 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3904 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3905 | cstream->rx.offset += data; |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 3906 | TRACE_DEVEL("buffered crypto data were provided to TLS stack", |
| 3907 | QUIC_EV_CONN_PHPKTS, qc, el); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3908 | } |
| 3909 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3910 | done: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3911 | ret = 1; |
| 3912 | leave: |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 3913 | if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) { |
| 3914 | 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] | 3915 | quic_free_ncbuf(ncbuf); |
Amaury Denoyelle | 2f668f0 | 2022-11-18 15:24:08 +0100 | [diff] [blame] | 3916 | } |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3917 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3918 | return ret; |
| 3919 | } |
| 3920 | |
| 3921 | /* Process all the packets at <el> and <next_el> encryption level. |
| 3922 | * This is the caller responsibility to check that <cur_el> is different of <next_el> |
| 3923 | * as pointer value. |
| 3924 | * Return 1 if succeeded, 0 if not. |
| 3925 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3926 | int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el, |
| 3927 | struct quic_enc_level *next_el, int force_ack) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3928 | { |
| 3929 | int ret = 0; |
| 3930 | struct eb64_node *node; |
| 3931 | int64_t largest_pn = -1; |
| 3932 | unsigned int largest_pn_time_received = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3933 | struct quic_enc_level *qel = cur_el; |
| 3934 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3935 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3936 | qel = cur_el; |
| 3937 | next_tel: |
| 3938 | if (!qel) |
| 3939 | goto out; |
| 3940 | |
| 3941 | node = eb64_first(&qel->rx.pkts); |
| 3942 | while (node) { |
| 3943 | struct quic_rx_packet *pkt; |
| 3944 | |
| 3945 | pkt = eb64_entry(node, struct quic_rx_packet, pn_node); |
| 3946 | TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT, |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3947 | qc, pkt, NULL, qc->xprt_ctx->ssl); |
Amaury Denoyelle | 518c98f | 2022-11-24 17:12:25 +0100 | [diff] [blame] | 3948 | if (!qc_pkt_decrypt(qc, qel, pkt)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3949 | /* Drop the packet */ |
| 3950 | TRACE_ERROR("packet decryption failed -> dropped", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3951 | QUIC_EV_CONN_RXPKT, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3952 | } |
| 3953 | else { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3954 | if (!qc_parse_pkt_frms(qc, pkt, qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3955 | /* Drop the packet */ |
| 3956 | TRACE_ERROR("packet parsing failed -> dropped", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3957 | QUIC_EV_CONN_RXPKT, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3958 | HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing); |
| 3959 | } |
| 3960 | else { |
| 3961 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3962 | |
| 3963 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) { |
| 3964 | qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED; |
| 3965 | qel->pktns->rx.nb_aepkts_since_last_ack++; |
| 3966 | qc_idle_timer_rearm(qc, 1); |
| 3967 | } |
| 3968 | if (pkt->pn > largest_pn) { |
| 3969 | largest_pn = pkt->pn; |
| 3970 | largest_pn_time_received = pkt->time_received; |
| 3971 | } |
| 3972 | /* Update the list of ranges to acknowledge. */ |
| 3973 | if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar)) |
| 3974 | TRACE_ERROR("Could not update ack range list", |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 3975 | QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3976 | } |
| 3977 | } |
| 3978 | node = eb64_next(node); |
| 3979 | eb64_delete(&pkt->pn_node); |
| 3980 | quic_rx_packet_refdec(pkt); |
| 3981 | } |
| 3982 | |
| 3983 | if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) { |
| 3984 | /* Update the largest packet number. */ |
| 3985 | qel->pktns->rx.largest_pn = largest_pn; |
| 3986 | /* Update the largest acknowledged packet timestamps */ |
| 3987 | qel->pktns->rx.largest_time_received = largest_pn_time_received; |
| 3988 | qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 3989 | } |
| 3990 | |
Frédéric Lécaille | 9f9263e | 2022-09-13 14:36:44 +0200 | [diff] [blame] | 3991 | 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] | 3992 | // trace already emitted by function above |
| 3993 | goto leave; |
| 3994 | } |
| 3995 | |
| 3996 | if (qel == cur_el) { |
| 3997 | BUG_ON(qel == next_el); |
| 3998 | qel = next_el; |
| 3999 | largest_pn = -1; |
| 4000 | goto next_tel; |
| 4001 | } |
| 4002 | |
| 4003 | out: |
| 4004 | ret = 1; |
| 4005 | leave: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4006 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4007 | return ret; |
| 4008 | } |
| 4009 | |
| 4010 | /* Check if it's possible to remove header protection for packets related to |
| 4011 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 4012 | * |
| 4013 | * Return true if the operation is possible else false. |
| 4014 | */ |
| 4015 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4016 | { |
| 4017 | int ret = 0; |
| 4018 | enum quic_tls_enc_level tel; |
| 4019 | |
| 4020 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
| 4021 | |
| 4022 | if (!qel) |
| 4023 | goto cant_rm_hp; |
| 4024 | |
| 4025 | tel = ssl_to_quic_enc_level(qel->level); |
| 4026 | |
| 4027 | /* check if tls secrets are available */ |
| 4028 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 4029 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
| 4030 | goto cant_rm_hp; |
| 4031 | } |
| 4032 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4033 | if (!quic_tls_has_rx_sec(qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4034 | TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc); |
| 4035 | goto cant_rm_hp; |
| 4036 | } |
| 4037 | |
| 4038 | /* check if the connection layer is ready before using app level */ |
| 4039 | if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) && |
| 4040 | qc->mux_state == QC_MUX_NULL) { |
| 4041 | TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc); |
| 4042 | goto cant_rm_hp; |
| 4043 | } |
| 4044 | |
| 4045 | ret = 1; |
| 4046 | cant_rm_hp: |
| 4047 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc); |
| 4048 | return ret; |
| 4049 | } |
| 4050 | |
| 4051 | /* Try to send application frames from list <frms> on connection <qc>. |
| 4052 | * |
| 4053 | * Use qc_send_app_probing wrapper when probing with old data. |
| 4054 | * |
| 4055 | * Returns 1 on success. Some data might not have been sent due to congestion, |
| 4056 | * in this case they are left in <frms> input list. The caller may subscribe on |
| 4057 | * quic-conn to retry later. |
| 4058 | * |
| 4059 | * Returns 0 on critical error. |
| 4060 | * TODO review and classify more distinctly transient from definitive errors to |
| 4061 | * allow callers to properly handle it. |
| 4062 | */ |
| 4063 | static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms) |
| 4064 | { |
| 4065 | int status = 0; |
| 4066 | struct buffer *buf; |
| 4067 | |
| 4068 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4069 | |
| 4070 | buf = qc_txb_alloc(qc); |
| 4071 | if (!buf) { |
| 4072 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 4073 | goto leave; |
| 4074 | } |
| 4075 | |
| 4076 | /* Prepare and send packets until we could not further prepare packets. */ |
| 4077 | while (1) { |
| 4078 | int ret; |
| 4079 | /* Currently buf cannot be non-empty at this stage. Even if a |
| 4080 | * previous sendto() has failed it is emptied to simulate |
| 4081 | * packet emission and rely on QUIC lost detection to try to |
| 4082 | * emit it. |
| 4083 | */ |
| 4084 | BUG_ON_HOT(b_data(buf)); |
| 4085 | b_reset(buf); |
| 4086 | |
| 4087 | ret = qc_prep_app_pkts(qc, buf, frms); |
| 4088 | if (ret == -1) |
| 4089 | goto err; |
| 4090 | else if (ret == 0) |
| 4091 | goto out; |
| 4092 | |
| 4093 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
| 4094 | goto err; |
| 4095 | } |
| 4096 | |
| 4097 | out: |
| 4098 | status = 1; |
| 4099 | qc_txb_release(qc); |
| 4100 | leave: |
| 4101 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4102 | return status; |
| 4103 | |
| 4104 | err: |
| 4105 | qc_txb_release(qc); |
| 4106 | goto leave; |
| 4107 | } |
| 4108 | |
| 4109 | /* Try to send application frames from list <frms> on connection <qc>. Use this |
| 4110 | * function when probing is required. |
| 4111 | * |
| 4112 | * Returns the result from qc_send_app_pkts function. |
| 4113 | */ |
| 4114 | static forceinline int qc_send_app_probing(struct quic_conn *qc, |
| 4115 | struct list *frms) |
| 4116 | { |
| 4117 | int ret; |
| 4118 | |
| 4119 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4120 | |
| 4121 | TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc); |
| 4122 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4123 | ret = qc_send_app_pkts(qc, frms); |
| 4124 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4125 | |
| 4126 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4127 | return ret; |
| 4128 | } |
| 4129 | |
| 4130 | /* Try to send application frames from list <frms> on connection <qc>. This |
| 4131 | * function is provided for MUX upper layer usage only. |
| 4132 | * |
| 4133 | * Returns the result from qc_send_app_pkts function. |
| 4134 | */ |
| 4135 | int qc_send_mux(struct quic_conn *qc, struct list *frms) |
| 4136 | { |
| 4137 | int ret; |
| 4138 | |
| 4139 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4140 | BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */ |
| 4141 | |
| 4142 | TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc); |
| 4143 | qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4144 | ret = qc_send_app_pkts(qc, frms); |
| 4145 | qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT; |
| 4146 | |
| 4147 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4148 | return ret; |
| 4149 | } |
| 4150 | |
| 4151 | /* Sends handshake packets from up to two encryption levels <tel> and <next_te> |
| 4152 | * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc> |
| 4153 | * QUIC connection. <old_data> is used as boolean to send data already sent but |
| 4154 | * not already acknowledged (in flight). |
| 4155 | * Returns 1 if succeeded, 0 if not. |
| 4156 | */ |
| 4157 | int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data, |
| 4158 | enum quic_tls_enc_level tel, struct list *tel_frms, |
| 4159 | enum quic_tls_enc_level next_tel, struct list *next_tel_frms) |
| 4160 | { |
| 4161 | int ret, status = 0; |
| 4162 | struct buffer *buf = qc_txb_alloc(qc); |
| 4163 | |
| 4164 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4165 | |
| 4166 | if (!buf) { |
| 4167 | TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc); |
| 4168 | goto leave; |
| 4169 | } |
| 4170 | |
| 4171 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4172 | * sendto() has failed it is emptied to simulate packet emission and |
| 4173 | * rely on QUIC lost detection to try to emit it. |
| 4174 | */ |
| 4175 | BUG_ON_HOT(b_data(buf)); |
| 4176 | b_reset(buf); |
| 4177 | |
| 4178 | if (old_data) { |
| 4179 | TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc); |
| 4180 | qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4181 | } |
| 4182 | |
| 4183 | ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms); |
| 4184 | if (ret == -1) |
| 4185 | goto out; |
| 4186 | else if (ret == 0) |
| 4187 | goto skip_send; |
| 4188 | |
| 4189 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
| 4190 | goto out; |
| 4191 | |
| 4192 | skip_send: |
| 4193 | status = 1; |
| 4194 | out: |
| 4195 | TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc); |
| 4196 | qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA; |
| 4197 | qc_txb_release(qc); |
| 4198 | leave: |
| 4199 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4200 | return status; |
| 4201 | } |
| 4202 | |
| 4203 | /* Retransmit up to two datagrams depending on packet number space */ |
| 4204 | static void qc_dgrams_retransmit(struct quic_conn *qc) |
| 4205 | { |
| 4206 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4207 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4208 | struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4209 | |
| 4210 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 4211 | |
| 4212 | if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4213 | struct list ifrms = LIST_HEAD_INIT(ifrms); |
| 4214 | struct list hfrms = LIST_HEAD_INIT(hfrms); |
| 4215 | |
| 4216 | qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms); |
| 4217 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms); |
| 4218 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 4219 | if (!LIST_ISEMPTY(&ifrms)) { |
| 4220 | iqel->pktns->tx.pto_probe = 1; |
| 4221 | if (!LIST_ISEMPTY(&hfrms)) { |
| 4222 | hqel->pktns->tx.pto_probe = 1; |
| 4223 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms, |
| 4224 | QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms); |
| 4225 | /* Put back unsent frames in their packet number spaces */ |
| 4226 | LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms); |
| 4227 | LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms); |
| 4228 | } |
| 4229 | } |
| 4230 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4231 | /* This list has potentially been already used and spliced |
| 4232 | * to another one attached to the connection. We must reinitialize it. |
| 4233 | */ |
| 4234 | LIST_INIT(&hfrms); |
| 4235 | qc_prep_fast_retrans(qc, hqel, &hfrms, NULL); |
| 4236 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms); |
| 4237 | if (!LIST_ISEMPTY(&hfrms)) { |
| 4238 | hqel->pktns->tx.pto_probe = 1; |
| 4239 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms, |
| 4240 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 4241 | /* Put back unsent frames into their packet number spaces */ |
| 4242 | LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms); |
| 4243 | } |
| 4244 | TRACE_STATE("no more need to probe Handshake packet number space", |
| 4245 | QUIC_EV_CONN_TXPKT, qc); |
| 4246 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4247 | } |
| 4248 | TRACE_STATE("no more need to probe Initial packet number space", |
| 4249 | QUIC_EV_CONN_TXPKT, qc); |
| 4250 | iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4251 | } |
| 4252 | else { |
| 4253 | int i; |
| 4254 | |
| 4255 | if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4256 | hqel->pktns->tx.pto_probe = 0; |
| 4257 | 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] | 4258 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4259 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4260 | qc_prep_fast_retrans(qc, hqel, &frms1, NULL); |
| 4261 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4262 | if (!LIST_ISEMPTY(&frms1)) { |
| 4263 | hqel->pktns->tx.pto_probe = 1; |
| 4264 | qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1, |
| 4265 | QUIC_TLS_ENC_LEVEL_NONE, NULL); |
| 4266 | /* Put back unsent frames into their packet number spaces */ |
| 4267 | LIST_SPLICE(&hqel->pktns->tx.frms, &frms1); |
| 4268 | } |
| 4269 | } |
| 4270 | TRACE_STATE("no more need to probe Handshake packet number space", |
| 4271 | QUIC_EV_CONN_TXPKT, qc); |
| 4272 | hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4273 | } |
| 4274 | else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) { |
| 4275 | struct list frms2 = LIST_HEAD_INIT(frms2); |
| 4276 | struct list frms1 = LIST_HEAD_INIT(frms1); |
| 4277 | |
| 4278 | aqel->pktns->tx.pto_probe = 0; |
| 4279 | qc_prep_fast_retrans(qc, aqel, &frms1, &frms2); |
| 4280 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1); |
| 4281 | TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2); |
| 4282 | if (!LIST_ISEMPTY(&frms1)) { |
| 4283 | aqel->pktns->tx.pto_probe = 1; |
| 4284 | qc_send_app_probing(qc, &frms1); |
| 4285 | /* Put back unsent frames into their packet number spaces */ |
| 4286 | LIST_SPLICE(&aqel->pktns->tx.frms, &frms1); |
| 4287 | } |
| 4288 | if (!LIST_ISEMPTY(&frms2)) { |
| 4289 | aqel->pktns->tx.pto_probe = 1; |
| 4290 | qc_send_app_probing(qc, &frms2); |
| 4291 | /* Put back unsent frames into their packet number spaces */ |
| 4292 | LIST_SPLICE(&aqel->pktns->tx.frms, &frms2); |
| 4293 | } |
| 4294 | TRACE_STATE("no more need to probe 01RTT packet number space", |
| 4295 | QUIC_EV_CONN_TXPKT, qc); |
| 4296 | aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4297 | } |
| 4298 | } |
| 4299 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 4300 | } |
| 4301 | |
| 4302 | /* QUIC connection packet handler task (post handshake) */ |
| 4303 | struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state) |
| 4304 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4305 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4306 | struct quic_enc_level *qel; |
| 4307 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4308 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 4309 | |
| 4310 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
| 4311 | TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state); |
| 4312 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 4313 | if (qc_test_fd(qc)) |
| 4314 | qc_rcv_buf(qc); |
| 4315 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4316 | /* Retranmissions */ |
| 4317 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 4318 | TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc); |
| 4319 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 4320 | qc_dgrams_retransmit(qc); |
| 4321 | } |
| 4322 | |
| 4323 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 4324 | qc_rm_hp_pkts(qc, qel); |
| 4325 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4326 | if (!qc_treat_rx_pkts(qc, qel, NULL, 0)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4327 | TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4328 | goto out; |
| 4329 | } |
| 4330 | |
| 4331 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4332 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) { |
| 4333 | TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc); |
| 4334 | goto out; |
| 4335 | } |
| 4336 | |
| 4337 | /* XXX TODO: how to limit the list frames to send */ |
| 4338 | if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) { |
| 4339 | TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc); |
| 4340 | goto out; |
| 4341 | } |
| 4342 | |
| 4343 | out: |
| 4344 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc); |
| 4345 | return t; |
| 4346 | } |
| 4347 | |
| 4348 | /* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */ |
| 4349 | static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4350 | { |
| 4351 | return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) || |
| 4352 | (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) || |
| 4353 | qel->pktns->tx.pto_probe || |
| 4354 | !LIST_ISEMPTY(&qel->pktns->tx.frms); |
| 4355 | } |
| 4356 | |
| 4357 | /* QUIC connection packet handler task. */ |
| 4358 | struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state) |
| 4359 | { |
| 4360 | int ret, ssl_err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4361 | struct quic_conn *qc = context; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4362 | enum quic_tls_enc_level tel, next_tel; |
| 4363 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4364 | /* Early-data encryption level */ |
| 4365 | struct quic_enc_level *eqel; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4366 | struct buffer *buf = NULL; |
| 4367 | int st, force_ack, zero_rtt; |
| 4368 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4369 | TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc); |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4370 | eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4371 | st = qc->state; |
| 4372 | TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st); |
| 4373 | |
| 4374 | /* Retranmissions */ |
| 4375 | if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) { |
| 4376 | TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc); |
| 4377 | qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED; |
| 4378 | qc_dgrams_retransmit(qc); |
| 4379 | } |
| 4380 | |
| 4381 | if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 4382 | qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP; |
| 4383 | TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached", |
| 4384 | QUIC_EV_CONN_IO_CB, qc); |
| 4385 | /* The I/O handler has been woken up by the dgram parser (qc_lstnr_pkt_rcv()) |
| 4386 | * after the anti-amplification was reached. |
| 4387 | * |
| 4388 | * TODO: this part should be removed. This was there because the |
| 4389 | * datagram parser was not executed by only one thread. |
| 4390 | */ |
| 4391 | qc_set_timer(qc); |
Amaury Denoyelle | 5ac6b3b | 2022-12-14 18:04:06 +0100 | [diff] [blame] | 4392 | if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4393 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 4394 | } |
| 4395 | ssl_err = SSL_ERROR_NONE; |
| 4396 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4397 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4398 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 4399 | |
| 4400 | if (qc_test_fd(qc)) |
| 4401 | qc_rcv_buf(qc); |
| 4402 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4403 | if (st >= QUIC_HS_ST_COMPLETE && |
| 4404 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 4405 | TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 4406 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 4407 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 4408 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 4409 | } |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4410 | 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] | 4411 | goto out; |
| 4412 | |
| 4413 | qel = &qc->els[tel]; |
| 4414 | next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel]; |
| 4415 | |
| 4416 | next_level: |
| 4417 | /* Treat packets waiting for header packet protection decryption */ |
| 4418 | if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
| 4419 | qc_rm_hp_pkts(qc, qel); |
| 4420 | |
| 4421 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 4422 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4423 | if (!qc_treat_rx_pkts(qc, qel, next_qel, force_ack)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4424 | goto out; |
| 4425 | |
| 4426 | if ((qc->flags & QUIC_FL_CONN_DRAINING) && |
| 4427 | !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) |
| 4428 | goto out; |
| 4429 | |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4430 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4431 | quic_tls_has_rx_sec(eqel) && |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4432 | (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel)); |
| 4433 | if (next_qel && next_qel == eqel && zero_rtt) { |
| 4434 | TRACE_DEVEL("select 0RTT as next encryption level", |
| 4435 | QUIC_EV_CONN_PHPKTS, qc); |
| 4436 | qel = next_qel; |
| 4437 | next_qel = NULL; |
| 4438 | goto next_level; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | st = qc->state; |
| 4442 | if (st >= QUIC_HS_ST_COMPLETE) { |
| 4443 | if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) && |
| 4444 | !quic_build_post_handshake_frames(qc)) |
| 4445 | goto out; |
| 4446 | |
| 4447 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags & |
| 4448 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 4449 | /* Discard the Handshake keys. */ |
| 4450 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4451 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 4452 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 4453 | qc_set_timer(qc); |
| 4454 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 4455 | qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
| 4456 | } |
| 4457 | |
| 4458 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 4459 | /* There may be remaining handshake to build (acks) */ |
| 4460 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 4461 | } |
| 4462 | } |
| 4463 | |
| 4464 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 4465 | * be considered. |
| 4466 | */ |
Frédéric Lécaille | 4aa7d81 | 2022-09-16 10:15:58 +0200 | [diff] [blame] | 4467 | if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4468 | goto out; |
| 4469 | |
| 4470 | if (!qc_need_sending(qc, qel) && |
| 4471 | (!next_qel || !qc_need_sending(qc, next_qel))) { |
| 4472 | goto skip_send; |
| 4473 | } |
| 4474 | |
| 4475 | buf = qc_txb_alloc(qc); |
| 4476 | if (!buf) |
| 4477 | goto out; |
| 4478 | |
| 4479 | /* Currently buf cannot be non-empty at this stage. Even if a previous |
| 4480 | * sendto() has failed it is emptied to simulate packet emission and |
| 4481 | * rely on QUIC lost detection to try to emit it. |
| 4482 | */ |
| 4483 | BUG_ON_HOT(b_data(buf)); |
| 4484 | b_reset(buf); |
| 4485 | |
| 4486 | ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms, |
| 4487 | next_tel, &qc->els[next_tel].pktns->tx.frms); |
| 4488 | if (ret == -1) |
| 4489 | goto out; |
| 4490 | else if (ret == 0) |
| 4491 | goto skip_send; |
| 4492 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4493 | if (!qc_send_ppkts(buf, qc->xprt_ctx)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4494 | goto out; |
| 4495 | |
| 4496 | skip_send: |
| 4497 | /* Check if there is something to do for the next level. |
| 4498 | */ |
| 4499 | if (next_qel && next_qel != qel && |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4500 | quic_tls_has_rx_sec(next_qel) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4501 | (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) { |
| 4502 | qel = next_qel; |
| 4503 | next_qel = NULL; |
| 4504 | goto next_level; |
| 4505 | } |
| 4506 | |
| 4507 | out: |
| 4508 | qc_txb_release(qc); |
| 4509 | TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err); |
| 4510 | return t; |
| 4511 | } |
| 4512 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4513 | /* Release the memory allocated for <cs> CRYPTO stream */ |
| 4514 | void quic_cstream_free(struct quic_cstream *cs) |
| 4515 | { |
| 4516 | if (!cs) { |
| 4517 | /* This is the case for ORTT encryption level */ |
| 4518 | return; |
| 4519 | } |
| 4520 | |
Amaury Denoyelle | bc174b2 | 2022-11-17 10:12:52 +0100 | [diff] [blame] | 4521 | quic_free_ncbuf(&cs->rx.ncbuf); |
| 4522 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4523 | qc_stream_desc_release(cs->desc); |
| 4524 | pool_free(pool_head_quic_cstream, cs); |
| 4525 | } |
| 4526 | |
| 4527 | /* Allocate a new QUIC stream for <qc>. |
| 4528 | * Return it if succeeded, NULL if not. |
| 4529 | */ |
| 4530 | struct quic_cstream *quic_cstream_new(struct quic_conn *qc) |
| 4531 | { |
| 4532 | struct quic_cstream *cs, *ret_cs = NULL; |
| 4533 | |
| 4534 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 4535 | cs = pool_alloc(pool_head_quic_cstream); |
| 4536 | if (!cs) { |
| 4537 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 4538 | goto leave; |
| 4539 | } |
| 4540 | |
| 4541 | cs->rx.offset = 0; |
| 4542 | cs->rx.ncbuf = NCBUF_NULL; |
| 4543 | cs->rx.offset = 0; |
| 4544 | |
| 4545 | cs->tx.offset = 0; |
| 4546 | cs->tx.sent_offset = 0; |
| 4547 | cs->tx.buf = BUF_NULL; |
| 4548 | cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc); |
| 4549 | if (!cs->desc) { |
| 4550 | TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc); |
| 4551 | goto err; |
| 4552 | } |
| 4553 | |
| 4554 | ret_cs = cs; |
| 4555 | leave: |
| 4556 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 4557 | return ret_cs; |
| 4558 | |
| 4559 | err: |
| 4560 | pool_free(pool_head_quic_cstream, cs); |
| 4561 | goto leave; |
| 4562 | } |
| 4563 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4564 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
| 4565 | static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel) |
| 4566 | { |
| 4567 | int i; |
| 4568 | |
| 4569 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4570 | |
| 4571 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 4572 | if (qel->tx.crypto.bufs[i]) { |
| 4573 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 4574 | qel->tx.crypto.bufs[i] = NULL; |
| 4575 | } |
| 4576 | } |
| 4577 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4578 | quic_cstream_free(qel->cstream); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4579 | |
| 4580 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4581 | } |
| 4582 | |
| 4583 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
| 4584 | * connection allocating everything needed. |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4585 | * |
| 4586 | * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use |
| 4587 | * quic_conn_enc_level_uninit() to cleanup partially allocated content. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4588 | */ |
| 4589 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 4590 | enum quic_tls_enc_level level) |
| 4591 | { |
| 4592 | int ret = 0; |
| 4593 | struct quic_enc_level *qel; |
| 4594 | |
| 4595 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4596 | |
| 4597 | qel = &qc->els[level]; |
| 4598 | qel->level = quic_to_ssl_enc_level(level); |
| 4599 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 4600 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 4601 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 4602 | qel->tls_ctx.flags = 0; |
| 4603 | |
| 4604 | qel->rx.pkts = EB_ROOT; |
| 4605 | LIST_INIT(&qel->rx.pqpkts); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4606 | |
| 4607 | /* Allocate only one buffer. */ |
| 4608 | /* TODO: use a pool */ |
| 4609 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 4610 | if (!qel->tx.crypto.bufs) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4611 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4612 | |
| 4613 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 4614 | if (!qel->tx.crypto.bufs[0]) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4615 | goto leave; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4616 | |
| 4617 | qel->tx.crypto.bufs[0]->sz = 0; |
| 4618 | qel->tx.crypto.nb_buf = 1; |
| 4619 | |
| 4620 | qel->tx.crypto.sz = 0; |
| 4621 | qel->tx.crypto.offset = 0; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4622 | /* No CRYPTO data for early data TLS encryption level */ |
| 4623 | if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA) |
| 4624 | qel->cstream = NULL; |
| 4625 | else { |
| 4626 | qel->cstream = quic_cstream_new(qc); |
| 4627 | if (!qel->cstream) |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4628 | goto leave; |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 4629 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4630 | |
| 4631 | ret = 1; |
| 4632 | leave: |
| 4633 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 4634 | return ret; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | /* Callback called upon loss detection and PTO timer expirations. */ |
| 4638 | struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state) |
| 4639 | { |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4640 | struct quic_conn *qc = ctx; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4641 | struct quic_pktns *pktns; |
| 4642 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4643 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
| 4644 | NULL, NULL, &qc->path->ifae_pkts); |
| 4645 | task->expire = TICK_ETERNITY; |
| 4646 | pktns = quic_loss_pktns(qc); |
| 4647 | if (tick_isset(pktns->tx.loss_time)) { |
| 4648 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 4649 | |
| 4650 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 4651 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4652 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4653 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 4654 | qc_set_timer(qc); |
| 4655 | goto out; |
| 4656 | } |
| 4657 | |
| 4658 | if (qc->path->in_flight) { |
| 4659 | pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL); |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 4660 | if (qc->subs && qc->subs->events & SUB_RETRY_SEND) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4661 | pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS; |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 4662 | tasklet_wakeup(qc->subs->tasklet); |
| 4663 | qc->subs->events &= ~SUB_RETRY_SEND; |
| 4664 | if (!qc->subs->events) |
| 4665 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4666 | } |
| 4667 | else { |
| 4668 | qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED; |
| 4669 | pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4670 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 4671 | TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4672 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) { |
| 4673 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED; |
| 4674 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4675 | } |
| 4676 | } |
| 4677 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) { |
| 4678 | TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4679 | } |
| 4680 | else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) { |
| 4681 | TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc); |
| 4682 | } |
| 4683 | } |
| 4684 | } |
| 4685 | else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) { |
| 4686 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 4687 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 4688 | |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4689 | if (quic_tls_has_tx_sec(hel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4690 | hel->pktns->tx.pto_probe = 1; |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 4691 | if (quic_tls_has_tx_sec(iel)) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4692 | iel->pktns->tx.pto_probe = 1; |
| 4693 | } |
| 4694 | |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4695 | tasklet_wakeup(qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4696 | qc->path->loss.pto_count++; |
| 4697 | |
| 4698 | out: |
| 4699 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
| 4700 | |
| 4701 | return task; |
| 4702 | } |
| 4703 | |
| 4704 | /* Parse the Retry token from buffer <token> with <end> a pointer to |
| 4705 | * one byte past the end of this buffer. This will extract the ODCID |
| 4706 | * which will be stored into <odcid> |
| 4707 | * |
| 4708 | * Returns 0 on success else non-zero. |
| 4709 | */ |
| 4710 | static int parse_retry_token(struct quic_conn *qc, |
| 4711 | const unsigned char *token, const unsigned char *end, |
| 4712 | struct quic_cid *odcid) |
| 4713 | { |
| 4714 | int ret = 0; |
| 4715 | uint64_t odcid_len; |
| 4716 | uint32_t timestamp; |
| 4717 | |
| 4718 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 4719 | |
| 4720 | if (!quic_dec_int(&odcid_len, &token, end)) { |
| 4721 | TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc); |
| 4722 | goto leave; |
| 4723 | } |
| 4724 | |
| 4725 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 4726 | * When an Initial packet is sent by a client that has not previously |
| 4727 | * received an Initial or Retry packet from the server, the client |
| 4728 | * populates the Destination Connection ID field with an unpredictable |
| 4729 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 4730 | */ |
| 4731 | if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) { |
| 4732 | TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 4733 | goto leave; |
| 4734 | } |
| 4735 | |
| 4736 | if (end - token < odcid_len + sizeof timestamp) { |
| 4737 | TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc); |
| 4738 | goto leave; |
| 4739 | } |
| 4740 | |
| 4741 | timestamp = ntohl(read_u32(token + odcid_len)); |
| 4742 | if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) { |
| 4743 | TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc); |
| 4744 | goto leave; |
| 4745 | } |
| 4746 | |
| 4747 | ret = 1; |
| 4748 | memcpy(odcid->data, token, odcid_len); |
| 4749 | odcid->len = odcid_len; |
| 4750 | leave: |
| 4751 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 4752 | return !ret; |
| 4753 | } |
| 4754 | |
| 4755 | /* Allocate a new QUIC connection with <version> as QUIC version. <ipv4> |
| 4756 | * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1 |
| 4757 | * for QUIC servers (or haproxy listeners). |
| 4758 | * <dcid> is the destination connection ID, <scid> is the source connection ID, |
| 4759 | * <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] | 4760 | * length. Endpoints addresses are specified via <local_addr> and <peer_addr>. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4761 | * Returns the connection if succeeded, NULL if not. |
| 4762 | */ |
| 4763 | static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4, |
| 4764 | struct quic_cid *dcid, struct quic_cid *scid, |
| 4765 | const struct quic_cid *token_odcid, |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 4766 | struct sockaddr_storage *local_addr, |
| 4767 | struct sockaddr_storage *peer_addr, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4768 | int server, int token, void *owner) |
| 4769 | { |
| 4770 | int i; |
| 4771 | struct quic_conn *qc; |
| 4772 | /* Initial CID. */ |
| 4773 | struct quic_connection_id *icid; |
| 4774 | char *buf_area = NULL; |
| 4775 | struct listener *l = NULL; |
| 4776 | struct quic_cc_algo *cc_algo = NULL; |
| 4777 | struct quic_tls_ctx *ictx; |
| 4778 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4779 | /* TODO replace pool_zalloc by pool_alloc(). This requires special care |
| 4780 | * to properly initialized internal quic_conn members to safely use |
| 4781 | * quic_conn_release() on alloc failure. |
| 4782 | */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4783 | qc = pool_zalloc(pool_head_quic_conn); |
| 4784 | if (!qc) { |
| 4785 | TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 4786 | goto err; |
| 4787 | } |
| 4788 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4789 | /* Initialize in priority qc members required for a safe dealloc. */ |
| 4790 | |
| 4791 | /* required to use MTLIST_IN_LIST */ |
| 4792 | MT_LIST_INIT(&qc->accept_list); |
| 4793 | |
| 4794 | LIST_INIT(&qc->rx.pkt_list); |
| 4795 | |
Amaury Denoyelle | 4244833 | 2022-12-12 11:24:05 +0100 | [diff] [blame] | 4796 | qc_init_fd(qc); |
| 4797 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4798 | /* Now proceeds to allocation of qc members. */ |
| 4799 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4800 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 4801 | if (!buf_area) { |
| 4802 | TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc); |
| 4803 | goto err; |
| 4804 | } |
| 4805 | |
| 4806 | qc->cids = EB_ROOT; |
| 4807 | /* QUIC Server (or listener). */ |
| 4808 | if (server) { |
| 4809 | struct proxy *prx; |
| 4810 | |
| 4811 | l = owner; |
| 4812 | prx = l->bind_conf->frontend; |
| 4813 | cc_algo = l->bind_conf->quic_cc_algo; |
| 4814 | |
| 4815 | qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 4816 | &quic_stats_module); |
| 4817 | qc->flags |= QUIC_FL_CONN_LISTENER; |
| 4818 | qc->state = QUIC_HS_ST_SERVER_INITIAL; |
| 4819 | /* Copy the initial DCID with the address. */ |
| 4820 | qc->odcid.len = dcid->len; |
| 4821 | qc->odcid.addrlen = dcid->addrlen; |
| 4822 | memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen); |
| 4823 | |
| 4824 | /* copy the packet SCID to reuse it as DCID for sending */ |
| 4825 | if (scid->len) |
| 4826 | memcpy(qc->dcid.data, scid->data, scid->len); |
| 4827 | qc->dcid.len = scid->len; |
| 4828 | qc->tx.buf = BUF_NULL; |
| 4829 | qc->li = l; |
| 4830 | } |
| 4831 | /* QUIC Client (outgoing connection to servers) */ |
| 4832 | else { |
| 4833 | qc->state = QUIC_HS_ST_CLIENT_INITIAL; |
| 4834 | if (dcid->len) |
| 4835 | memcpy(qc->dcid.data, dcid->data, dcid->len); |
| 4836 | qc->dcid.len = dcid->len; |
| 4837 | } |
| 4838 | qc->mux_state = QC_MUX_NULL; |
| 4839 | qc->err = quic_err_transport(QC_ERR_NO_ERROR); |
| 4840 | |
| 4841 | icid = new_quic_cid(&qc->cids, qc, 0); |
| 4842 | if (!icid) { |
| 4843 | TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc); |
| 4844 | goto err; |
| 4845 | } |
| 4846 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 4847 | if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) && |
| 4848 | is_addr(local_addr)) { |
| 4849 | TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc); |
| 4850 | qc_alloc_fd(qc, local_addr, peer_addr); |
| 4851 | } |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 4852 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4853 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 4854 | if (server) |
| 4855 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
| 4856 | |
| 4857 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 4858 | qc->scid = icid->cid; |
| 4859 | |
| 4860 | /* Packet number spaces initialization. */ |
| 4861 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 4862 | quic_pktns_init(&qc->pktns[i]); |
| 4863 | /* QUIC encryption level context initialization. */ |
| 4864 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 4865 | if (!quic_conn_enc_level_init(qc, i)) { |
| 4866 | TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc); |
| 4867 | goto err; |
| 4868 | } |
| 4869 | /* Initialize the packet number space. */ |
| 4870 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 4871 | } |
| 4872 | |
| 4873 | qc->original_version = qv; |
| 4874 | qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ? |
| 4875 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 4876 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
| 4877 | /* TX part. */ |
| 4878 | LIST_INIT(&qc->tx.frms_to_send); |
| 4879 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 4880 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 4881 | qc->tx.bytes = 0; |
| 4882 | qc->tx.buf = BUF_NULL; |
| 4883 | /* RX part. */ |
| 4884 | qc->rx.bytes = 0; |
| 4885 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 4886 | for (i = 0; i < QCS_MAX_TYPES; i++) |
| 4887 | qc->rx.strms[i].nb_streams = 0; |
| 4888 | |
| 4889 | qc->nb_pkt_for_cc = 1; |
| 4890 | qc->nb_pkt_since_cc = 0; |
| 4891 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4892 | if (!quic_tls_ku_init(qc)) { |
| 4893 | TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc); |
| 4894 | goto err; |
| 4895 | } |
| 4896 | |
| 4897 | /* XXX TO DO: Only one path at this time. */ |
| 4898 | qc->path = &qc->paths[0]; |
| 4899 | quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc); |
| 4900 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4901 | qc->streams_by_id = EB_ROOT_UNIQUE; |
| 4902 | qc->stream_buf_count = 0; |
Amaury Denoyelle | 97ecc7a | 2022-09-23 17:15:58 +0200 | [diff] [blame] | 4903 | memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr)); |
| 4904 | memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4905 | |
| 4906 | if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params, |
| 4907 | icid->stateless_reset_token, |
| 4908 | dcid->data, dcid->len, |
| 4909 | qc->scid.data, qc->scid.len, token_odcid)) |
| 4910 | goto err; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 4911 | |
| 4912 | qc->wait_event.tasklet = tasklet_new(); |
| 4913 | if (!qc->wait_event.tasklet) { |
| 4914 | TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT); |
| 4915 | goto err; |
| 4916 | } |
| 4917 | qc->wait_event.tasklet->process = quic_conn_io_cb; |
| 4918 | qc->wait_event.tasklet->context = qc; |
| 4919 | qc->wait_event.events = 0; |
| 4920 | /* Set tasklet tid based on the SCID selected by us for this |
| 4921 | * connection. The upper layer will also be binded on the same thread. |
| 4922 | */ |
Willy Tarreau | eed7826 | 2022-12-21 09:09:19 +0100 | [diff] [blame] | 4923 | qc->tid = quic_get_cid_tid(qc->scid.data, &l->rx); |
Willy Tarreau | f5a0c8a | 2022-10-13 16:14:11 +0200 | [diff] [blame] | 4924 | qc->wait_event.tasklet->tid = qc->tid; |
Amaury Denoyelle | bbb1c68 | 2022-09-28 15:15:51 +0200 | [diff] [blame] | 4925 | qc->subs = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4926 | |
| 4927 | if (qc_conn_alloc_ssl_ctx(qc) || |
| 4928 | !quic_conn_init_timer(qc) || |
| 4929 | !quic_conn_init_idle_timer_task(qc)) |
| 4930 | goto err; |
| 4931 | |
| 4932 | ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx; |
| 4933 | if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1)) |
| 4934 | goto err; |
| 4935 | |
| 4936 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
| 4937 | |
| 4938 | return qc; |
| 4939 | |
| 4940 | err: |
| 4941 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4942 | if (qc) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4943 | qc->rx.buf.area = NULL; |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 4944 | quic_conn_release(qc); |
| 4945 | } |
| 4946 | TRACE_LEAVE(QUIC_EV_CONN_INIT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4947 | return NULL; |
| 4948 | } |
| 4949 | |
| 4950 | /* Release the quic_conn <qc>. The connection is removed from the CIDs tree. |
| 4951 | * The connection tasklet is killed. |
| 4952 | * |
| 4953 | * This function must only be called by the thread responsible of the quic_conn |
| 4954 | * tasklet. |
| 4955 | */ |
| 4956 | void quic_conn_release(struct quic_conn *qc) |
| 4957 | { |
| 4958 | int i; |
| 4959 | struct ssl_sock_ctx *conn_ctx; |
| 4960 | struct eb64_node *node; |
| 4961 | struct quic_tls_ctx *app_tls_ctx; |
| 4962 | struct quic_rx_packet *pkt, *pktback; |
| 4963 | |
| 4964 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 4965 | |
| 4966 | /* We must not free the quic-conn if the MUX is still allocated. */ |
| 4967 | BUG_ON(qc->mux_state == QC_MUX_READY); |
| 4968 | |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 4969 | /* Close quic-conn socket fd. */ |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 4970 | qc_release_fd(qc, 0); |
Amaury Denoyelle | 40909df | 2022-10-24 17:08:43 +0200 | [diff] [blame] | 4971 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 4972 | /* in the unlikely (but possible) case the connection was just added to |
| 4973 | * the accept_list we must delete it from there. |
| 4974 | */ |
| 4975 | MT_LIST_DELETE(&qc->accept_list); |
| 4976 | |
| 4977 | /* free remaining stream descriptors */ |
| 4978 | node = eb64_first(&qc->streams_by_id); |
| 4979 | while (node) { |
| 4980 | struct qc_stream_desc *stream; |
| 4981 | |
| 4982 | stream = eb64_entry(node, struct qc_stream_desc, by_id); |
| 4983 | node = eb64_next(node); |
| 4984 | |
| 4985 | /* all streams attached to the quic-conn are released, so |
| 4986 | * qc_stream_desc_free will liberate the stream instance. |
| 4987 | */ |
| 4988 | BUG_ON(!stream->release); |
| 4989 | qc_stream_desc_free(stream, 1); |
| 4990 | } |
| 4991 | |
| 4992 | /* Purge Rx packet list. */ |
| 4993 | list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) { |
| 4994 | LIST_DELETE(&pkt->qc_rx_pkt_list); |
| 4995 | pool_free(pool_head_quic_rx_packet, pkt); |
| 4996 | } |
| 4997 | |
| 4998 | if (qc->idle_timer_task) { |
| 4999 | task_destroy(qc->idle_timer_task); |
| 5000 | qc->idle_timer_task = NULL; |
| 5001 | } |
| 5002 | |
| 5003 | if (qc->timer_task) { |
| 5004 | task_destroy(qc->timer_task); |
| 5005 | qc->timer_task = NULL; |
| 5006 | } |
| 5007 | |
Amaury Denoyelle | dbf6ad4 | 2022-12-12 11:22:42 +0100 | [diff] [blame] | 5008 | if (qc->wait_event.tasklet) |
| 5009 | tasklet_free(qc->wait_event.tasklet); |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5010 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5011 | /* remove the connection from receiver cids trees */ |
| 5012 | ebmb_delete(&qc->odcid_node); |
| 5013 | ebmb_delete(&qc->scid_node); |
| 5014 | free_quic_conn_cids(qc); |
| 5015 | |
| 5016 | conn_ctx = qc->xprt_ctx; |
| 5017 | if (conn_ctx) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5018 | SSL_free(conn_ctx->ssl); |
| 5019 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
| 5020 | } |
| 5021 | |
| 5022 | quic_tls_ku_free(qc); |
| 5023 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 5024 | quic_tls_ctx_secs_free(&qc->els[i].tls_ctx); |
| 5025 | quic_conn_enc_level_uninit(qc, &qc->els[i]); |
| 5026 | } |
| 5027 | quic_tls_ctx_secs_free(&qc->negotiated_ictx); |
| 5028 | |
| 5029 | app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 5030 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret); |
| 5031 | pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret); |
| 5032 | |
| 5033 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 5034 | quic_pktns_tx_pkts_release(&qc->pktns[i], qc); |
| 5035 | quic_free_arngs(qc, &qc->pktns[i].rx.arngs); |
| 5036 | } |
| 5037 | |
| 5038 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 5039 | pool_free(pool_head_quic_conn, qc); |
| 5040 | TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc); |
| 5041 | |
| 5042 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 5043 | } |
| 5044 | |
| 5045 | /* Initialize the timer task of <qc> QUIC connection. |
| 5046 | * Returns 1 if succeeded, 0 if not. |
| 5047 | */ |
| 5048 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 5049 | { |
| 5050 | int ret = 0; |
| 5051 | /* Attach this task to the same thread ID used for the connection */ |
| 5052 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5053 | |
| 5054 | qc->timer_task = task_new_on(qc->tid); |
| 5055 | if (!qc->timer_task) { |
| 5056 | TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 5057 | goto leave; |
| 5058 | } |
| 5059 | |
| 5060 | qc->timer = TICK_ETERNITY; |
| 5061 | qc->timer_task->process = qc_process_timer; |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 5062 | qc->timer_task->context = qc; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5063 | |
| 5064 | ret = 1; |
| 5065 | leave: |
| 5066 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5067 | return ret; |
| 5068 | } |
| 5069 | |
| 5070 | /* Rearm the idle timer for <qc> QUIC connection. */ |
| 5071 | static void qc_idle_timer_do_rearm(struct quic_conn *qc) |
| 5072 | { |
| 5073 | unsigned int expire; |
| 5074 | |
| 5075 | expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout); |
| 5076 | qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire)); |
| 5077 | } |
| 5078 | |
| 5079 | /* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean |
| 5080 | * which is set to 1 when receiving a packet , and 0 when sending packet |
| 5081 | */ |
| 5082 | static void qc_idle_timer_rearm(struct quic_conn *qc, int read) |
| 5083 | { |
| 5084 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5085 | |
| 5086 | if (read) { |
| 5087 | qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 5088 | } |
| 5089 | else { |
| 5090 | qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ; |
| 5091 | } |
| 5092 | qc_idle_timer_do_rearm(qc); |
| 5093 | |
| 5094 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5095 | } |
| 5096 | |
| 5097 | /* The task handling the idle timeout */ |
| 5098 | struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state) |
| 5099 | { |
| 5100 | struct quic_conn *qc = ctx; |
| 5101 | struct quic_counters *prx_counters = qc->prx_counters; |
| 5102 | unsigned int qc_flags = qc->flags; |
| 5103 | |
| 5104 | TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5105 | |
| 5106 | /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX |
| 5107 | * might free the quic-conn too early via quic_close(). |
| 5108 | */ |
| 5109 | qc_notify_close(qc); |
| 5110 | |
| 5111 | /* If the MUX is still alive, keep the quic-conn. The MUX is |
| 5112 | * responsible to call quic_close to release it. |
| 5113 | */ |
| 5114 | qc->flags |= QUIC_FL_CONN_EXP_TIMER; |
| 5115 | if (qc->mux_state != QC_MUX_READY) |
| 5116 | quic_conn_release(qc); |
| 5117 | |
| 5118 | /* TODO if the quic-conn cannot be freed because of the MUX, we may at |
| 5119 | * least clean some parts of it such as the tasklet. |
| 5120 | */ |
| 5121 | |
| 5122 | if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) { |
| 5123 | qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED; |
| 5124 | TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc); |
| 5125 | HA_ATOMIC_DEC(&prx_counters->half_open_conn); |
| 5126 | } |
| 5127 | |
| 5128 | TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc); |
| 5129 | return NULL; |
| 5130 | } |
| 5131 | |
| 5132 | /* Initialize the idle timeout task for <qc>. |
| 5133 | * Returns 1 if succeeded, 0 if not. |
| 5134 | */ |
| 5135 | static int quic_conn_init_idle_timer_task(struct quic_conn *qc) |
| 5136 | { |
| 5137 | int ret = 0; |
| 5138 | |
| 5139 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5140 | |
| 5141 | qc->idle_timer_task = task_new_here(); |
| 5142 | if (!qc->idle_timer_task) { |
| 5143 | TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc); |
| 5144 | goto leave; |
| 5145 | } |
| 5146 | |
| 5147 | qc->idle_timer_task->process = qc_idle_timer_task; |
| 5148 | qc->idle_timer_task->context = qc; |
| 5149 | qc_idle_timer_rearm(qc, 1); |
| 5150 | task_queue(qc->idle_timer_task); |
| 5151 | |
| 5152 | ret = 1; |
| 5153 | leave: |
| 5154 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5155 | return ret; |
| 5156 | } |
| 5157 | |
| 5158 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 5159 | * past one byte of this buffer. |
| 5160 | */ |
| 5161 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 5162 | struct quic_rx_packet *pkt) |
| 5163 | { |
| 5164 | int ret = 0; |
| 5165 | unsigned char dcid_len, scid_len; |
| 5166 | |
| 5167 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5168 | |
| 5169 | if (end == *buf) { |
| 5170 | TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT); |
| 5171 | goto leave; |
| 5172 | } |
| 5173 | |
| 5174 | /* Destination Connection ID Length */ |
| 5175 | dcid_len = *(*buf)++; |
| 5176 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 5177 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) { |
| 5178 | TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT); |
| 5179 | goto leave; |
| 5180 | } |
| 5181 | |
| 5182 | if (dcid_len) { |
| 5183 | /* Check that the length of this received DCID matches the CID lengths |
| 5184 | * of our implementation for non Initials packets only. |
| 5185 | */ |
| 5186 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 5187 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
| 5188 | dcid_len != QUIC_HAP_CID_LEN) { |
| 5189 | TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT); |
| 5190 | goto leave; |
| 5191 | } |
| 5192 | |
| 5193 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 5194 | } |
| 5195 | |
| 5196 | pkt->dcid.len = dcid_len; |
| 5197 | *buf += dcid_len; |
| 5198 | |
| 5199 | /* Source Connection ID Length */ |
| 5200 | scid_len = *(*buf)++; |
| 5201 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) { |
| 5202 | TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT); |
| 5203 | goto leave; |
| 5204 | } |
| 5205 | |
| 5206 | if (scid_len) |
| 5207 | memcpy(pkt->scid.data, *buf, scid_len); |
| 5208 | pkt->scid.len = scid_len; |
| 5209 | *buf += scid_len; |
| 5210 | |
| 5211 | ret = 1; |
| 5212 | leave: |
| 5213 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 5214 | return ret; |
| 5215 | } |
| 5216 | |
| 5217 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 5218 | static void qc_pkt_insert(struct quic_conn *qc, |
| 5219 | struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 5220 | { |
| 5221 | TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc); |
| 5222 | |
| 5223 | pkt->pn_node.key = pkt->pn; |
| 5224 | quic_rx_packet_refinc(pkt); |
| 5225 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 5226 | |
| 5227 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 5228 | } |
| 5229 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5230 | /* Try to remove the header protection of <pkt> QUIC packet with <beg> the |
| 5231 | * address of the packet first byte, using the keys from encryption level <el>. |
| 5232 | * |
| 5233 | * If header protection has been successfully removed, packet data are copied |
| 5234 | * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also |
| 5235 | * proceeded, and the packet is inserted into <qc> protected packets tree. In |
| 5236 | * both cases, packet can now be considered handled by the <qc> connection. |
| 5237 | * |
| 5238 | * If header protection cannot be removed due to <el> secrets already |
| 5239 | * discarded, no operation is conducted. |
| 5240 | * |
| 5241 | * Returns 1 on success : packet data is now handled by the connection. On |
| 5242 | * error 0 is returned : packet should be dropped by the caller. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5243 | */ |
| 5244 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 5245 | struct quic_rx_packet *pkt, |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5246 | unsigned char *beg, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5247 | struct quic_enc_level **el) |
| 5248 | { |
| 5249 | int ret = 0; |
| 5250 | unsigned char *pn = NULL; /* Packet number field */ |
| 5251 | enum quic_tls_enc_level tel; |
| 5252 | struct quic_enc_level *qel; |
| 5253 | /* Only for traces. */ |
| 5254 | struct quic_rx_packet *qpkt_trace; |
| 5255 | |
| 5256 | qpkt_trace = NULL; |
| 5257 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5258 | BUG_ON(!pkt->pn_offset); |
| 5259 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5260 | /* The packet number is here. This is also the start minus |
| 5261 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 5262 | * protection. |
| 5263 | */ |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5264 | pn = beg + pkt->pn_offset; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5265 | |
| 5266 | tel = quic_packet_type_enc_level(pkt->type); |
| 5267 | qel = &qc->els[tel]; |
| 5268 | |
| 5269 | if (qc_qel_may_rm_hp(qc, qel)) { |
| 5270 | /* Note that the following function enables us to unprotect the packet |
| 5271 | * number and its length subsequently used to decrypt the entire |
| 5272 | * packets. |
| 5273 | */ |
| 5274 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 5275 | qel->pktns->rx.largest_pn, pn, beg)) { |
| 5276 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
| 5277 | goto out; |
| 5278 | } |
| 5279 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 5280 | /* The AAD includes the packet number field. */ |
| 5281 | pkt->aad_len = pkt->pn_offset + pkt->pnl; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5282 | if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) { |
| 5283 | TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc); |
| 5284 | goto out; |
| 5285 | } |
| 5286 | |
| 5287 | qpkt_trace = pkt; |
| 5288 | } |
| 5289 | else { |
| 5290 | if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 5291 | /* If the packet number space has been discarded, this packet |
| 5292 | * will be not parsed. |
| 5293 | */ |
| 5294 | TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt); |
| 5295 | goto out; |
| 5296 | } |
| 5297 | |
| 5298 | TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5299 | LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 5300 | quic_rx_packet_refinc(pkt); |
| 5301 | } |
| 5302 | |
| 5303 | *el = qel; |
| 5304 | /* No reference counter incrementation here!!! */ |
| 5305 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 5306 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 5307 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 5308 | b_add(&qc->rx.buf, pkt->len); |
| 5309 | |
| 5310 | ret = 1; |
| 5311 | out: |
| 5312 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
| 5313 | return ret; |
| 5314 | } |
| 5315 | |
| 5316 | /* Parse the header form from <byte0> first byte of <pkt> packet to set its type. |
| 5317 | * Also set <*long_header> to 1 if this form is long, 0 if not and the version |
| 5318 | * of this packet into <*version>. |
| 5319 | */ |
| 5320 | static inline int qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 5321 | unsigned char **buf, const unsigned char *end, |
| 5322 | int *long_header, uint32_t *version) |
| 5323 | { |
| 5324 | int ret = 0; |
| 5325 | const unsigned char byte0 = **buf; |
| 5326 | |
| 5327 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5328 | |
| 5329 | (*buf)++; |
| 5330 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 5331 | unsigned char type = |
| 5332 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 5333 | |
| 5334 | *long_header = 1; |
| 5335 | /* Version */ |
| 5336 | if (!quic_read_uint32(version, (const unsigned char **)buf, end)) { |
| 5337 | TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT); |
| 5338 | goto out; |
| 5339 | } |
| 5340 | |
| 5341 | if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) { |
| 5342 | pkt->type = type; |
| 5343 | } |
| 5344 | else { |
| 5345 | switch (type) { |
| 5346 | case 0: |
| 5347 | pkt->type = QUIC_PACKET_TYPE_RETRY; |
| 5348 | break; |
| 5349 | case 1: |
| 5350 | pkt->type = QUIC_PACKET_TYPE_INITIAL; |
| 5351 | break; |
| 5352 | case 2: |
| 5353 | pkt->type = QUIC_PACKET_TYPE_0RTT; |
| 5354 | break; |
| 5355 | case 3: |
| 5356 | pkt->type = QUIC_PACKET_TYPE_HANDSHAKE; |
| 5357 | break; |
| 5358 | } |
| 5359 | } |
| 5360 | } |
| 5361 | else { |
| 5362 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 5363 | *long_header = 0; |
| 5364 | } |
| 5365 | |
| 5366 | ret = 1; |
| 5367 | out: |
| 5368 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 5369 | return ret; |
| 5370 | } |
| 5371 | |
| 5372 | /* Return the QUIC version (quic_version struct) with <version> as version number |
| 5373 | * if supported or NULL if not. |
| 5374 | */ |
| 5375 | static inline const struct quic_version *qc_supported_version(uint32_t version) |
| 5376 | { |
| 5377 | int i; |
| 5378 | |
| 5379 | for (i = 0; i < quic_versions_nb; i++) |
| 5380 | if (quic_versions[i].num == version) |
| 5381 | return &quic_versions[i]; |
| 5382 | |
| 5383 | return NULL; |
| 5384 | } |
| 5385 | |
| 5386 | /* |
| 5387 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 5388 | * address <addr>. |
| 5389 | * Implementation of RFC9000 6. Version Negotiation |
| 5390 | * |
| 5391 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 5392 | * |
| 5393 | * Returns 0 on success else non-zero |
| 5394 | */ |
| 5395 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 5396 | struct quic_rx_packet *pkt) |
| 5397 | { |
| 5398 | char buf[256]; |
| 5399 | int ret = 0, i = 0, j; |
| 5400 | uint32_t version; |
| 5401 | const socklen_t addrlen = get_addr_len(addr); |
| 5402 | |
| 5403 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5404 | /* |
| 5405 | * header form |
| 5406 | * long header, fixed bit to 0 for Version Negotiation |
| 5407 | */ |
| 5408 | /* TODO: RAND_bytes() should be replaced? */ |
| 5409 | if (RAND_bytes((unsigned char *)buf, 1) != 1) { |
| 5410 | TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT); |
| 5411 | goto out; |
| 5412 | } |
| 5413 | |
| 5414 | buf[i++] |= '\x80'; |
| 5415 | /* null version for Version Negotiation */ |
| 5416 | buf[i++] = '\x00'; |
| 5417 | buf[i++] = '\x00'; |
| 5418 | buf[i++] = '\x00'; |
| 5419 | buf[i++] = '\x00'; |
| 5420 | |
| 5421 | /* source connection id */ |
| 5422 | buf[i++] = pkt->scid.len; |
| 5423 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5424 | i += pkt->scid.len; |
| 5425 | |
| 5426 | /* destination connection id */ |
| 5427 | buf[i++] = pkt->dcid.len; |
| 5428 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 5429 | i += pkt->dcid.len; |
| 5430 | |
| 5431 | /* supported version */ |
| 5432 | for (j = 0; j < quic_versions_nb; j++) { |
| 5433 | version = htonl(quic_versions[j].num); |
| 5434 | memcpy(&buf[i], &version, sizeof(version)); |
| 5435 | i += sizeof(version); |
| 5436 | } |
| 5437 | |
| 5438 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 5439 | goto out; |
| 5440 | |
| 5441 | ret = 1; |
| 5442 | out: |
| 5443 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5444 | return !ret; |
| 5445 | } |
| 5446 | |
| 5447 | /* Send a stateless reset packet depending on <pkt> RX packet information |
| 5448 | * from <fd> UDP socket to <dst> |
| 5449 | * Return 1 if succeeded, 0 if not. |
| 5450 | */ |
| 5451 | static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr, |
| 5452 | struct quic_rx_packet *rxpkt) |
| 5453 | { |
| 5454 | int ret = 0, pktlen, rndlen; |
| 5455 | unsigned char pkt[64]; |
| 5456 | const socklen_t addrlen = get_addr_len(dstaddr); |
| 5457 | struct proxy *prx; |
| 5458 | struct quic_counters *prx_counters; |
| 5459 | |
| 5460 | TRACE_ENTER(QUIC_EV_STATELESS_RST); |
| 5461 | |
| 5462 | prx = l->bind_conf->frontend; |
| 5463 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 5464 | /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3) |
| 5465 | * The resulting minimum size of 21 bytes does not guarantee that a Stateless |
| 5466 | * Reset is difficult to distinguish from other packets if the recipient requires |
| 5467 | * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure |
| 5468 | * that all packets it sends are at least 22 bytes longer than the minimum |
| 5469 | * connection ID length that it requests the peer to include in its packets, |
| 5470 | * adding PADDING frames as necessary. This ensures that any Stateless Reset |
| 5471 | * sent by the peer is indistinguishable from a valid packet sent to the endpoint. |
| 5472 | * An endpoint that sends a Stateless Reset in response to a packet that is |
| 5473 | * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter |
| 5474 | * than the packet it responds to. |
| 5475 | */ |
| 5476 | |
| 5477 | /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */ |
| 5478 | pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0; |
| 5479 | pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen); |
| 5480 | rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN; |
| 5481 | |
| 5482 | /* Put a header of random bytes */ |
| 5483 | /* TODO: RAND_bytes() should be replaced */ |
| 5484 | if (RAND_bytes(pkt, rndlen) != 1) { |
| 5485 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST); |
| 5486 | goto leave; |
| 5487 | } |
| 5488 | |
| 5489 | /* Clear the most significant bit, and set the second one */ |
| 5490 | *pkt = (*pkt & ~0x80) | 0x40; |
| 5491 | if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN, |
| 5492 | rxpkt->dcid.data, rxpkt->dcid.len)) |
| 5493 | goto leave; |
| 5494 | |
| 5495 | if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0) |
| 5496 | goto leave; |
| 5497 | |
| 5498 | ret = 1; |
| 5499 | HA_ATOMIC_INC(&prx_counters->stateless_reset_sent); |
| 5500 | TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid); |
| 5501 | leave: |
| 5502 | TRACE_LEAVE(QUIC_EV_STATELESS_RST); |
| 5503 | return ret; |
| 5504 | } |
| 5505 | |
| 5506 | /* QUIC server only function. |
| 5507 | * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address. |
| 5508 | * This is the responsibility of the caller to check <aad> size is big enough |
| 5509 | * to contain these data. |
| 5510 | * Return the number of bytes copied to <aad>. |
| 5511 | */ |
| 5512 | static int quic_generate_retry_token_aad(unsigned char *aad, |
| 5513 | uint32_t version, |
| 5514 | const struct quic_cid *cid, |
| 5515 | const struct sockaddr_storage *addr) |
| 5516 | { |
| 5517 | unsigned char *p; |
| 5518 | |
| 5519 | p = aad; |
| 5520 | memcpy(p, &version, sizeof version); |
| 5521 | p += sizeof version; |
| 5522 | p += quic_saddr_cpy(p, addr); |
| 5523 | memcpy(p, cid->data, cid->len); |
| 5524 | p += cid->len; |
| 5525 | |
| 5526 | return p - aad; |
| 5527 | } |
| 5528 | |
| 5529 | /* QUIC server only function. |
| 5530 | * 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] | 5531 | * <buf> with <len> as length. <odcid> is the original destination connection |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5532 | * ID and <dcid> is our side destination connection ID (or client source |
| 5533 | * connection ID). |
| 5534 | * Returns the length of the encoded token or 0 on error. |
| 5535 | */ |
| 5536 | static int quic_generate_retry_token(unsigned char *buf, size_t len, |
| 5537 | const uint32_t version, |
| 5538 | const struct quic_cid *odcid, |
| 5539 | const struct quic_cid *dcid, |
| 5540 | struct sockaddr_storage *addr) |
| 5541 | { |
| 5542 | int ret = 0; |
| 5543 | unsigned char *p; |
| 5544 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
Amaury Denoyelle | 6c94056 | 2022-10-18 11:05:02 +0200 | [diff] [blame] | 5545 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5546 | size_t aadlen; |
| 5547 | unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN]; |
| 5548 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5549 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5550 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5551 | size_t seclen = strlen(global.cluster_secret); |
| 5552 | EVP_CIPHER_CTX *ctx = NULL; |
| 5553 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
| 5554 | uint32_t timestamp = now_ms; |
| 5555 | |
| 5556 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5557 | |
| 5558 | /* We copy the odcid into the token, prefixed by its one byte |
| 5559 | * length, the format token byte. It is followed by an AEAD TAG, and finally |
| 5560 | * the random bytes used to derive the secret to encrypt the token. |
| 5561 | */ |
| 5562 | if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len) |
| 5563 | goto err; |
| 5564 | |
| 5565 | aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr); |
| 5566 | /* TODO: RAND_bytes() should be replaced */ |
| 5567 | if (RAND_bytes(salt, sizeof salt) != 1) { |
| 5568 | TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT); |
| 5569 | goto err; |
| 5570 | } |
| 5571 | |
| 5572 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 5573 | salt, sizeof salt, sec, seclen)) { |
| 5574 | TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT); |
| 5575 | goto err; |
| 5576 | } |
| 5577 | |
| 5578 | if (!quic_tls_tx_ctx_init(&ctx, aead, key)) { |
| 5579 | TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT); |
| 5580 | goto err; |
| 5581 | } |
| 5582 | |
| 5583 | /* Token build */ |
| 5584 | p = buf; |
| 5585 | *p++ = QUIC_TOKEN_FMT_RETRY, |
| 5586 | *p++ = odcid->len; |
| 5587 | memcpy(p, odcid->data, odcid->len); |
| 5588 | p += odcid->len; |
| 5589 | write_u32(p, htonl(timestamp)); |
| 5590 | p += sizeof timestamp; |
| 5591 | |
| 5592 | /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 5593 | if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) { |
| 5594 | TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT); |
| 5595 | goto err; |
| 5596 | } |
| 5597 | |
| 5598 | p += QUIC_TLS_TAG_LEN; |
| 5599 | memcpy(p, salt, sizeof salt); |
| 5600 | p += sizeof salt; |
| 5601 | EVP_CIPHER_CTX_free(ctx); |
| 5602 | |
| 5603 | ret = p - buf; |
| 5604 | leave: |
| 5605 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5606 | return ret; |
| 5607 | |
| 5608 | err: |
| 5609 | if (ctx) |
| 5610 | EVP_CIPHER_CTX_free(ctx); |
| 5611 | goto leave; |
| 5612 | } |
| 5613 | |
| 5614 | /* QUIC server only function. |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5615 | * |
| 5616 | * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is |
| 5617 | * the UDP datagram containing <pkt> and <l> is the listener instance on which |
| 5618 | * it was received. If the token is valid, the ODCID of <qc> QUIC connection |
| 5619 | * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed |
| 5620 | * to validate the token but it can be NULL : in this case the version will be |
| 5621 | * retrieved from the packet. |
| 5622 | * |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5623 | * Return 1 if succeeded, 0 if not. |
| 5624 | */ |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5625 | |
| 5626 | static int quic_retry_token_check(struct quic_rx_packet *pkt, |
| 5627 | struct quic_dgram *dgram, |
| 5628 | struct listener *l, |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5629 | struct quic_conn *qc, |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5630 | struct quic_cid *odcid) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5631 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5632 | struct proxy *prx; |
| 5633 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5634 | int ret = 0; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5635 | unsigned char *token = pkt->token; |
| 5636 | const uint64_t tokenlen = pkt->token_len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5637 | unsigned char buf[128]; |
| 5638 | unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) + |
Amaury Denoyelle | 6c94056 | 2022-10-18 11:05:02 +0200 | [diff] [blame] | 5639 | sizeof(struct in6_addr) + QUIC_CID_MAXLEN]; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5640 | size_t aadlen; |
| 5641 | const unsigned char *salt; |
| 5642 | unsigned char key[QUIC_TLS_KEY_LEN]; |
| 5643 | unsigned char iv[QUIC_TLS_IV_LEN]; |
| 5644 | const unsigned char *sec = (const unsigned char *)global.cluster_secret; |
| 5645 | size_t seclen = strlen(global.cluster_secret); |
| 5646 | EVP_CIPHER_CTX *ctx = NULL; |
| 5647 | const EVP_CIPHER *aead = EVP_aes_128_gcm(); |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5648 | const struct quic_version *qv = qc ? qc->original_version : |
| 5649 | pkt->version; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5650 | |
| 5651 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 5652 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5653 | /* The caller must ensure this. */ |
| 5654 | BUG_ON(!global.cluster_secret || !pkt->token_len); |
| 5655 | |
| 5656 | prx = l->bind_conf->frontend; |
| 5657 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 5658 | |
| 5659 | if (*pkt->token != QUIC_TOKEN_FMT_RETRY) { |
| 5660 | /* TODO: New token check */ |
| 5661 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 5662 | goto leave; |
| 5663 | } |
| 5664 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5665 | if (sizeof buf < tokenlen) { |
| 5666 | TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc); |
| 5667 | goto err; |
| 5668 | } |
| 5669 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5670 | 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] | 5671 | salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN; |
| 5672 | if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv, |
| 5673 | salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) { |
| 5674 | TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc); |
| 5675 | goto err; |
| 5676 | } |
| 5677 | |
| 5678 | if (!quic_tls_rx_ctx_init(&ctx, aead, key)) { |
| 5679 | TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc); |
| 5680 | goto err; |
| 5681 | } |
| 5682 | |
| 5683 | /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */ |
| 5684 | if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen, |
| 5685 | ctx, aead, key, iv)) { |
| 5686 | TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc); |
| 5687 | goto err; |
| 5688 | } |
| 5689 | |
| 5690 | if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) { |
| 5691 | TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 5692 | goto err; |
| 5693 | } |
| 5694 | |
| 5695 | EVP_CIPHER_CTX_free(ctx); |
| 5696 | |
| 5697 | ret = 1; |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5698 | HA_ATOMIC_INC(&prx_counters->retry_validated); |
| 5699 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5700 | leave: |
| 5701 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 5702 | return ret; |
| 5703 | |
| 5704 | err: |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 5705 | HA_ATOMIC_INC(&prx_counters->retry_error); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5706 | if (ctx) |
| 5707 | EVP_CIPHER_CTX_free(ctx); |
| 5708 | goto leave; |
| 5709 | } |
| 5710 | |
| 5711 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 5712 | * the Initial <pkt> packet. |
| 5713 | * |
| 5714 | * Returns 0 on success else non-zero. |
| 5715 | */ |
| 5716 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 5717 | struct quic_rx_packet *pkt, const struct quic_version *qv) |
| 5718 | { |
| 5719 | int ret = 0; |
| 5720 | unsigned char buf[128]; |
| 5721 | int i = 0, token_len; |
| 5722 | const socklen_t addrlen = get_addr_len(addr); |
| 5723 | struct quic_cid scid; |
| 5724 | |
| 5725 | TRACE_ENTER(QUIC_EV_CONN_TXPKT); |
| 5726 | |
| 5727 | /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */ |
| 5728 | buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) | |
| 5729 | (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT); |
| 5730 | /* version */ |
| 5731 | buf[i++] = *((unsigned char *)&qv->num + 3); |
| 5732 | buf[i++] = *((unsigned char *)&qv->num + 2); |
| 5733 | buf[i++] = *((unsigned char *)&qv->num + 1); |
| 5734 | buf[i++] = *(unsigned char *)&qv->num; |
| 5735 | |
| 5736 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 5737 | buf[i++] = pkt->scid.len; |
| 5738 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 5739 | i += pkt->scid.len; |
| 5740 | |
| 5741 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 5742 | scid.len = QUIC_HAP_CID_LEN; |
| 5743 | /* TODO: RAND_bytes() should be replaced */ |
| 5744 | if (RAND_bytes(scid.data, scid.len) != 1) { |
| 5745 | TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT); |
| 5746 | goto out; |
| 5747 | } |
| 5748 | |
| 5749 | buf[i++] = scid.len; |
| 5750 | memcpy(&buf[i], scid.data, scid.len); |
| 5751 | i += scid.len; |
| 5752 | |
| 5753 | /* token */ |
| 5754 | if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num, |
| 5755 | &pkt->dcid, &pkt->scid, addr))) { |
| 5756 | TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT); |
| 5757 | goto out; |
| 5758 | } |
| 5759 | |
| 5760 | i += token_len; |
| 5761 | |
| 5762 | /* token integrity tag */ |
| 5763 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 5764 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 5765 | pkt->dcid.len, buf, i, qv)) { |
| 5766 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 5767 | goto out; |
| 5768 | } |
| 5769 | |
| 5770 | i += QUIC_TLS_TAG_LEN; |
| 5771 | |
| 5772 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) { |
| 5773 | TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT); |
| 5774 | goto out; |
| 5775 | } |
| 5776 | |
| 5777 | ret = 1; |
| 5778 | out: |
| 5779 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT); |
| 5780 | return !ret; |
| 5781 | } |
| 5782 | |
| 5783 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 5784 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 5785 | * concatenated to the <pkt> DCID field. |
| 5786 | * |
| 5787 | * Returns the instance or NULL if not found. |
| 5788 | */ |
| 5789 | static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt, |
| 5790 | struct listener *l, |
| 5791 | struct sockaddr_storage *saddr) |
| 5792 | { |
| 5793 | struct quic_conn *qc = NULL; |
| 5794 | struct ebmb_node *node; |
| 5795 | struct quic_connection_id *id; |
| 5796 | /* set if the quic_conn is found in the second DCID tree */ |
| 5797 | |
| 5798 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 5799 | |
| 5800 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 5801 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 5802 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 5803 | /* DCIDs of first packets coming from multiple clients may have |
| 5804 | * the same values. Let's distinguish them by concatenating the |
| 5805 | * socket addresses. |
| 5806 | */ |
| 5807 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 5808 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
| 5809 | pkt->dcid.len + pkt->dcid.addrlen); |
| 5810 | if (node) { |
| 5811 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 5812 | goto end; |
| 5813 | } |
| 5814 | } |
| 5815 | |
| 5816 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 5817 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 5818 | * used. |
| 5819 | */ |
| 5820 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len); |
| 5821 | if (!node) |
| 5822 | goto end; |
| 5823 | |
| 5824 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 5825 | qc = id->qc; |
| 5826 | |
| 5827 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 5828 | * If already done, this is a noop. |
| 5829 | */ |
| 5830 | if (qc) |
| 5831 | ebmb_delete(&qc->odcid_node); |
| 5832 | |
| 5833 | end: |
| 5834 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc); |
| 5835 | return qc; |
| 5836 | } |
| 5837 | |
| 5838 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 5839 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 5840 | * parameters of this session. |
| 5841 | * This is the responsibility of the caller to check the validity of all the |
| 5842 | * pointers passed as parameter to this function. |
| 5843 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 5844 | * CO_ER_SSL_NO_MEM. |
| 5845 | */ |
| 5846 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 5847 | unsigned char *params, size_t params_len) |
| 5848 | { |
| 5849 | int retry, ret = -1; |
| 5850 | |
| 5851 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5852 | |
| 5853 | retry = 1; |
| 5854 | retry: |
| 5855 | *ssl = SSL_new(ssl_ctx); |
| 5856 | if (!*ssl) { |
| 5857 | if (!retry--) |
| 5858 | goto err; |
| 5859 | |
| 5860 | pool_gc(NULL); |
| 5861 | goto retry; |
| 5862 | } |
| 5863 | |
| 5864 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 5865 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) { |
| 5866 | SSL_free(*ssl); |
| 5867 | *ssl = NULL; |
| 5868 | if (!retry--) |
| 5869 | goto err; |
| 5870 | |
| 5871 | pool_gc(NULL); |
| 5872 | goto retry; |
| 5873 | } |
| 5874 | |
| 5875 | ret = 0; |
| 5876 | leave: |
| 5877 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5878 | return ret; |
| 5879 | |
| 5880 | err: |
| 5881 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 5882 | goto leave; |
| 5883 | } |
| 5884 | |
| 5885 | /* Finalize <qc> QUIC connection: |
| 5886 | * - initialize the Initial QUIC TLS context for negotiated version, |
| 5887 | * - derive the secrets for this context, |
| 5888 | * - encode the transport parameters to be sent, |
| 5889 | * - set them into the TLS stack, |
| 5890 | * - initialize ->max_ack_delay and max_idle_timeout, |
| 5891 | * |
| 5892 | * MUST be called after having received the remote transport parameters. |
| 5893 | * Return 1 if succeeded, 0 if not. |
| 5894 | */ |
| 5895 | int qc_conn_finalize(struct quic_conn *qc, int server) |
| 5896 | { |
| 5897 | int ret = 0; |
| 5898 | struct quic_transport_params *tx_tp = &qc->tx.params; |
| 5899 | struct quic_transport_params *rx_tp = &qc->rx.params; |
| 5900 | const struct quic_version *ver; |
| 5901 | |
| 5902 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5903 | |
| 5904 | if (tx_tp->version_information.negotiated_version && |
| 5905 | tx_tp->version_information.negotiated_version != qc->original_version) { |
| 5906 | qc->negotiated_version = |
| 5907 | qc->tx.params.version_information.negotiated_version; |
| 5908 | if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version, |
| 5909 | qc->odcid.data, qc->odcid.len, !server)) |
| 5910 | goto out; |
| 5911 | |
| 5912 | ver = qc->negotiated_version; |
| 5913 | } |
| 5914 | else { |
| 5915 | ver = qc->original_version; |
| 5916 | } |
| 5917 | |
| 5918 | qc->enc_params_len = |
| 5919 | quic_transport_params_encode(qc->enc_params, |
| 5920 | qc->enc_params + sizeof qc->enc_params, |
| 5921 | &qc->rx.params, ver, 1); |
| 5922 | if (!qc->enc_params_len) { |
| 5923 | TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_TXPKT); |
| 5924 | goto out; |
| 5925 | } |
| 5926 | |
| 5927 | if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) { |
| 5928 | TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_TXPKT); |
| 5929 | goto out; |
| 5930 | } |
| 5931 | |
| 5932 | if (tx_tp->max_ack_delay) |
| 5933 | qc->max_ack_delay = tx_tp->max_ack_delay; |
| 5934 | |
| 5935 | if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout) |
| 5936 | qc->max_idle_timeout = |
| 5937 | QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout); |
| 5938 | else |
| 5939 | qc->max_idle_timeout = |
| 5940 | QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout); |
| 5941 | |
| 5942 | TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp); |
| 5943 | |
| 5944 | ret = 1; |
| 5945 | out: |
| 5946 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5947 | return ret; |
| 5948 | } |
| 5949 | |
| 5950 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 5951 | * used to process <qc> received packets. The allocated context is stored in |
| 5952 | * <qc.xprt_ctx>. |
| 5953 | * |
| 5954 | * Returns 0 on success else non-zero. |
| 5955 | */ |
| 5956 | static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 5957 | { |
| 5958 | int ret = 0; |
| 5959 | struct bind_conf *bc = qc->li->bind_conf; |
| 5960 | struct ssl_sock_ctx *ctx = NULL; |
| 5961 | |
| 5962 | TRACE_ENTER(QUIC_EV_CONN_NEW, qc); |
| 5963 | |
| 5964 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 5965 | if (!ctx) { |
| 5966 | TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT); |
| 5967 | goto err; |
| 5968 | } |
| 5969 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5970 | ctx->subs = NULL; |
| 5971 | ctx->xprt_ctx = NULL; |
| 5972 | ctx->qc = qc; |
| 5973 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5974 | if (qc_is_listener(qc)) { |
| 5975 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 5976 | qc->enc_params, qc->enc_params_len) == -1) { |
| 5977 | goto err; |
| 5978 | } |
| 5979 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5980 | /* Enabling 0-RTT */ |
| 5981 | if (bc->ssl_conf.early_data) |
| 5982 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 5983 | #endif |
| 5984 | |
| 5985 | SSL_set_accept_state(ctx->ssl); |
| 5986 | } |
| 5987 | |
| 5988 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 5989 | |
| 5990 | /* Store the allocated context in <qc>. */ |
| 5991 | qc->xprt_ctx = ctx; |
| 5992 | |
| 5993 | ret = 1; |
| 5994 | leave: |
| 5995 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
| 5996 | return !ret; |
| 5997 | |
| 5998 | err: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 5999 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 6000 | goto leave; |
| 6001 | } |
| 6002 | |
| 6003 | /* Check that all the bytes between <buf> included and <end> address |
| 6004 | * excluded are null. This is the responsibility of the caller to |
| 6005 | * check that there is at least one byte between <buf> end <end>. |
| 6006 | * Return 1 if this all the bytes are null, 0 if not. |
| 6007 | */ |
| 6008 | static inline int quic_padding_check(const unsigned char *buf, |
| 6009 | const unsigned char *end) |
| 6010 | { |
| 6011 | while (buf < end && !*buf) |
| 6012 | buf++; |
| 6013 | |
| 6014 | return buf == end; |
| 6015 | } |
| 6016 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6017 | /* Find the associated connection to the packet <pkt> or create a new one if |
| 6018 | * this is an Initial packet. <dgram> is the datagram containing the packet and |
| 6019 | * <l> is the listener instance on which it was received. |
| 6020 | * |
| 6021 | * Returns the quic-conn instance or NULL. |
| 6022 | */ |
| 6023 | static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt, |
| 6024 | struct quic_dgram *dgram, |
| 6025 | struct listener *l) |
| 6026 | { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6027 | struct quic_cid token_odcid = { .len = 0 }; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6028 | struct quic_conn *qc = NULL; |
| 6029 | struct proxy *prx; |
| 6030 | struct quic_counters *prx_counters; |
| 6031 | |
| 6032 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 6033 | |
| 6034 | prx = l->bind_conf->frontend; |
| 6035 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6036 | |
| 6037 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
| 6038 | |
| 6039 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 6040 | BUG_ON(!pkt->version); /* This must not happen. */ |
| 6041 | |
| 6042 | if (global.cluster_secret && pkt->token_len) { |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6043 | if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid)) |
| 6044 | goto err; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6045 | } |
| 6046 | |
| 6047 | if (!qc) { |
| 6048 | int ipv4; |
| 6049 | |
| 6050 | if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) && |
| 6051 | HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) { |
| 6052 | TRACE_PROTO("Initial without token, sending retry", |
| 6053 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6054 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) { |
| 6055 | TRACE_ERROR("Error during Retry generation", |
| 6056 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6057 | goto out; |
| 6058 | } |
| 6059 | |
| 6060 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
| 6061 | goto out; |
| 6062 | } |
| 6063 | |
| 6064 | /* RFC 9000 7.2. Negotiating Connection IDs: |
| 6065 | * When an Initial packet is sent by a client that has not previously |
| 6066 | * received an Initial or Retry packet from the server, the client |
| 6067 | * populates the Destination Connection ID field with an unpredictable |
| 6068 | * value. This Destination Connection ID MUST be at least 8 bytes in length. |
| 6069 | */ |
| 6070 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 6071 | TRACE_PROTO("dropped packet", |
| 6072 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6073 | goto err; |
| 6074 | } |
| 6075 | |
| 6076 | pkt->saddr = dgram->saddr; |
| 6077 | ipv4 = dgram->saddr.ss_family == AF_INET; |
| 6078 | |
Amaury Denoyelle | 9e3026c | 2022-10-17 11:13:07 +0200 | [diff] [blame] | 6079 | 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] | 6080 | &dgram->daddr, &pkt->saddr, 1, |
| 6081 | !!pkt->token_len, l); |
| 6082 | if (qc == NULL) |
| 6083 | goto err; |
| 6084 | |
| 6085 | HA_ATOMIC_INC(&prx_counters->half_open_conn); |
| 6086 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
| 6087 | ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
| 6088 | qc->odcid.len + qc->odcid.addrlen); |
| 6089 | } |
| 6090 | } |
| 6091 | else if (!qc) { |
| 6092 | TRACE_PROTO("No connection on a non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version); |
| 6093 | if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt)) |
| 6094 | TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc); |
| 6095 | goto err; |
| 6096 | } |
| 6097 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6098 | out: |
| 6099 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6100 | return qc; |
| 6101 | |
| 6102 | err: |
| 6103 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
| 6104 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 6105 | return NULL; |
| 6106 | } |
| 6107 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6108 | /* Parse a QUIC packet starting at <buf>. Data won't be read after <end> even |
| 6109 | * if the packet is incomplete. This function will populate fields of <pkt> |
| 6110 | * instance, most notably its length. <dgram> is the UDP datagram which |
| 6111 | * contains the parsed packet. <l> is the listener instance on which it was |
| 6112 | * received. |
| 6113 | * |
| 6114 | * Returns 0 on success else non-zero. Packet length is guaranteed to be set to |
| 6115 | * the real packet value or to cover all data between <buf> and <end> : this is |
| 6116 | * useful to reject a whole datagram. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6117 | */ |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6118 | static int quic_rx_pkt_parse(struct quic_rx_packet *pkt, |
| 6119 | unsigned char *buf, const unsigned char *end, |
| 6120 | struct quic_dgram *dgram, struct listener *l) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6121 | { |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6122 | const unsigned char *beg = buf; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6123 | struct proxy *prx; |
| 6124 | struct quic_counters *prx_counters; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6125 | int long_header = 0; |
Willy Tarreau | 33a6870 | 2022-11-24 09:16:41 +0100 | [diff] [blame] | 6126 | uint32_t version = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6127 | const struct quic_version *qv = NULL; |
| 6128 | |
| 6129 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 6130 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6131 | prx = l->bind_conf->frontend; |
| 6132 | prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module); |
| 6133 | /* This ist only to please to traces and distinguish the |
| 6134 | * packet with parsed packet number from others. |
| 6135 | */ |
| 6136 | pkt->pn_node.key = (uint64_t)-1; |
| 6137 | if (end <= buf) { |
| 6138 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6139 | goto drop; |
| 6140 | } |
| 6141 | |
| 6142 | /* Fixed bit */ |
| 6143 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6144 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
| 6145 | quic_padding_check(buf, end)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6146 | /* Some browsers may pad the remaining datagram space with null bytes. |
| 6147 | * That is what we called add padding out of QUIC packets. Such |
| 6148 | * datagrams must be considered as valid. But we can only consume |
| 6149 | * the remaining space. |
| 6150 | */ |
| 6151 | pkt->len = end - buf; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6152 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6153 | } |
| 6154 | |
| 6155 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6156 | goto drop; |
| 6157 | } |
| 6158 | |
| 6159 | /* Header form */ |
| 6160 | if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) { |
| 6161 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6162 | goto drop; |
| 6163 | } |
| 6164 | |
| 6165 | if (long_header) { |
| 6166 | uint64_t len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6167 | |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6168 | TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6169 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
| 6170 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6171 | goto drop; |
| 6172 | } |
| 6173 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6174 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 6175 | dgram->len < QUIC_INITIAL_PACKET_MINLEN) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6176 | TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6177 | HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram); |
| 6178 | goto drop; |
| 6179 | } |
| 6180 | |
| 6181 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6182 | * they must have the same DCID. |
| 6183 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6184 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6185 | (pkt->dcid.len != dgram->dcid_len || |
| 6186 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6187 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6188 | goto drop; |
| 6189 | } |
| 6190 | |
| 6191 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 6192 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) { |
| 6193 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6194 | goto drop; |
| 6195 | } |
| 6196 | |
| 6197 | /* RFC9000 6. Version Negotiation */ |
| 6198 | qv = qc_supported_version(version); |
| 6199 | if (!qv) { |
| 6200 | /* unsupported version, send Negotiation packet */ |
| 6201 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
| 6202 | TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6203 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6204 | } |
| 6205 | |
| 6206 | TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6207 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6208 | } |
Amaury Denoyelle | 0eae572 | 2022-10-17 18:05:18 +0200 | [diff] [blame] | 6209 | pkt->version = qv; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6210 | |
| 6211 | /* For Initial packets, and for servers (QUIC clients connections), |
| 6212 | * there is no Initial connection IDs storage. |
| 6213 | */ |
| 6214 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 6215 | uint64_t token_len; |
| 6216 | |
| 6217 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 6218 | end - buf < token_len) { |
| 6219 | TRACE_PROTO("Packet dropped", |
| 6220 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6221 | goto drop; |
| 6222 | } |
| 6223 | |
| 6224 | /* TODO Retry should be automatically activated if |
| 6225 | * suspect network usage is detected. |
| 6226 | */ |
| 6227 | if (global.cluster_secret && !token_len) { |
| 6228 | if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) { |
| 6229 | TRACE_PROTO("Initial without token, sending retry", |
Amaury Denoyelle | 90121b3 | 2022-09-27 10:35:29 +0200 | [diff] [blame] | 6230 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6231 | if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) { |
| 6232 | TRACE_PROTO("Error during Retry generation", |
Amaury Denoyelle | 90121b3 | 2022-09-27 10:35:29 +0200 | [diff] [blame] | 6233 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6234 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6235 | } |
| 6236 | |
| 6237 | HA_ATOMIC_INC(&prx_counters->retry_sent); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6238 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6239 | } |
| 6240 | } |
| 6241 | else if (!global.cluster_secret && token_len) { |
| 6242 | /* Impossible case: a token was received without configured |
| 6243 | * cluster secret. |
| 6244 | */ |
| 6245 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, |
| 6246 | NULL, NULL, NULL, qv); |
| 6247 | goto drop; |
| 6248 | } |
| 6249 | |
| 6250 | pkt->token = buf; |
| 6251 | pkt->token_len = token_len; |
| 6252 | buf += pkt->token_len; |
| 6253 | } |
| 6254 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
| 6255 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
| 6256 | TRACE_PROTO("Packet dropped", |
| 6257 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6258 | goto drop; |
| 6259 | } |
| 6260 | } |
| 6261 | |
| 6262 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 6263 | end - buf < len) { |
| 6264 | TRACE_PROTO("Packet dropped", |
| 6265 | QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv); |
| 6266 | goto drop; |
| 6267 | } |
| 6268 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6269 | /* Packet Number is stored here. Packet Length totalizes the |
| 6270 | * rest of the content. |
| 6271 | */ |
| 6272 | pkt->pn_offset = buf - beg; |
| 6273 | pkt->len = pkt->pn_offset + len; |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6274 | |
| 6275 | /* Interrupt parsing after packet length retrieval : this |
| 6276 | * ensures that only the packet is dropped but not the whole |
| 6277 | * datagram. |
| 6278 | */ |
| 6279 | if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) { |
| 6280 | TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT); |
| 6281 | goto drop; |
| 6282 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6283 | } |
| 6284 | else { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6285 | TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6286 | if (end - buf < QUIC_HAP_CID_LEN) { |
| 6287 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 6288 | goto drop; |
| 6289 | } |
| 6290 | |
| 6291 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
| 6292 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
| 6293 | |
| 6294 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 6295 | * they must have the same DCID. |
| 6296 | */ |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6297 | if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6298 | (pkt->dcid.len != dgram->dcid_len || |
| 6299 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6300 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6301 | goto drop; |
| 6302 | } |
| 6303 | |
| 6304 | buf += QUIC_HAP_CID_LEN; |
| 6305 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6306 | pkt->pn_offset = buf - beg; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6307 | /* A short packet is the last one of a UDP datagram. */ |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6308 | pkt->len = end - beg; |
Amaury Denoyelle | 449b1a8 | 2022-10-19 15:28:44 +0200 | [diff] [blame] | 6309 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6310 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6311 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv); |
| 6312 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6313 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6314 | drop: |
| 6315 | HA_ATOMIC_INC(&prx_counters->dropped_pkt); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6316 | drop_silent: |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6317 | if (!pkt->len) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6318 | pkt->len = end - beg; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6319 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv); |
| 6320 | return -1; |
| 6321 | } |
| 6322 | |
| 6323 | /* Check if received packet <pkt> should be drop due to <qc> already in closing |
| 6324 | * state. This can be true if a CONNECTION_CLOSE has already been emitted for |
| 6325 | * this connection. |
| 6326 | * |
| 6327 | * Returns false if connection is not in closing state else true. The caller |
| 6328 | * should drop the whole datagram in the last case to not mess up <qc> |
| 6329 | * CONNECTION_CLOSE rate limit counter. |
| 6330 | */ |
| 6331 | static int qc_rx_check_closing(struct quic_conn *qc, |
| 6332 | struct quic_rx_packet *pkt) |
| 6333 | { |
| 6334 | if (!(qc->flags & QUIC_FL_CONN_CLOSING)) |
| 6335 | return 0; |
| 6336 | |
| 6337 | TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version); |
| 6338 | |
| 6339 | /* Check if CONNECTION_CLOSE rate reemission is reached. */ |
| 6340 | if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) { |
| 6341 | qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 6342 | qc->nb_pkt_for_cc++; |
| 6343 | qc->nb_pkt_since_cc = 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6344 | } |
| 6345 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6346 | return 1; |
| 6347 | } |
| 6348 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6349 | /* React to a connection migration initiated on <qc> by a client with the new |
| 6350 | * path addresses <peer_addr>/<local_addr>. |
| 6351 | * |
| 6352 | * Returns 0 on success else non-zero. |
| 6353 | */ |
| 6354 | static int qc_handle_conn_migration(struct quic_conn *qc, |
| 6355 | const struct sockaddr_storage *peer_addr, |
| 6356 | const struct sockaddr_storage *local_addr) |
| 6357 | { |
| 6358 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6359 | |
| 6360 | /* RFC 9000 9. Connection Migration |
| 6361 | * |
Amaury Denoyelle | eb6be98 | 2022-11-21 11:14:45 +0100 | [diff] [blame] | 6362 | * The design of QUIC relies on endpoints retaining a stable address for |
| 6363 | * the duration of the handshake. An endpoint MUST NOT initiate |
| 6364 | * connection migration before the handshake is confirmed, as defined in |
| 6365 | * Section 4.1.2 of [QUIC-TLS]. |
| 6366 | */ |
| 6367 | if (qc->state < QUIC_HS_ST_COMPLETE) { |
| 6368 | TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc); |
| 6369 | goto err; |
| 6370 | } |
| 6371 | |
| 6372 | /* RFC 9000 9. Connection Migration |
| 6373 | * |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6374 | * TODO |
| 6375 | * An endpoint MUST |
| 6376 | * perform path validation (Section 8.2) if it detects any change to a |
| 6377 | * peer's address, unless it has previously validated that address. |
| 6378 | */ |
| 6379 | |
Amaury Denoyelle | d3083c9 | 2022-12-01 16:20:06 +0100 | [diff] [blame] | 6380 | /* Update quic-conn owned socket if in used. |
| 6381 | * TODO try to reuse it instead of closing and opening a new one. |
| 6382 | */ |
| 6383 | if (qc_test_fd(qc)) { |
| 6384 | /* TODO try to reuse socket instead of closing it and opening a new one. */ |
| 6385 | TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc); |
| 6386 | qc_release_fd(qc, 1); |
| 6387 | qc_alloc_fd(qc, local_addr, peer_addr); |
| 6388 | } |
| 6389 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 6390 | qc->local_addr = *local_addr; |
| 6391 | qc->peer_addr = *peer_addr; |
| 6392 | HA_ATOMIC_INC(&qc->prx_counters->conn_migration_done); |
| 6393 | |
| 6394 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6395 | return 0; |
| 6396 | |
| 6397 | err: |
| 6398 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6399 | return 1; |
| 6400 | } |
| 6401 | |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6402 | /* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied |
| 6403 | * into <qc> receive buffer after header protection removal procedure. |
| 6404 | * |
| 6405 | * <dgram> must be set to the datagram which contains the QUIC packet. <beg> |
| 6406 | * must point to packet buffer first byte. |
| 6407 | * |
| 6408 | * <tasklist_head> may be non-NULL when the caller treat several datagrams for |
| 6409 | * different quic-conn. In this case, each quic-conn tasklet will be appended |
| 6410 | * to it in order to be woken up after the current task. |
| 6411 | * |
| 6412 | * The caller can safely removed the packet data. If packet refcount was not |
| 6413 | * incremented by this function, it means that the connection did not handled |
| 6414 | * it and it should be freed by the caller. |
| 6415 | */ |
| 6416 | static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt, |
| 6417 | struct quic_dgram *dgram, unsigned char *beg, |
| 6418 | struct list **tasklist_head) |
| 6419 | { |
| 6420 | const struct quic_version *qv = pkt->version; |
| 6421 | struct quic_enc_level *qel = NULL; |
| 6422 | size_t b_cspace; |
| 6423 | int io_cb_wakeup = 1; |
| 6424 | |
Amaury Denoyelle | 3f474e6 | 2022-11-24 17:15:08 +0100 | [diff] [blame] | 6425 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 6426 | |
Amaury Denoyelle | deb7c87 | 2022-10-19 17:14:28 +0200 | [diff] [blame] | 6427 | if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST && |
| 6428 | !quic_peer_validated_addr(qc) && |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6429 | qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
| 6430 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 6431 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6432 | /* Reset the anti-amplification bit. It will be set again |
| 6433 | * when sending the next packet if reached again. |
| 6434 | */ |
| 6435 | qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 6436 | qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP; |
| 6437 | io_cb_wakeup = 1; |
| 6438 | } |
| 6439 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6440 | if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 6441 | TRACE_PROTO("Connection error", |
| 6442 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6443 | goto out; |
| 6444 | } |
| 6445 | |
| 6446 | pkt->raw_len = pkt->len; |
| 6447 | quic_rx_pkts_del(qc); |
| 6448 | b_cspace = b_contig_space(&qc->rx.buf); |
| 6449 | if (b_cspace < pkt->len) { |
| 6450 | /* Do not consume buf if space not at the end. */ |
| 6451 | if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) { |
| 6452 | TRACE_PROTO("Packet dropped", |
| 6453 | QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6454 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6455 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6456 | } |
| 6457 | |
| 6458 | /* Let us consume the remaining contiguous space. */ |
| 6459 | if (b_cspace) { |
| 6460 | b_putchr(&qc->rx.buf, 0x00); |
| 6461 | b_cspace--; |
| 6462 | } |
| 6463 | b_add(&qc->rx.buf, b_cspace); |
| 6464 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 6465 | TRACE_PROTO("Too big packet", |
| 6466 | QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv); |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6467 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun); |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6468 | goto drop_silent; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6469 | } |
| 6470 | } |
| 6471 | |
Amaury Denoyelle | 845169d | 2022-10-17 18:05:26 +0200 | [diff] [blame] | 6472 | if (!qc_try_rm_hp(qc, pkt, beg, &qel)) { |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6473 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv); |
| 6474 | goto drop; |
| 6475 | } |
| 6476 | |
| 6477 | TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv); |
| 6478 | if (pkt->aad_len) |
| 6479 | qc_pkt_insert(qc, pkt, qel); |
| 6480 | out: |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 6481 | *tasklist_head = tasklet_wakeup_after(*tasklist_head, |
| 6482 | qc->wait_event.tasklet); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6483 | |
Amaury Denoyelle | 6e56a9e | 2022-10-17 12:04:49 +0200 | [diff] [blame] | 6484 | drop_silent: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6485 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6486 | return; |
| 6487 | |
| 6488 | drop: |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 6489 | HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6490 | err: |
| 6491 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 6492 | * This cannot be done by this thread. |
| 6493 | */ |
Amaury Denoyelle | 2ed8400 | 2022-09-26 14:53:59 +0200 | [diff] [blame] | 6494 | if (io_cb_wakeup) |
| 6495 | tasklet_wakeup(qc->wait_event.tasklet); |
| 6496 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6497 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv); |
| 6498 | } |
| 6499 | |
| 6500 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 6501 | * Return 1 if enough room to build this header, 0 if not. |
| 6502 | */ |
| 6503 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 6504 | int type, size_t pn_len, |
| 6505 | struct quic_conn *qc, const struct quic_version *ver) |
| 6506 | { |
| 6507 | int ret = 0; |
| 6508 | |
| 6509 | TRACE_ENTER(QUIC_EV_CONN_LPKT, qc); |
| 6510 | |
| 6511 | if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) { |
| 6512 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6513 | goto leave; |
| 6514 | } |
| 6515 | |
| 6516 | type = quic_pkt_type(type, ver->num); |
| 6517 | /* #0 byte flags */ |
| 6518 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 6519 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 6520 | /* Version */ |
| 6521 | quic_write_uint32(buf, end, ver->num); |
| 6522 | *(*buf)++ = qc->dcid.len; |
| 6523 | /* Destination connection ID */ |
| 6524 | if (qc->dcid.len) { |
| 6525 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6526 | *buf += qc->dcid.len; |
| 6527 | } |
| 6528 | /* Source connection ID */ |
| 6529 | *(*buf)++ = qc->scid.len; |
| 6530 | if (qc->scid.len) { |
| 6531 | memcpy(*buf, qc->scid.data, qc->scid.len); |
| 6532 | *buf += qc->scid.len; |
| 6533 | } |
| 6534 | |
| 6535 | ret = 1; |
| 6536 | leave: |
| 6537 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc); |
| 6538 | return ret; |
| 6539 | } |
| 6540 | |
| 6541 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 6542 | * Return 1 if enough room to build this header, 0 if not. |
| 6543 | */ |
| 6544 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 6545 | size_t pn_len, struct quic_conn *qc, |
| 6546 | unsigned char tls_flags) |
| 6547 | { |
| 6548 | int ret = 0; |
| 6549 | |
| 6550 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6551 | |
| 6552 | if (end - *buf < 1 + qc->dcid.len) { |
| 6553 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc); |
| 6554 | goto leave; |
| 6555 | } |
| 6556 | |
| 6557 | /* #0 byte flags */ |
| 6558 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 6559 | ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1); |
| 6560 | /* Destination connection ID */ |
| 6561 | if (qc->dcid.len) { |
| 6562 | memcpy(*buf, qc->dcid.data, qc->dcid.len); |
| 6563 | *buf += qc->dcid.len; |
| 6564 | } |
| 6565 | |
| 6566 | ret = 1; |
| 6567 | leave: |
| 6568 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6569 | return ret; |
| 6570 | } |
| 6571 | |
| 6572 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 6573 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 6574 | * with <aead> as AEAD cipher and <key> as secret key. |
| 6575 | * Returns 1 if succeeded or 0 if failed. |
| 6576 | */ |
| 6577 | static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf, |
| 6578 | unsigned char *pn, size_t pnlen, |
| 6579 | struct quic_tls_ctx *tls_ctx) |
| 6580 | |
| 6581 | { |
| 6582 | int i, ret = 0; |
| 6583 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 6584 | * and at most 4 bytes for the packet number |
| 6585 | */ |
| 6586 | unsigned char mask[5] = {0}; |
| 6587 | EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx; |
| 6588 | |
| 6589 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6590 | |
| 6591 | if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) { |
| 6592 | TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc); |
| 6593 | goto out; |
| 6594 | } |
| 6595 | |
| 6596 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 6597 | for (i = 0; i < pnlen; i++) |
| 6598 | pn[i] ^= mask[i + 1]; |
| 6599 | |
| 6600 | ret = 1; |
| 6601 | out: |
| 6602 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6603 | return ret; |
| 6604 | } |
| 6605 | |
| 6606 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 6607 | * ACK ranges if needed to a value below <limit> in bytes. |
| 6608 | * Return 1 if succeeded, 0 if not. |
| 6609 | */ |
| 6610 | static int quic_ack_frm_reduce_sz(struct quic_conn *qc, |
| 6611 | struct quic_frame *ack_frm, size_t limit) |
| 6612 | { |
| 6613 | size_t room, ack_delay_sz; |
| 6614 | int ret = 0; |
| 6615 | |
| 6616 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6617 | |
| 6618 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 6619 | /* A frame is made of 1 byte for the frame type. */ |
| 6620 | room = limit - ack_delay_sz - 1; |
| 6621 | if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room)) |
| 6622 | goto leave; |
| 6623 | |
| 6624 | ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz; |
| 6625 | leave: |
| 6626 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 6627 | return ret; |
| 6628 | } |
| 6629 | |
| 6630 | /* Prepare into <outlist> as most as possible ack-eliciting frame from their |
| 6631 | * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer |
| 6632 | * with <room> as available room, and <*len> the packet Length field initialized |
| 6633 | * with the number of bytes already present in this buffer which must be taken |
| 6634 | * into an account for the Length packet field value. <headlen> is the number of |
| 6635 | * bytes already present in this packet before building frames. |
| 6636 | * |
| 6637 | * Update consequently <*len> to reflect the size of these frames built |
| 6638 | * by this function. Also attach these frames to <l> frame list. |
| 6639 | * Return 1 if at least one ack-eleciting frame could be built, 0 if not. |
| 6640 | */ |
| 6641 | static inline int qc_build_frms(struct list *outlist, struct list *inlist, |
| 6642 | size_t room, size_t *len, size_t headlen, |
| 6643 | struct quic_enc_level *qel, |
| 6644 | struct quic_conn *qc) |
| 6645 | { |
| 6646 | int ret; |
| 6647 | struct quic_frame *cf, *cfbak; |
| 6648 | |
| 6649 | TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc); |
| 6650 | |
| 6651 | ret = 0; |
| 6652 | if (*len > room) |
| 6653 | goto leave; |
| 6654 | |
| 6655 | /* If we are not probing we must take into an account the congestion |
| 6656 | * control window. |
| 6657 | */ |
| 6658 | if (!qel->pktns->tx.pto_probe) { |
| 6659 | size_t remain = quic_path_prep_data(qc->path); |
| 6660 | |
| 6661 | if (headlen > remain) |
| 6662 | goto leave; |
| 6663 | |
| 6664 | room = QUIC_MIN(room, remain - headlen); |
| 6665 | } |
| 6666 | |
| 6667 | TRACE_PROTO("************** frames build (headlen)", |
| 6668 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
| 6669 | |
| 6670 | /* NOTE: switch/case block inside a loop, a successful status must be |
| 6671 | * returned by this function only if at least one frame could be built |
| 6672 | * in the switch/case block. |
| 6673 | */ |
| 6674 | list_for_each_entry_safe(cf, cfbak, inlist, list) { |
| 6675 | /* header length, data length, frame length. */ |
| 6676 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
| 6677 | |
| 6678 | if (!room) |
| 6679 | break; |
| 6680 | |
| 6681 | switch (cf->type) { |
| 6682 | case QUIC_FT_CRYPTO: |
| 6683 | TRACE_DEVEL(" New CRYPTO frame build (room, len)", |
| 6684 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
| 6685 | /* Compute the length of this CRYPTO frame header */ |
| 6686 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 6687 | /* Compute the data length of this CRyPTO frame. */ |
| 6688 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 6689 | TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)", |
| 6690 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen); |
| 6691 | if (!dlen) |
| 6692 | continue; |
| 6693 | |
| 6694 | /* CRYPTO frame length. */ |
| 6695 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 6696 | TRACE_DEVEL(" CRYPTO frame length (flen)", |
| 6697 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 6698 | /* Add the CRYPTO data length and its encoded length to the packet |
| 6699 | * length and the length of this length. |
| 6700 | */ |
| 6701 | *len += flen; |
| 6702 | room -= flen; |
| 6703 | if (dlen == cf->crypto.len) { |
| 6704 | /* <cf> CRYPTO data have been consumed. */ |
| 6705 | LIST_DELETE(&cf->list); |
| 6706 | LIST_APPEND(outlist, &cf->list); |
| 6707 | } |
| 6708 | else { |
| 6709 | struct quic_frame *new_cf; |
| 6710 | |
| 6711 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 6712 | if (!new_cf) { |
| 6713 | TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
| 6714 | continue; |
| 6715 | } |
| 6716 | |
| 6717 | LIST_INIT(&new_cf->reflist); |
| 6718 | new_cf->type = QUIC_FT_CRYPTO; |
| 6719 | new_cf->crypto.len = dlen; |
| 6720 | new_cf->crypto.offset = cf->crypto.offset; |
| 6721 | new_cf->crypto.qel = qel; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 6722 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6723 | if (cf->origin) { |
| 6724 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 6725 | /* This <cf> frame was duplicated */ |
| 6726 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 6727 | new_cf->origin = cf->origin; |
| 6728 | } |
| 6729 | LIST_APPEND(outlist, &new_cf->list); |
| 6730 | /* Consume <dlen> bytes of the current frame. */ |
| 6731 | cf->crypto.len -= dlen; |
| 6732 | cf->crypto.offset += dlen; |
| 6733 | } |
| 6734 | break; |
| 6735 | |
| 6736 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 6737 | if (cf->flags & QUIC_FL_TX_FRAME_LOST) { |
| 6738 | struct eb64_node *node = NULL; |
| 6739 | struct qc_stream_desc *stream_desc = NULL; |
| 6740 | struct quic_stream *strm = &cf->stream; |
| 6741 | |
| 6742 | /* As this frame has been already lost, ensure the stream is always |
| 6743 | * available or the range of this frame is not consumed before |
| 6744 | * resending it. |
| 6745 | */ |
| 6746 | node = eb64_lookup(&qc->streams_by_id, strm->id); |
| 6747 | if (!node) { |
| 6748 | TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf); |
| 6749 | LIST_DELETE(&cf->list); |
| 6750 | pool_free(pool_head_quic_frame, cf); |
| 6751 | continue; |
| 6752 | } |
| 6753 | |
| 6754 | stream_desc = eb64_entry(node, struct qc_stream_desc, by_id); |
| 6755 | if (strm->offset.key + strm->len <= stream_desc->ack_offset) { |
| 6756 | TRACE_DEVEL("ignored frame frame in already acked range", |
| 6757 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
| 6758 | LIST_DELETE(&cf->list); |
| 6759 | pool_free(pool_head_quic_frame, cf); |
| 6760 | continue; |
| 6761 | } |
| 6762 | else if (strm->offset.key < stream_desc->ack_offset) { |
| 6763 | strm->offset.key = stream_desc->ack_offset; |
| 6764 | TRACE_DEVEL("updated partially acked frame", |
| 6765 | QUIC_EV_CONN_PRSAFRM, qc, cf); |
| 6766 | } |
| 6767 | } |
| 6768 | /* Note that these frames are accepted in short packets only without |
| 6769 | * "Length" packet field. Here, <*len> is used only to compute the |
| 6770 | * sum of the lengths of the already built frames for this packet. |
| 6771 | * |
| 6772 | * Compute the length of this STREAM frame "header" made a all the field |
| 6773 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 6774 | */ |
| 6775 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
| 6776 | ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0); |
| 6777 | /* Compute the data length of this STREAM frame. */ |
| 6778 | avail_room = room - hlen - *len; |
| 6779 | if ((ssize_t)avail_room <= 0) |
| 6780 | continue; |
| 6781 | |
| 6782 | TRACE_DEVEL(" New STREAM frame build (room, len)", |
| 6783 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
| 6784 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 6785 | dlen = max_available_room(avail_room, &dlen_sz); |
| 6786 | if (dlen > cf->stream.len) { |
| 6787 | dlen = cf->stream.len; |
| 6788 | } |
| 6789 | dlen_sz = quic_int_getsize(dlen); |
| 6790 | flen = hlen + dlen_sz + dlen; |
| 6791 | } |
| 6792 | else { |
| 6793 | dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len); |
| 6794 | flen = hlen + dlen; |
| 6795 | } |
| 6796 | TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)", |
| 6797 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen); |
| 6798 | TRACE_DEVEL(" STREAM frame length (flen)", |
| 6799 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
| 6800 | /* Add the STREAM data length and its encoded length to the packet |
| 6801 | * length and the length of this length. |
| 6802 | */ |
| 6803 | *len += flen; |
| 6804 | room -= flen; |
| 6805 | if (dlen == cf->stream.len) { |
| 6806 | /* <cf> STREAM data have been consumed. */ |
| 6807 | LIST_DELETE(&cf->list); |
| 6808 | LIST_APPEND(outlist, &cf->list); |
| 6809 | |
| 6810 | /* Do not notify MUX on retransmission. */ |
| 6811 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 6812 | qcc_streams_sent_done(cf->stream.stream->ctx, |
| 6813 | cf->stream.len, |
| 6814 | cf->stream.offset.key); |
| 6815 | } |
| 6816 | } |
| 6817 | else { |
| 6818 | struct quic_frame *new_cf; |
| 6819 | struct buffer cf_buf; |
| 6820 | |
| 6821 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 6822 | if (!new_cf) { |
| 6823 | TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
| 6824 | continue; |
| 6825 | } |
| 6826 | |
| 6827 | LIST_INIT(&new_cf->reflist); |
| 6828 | new_cf->type = cf->type; |
| 6829 | new_cf->stream.stream = cf->stream.stream; |
| 6830 | new_cf->stream.buf = cf->stream.buf; |
| 6831 | new_cf->stream.id = cf->stream.id; |
| 6832 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 6833 | new_cf->stream.offset = cf->stream.offset; |
| 6834 | new_cf->stream.len = dlen; |
| 6835 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 6836 | /* FIN bit reset */ |
| 6837 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 6838 | new_cf->stream.data = cf->stream.data; |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 6839 | TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 6840 | if (cf->origin) { |
| 6841 | TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc); |
| 6842 | /* This <cf> frame was duplicated */ |
| 6843 | LIST_APPEND(&cf->origin->reflist, &new_cf->ref); |
| 6844 | new_cf->origin = cf->origin; |
| 6845 | } |
| 6846 | LIST_APPEND(outlist, &new_cf->list); |
| 6847 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 6848 | /* Consume <dlen> bytes of the current frame. */ |
| 6849 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 6850 | b_size(cf->stream.buf), |
| 6851 | (char *)cf->stream.data - b_orig(cf->stream.buf), 0); |
| 6852 | cf->stream.len -= dlen; |
| 6853 | cf->stream.offset.key += dlen; |
| 6854 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
| 6855 | |
| 6856 | /* Do not notify MUX on retransmission. */ |
| 6857 | if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) { |
| 6858 | qcc_streams_sent_done(new_cf->stream.stream->ctx, |
| 6859 | new_cf->stream.len, |
| 6860 | new_cf->stream.offset.key); |
| 6861 | } |
| 6862 | } |
| 6863 | |
| 6864 | /* TODO the MUX is notified about the frame sending via |
| 6865 | * previous qcc_streams_sent_done call. However, the |
| 6866 | * sending can fail later, for example if the sendto |
| 6867 | * system call returns an error. As the MUX has been |
| 6868 | * notified, the transport layer is responsible to |
| 6869 | * bufferize and resent the announced data later. |
| 6870 | */ |
| 6871 | |
| 6872 | break; |
| 6873 | |
| 6874 | default: |
| 6875 | flen = qc_frm_len(cf); |
| 6876 | BUG_ON(!flen); |
| 6877 | if (flen > room) |
| 6878 | continue; |
| 6879 | |
| 6880 | *len += flen; |
| 6881 | room -= flen; |
| 6882 | LIST_DELETE(&cf->list); |
| 6883 | LIST_APPEND(outlist, &cf->list); |
| 6884 | break; |
| 6885 | } |
| 6886 | |
| 6887 | /* Successful status as soon as a frame could be built */ |
| 6888 | ret = 1; |
| 6889 | } |
| 6890 | |
| 6891 | leave: |
| 6892 | TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc); |
| 6893 | return ret; |
| 6894 | } |
| 6895 | |
| 6896 | /* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out> |
| 6897 | * is used as return parameter and should be zero'ed by the caller. |
| 6898 | */ |
| 6899 | static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel, |
| 6900 | struct quic_frame *out) |
| 6901 | { |
| 6902 | /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels |
| 6903 | * |
| 6904 | * A CONNECTION_CLOSE frame should be sent in several packets with |
| 6905 | * different encryption levels depending on the client context. This is |
| 6906 | * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for |
| 6907 | * more details on how to implement it. |
| 6908 | */ |
| 6909 | TRACE_ENTER(QUIC_EV_CONN_BFRM, qc); |
| 6910 | |
| 6911 | |
| 6912 | if (qc->err.app) { |
| 6913 | if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 6914 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 6915 | /* RFC 9000 10.2.3. Immediate Close during the Handshake |
| 6916 | * |
| 6917 | * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake |
| 6918 | * packet could expose application state or be used to alter application |
| 6919 | * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a |
| 6920 | * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or |
| 6921 | * Handshake packets. Otherwise, information about the application |
| 6922 | * state might be revealed. Endpoints MUST clear the value of the |
| 6923 | * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when |
| 6924 | * converting to a CONNECTION_CLOSE of type 0x1c. |
| 6925 | */ |
| 6926 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 6927 | out->connection_close.error_code = QC_ERR_APPLICATION_ERROR; |
| 6928 | out->connection_close.reason_phrase_len = 0; |
| 6929 | } |
| 6930 | else { |
| 6931 | out->type = QUIC_FT_CONNECTION_CLOSE_APP; |
| 6932 | out->connection_close.error_code = qc->err.code; |
| 6933 | } |
| 6934 | } |
| 6935 | else { |
| 6936 | out->type = QUIC_FT_CONNECTION_CLOSE; |
| 6937 | out->connection_close.error_code = qc->err.code; |
| 6938 | } |
| 6939 | TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc); |
| 6940 | |
| 6941 | } |
| 6942 | |
| 6943 | /* This function builds a clear packet from <pkt> information (its type) |
| 6944 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 6945 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 6946 | * filling the buffer with as much frames as possible from <frms> list of |
| 6947 | * prebuilt frames. |
| 6948 | * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are |
| 6949 | * reserved so that to ensure there is enough room to build this AEAD TAG after |
| 6950 | * having returned from this function. |
| 6951 | * This function also updates the value of <buf_pn> pointer to point to the packet |
| 6952 | * number field in this packet. <pn_len> will also have the packet number |
| 6953 | * length as value. |
| 6954 | * |
| 6955 | * Return 1 if succeeded (enough room to buile this packet), O if not. |
| 6956 | */ |
| 6957 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 6958 | size_t dglen, struct quic_tx_packet *pkt, |
| 6959 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
| 6960 | int force_ack, int padding, int cc, int probe, |
| 6961 | struct quic_enc_level *qel, struct quic_conn *qc, |
| 6962 | const struct quic_version *ver, struct list *frms) |
| 6963 | { |
| 6964 | unsigned char *beg, *payload; |
| 6965 | size_t len, len_sz, len_frms, padding_len; |
| 6966 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 6967 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
| 6968 | struct quic_frame cc_frm = { }; |
| 6969 | size_t ack_frm_len, head_len; |
| 6970 | int64_t rx_largest_acked_pn; |
| 6971 | int add_ping_frm; |
| 6972 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
| 6973 | struct quic_frame *cf; |
| 6974 | int must_ack, ret = 0; |
| 6975 | int nb_aepkts_since_last_ack; |
| 6976 | |
| 6977 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc); |
| 6978 | |
| 6979 | /* Length field value with CRYPTO frames if present. */ |
| 6980 | len_frms = 0; |
| 6981 | beg = pos; |
| 6982 | /* When not probing, and no immediate close is required, reduce the size of this |
| 6983 | * buffer to respect the congestion controller window. |
| 6984 | * This size will be limited if we have ack-eliciting frames to send from <frms>. |
| 6985 | */ |
| 6986 | if (!probe && !LIST_ISEMPTY(frms) && !cc) { |
| 6987 | size_t path_room; |
| 6988 | |
| 6989 | path_room = quic_path_prep_data(qc->path); |
| 6990 | if (end - beg > path_room) |
| 6991 | end = beg + path_room; |
| 6992 | } |
| 6993 | |
| 6994 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 6995 | * length field if any. |
| 6996 | */ |
| 6997 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 6998 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 6999 | goto no_room; |
| 7000 | |
| 7001 | end -= QUIC_TLS_TAG_LEN; |
| 7002 | rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn; |
| 7003 | /* packet number length */ |
| 7004 | *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn); |
| 7005 | /* Build the header */ |
| 7006 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
| 7007 | !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) || |
| 7008 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
| 7009 | !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver))) |
| 7010 | goto no_room; |
| 7011 | |
| 7012 | /* Encode the token length (0) for an Initial packet. */ |
| 7013 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
| 7014 | *pos++ = 0; |
| 7015 | head_len = pos - beg; |
| 7016 | /* Build an ACK frame if required. */ |
| 7017 | ack_frm_len = 0; |
| 7018 | nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack; |
| 7019 | must_ack = !qel->pktns->tx.pto_probe && |
| 7020 | (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && |
| 7021 | (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK))); |
| 7022 | if (must_ack) { |
| 7023 | struct quic_arngs *arngs = &qel->pktns->rx.arngs; |
| 7024 | BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root)); |
| 7025 | ack_frm.tx_ack.arngs = arngs; |
| 7026 | if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) { |
| 7027 | qel->pktns->tx.ack_delay = |
| 7028 | quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc); |
| 7029 | qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN; |
| 7030 | } |
| 7031 | ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay; |
| 7032 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 7033 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 7034 | * that from here, we do not know if we will have to send a PING frame. |
| 7035 | * This will be decided after having computed the ack-eliciting frames |
| 7036 | * to be added to this packet. |
| 7037 | */ |
| 7038 | ack_frm_len = quic_ack_frm_reduce_sz(qc, &ack_frm, end - 1 - *pn_len - pos); |
| 7039 | if (!ack_frm_len) |
| 7040 | goto no_room; |
| 7041 | } |
| 7042 | |
| 7043 | /* Length field value without the ack-eliciting frames. */ |
| 7044 | len = ack_frm_len + *pn_len; |
| 7045 | len_frms = 0; |
| 7046 | if (!cc && !LIST_ISEMPTY(frms)) { |
| 7047 | ssize_t room = end - pos; |
| 7048 | |
| 7049 | TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 7050 | /* Initialize the length of the frames built below to <len>. |
| 7051 | * If any frame could be successfully built by qc_build_frms(), |
| 7052 | * we will have len_frms > len. |
| 7053 | */ |
| 7054 | len_frms = len; |
| 7055 | if (!qc_build_frms(&frm_list, frms, |
| 7056 | end - pos, &len_frms, pos - beg, qel, qc)) { |
| 7057 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
| 7058 | qc, NULL, NULL, &room); |
| 7059 | if (!ack_frm_len && !qel->pktns->tx.pto_probe) |
| 7060 | goto no_room; |
| 7061 | } |
| 7062 | } |
| 7063 | |
| 7064 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 7065 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 7066 | * for the encryption tag. It must be taken into an account for the length |
| 7067 | * of this packet. |
| 7068 | */ |
| 7069 | if (len_frms) |
| 7070 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 7071 | else |
| 7072 | len += QUIC_TLS_TAG_LEN; |
| 7073 | /* CONNECTION_CLOSE frame */ |
| 7074 | if (cc) { |
| 7075 | qc_build_cc_frm(qc, qel, &cc_frm); |
| 7076 | len += qc_frm_len(&cc_frm); |
| 7077 | } |
| 7078 | add_ping_frm = 0; |
| 7079 | padding_len = 0; |
| 7080 | len_sz = quic_int_getsize(len); |
| 7081 | /* Add this packet size to <dglen> */ |
| 7082 | dglen += head_len + len_sz + len; |
| 7083 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 7084 | /* This is a maximum padding size */ |
| 7085 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 7086 | /* The length field value is of this packet is <len> + <padding_len> |
| 7087 | * the size of which may be greater than the initial computed size |
| 7088 | * <len_sz>. So, let's deduce the difference between these to packet |
| 7089 | * sizes from <padding_len>. |
| 7090 | */ |
| 7091 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 7092 | len += padding_len; |
| 7093 | } |
| 7094 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
| 7095 | if (qel->pktns->tx.pto_probe) { |
| 7096 | /* If we cannot send a frame, we send a PING frame. */ |
| 7097 | add_ping_frm = 1; |
| 7098 | len += 1; |
| 7099 | } |
| 7100 | /* If there is no frame at all to follow, add at least a PADDING frame. */ |
| 7101 | if (!ack_frm_len && !cc) |
| 7102 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 7103 | } |
| 7104 | |
| 7105 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 7106 | goto no_room; |
| 7107 | |
| 7108 | /* Packet number field address. */ |
| 7109 | *buf_pn = pos; |
| 7110 | |
| 7111 | /* Packet number encoding. */ |
| 7112 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 7113 | goto no_room; |
| 7114 | |
| 7115 | /* payload building (ack-eliciting or not frames) */ |
| 7116 | payload = pos; |
| 7117 | if (ack_frm_len) { |
| 7118 | if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
| 7119 | goto no_room; |
| 7120 | |
| 7121 | pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns); |
| 7122 | pkt->flags |= QUIC_FL_TX_PACKET_ACK; |
| 7123 | } |
| 7124 | |
| 7125 | /* Ack-eliciting frames */ |
| 7126 | if (!LIST_ISEMPTY(&frm_list)) { |
| 7127 | struct quic_frame *tmp_cf; |
| 7128 | list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) { |
| 7129 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
| 7130 | ssize_t room = end - pos; |
| 7131 | TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT, |
| 7132 | qc, NULL, NULL, &room); |
| 7133 | /* Note that <cf> was added from <frms> to <frm_list> list by |
| 7134 | * qc_build_frms(). |
| 7135 | */ |
| 7136 | LIST_DELETE(&cf->list); |
| 7137 | LIST_INSERT(frms, &cf->list); |
| 7138 | continue; |
| 7139 | } |
| 7140 | |
| 7141 | quic_tx_packet_refinc(pkt); |
| 7142 | cf->pkt = pkt; |
| 7143 | } |
| 7144 | } |
| 7145 | |
| 7146 | /* Build a PING frame if needed. */ |
| 7147 | if (add_ping_frm) { |
| 7148 | frm.type = QUIC_FT_PING; |
| 7149 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 7150 | goto no_room; |
| 7151 | } |
| 7152 | |
| 7153 | /* Build a CONNECTION_CLOSE frame if needed. */ |
| 7154 | if (cc) { |
| 7155 | if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
| 7156 | goto no_room; |
| 7157 | |
| 7158 | pkt->flags |= QUIC_FL_TX_PACKET_CC; |
| 7159 | } |
| 7160 | |
| 7161 | /* Build a PADDING frame if needed. */ |
| 7162 | if (padding_len) { |
| 7163 | frm.type = QUIC_FT_PADDING; |
| 7164 | frm.padding.len = padding_len; |
| 7165 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
| 7166 | goto no_room; |
| 7167 | } |
| 7168 | |
| 7169 | if (pos == payload) { |
| 7170 | /* No payload was built because of congestion control */ |
| 7171 | TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc); |
| 7172 | goto no_room; |
| 7173 | } |
| 7174 | |
| 7175 | /* If this packet is ack-eliciting and we are probing let's |
| 7176 | * decrement the PTO probe counter. |
| 7177 | */ |
| 7178 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 7179 | qel->pktns->tx.pto_probe) |
| 7180 | qel->pktns->tx.pto_probe--; |
| 7181 | |
| 7182 | pkt->len = pos - beg; |
| 7183 | LIST_SPLICE(&pkt->frms, &frm_list); |
| 7184 | |
| 7185 | ret = 1; |
| 7186 | TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt); |
| 7187 | leave: |
| 7188 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc); |
| 7189 | return ret; |
| 7190 | |
| 7191 | no_room: |
| 7192 | /* Replace the pre-built frames which could not be add to this packet */ |
| 7193 | LIST_SPLICE(frms, &frm_list); |
| 7194 | TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms); |
| 7195 | goto leave; |
| 7196 | } |
| 7197 | |
| 7198 | static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type) |
| 7199 | { |
| 7200 | pkt->type = type; |
| 7201 | pkt->len = 0; |
| 7202 | pkt->in_flight_len = 0; |
| 7203 | pkt->pn_node.key = (uint64_t)-1; |
| 7204 | LIST_INIT(&pkt->frms); |
| 7205 | pkt->time_sent = TICK_ETERNITY; |
| 7206 | pkt->next = NULL; |
Frédéric Lécaille | 814645f | 2022-11-18 18:15:28 +0100 | [diff] [blame] | 7207 | pkt->prev = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7208 | pkt->largest_acked_pn = -1; |
| 7209 | pkt->flags = 0; |
| 7210 | pkt->refcnt = 0; |
| 7211 | } |
| 7212 | |
| 7213 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 7214 | * type for <qc> QUIC connection from <qel> encryption level from <frms> list |
| 7215 | * of prebuilt frames. |
| 7216 | * |
| 7217 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 7218 | * -1 if there was not enough room to build a packet. |
| 7219 | * XXX NOTE XXX |
| 7220 | * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as |
| 7221 | * possible (to fill an MTU), the unique reason why this function may fail is the congestion |
| 7222 | * control window limitation. |
| 7223 | */ |
| 7224 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 7225 | const unsigned char *buf_end, |
| 7226 | struct quic_enc_level *qel, |
| 7227 | struct quic_tls_ctx *tls_ctx, struct list *frms, |
| 7228 | struct quic_conn *qc, const struct quic_version *ver, |
| 7229 | size_t dglen, int pkt_type, int force_ack, |
| 7230 | int padding, int probe, int cc, int *err) |
| 7231 | { |
| 7232 | struct quic_tx_packet *ret_pkt = NULL; |
| 7233 | /* The pointer to the packet number field. */ |
| 7234 | unsigned char *buf_pn; |
| 7235 | unsigned char *beg, *end, *payload; |
| 7236 | int64_t pn; |
| 7237 | size_t pn_len, payload_len, aad_len; |
| 7238 | struct quic_tx_packet *pkt; |
| 7239 | |
| 7240 | TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel); |
| 7241 | *err = 0; |
| 7242 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 7243 | if (!pkt) { |
| 7244 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc); |
| 7245 | *err = -2; |
| 7246 | goto err; |
| 7247 | } |
| 7248 | |
| 7249 | quic_tx_packet_init(pkt, pkt_type); |
| 7250 | beg = *pos; |
| 7251 | pn_len = 0; |
| 7252 | buf_pn = NULL; |
| 7253 | |
| 7254 | pn = qel->pktns->tx.next_pn + 1; |
| 7255 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
| 7256 | force_ack, padding, cc, probe, qel, qc, ver, frms)) { |
| 7257 | // trace already emitted by function above |
| 7258 | *err = -1; |
| 7259 | goto err; |
| 7260 | } |
| 7261 | |
| 7262 | end = beg + pkt->len; |
| 7263 | payload = buf_pn + pn_len; |
| 7264 | payload_len = end - payload; |
| 7265 | aad_len = payload - beg; |
| 7266 | |
| 7267 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) { |
| 7268 | // trace already emitted by function above |
| 7269 | *err = -2; |
| 7270 | goto err; |
| 7271 | } |
| 7272 | |
| 7273 | end += QUIC_TLS_TAG_LEN; |
| 7274 | pkt->len += QUIC_TLS_TAG_LEN; |
| 7275 | if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) { |
| 7276 | // trace already emitted by function above |
| 7277 | *err = -2; |
| 7278 | goto err; |
| 7279 | } |
| 7280 | |
| 7281 | /* Consume a packet number */ |
| 7282 | qel->pktns->tx.next_pn++; |
| 7283 | qc->tx.prep_bytes += pkt->len; |
| 7284 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) { |
| 7285 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 7286 | TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc); |
| 7287 | } |
| 7288 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 7289 | *pos = end; |
| 7290 | /* Attach the built packet to its tree. */ |
| 7291 | pkt->pn_node.key = pn; |
| 7292 | /* Set the packet in fligth length for in flight packet only. */ |
| 7293 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
| 7294 | pkt->in_flight_len = pkt->len; |
| 7295 | qc->path->prep_in_flight += pkt->len; |
| 7296 | } |
| 7297 | /* Always reset this flags */ |
| 7298 | qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 7299 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK) { |
| 7300 | qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED; |
| 7301 | qel->pktns->rx.nb_aepkts_since_last_ack = 0; |
| 7302 | } |
| 7303 | |
| 7304 | pkt->pktns = qel->pktns; |
| 7305 | |
| 7306 | ret_pkt = pkt; |
| 7307 | leave: |
| 7308 | TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt); |
| 7309 | return ret_pkt; |
| 7310 | |
| 7311 | err: |
| 7312 | /* TODO: what about the frames which have been built |
| 7313 | * for this packet. |
| 7314 | */ |
| 7315 | free_quic_tx_packet(qc, pkt); |
| 7316 | goto leave; |
| 7317 | } |
| 7318 | |
| 7319 | |
| 7320 | static void __quic_conn_init(void) |
| 7321 | { |
| 7322 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 7323 | } |
| 7324 | INITCALL0(STG_REGISTER, __quic_conn_init); |
| 7325 | |
| 7326 | static void __quic_conn_deinit(void) |
| 7327 | { |
| 7328 | BIO_meth_free(ha_quic_meth); |
| 7329 | } |
| 7330 | REGISTER_POST_DEINIT(__quic_conn_deinit); |
| 7331 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7332 | /* Handle a new <dgram> received. Parse each QUIC packets and copied their |
| 7333 | * content to a quic-conn instance. The datagram content can be released after |
| 7334 | * this function. |
| 7335 | * |
| 7336 | * If datagram has been received on a quic-conn owned FD, <from_qc> must be set |
| 7337 | * to the connection instance. <li> is the attached listener. The caller is |
| 7338 | * responsible to ensure that the first packet is destined to this connection |
| 7339 | * by comparing CIDs. |
| 7340 | * |
| 7341 | * If datagram has been received on a receiver FD, <from_qc> will be NULL. This |
| 7342 | * function will thus retrieve the connection from the CID tree or allocate a |
| 7343 | * new one if possible. <li> is the listener attached to the receiver. |
| 7344 | * |
| 7345 | * Returns 0 on success else non-zero. If an error happens, some packets from |
| 7346 | * the datagram may not have been parsed. |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7347 | */ |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7348 | int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc, |
| 7349 | struct listener *li) |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7350 | { |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7351 | struct quic_rx_packet *pkt; |
| 7352 | struct quic_conn *qc = NULL; |
| 7353 | unsigned char *pos, *end; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7354 | struct list *tasklist_head = NULL; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7355 | |
| 7356 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 7357 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7358 | pos = dgram->buf; |
| 7359 | end = pos + dgram->len; |
| 7360 | do { |
| 7361 | /* TODO replace zalloc -> alloc. */ |
| 7362 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 7363 | if (!pkt) { |
| 7364 | TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT); |
| 7365 | goto err; |
| 7366 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7367 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7368 | pkt->version = NULL; |
| 7369 | pkt->pn_offset = 0; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7370 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7371 | /* Set flag if pkt is the first one in dgram. */ |
| 7372 | if (pos == dgram->buf) |
| 7373 | pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7374 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7375 | LIST_INIT(&pkt->qc_rx_pkt_list); |
| 7376 | pkt->time_received = now_ms; |
| 7377 | quic_rx_packet_refinc(pkt); |
| 7378 | if (quic_rx_pkt_parse(pkt, pos, end, dgram, li)) |
| 7379 | goto next; |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7380 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7381 | /* Search quic-conn instance for first packet of the datagram. |
| 7382 | * quic_rx_packet_parse() is responsible to discard packets |
| 7383 | * with different DCID as the first one in the same datagram. |
| 7384 | */ |
| 7385 | if (!qc) { |
| 7386 | qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li); |
| 7387 | /* qc is NULL if receiving a non Initial packet for an |
| 7388 | * unknown connection. |
| 7389 | */ |
| 7390 | if (!qc) { |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7391 | /* Skip the entire datagram. */ |
| 7392 | pkt->len = end - pos; |
| 7393 | goto next; |
| 7394 | } |
| 7395 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7396 | dgram->qc = qc; |
| 7397 | } |
Amaury Denoyelle | 9828969 | 2022-10-19 15:37:44 +0200 | [diff] [blame] | 7398 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7399 | if (qc_rx_check_closing(qc, pkt)) { |
| 7400 | /* Skip the entire datagram. */ |
| 7401 | pkt->len = end - pos; |
| 7402 | goto next; |
| 7403 | } |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7404 | |
Amaury Denoyelle | eec0b3c | 2022-12-02 09:57:32 +0100 | [diff] [blame] | 7405 | /* Detect QUIC connection migration. */ |
| 7406 | if (ipcmp(&qc->peer_addr, &dgram->saddr, 1) || |
| 7407 | ipcmp(&qc->local_addr, &dgram->daddr, 1)) { |
| 7408 | if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) { |
| 7409 | /* Skip the entire datagram. */ |
| 7410 | TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc); |
| 7411 | pkt->len = end - pos; |
| 7412 | goto next; |
| 7413 | } |
| 7414 | } |
| 7415 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7416 | qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7417 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7418 | next: |
| 7419 | pos += pkt->len; |
| 7420 | quic_rx_packet_refdec(pkt); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7421 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7422 | /* Free rejected packets */ |
| 7423 | if (!pkt->refcnt) { |
| 7424 | BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list)); |
| 7425 | pool_free(pool_head_quic_rx_packet, pkt); |
| 7426 | } |
| 7427 | } while (pos < end); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7428 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7429 | /* Increasing the received bytes counter by the UDP datagram length |
| 7430 | * if this datagram could be associated to a connection. |
| 7431 | */ |
| 7432 | if (dgram->qc) |
| 7433 | dgram->qc->rx.bytes += dgram->len; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7434 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7435 | /* This must never happen. */ |
| 7436 | BUG_ON(pos > end); |
| 7437 | BUG_ON(pos < end || pos > dgram->buf + dgram->len); |
| 7438 | /* Mark this datagram as consumed */ |
| 7439 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7440 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7441 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
| 7442 | return 0; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7443 | |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7444 | err: |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7445 | TRACE_LEAVE(QUIC_EV_CONN_LPKT); |
Amaury Denoyelle | 8687b63 | 2022-09-27 14:22:09 +0200 | [diff] [blame] | 7446 | return -1; |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7447 | } |
| 7448 | |
Amaury Denoyelle | 7c9fdd9 | 2022-11-16 11:01:02 +0100 | [diff] [blame] | 7449 | /* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local |
| 7450 | * CIDs. This can be used to determine if a datagram is addressed to the right |
| 7451 | * connection instance. |
| 7452 | * |
| 7453 | * Returns a boolean value. |
| 7454 | */ |
| 7455 | int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len) |
| 7456 | { |
| 7457 | struct ebmb_node *node; |
| 7458 | struct quic_connection_id *id; |
| 7459 | |
| 7460 | /* For ODCID, address is concatenated to it after qc.odcid.len so this |
| 7461 | * comparison is safe. |
| 7462 | */ |
| 7463 | if ((qc->scid.len == dcid_len && |
| 7464 | memcmp(qc->scid.data, dcid, dcid_len) == 0) || |
| 7465 | (qc->odcid.len == dcid_len && |
| 7466 | memcmp(qc->odcid.data, dcid, dcid_len)) == 0) { |
| 7467 | return 1; |
| 7468 | } |
| 7469 | |
| 7470 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, dcid, dcid_len); |
| 7471 | if (node) { |
| 7472 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 7473 | if (qc == id->qc) |
| 7474 | return 1; |
| 7475 | } |
| 7476 | |
| 7477 | return 0; |
| 7478 | } |
| 7479 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 7480 | /* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 7481 | * Returns 1 if succeeded, 0 if not. |
| 7482 | */ |
| 7483 | int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 7484 | unsigned char **dcid, size_t *dcid_len) |
| 7485 | { |
| 7486 | int ret = 0, long_header; |
| 7487 | size_t minlen, skip; |
| 7488 | |
| 7489 | TRACE_ENTER(QUIC_EV_CONN_RXPKT); |
| 7490 | |
| 7491 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
| 7492 | TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT); |
| 7493 | goto err; |
| 7494 | } |
| 7495 | |
| 7496 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
| 7497 | minlen = long_header ? QUIC_LONG_PACKET_MINLEN : |
| 7498 | QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN; |
| 7499 | skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF; |
| 7500 | if (end - buf < minlen) |
| 7501 | goto err; |
| 7502 | |
| 7503 | buf += skip; |
| 7504 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 7505 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 7506 | goto err; |
| 7507 | |
| 7508 | *dcid = buf; |
| 7509 | |
| 7510 | ret = 1; |
| 7511 | leave: |
| 7512 | TRACE_LEAVE(QUIC_EV_CONN_RXPKT); |
| 7513 | return ret; |
| 7514 | |
| 7515 | err: |
| 7516 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT); |
| 7517 | goto leave; |
| 7518 | } |
| 7519 | |
| 7520 | /* Notify the MUX layer if alive about an imminent close of <qc>. */ |
| 7521 | void qc_notify_close(struct quic_conn *qc) |
| 7522 | { |
| 7523 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
| 7524 | |
| 7525 | if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE) |
| 7526 | goto leave; |
| 7527 | |
| 7528 | qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE; |
| 7529 | /* wake up the MUX */ |
| 7530 | if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) { |
| 7531 | TRACE_STATE("connection closure notidfied to mux", |
| 7532 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7533 | qc->conn->mux->wake(qc->conn); |
| 7534 | } |
| 7535 | else |
| 7536 | TRACE_STATE("connection closure not notidfied to mux", |
| 7537 | QUIC_FL_CONN_NOTIFY_CLOSE, qc); |
| 7538 | leave: |
| 7539 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
| 7540 | } |
| 7541 | |
| 7542 | /* |
| 7543 | * Local variables: |
| 7544 | * c-indent-level: 8 |
| 7545 | * c-basic-offset: 8 |
| 7546 | * End: |
| 7547 | */ |