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