Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QUIC transport layer over SOCK_DGRAM sockets. |
| 3 | * |
| 4 | * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define _GNU_SOURCE |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <netinet/tcp.h> |
| 24 | |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 25 | #include <import/ebmbtree.h> |
| 26 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 27 | #include <haproxy/buf-t.h> |
| 28 | #include <haproxy/compat.h> |
| 29 | #include <haproxy/api.h> |
| 30 | #include <haproxy/debug.h> |
| 31 | #include <haproxy/tools.h> |
| 32 | #include <haproxy/ticks.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 33 | |
| 34 | #include <haproxy/connection.h> |
| 35 | #include <haproxy/fd.h> |
| 36 | #include <haproxy/freq_ctr.h> |
| 37 | #include <haproxy/global.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 38 | #include <haproxy/h3.h> |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 39 | #include <haproxy/hq_interop.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 40 | #include <haproxy/log.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 41 | #include <haproxy/mux_quic.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 42 | #include <haproxy/pipe.h> |
| 43 | #include <haproxy/proxy.h> |
| 44 | #include <haproxy/quic_cc.h> |
| 45 | #include <haproxy/quic_frame.h> |
| 46 | #include <haproxy/quic_loss.h> |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 47 | #include <haproxy/cbuf.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 48 | #include <haproxy/quic_tls.h> |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 49 | #include <haproxy/sink.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 50 | #include <haproxy/ssl_sock.h> |
| 51 | #include <haproxy/stream_interface.h> |
| 52 | #include <haproxy/task.h> |
| 53 | #include <haproxy/trace.h> |
| 54 | #include <haproxy/xprt_quic.h> |
| 55 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 56 | /* list of supported QUIC versions by this implementation */ |
| 57 | static int quic_supported_version[] = { |
| 58 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 59 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 60 | |
| 61 | /* placeholder, do not add entry after this */ |
| 62 | 0x0 |
| 63 | }; |
| 64 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 65 | /* This is the values of some QUIC transport parameters when absent. |
| 66 | * Should be used to initialize any transport parameters (local or remote) |
| 67 | * before updating them with customized values. |
| 68 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 69 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 70 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 71 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 72 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 73 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /* trace source and events */ |
| 77 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 78 | const struct trace_source *src, |
| 79 | const struct ist where, const struct ist func, |
| 80 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 81 | |
| 82 | static const struct trace_event quic_trace_events[] = { |
| 83 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 84 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 85 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 86 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 87 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 89 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 90 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 91 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 92 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 93 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 94 | { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" }, |
| 95 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 96 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 97 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 100 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 101 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 102 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 103 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 104 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 105 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 106 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 107 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 108 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 109 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 110 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 111 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 112 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 113 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 114 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 115 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 116 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 117 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 118 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 119 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 120 | { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 121 | { /* end */ } |
| 122 | }; |
| 123 | |
| 124 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 125 | /* arg1 */ { /* already used by the connection */ }, |
| 126 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 127 | /* arg3 */ { }, |
| 128 | /* arg4 */ { } |
| 129 | }; |
| 130 | |
| 131 | static const struct name_desc quic_trace_decoding[] = { |
| 132 | #define QUIC_VERB_CLEAN 1 |
| 133 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 134 | { /* end */ } |
| 135 | }; |
| 136 | |
| 137 | |
| 138 | struct trace_source trace_quic = { |
| 139 | .name = IST("quic"), |
| 140 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 141 | .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 142 | .default_cb = quic_trace, |
| 143 | .known_events = quic_trace_events, |
| 144 | .lockon_args = quic_trace_lockon_args, |
| 145 | .decoding = quic_trace_decoding, |
| 146 | .report_events = ~0, /* report everything by default */ |
| 147 | }; |
| 148 | |
| 149 | #define TRACE_SOURCE &trace_quic |
| 150 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 151 | |
| 152 | static BIO_METHOD *ha_quic_meth; |
| 153 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 154 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ); |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 155 | DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 156 | DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 157 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 158 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 159 | DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 160 | DECLARE_POOL(pool_head_quic_connection_id, |
| 161 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 162 | DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 163 | DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 164 | DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm)); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 165 | DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 166 | DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf)); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 167 | DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame)); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 168 | DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 169 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 170 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 171 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 172 | struct quic_conn *qc, size_t dglen, int pkt_type, |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 173 | int padding, int ack, int probe, int cc, int *err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 174 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 175 | /* Only for debug purpose */ |
| 176 | struct enc_debug_info { |
| 177 | unsigned char *payload; |
| 178 | size_t payload_len; |
| 179 | unsigned char *aad; |
| 180 | size_t aad_len; |
| 181 | uint64_t pn; |
| 182 | }; |
| 183 | |
| 184 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 185 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 186 | unsigned char *payload, size_t payload_len, |
| 187 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 188 | { |
| 189 | edi->payload = payload; |
| 190 | edi->payload_len = payload_len; |
| 191 | edi->aad = aad; |
| 192 | edi->aad_len = aad_len; |
| 193 | edi->pn = pn; |
| 194 | } |
| 195 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 196 | /* Trace callback for QUIC. |
| 197 | * These traces always expect that arg1, if non-null, is of type connection. |
| 198 | */ |
| 199 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 200 | const struct ist where, const struct ist func, |
| 201 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 202 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 203 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 204 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 205 | if (qc) { |
| 206 | const struct quic_tls_secrets *secs; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 207 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 208 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 209 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 210 | chunk_appendf(&trace_buf, "\n odcid"); |
| 211 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 212 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 213 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 214 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 215 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 219 | const enum ssl_encryption_level_t *level = a2; |
| 220 | const size_t *len = a3; |
| 221 | |
| 222 | if (level) { |
| 223 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 224 | |
| 225 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 226 | } |
| 227 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 228 | chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 229 | } |
| 230 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 231 | /* Initial read & write secrets. */ |
| 232 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 233 | const unsigned char *rx_sec = a2; |
| 234 | const unsigned char *tx_sec = a3; |
| 235 | |
| 236 | secs = &qc->els[level].tls_ctx.rx; |
| 237 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 238 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 239 | if (rx_sec) |
| 240 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 241 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 242 | } |
| 243 | secs = &qc->els[level].tls_ctx.tx; |
| 244 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 245 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 246 | if (tx_sec) |
| 247 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 248 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 249 | } |
| 250 | } |
| 251 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 252 | const enum ssl_encryption_level_t *level = a2; |
| 253 | const unsigned char *secret = a3; |
| 254 | const size_t *secret_len = a4; |
| 255 | |
| 256 | if (level) { |
| 257 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 258 | |
| 259 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 260 | if (secret && secret_len) |
| 261 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 262 | secs = &qc->els[lvl].tls_ctx.rx; |
| 263 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 264 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 269 | const enum ssl_encryption_level_t *level = a2; |
| 270 | const unsigned char *secret = a3; |
| 271 | const size_t *secret_len = a4; |
| 272 | |
| 273 | if (level) { |
| 274 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 275 | |
| 276 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 277 | if (secret && secret_len) |
| 278 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 279 | secs = &qc->els[lvl].tls_ctx.tx; |
| 280 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 281 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 282 | } |
| 283 | |
| 284 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 285 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 286 | if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 287 | const struct quic_tx_packet *pkt = a2; |
| 288 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 289 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 290 | |
| 291 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 292 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 293 | chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu " |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 294 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 295 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 296 | (unsigned long long)qc->path->cwnd, |
| 297 | (unsigned long long)qc->path->prep_in_flight, |
| 298 | (unsigned long long)qc->path->in_flight, |
| 299 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 300 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 301 | } |
| 302 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 303 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 304 | if (pkt->pn_node.key != (uint64_t)-1) |
| 305 | chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 306 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 307 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 308 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 309 | (unsigned long long)qc->rx.bytes, |
| 310 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | if (room) { |
| 314 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 315 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 316 | (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | if (mask & QUIC_EV_CONN_HDSHK) { |
| 321 | const enum quic_handshake_state *state = a2; |
| 322 | const int *err = a3; |
| 323 | |
| 324 | if (state) |
| 325 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 326 | if (err) |
| 327 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 328 | } |
| 329 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 330 | if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 331 | const struct quic_rx_packet *pkt = a2; |
| 332 | const unsigned long *pktlen = a3; |
| 333 | const SSL *ssl = a4; |
| 334 | |
| 335 | if (pkt) { |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 336 | chunk_appendf(&trace_buf, " pkt@%p el=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 337 | pkt, quic_packet_type_enc_level_char(pkt->type)); |
| 338 | if (pkt->pnl) |
| 339 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 340 | (unsigned long long)pkt->pn); |
| 341 | if (pkt->token_len) |
| 342 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 343 | (unsigned long long)pkt->token_len); |
| 344 | if (pkt->aad_len) |
| 345 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 346 | (unsigned long long)pkt->aad_len); |
| 347 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 348 | pkt->flags, (unsigned long long)pkt->len); |
| 349 | } |
| 350 | if (pktlen) |
| 351 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 352 | if (ssl) { |
| 353 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 354 | chunk_appendf(&trace_buf, " el=%c", |
| 355 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 360 | const struct quic_rx_packet *pkt = a2; |
| 361 | const struct quic_rx_crypto_frm *cf = a3; |
| 362 | const SSL *ssl = a4; |
| 363 | |
| 364 | if (pkt) |
| 365 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 366 | quic_packet_type_enc_level_char(pkt->type), |
| 367 | (unsigned long long)pkt->pn); |
| 368 | if (cf) |
| 369 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 370 | (unsigned long long)cf->offset_node.key, |
| 371 | (unsigned long long)cf->len); |
| 372 | if (ssl) { |
| 373 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
Frédéric Lécaille | 57e6e9e | 2021-09-23 18:10:56 +0200 | [diff] [blame] | 374 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 375 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 376 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 377 | |
| 378 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 379 | chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 383 | const struct quic_frame *frm = a2; |
| 384 | |
| 385 | if (frm) |
| 386 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 387 | } |
| 388 | |
| 389 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 390 | const struct quic_enc_level *qel = a2; |
| 391 | |
| 392 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 393 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 394 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 395 | " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 396 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 546186b | 2021-08-03 14:25:36 +0200 | [diff] [blame] | 397 | quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)), |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 398 | !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 399 | (unsigned long long)qc->path->cwnd, |
| 400 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 401 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 402 | (unsigned long long)pktns->tx.in_flight, |
| 403 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 407 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 408 | const struct enc_debug_info *edi = a2; |
| 409 | |
| 410 | if (edi) |
| 411 | chunk_appendf(&trace_buf, |
| 412 | " payload=@%p payload_len=%llu" |
| 413 | " aad=@%p aad_len=%llu pn=%llu", |
| 414 | edi->payload, (unsigned long long)edi->payload_len, |
| 415 | edi->aad, (unsigned long long)edi->aad_len, |
| 416 | (unsigned long long)edi->pn); |
| 417 | } |
| 418 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 419 | if (mask & QUIC_EV_CONN_RMHP) { |
| 420 | const struct quic_rx_packet *pkt = a2; |
| 421 | |
| 422 | if (pkt) { |
| 423 | const int *ret = a3; |
| 424 | |
| 425 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 426 | if (ret && *ret) |
| 427 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 428 | pkt->pnl, (unsigned long long)pkt->pn); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 433 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 434 | const unsigned long *val1 = a3; |
| 435 | const unsigned long *val2 = a4; |
| 436 | |
| 437 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 438 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 439 | if (val1) |
| 440 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 441 | if (val2) |
| 442 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 443 | } |
| 444 | |
| 445 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 446 | const unsigned int *rtt_sample = a2; |
| 447 | const unsigned int *ack_delay = a3; |
| 448 | const struct quic_loss *ql = a4; |
| 449 | |
| 450 | if (rtt_sample) |
| 451 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 452 | if (ack_delay) |
| 453 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 454 | if (ql) |
| 455 | chunk_appendf(&trace_buf, |
| 456 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 457 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 458 | } |
| 459 | if (mask & QUIC_EV_CONN_CC) { |
| 460 | const struct quic_cc_event *ev = a2; |
| 461 | const struct quic_cc *cc = a3; |
| 462 | |
| 463 | if (a2) |
| 464 | quic_cc_event_trace(&trace_buf, ev); |
| 465 | if (a3) |
| 466 | quic_cc_state_trace(&trace_buf, cc); |
| 467 | } |
| 468 | |
| 469 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 470 | const struct quic_pktns *pktns = a2; |
| 471 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 472 | |
| 473 | if (pktns) { |
| 474 | chunk_appendf(&trace_buf, " pktns=%s", |
| 475 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 476 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 477 | if (pktns->tx.loss_time) |
| 478 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 479 | TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time))); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 480 | } |
| 481 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 482 | struct quic_tx_packet *pkt; |
| 483 | |
| 484 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 485 | list_for_each_entry(pkt, lost_pkts, list) |
| 486 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 491 | const struct quic_pktns *pktns = a2; |
| 492 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 493 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 494 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 495 | if (ifae_pkts) |
| 496 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 497 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 498 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 499 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 500 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 501 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 502 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 503 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 504 | if (pktns->tx.in_flight) |
| 505 | chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 506 | if (pktns->tx.loss_time) |
| 507 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 508 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 509 | } |
| 510 | if (mask & QUIC_EV_CONN_SPTO) { |
| 511 | if (pktns->tx.time_of_last_eliciting) |
| 512 | chunk_appendf(&trace_buf, " tole=%dms", |
| 513 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 514 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 515 | chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) { |
| 520 | chunk_appendf(&trace_buf, |
| 521 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 526 | const struct quic_tx_packet *pkt = a2; |
| 527 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 528 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 529 | (unsigned long long)qc->path->cwnd, |
| 530 | (unsigned long long)qc->path->prep_in_flight, |
| 531 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 532 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 533 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 534 | (unsigned long)pkt->pn_node.key, |
| 535 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 536 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 537 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 538 | } |
| 539 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 540 | |
| 541 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 542 | const uint8_t *alert = a2; |
| 543 | const enum ssl_encryption_level_t *level = a3; |
| 544 | |
| 545 | if (alert) |
| 546 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 547 | if (level) |
| 548 | chunk_appendf(&trace_buf, " el=%c", |
| 549 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 550 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 551 | |
| 552 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 553 | const size_t *sz1 = a2; |
| 554 | const size_t *sz2 = a3; |
| 555 | const size_t *sz3 = a4; |
| 556 | |
| 557 | if (sz1) |
| 558 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 559 | if (sz2) |
| 560 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 561 | if (sz3) |
| 562 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 563 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 564 | |
| 565 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 566 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 567 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 568 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 569 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 570 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 571 | } |
| 572 | if (mask & QUIC_EV_CONN_LPKT) { |
| 573 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 574 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 575 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 576 | if (pkt) { |
| 577 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 578 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 579 | if (pkt->pn_node.key != (uint64_t)-1) |
| 580 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 581 | } |
| 582 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 583 | if (len) |
| 584 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | } |
| 588 | |
| 589 | /* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 590 | static inline int quic_peer_validated_addr(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 591 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 592 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 593 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 594 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 595 | return 1; |
| 596 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 597 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 598 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 599 | if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) || |
| 600 | (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) || |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 601 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 602 | return 1; |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 608 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 609 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 610 | static inline void qc_set_timer(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 611 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 612 | struct quic_pktns *pktns; |
| 613 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 614 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 615 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 616 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 617 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 618 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 619 | pktns = quic_loss_pktns(qc); |
| 620 | if (tick_isset(pktns->tx.loss_time)) { |
| 621 | qc->timer = pktns->tx.loss_time; |
| 622 | goto out; |
| 623 | } |
| 624 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 625 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 626 | * cancelled for a server which reached the anti-amplification limit. |
| 627 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 628 | if (!quic_peer_validated_addr(qc) && |
| 629 | (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 630 | TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 631 | qc->timer = TICK_ETERNITY; |
| 632 | goto out; |
| 633 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 634 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 635 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 636 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 637 | /* Timer cancellation. */ |
| 638 | qc->timer = TICK_ETERNITY; |
| 639 | goto out; |
| 640 | } |
| 641 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 642 | handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE; |
| 643 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 644 | if (tick_isset(pto)) |
| 645 | qc->timer = pto; |
| 646 | out: |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 647 | if (qc->timer_task && qc->timer != TICK_ETERNITY) |
Amaury Denoyelle | 336f6fd | 2021-10-05 14:42:25 +0200 | [diff] [blame] | 648 | task_schedule(qc->timer_task, qc->timer); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 649 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 652 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 653 | * connection. |
| 654 | * Return 1 if succeeded, 0 if not. |
| 655 | */ |
| 656 | static int quic_tls_key_update(struct quic_conn *qc) |
| 657 | { |
| 658 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 659 | struct quic_tls_secrets *rx, *tx; |
| 660 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 661 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 662 | |
| 663 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 664 | rx = &tls_ctx->rx; |
| 665 | tx = &tls_ctx->tx; |
| 666 | nxt_rx = &qc->ku.nxt_rx; |
| 667 | nxt_tx = &qc->ku.nxt_tx; |
| 668 | |
| 669 | /* Prepare new RX secrets */ |
| 670 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 671 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 672 | TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 677 | nxt_rx->key, nxt_rx->keylen, |
| 678 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 679 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 680 | TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | /* Prepare new TX secrets */ |
| 685 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 686 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 687 | TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 692 | nxt_tx->key, nxt_tx->keylen, |
| 693 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 694 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 695 | TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | return 1; |
| 700 | } |
| 701 | |
| 702 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 703 | * Must be used after having updated them. |
| 704 | * Always succeeds. |
| 705 | */ |
| 706 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 707 | { |
| 708 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 709 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 710 | |
| 711 | /* Rotate the RX secrets */ |
| 712 | curr_secret = tls_ctx->rx.secret; |
| 713 | curr_iv = tls_ctx->rx.iv; |
| 714 | curr_key = tls_ctx->rx.key; |
| 715 | |
| 716 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 717 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 718 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 719 | |
| 720 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 721 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 722 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 723 | |
| 724 | qc->ku.prv_rx.secret = curr_secret; |
| 725 | qc->ku.prv_rx.iv = curr_iv; |
| 726 | qc->ku.prv_rx.key = curr_key; |
| 727 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 728 | |
| 729 | /* Update the TX secrets */ |
| 730 | curr_secret = tls_ctx->tx.secret; |
| 731 | curr_iv = tls_ctx->tx.iv; |
| 732 | curr_key = tls_ctx->tx.key; |
| 733 | |
| 734 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 735 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 736 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 737 | |
| 738 | qc->ku.nxt_tx.secret = curr_secret; |
| 739 | qc->ku.nxt_tx.iv = curr_iv; |
| 740 | qc->ku.nxt_tx.key = curr_key; |
| 741 | } |
| 742 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 743 | #ifndef OPENSSL_IS_BORINGSSL |
| 744 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 745 | const uint8_t *read_secret, |
| 746 | const uint8_t *write_secret, size_t secret_len) |
| 747 | { |
| 748 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 749 | struct quic_tls_ctx *tls_ctx = |
| 750 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 751 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 752 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 753 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 754 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 755 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 756 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 757 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 758 | goto out; |
| 759 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 760 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 761 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 762 | TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 763 | goto err; |
| 764 | } |
| 765 | |
| 766 | rx = &tls_ctx->rx; |
| 767 | tx = &tls_ctx->tx; |
| 768 | |
| 769 | rx->aead = tx->aead = tls_aead(cipher); |
| 770 | rx->md = tx->md = tls_md(cipher); |
| 771 | rx->hp = tx->hp = tls_hp(cipher); |
| 772 | |
| 773 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 774 | rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 775 | read_secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 776 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 777 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 778 | } |
| 779 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 780 | rx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 781 | |
| 782 | if (!write_secret) |
| 783 | goto tp; |
| 784 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 785 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 786 | tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 787 | write_secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 788 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 789 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 790 | } |
| 791 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 792 | tx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 793 | tp: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 794 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 795 | const unsigned char *buf; |
| 796 | size_t buflen; |
| 797 | |
| 798 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 799 | if (!buflen) |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 800 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 801 | |
| 802 | if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen)) |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 803 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 804 | } |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 805 | |
| 806 | if (level == ssl_encryption_application) { |
| 807 | struct quic_conn *qc = conn->qc; |
| 808 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 809 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 810 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 811 | |
| 812 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 813 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 814 | TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 815 | goto err; |
| 816 | } |
| 817 | |
| 818 | memcpy(rx->secret, read_secret, secret_len); |
| 819 | rx->secretlen = secret_len; |
| 820 | memcpy(tx->secret, write_secret, secret_len); |
| 821 | tx->secretlen = secret_len; |
| 822 | /* Initialize all the secret keys lengths */ |
| 823 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 824 | /* Prepare the next key update */ |
| 825 | if (!quic_tls_key_update(qc)) |
| 826 | goto err; |
| 827 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 828 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 829 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn->qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 830 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 831 | |
| 832 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 833 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, conn->qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 834 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 835 | } |
| 836 | #else |
| 837 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 838 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 839 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 840 | */ |
| 841 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 842 | const SSL_CIPHER *cipher, |
| 843 | const uint8_t *secret, size_t secret_len) |
| 844 | { |
| 845 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 846 | struct quic_tls_ctx *tls_ctx = |
| 847 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 848 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 849 | TRACE_ENTER(QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 850 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 851 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 852 | goto out; |
| 853 | } |
| 854 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 855 | tls_ctx->rx.aead = tls_aead(cipher); |
| 856 | tls_ctx->rx.md = tls_md(cipher); |
| 857 | tls_ctx->rx.hp = tls_hp(cipher); |
| 858 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 859 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 860 | goto err; |
| 861 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 862 | if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md, |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 863 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 864 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 865 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 866 | secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 867 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 868 | goto err; |
| 869 | } |
| 870 | |
| 871 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 872 | const unsigned char *buf; |
| 873 | size_t buflen; |
| 874 | |
| 875 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 876 | if (!buflen) |
| 877 | goto err; |
| 878 | |
| 879 | if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen)) |
| 880 | goto err; |
| 881 | } |
| 882 | |
| 883 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 884 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 885 | TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn->qc, &level, secret, &secret_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 886 | |
| 887 | return 1; |
| 888 | |
| 889 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 890 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 895 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 896 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 897 | */ |
| 898 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 899 | const SSL_CIPHER *cipher, |
| 900 | const uint8_t *secret, size_t secret_len) |
| 901 | { |
| 902 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 903 | struct quic_tls_ctx *tls_ctx = |
| 904 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 905 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 906 | TRACE_ENTER(QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 907 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 908 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 909 | goto out; |
| 910 | } |
| 911 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 912 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 913 | goto err; |
| 914 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 915 | tls_ctx->tx.aead = tls_aead(cipher); |
| 916 | tls_ctx->tx.md = tls_md(cipher); |
| 917 | tls_ctx->tx.hp = tls_hp(cipher); |
| 918 | |
| 919 | if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md, |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 920 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 921 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 922 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 923 | secret, secret_len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 924 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 925 | goto err; |
| 926 | } |
| 927 | |
| 928 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 929 | TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn->qc, &level, secret, &secret_len); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 930 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 931 | return 1; |
| 932 | |
| 933 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 934 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 935 | return 0; |
| 936 | } |
| 937 | #endif |
| 938 | |
| 939 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 940 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 941 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 942 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 943 | * store all the data. |
| 944 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 945 | */ |
| 946 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 947 | const unsigned char *data, size_t len) |
| 948 | { |
| 949 | struct quic_crypto_buf **qcb; |
| 950 | /* The remaining byte to store in CRYPTO buffers. */ |
| 951 | size_t cf_offset, cf_len, *nb_buf; |
| 952 | unsigned char *pos; |
| 953 | |
| 954 | nb_buf = &qel->tx.crypto.nb_buf; |
| 955 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 956 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 957 | cf_len = len; |
| 958 | |
| 959 | while (len) { |
| 960 | size_t to_copy, room; |
| 961 | |
| 962 | pos = (*qcb)->data + (*qcb)->sz; |
| 963 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 964 | to_copy = len > room ? room : len; |
| 965 | if (to_copy) { |
| 966 | memcpy(pos, data, to_copy); |
| 967 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 968 | qel->tx.crypto.sz += to_copy; |
| 969 | (*qcb)->sz += to_copy; |
| 970 | pos += to_copy; |
| 971 | len -= to_copy; |
| 972 | data += to_copy; |
| 973 | } |
| 974 | else { |
| 975 | struct quic_crypto_buf **tmp; |
| 976 | |
| 977 | tmp = realloc(qel->tx.crypto.bufs, |
| 978 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 979 | if (tmp) { |
| 980 | qel->tx.crypto.bufs = tmp; |
| 981 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 982 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 983 | if (!*qcb) |
| 984 | return 0; |
| 985 | |
| 986 | (*qcb)->sz = 0; |
| 987 | ++*nb_buf; |
| 988 | } |
| 989 | else { |
| 990 | break; |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 996 | * have been buffered. |
| 997 | */ |
| 998 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 999 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1000 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1001 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1002 | /* There is at most one CRYPTO frame in this packet number |
| 1003 | * space. Let's look for it. |
| 1004 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1005 | list_for_each_entry(frm, &qel->pktns->tx.frms, list) { |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1006 | if (frm->type != QUIC_FT_CRYPTO) |
| 1007 | continue; |
| 1008 | |
| 1009 | /* Found */ |
| 1010 | found = frm; |
| 1011 | break; |
| 1012 | } |
| 1013 | |
| 1014 | if (found) { |
| 1015 | found->crypto.len += cf_len; |
| 1016 | } |
| 1017 | else { |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1018 | frm = pool_alloc(pool_head_quic_frame); |
| 1019 | if (!frm) |
| 1020 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1021 | |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1022 | frm->type = QUIC_FT_CRYPTO; |
| 1023 | frm->crypto.offset = cf_offset; |
| 1024 | frm->crypto.len = cf_len; |
| 1025 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1026 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1027 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | return len == 0; |
| 1031 | } |
| 1032 | |
| 1033 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1034 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1035 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1036 | { |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1037 | HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1038 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1039 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1040 | } |
| 1041 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1042 | /* Set the application for <qc> QUIC connection. |
| 1043 | * Return 1 if succeeded, 0 if not. |
| 1044 | */ |
| 1045 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1046 | { |
| 1047 | const struct qcc_app_ops *app_ops; |
| 1048 | |
| 1049 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) { |
| 1050 | app_ops = qc->qcc->app_ops = &h3_ops; |
| 1051 | } |
| 1052 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) { |
| 1053 | app_ops = qc->qcc->app_ops = &hq_interop_ops; |
| 1054 | } |
| 1055 | else |
| 1056 | return 0; |
| 1057 | |
| 1058 | if (app_ops->init && !app_ops->init(qc->qcc)) |
| 1059 | return 0; |
| 1060 | |
| 1061 | if (app_ops->finalize) |
| 1062 | app_ops->finalize(qc->qcc->ctx); |
| 1063 | |
| 1064 | return 1; |
| 1065 | } |
| 1066 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1067 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1068 | * wants to provide the QUIC layer with CRYPTO data. |
| 1069 | * Returns 1 if succeeded, 0 if not. |
| 1070 | */ |
| 1071 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1072 | const uint8_t *data, size_t len) |
| 1073 | { |
| 1074 | struct connection *conn; |
| 1075 | enum quic_tls_enc_level tel; |
| 1076 | struct quic_enc_level *qel; |
| 1077 | |
| 1078 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1079 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1080 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1081 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1082 | goto out; |
| 1083 | } |
| 1084 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1085 | tel = ssl_to_quic_enc_level(level); |
| 1086 | qel = &conn->qc->els[tel]; |
| 1087 | |
| 1088 | if (tel == -1) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1089 | TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1090 | goto err; |
| 1091 | } |
| 1092 | |
| 1093 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1094 | TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1095 | goto err; |
| 1096 | } |
| 1097 | |
| 1098 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1099 | conn->qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1100 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1101 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1102 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1103 | return 1; |
| 1104 | |
| 1105 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1106 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | int ha_quic_flush_flight(SSL *ssl) |
| 1111 | { |
| 1112 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1113 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1114 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn->qc); |
| 1115 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1116 | |
| 1117 | return 1; |
| 1118 | } |
| 1119 | |
| 1120 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1121 | { |
| 1122 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1123 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1124 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn->qc, &alert, &level); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1125 | quic_set_tls_alert(conn->qc, alert); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1126 | HA_ATOMIC_STORE(&conn->qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1127 | return 1; |
| 1128 | } |
| 1129 | |
| 1130 | /* QUIC TLS methods */ |
| 1131 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1132 | #ifdef OPENSSL_IS_BORINGSSL |
| 1133 | .set_read_secret = ha_set_rsec, |
| 1134 | .set_write_secret = ha_set_wsec, |
| 1135 | #else |
| 1136 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1137 | #endif |
| 1138 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1139 | .flush_flight = ha_quic_flush_flight, |
| 1140 | .send_alert = ha_quic_send_alert, |
| 1141 | }; |
| 1142 | |
| 1143 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1144 | * Returns an error count. |
| 1145 | */ |
| 1146 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1147 | { |
| 1148 | struct proxy *curproxy = bind_conf->frontend; |
| 1149 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1150 | int cfgerr = 0; |
| 1151 | |
| 1152 | #if 0 |
| 1153 | /* XXX Did not manage to use this. */ |
| 1154 | const char *ciphers = |
| 1155 | "TLS_AES_128_GCM_SHA256:" |
| 1156 | "TLS_AES_256_GCM_SHA384:" |
| 1157 | "TLS_CHACHA20_POLY1305_SHA256:" |
| 1158 | "TLS_AES_128_CCM_SHA256"; |
| 1159 | #endif |
Frédéric Lécaille | 4b1fddc | 2021-07-01 17:09:05 +0200 | [diff] [blame] | 1160 | const char *groups = "X25519:P-256:P-384:P-521"; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1161 | long options = |
| 1162 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1163 | SSL_OP_SINGLE_ECDH_USE | |
| 1164 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1165 | SSL_CTX *ctx; |
| 1166 | |
| 1167 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1168 | bind_conf->initial_ctx = ctx; |
| 1169 | |
| 1170 | SSL_CTX_set_options(ctx, options); |
| 1171 | #if 0 |
| 1172 | if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) { |
| 1173 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' " |
| 1174 | "for bind '%s' at [%s:%d].\n", |
| 1175 | curproxy->id, ciphers, |
| 1176 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1177 | cfgerr++; |
| 1178 | } |
| 1179 | #endif |
| 1180 | |
| 1181 | if (SSL_CTX_set1_curves_list(ctx, groups) != 1) { |
| 1182 | ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' " |
| 1183 | "for bind '%s' at [%s:%d].\n", |
| 1184 | curproxy->id, groups, |
| 1185 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1186 | cfgerr++; |
| 1187 | } |
| 1188 | |
| 1189 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1190 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1191 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1192 | SSL_CTX_set_default_verify_paths(ctx); |
| 1193 | |
| 1194 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1195 | #ifdef OPENSSL_IS_BORINGSSL |
| 1196 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1197 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1198 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1199 | if (bind_conf->ssl_conf.early_data) { |
| 1200 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Frédéric Lécaille | ad3c07a | 2021-12-14 19:23:43 +0100 | [diff] [blame] | 1201 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1202 | } |
| 1203 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1204 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1205 | #else |
| 1206 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1207 | #endif |
| 1208 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1209 | #endif |
| 1210 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1211 | |
| 1212 | return cfgerr; |
| 1213 | } |
| 1214 | |
| 1215 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1216 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1217 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1218 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1219 | */ |
| 1220 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1221 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1222 | { |
| 1223 | uint64_t expected_pn = largest_pn + 1; |
| 1224 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1225 | uint64_t pn_hwin = pn_win / 2; |
| 1226 | uint64_t pn_mask = pn_win - 1; |
| 1227 | uint64_t candidate_pn; |
| 1228 | |
| 1229 | |
| 1230 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1231 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1232 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1233 | candidate_pn + pn_hwin <= expected_pn) |
| 1234 | return candidate_pn + pn_win; |
| 1235 | |
| 1236 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1237 | return candidate_pn - pn_win; |
| 1238 | |
| 1239 | return candidate_pn; |
| 1240 | } |
| 1241 | |
| 1242 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1243 | * cryptographic context. |
| 1244 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1245 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1246 | * <end> points to one byte past the end of this packet. |
| 1247 | * Returns 1 if succeeded, 0 if not. |
| 1248 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1249 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1250 | struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1251 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1252 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1253 | { |
| 1254 | int ret, outlen, i, pnlen; |
| 1255 | uint64_t packet_number; |
| 1256 | uint32_t truncated_pn = 0; |
| 1257 | unsigned char mask[5] = {0}; |
| 1258 | unsigned char *sample; |
| 1259 | EVP_CIPHER_CTX *cctx; |
| 1260 | unsigned char *hp_key; |
| 1261 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1262 | /* Check there is enough data in this packet. */ |
| 1263 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1264 | TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | cctx = EVP_CIPHER_CTX_new(); |
| 1269 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1270 | TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | ret = 0; |
| 1275 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1276 | |
| 1277 | hp_key = tls_ctx->rx.hp_key; |
| 1278 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1279 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1280 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1281 | TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1282 | goto out; |
| 1283 | } |
| 1284 | |
| 1285 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1286 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1287 | for (i = 0; i < pnlen; i++) { |
| 1288 | pn[i] ^= mask[i + 1]; |
| 1289 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1290 | } |
| 1291 | |
| 1292 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1293 | /* Store remaining information for this unprotected header */ |
| 1294 | pkt->pn = packet_number; |
| 1295 | pkt->pnl = pnlen; |
| 1296 | |
| 1297 | ret = 1; |
| 1298 | |
| 1299 | out: |
| 1300 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1301 | |
| 1302 | return ret; |
| 1303 | } |
| 1304 | |
| 1305 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1306 | * address, with <payload_len> as payload length, <aad> as address of |
| 1307 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1308 | * context. |
| 1309 | * Returns 1 if succeeded, 0 if not. |
| 1310 | */ |
| 1311 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1312 | unsigned char *aad, size_t aad_len, uint64_t pn, |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1313 | struct quic_tls_ctx *tls_ctx, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1314 | { |
| 1315 | unsigned char iv[12]; |
| 1316 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1317 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1318 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1319 | |
| 1320 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1321 | TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1322 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1326 | tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1327 | TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1328 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1332 | |
| 1333 | err: |
| 1334 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1335 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1336 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1340 | * Returns 1 if succeeded, 0 if not. |
| 1341 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1342 | static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1343 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1344 | int ret, kp_changed; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1345 | unsigned char iv[12]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1346 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1347 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1348 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1349 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1350 | |
| 1351 | kp_changed = 0; |
| 1352 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1353 | /* The two tested bits are not at the same position, |
| 1354 | * this is why they are first both inversed. |
| 1355 | */ |
| 1356 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1357 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1358 | /* The lowest packet number of a previous key phase |
| 1359 | * cannot be null if it really stores previous key phase |
| 1360 | * secrets. |
| 1361 | */ |
| 1362 | if (!pkt->qc->ku.prv_rx.pn) |
| 1363 | return 0; |
| 1364 | |
| 1365 | rx_iv = pkt->qc->ku.prv_rx.iv; |
| 1366 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1367 | } |
| 1368 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1369 | /* Next key phase */ |
| 1370 | kp_changed = 1; |
| 1371 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
| 1372 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1373 | } |
| 1374 | } |
| 1375 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1376 | |
| 1377 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1378 | return 0; |
| 1379 | |
| 1380 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1381 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1382 | tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1383 | if (!ret) |
| 1384 | return 0; |
| 1385 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1386 | /* Update the keys only if the packet decryption succeeded. */ |
| 1387 | if (kp_changed) { |
| 1388 | quic_tls_rotate_keys(pkt->qc); |
| 1389 | /* Toggle the Key Phase bit */ |
| 1390 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1391 | /* Store the lowest packet number received for the current key phase */ |
| 1392 | tls_ctx->rx.pn = pkt->pn; |
| 1393 | /* Prepare the next key update */ |
| 1394 | if (!quic_tls_key_update(pkt->qc)) |
| 1395 | return 0; |
| 1396 | } |
| 1397 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1398 | /* Update the packet length (required to parse the frames). */ |
| 1399 | pkt->len = pkt->aad_len + ret; |
| 1400 | |
| 1401 | return 1; |
| 1402 | } |
| 1403 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1404 | /* Remove from <qcs> stream the acknowledged frames. |
| 1405 | * Never fails. |
| 1406 | */ |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1407 | static int qcs_try_to_consume(struct qcs *qcs) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1408 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1409 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1410 | struct eb64_node *frm_node; |
| 1411 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1412 | ret = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1413 | frm_node = eb64_first(&qcs->tx.acked_frms); |
| 1414 | while (frm_node) { |
| 1415 | struct quic_stream *strm; |
| 1416 | |
| 1417 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
| 1418 | if (strm->offset.key != qcs->tx.ack_offset) |
| 1419 | break; |
| 1420 | |
| 1421 | b_del(strm->buf, strm->len); |
| 1422 | qcs->tx.ack_offset += strm->len; |
| 1423 | frm_node = eb64_next(frm_node); |
| 1424 | eb64_delete(&strm->offset); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1425 | ret = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1426 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1427 | |
| 1428 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1429 | } |
| 1430 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1431 | /* Treat <frm> frame whose packet it is attached to has just been acknowledged. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1432 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1433 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1434 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1435 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1436 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1437 | TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1438 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1439 | switch (frm->type) { |
| 1440 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1441 | { |
| 1442 | struct qcs *qcs = frm->stream.qcs; |
| 1443 | struct quic_stream *strm = &frm->stream; |
| 1444 | |
| 1445 | if (qcs->tx.ack_offset == strm->offset.key) { |
| 1446 | b_del(strm->buf, strm->len); |
| 1447 | qcs->tx.ack_offset += strm->len; |
| 1448 | LIST_DELETE(&frm->list); |
| 1449 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1450 | stream_acked = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1451 | } |
| 1452 | else { |
| 1453 | eb64_insert(&qcs->tx.acked_frms, &strm->offset); |
| 1454 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1455 | stream_acked |= qcs_try_to_consume(qcs); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1456 | } |
| 1457 | break; |
| 1458 | default: |
| 1459 | LIST_DELETE(&frm->list); |
| 1460 | pool_free(pool_head_quic_frame, frm); |
| 1461 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1462 | |
| 1463 | if (stream_acked) { |
| 1464 | struct qcc *qcc = qc->qcc; |
| 1465 | |
| 1466 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1467 | tasklet_wakeup(qcc->subs->tasklet); |
| 1468 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1469 | if (!qcc->subs->events) |
| 1470 | qcc->subs = NULL; |
| 1471 | } |
| 1472 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1476 | * deallocating them, and their TX frames. |
| 1477 | * Returns the last node reached to be used for the next range. |
| 1478 | * May be NULL if <largest> node could not be found. |
| 1479 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1480 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1481 | struct eb_root *pkts, |
| 1482 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1483 | struct list *newly_acked_pkts, |
| 1484 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1485 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1486 | { |
| 1487 | struct eb64_node *node; |
| 1488 | struct quic_tx_packet *pkt; |
| 1489 | |
| 1490 | if (largest_node) |
| 1491 | node = largest_node; |
| 1492 | else { |
| 1493 | node = eb64_lookup(pkts, largest); |
| 1494 | while (!node && largest > smallest) { |
| 1495 | node = eb64_lookup(pkts, --largest); |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1500 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1501 | |
| 1502 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1503 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1504 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1505 | TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1506 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1507 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1508 | node = eb64_prev(node); |
| 1509 | eb64_delete(&pkt->pn_node); |
| 1510 | } |
| 1511 | |
| 1512 | return node; |
| 1513 | } |
| 1514 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1515 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1516 | * same order they have been sent into <pktns_frm_list>. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1517 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1518 | static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1519 | struct list *pkt_frm_list, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1520 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1521 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1522 | struct quic_frame *frm, *frmbak; |
| 1523 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1524 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1525 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1526 | LIST_DELETE(&frm->list); |
| 1527 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1528 | LIST_APPEND(&tmp, &frm->list); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1529 | } |
| 1530 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1531 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1532 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1533 | |
| 1534 | /* Free the TX packets of <pkts> list */ |
| 1535 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1536 | { |
| 1537 | struct quic_tx_packet *pkt, *tmp; |
| 1538 | |
| 1539 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1540 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1541 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1542 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | /* Send a packet loss event nofification to the congestion controller |
| 1547 | * attached to <qc> connection with <lost_bytes> the number of lost bytes, |
| 1548 | * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet |
| 1549 | * at <now_us> current time. |
| 1550 | * Always succeeds. |
| 1551 | */ |
| 1552 | static inline void qc_cc_loss_event(struct quic_conn *qc, |
| 1553 | unsigned int lost_bytes, |
| 1554 | unsigned int newest_time_sent, |
| 1555 | unsigned int period, |
| 1556 | unsigned int now_us) |
| 1557 | { |
| 1558 | struct quic_cc_event ev = { |
| 1559 | .type = QUIC_CC_EVT_LOSS, |
| 1560 | .loss.now_ms = now_ms, |
| 1561 | .loss.max_ack_delay = qc->max_ack_delay, |
| 1562 | .loss.lost_bytes = lost_bytes, |
| 1563 | .loss.newest_time_sent = newest_time_sent, |
| 1564 | .loss.period = period, |
| 1565 | }; |
| 1566 | |
| 1567 | quic_cc_event(&qc->path->cc, &ev); |
| 1568 | } |
| 1569 | |
| 1570 | /* Send a packet ack event nofication for each newly acked packet of |
| 1571 | * <newly_acked_pkts> list and free them. |
| 1572 | * Always succeeds. |
| 1573 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1574 | static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1575 | struct list *newly_acked_pkts) |
| 1576 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1577 | struct quic_tx_packet *pkt, *tmp; |
| 1578 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1579 | |
| 1580 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1581 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1582 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1583 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1584 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1585 | ev.ack.acked = pkt->in_flight_len; |
| 1586 | ev.ack.time_sent = pkt->time_sent; |
| 1587 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1588 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1589 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1590 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | } |
| 1594 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1595 | /* Release all the frames attached to <pktns> packet number space */ |
| 1596 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1597 | { |
| 1598 | struct quic_frame *frm, *frmbak; |
| 1599 | |
| 1600 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1601 | LIST_DELETE(&frm->list); |
| 1602 | pool_free(pool_head_quic_frame, frm); |
| 1603 | } |
| 1604 | } |
| 1605 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1606 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1607 | * their TX frames. |
| 1608 | * Send a packet loss event to the congestion controller if |
| 1609 | * in flight packet have been lost. |
| 1610 | * Also frees the packet in <pkts> list. |
| 1611 | * Never fails. |
| 1612 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1613 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1614 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1615 | struct list *pkts, |
| 1616 | uint64_t now_us) |
| 1617 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1618 | struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1619 | uint64_t lost_bytes; |
| 1620 | |
| 1621 | lost_bytes = 0; |
| 1622 | oldest_lost = newest_lost = NULL; |
| 1623 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1624 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1625 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1626 | lost_bytes += pkt->in_flight_len; |
| 1627 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1628 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1629 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1630 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1631 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1632 | qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1633 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1634 | if (!oldest_lost) { |
| 1635 | oldest_lost = newest_lost = pkt; |
| 1636 | } |
| 1637 | else { |
| 1638 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1639 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1640 | newest_lost = pkt; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | if (lost_bytes) { |
| 1645 | /* Sent a packet loss event to the congestion controller. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1646 | qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1647 | newest_lost->time_sent - oldest_lost->time_sent, now_us); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1648 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1649 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1650 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1655 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1656 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1657 | * space if any not deemed lost. |
| 1658 | * Should be called after having received an ACK frame with newly acknowledged |
| 1659 | * packets or when the the loss detection timer has expired. |
| 1660 | * Always succeeds. |
| 1661 | */ |
| 1662 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1663 | struct quic_conn *qc, |
| 1664 | struct list *lost_pkts) |
| 1665 | { |
| 1666 | struct eb_root *pkts; |
| 1667 | struct eb64_node *node; |
| 1668 | struct quic_loss *ql; |
| 1669 | unsigned int loss_delay; |
| 1670 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1671 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1672 | pkts = &pktns->tx.pkts; |
| 1673 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1674 | if (eb_is_empty(pkts)) |
| 1675 | goto out; |
| 1676 | |
| 1677 | ql = &qc->path->loss; |
| 1678 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
| 1679 | loss_delay += loss_delay >> 3; |
| 1680 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1681 | |
| 1682 | node = eb64_first(pkts); |
| 1683 | while (node) { |
| 1684 | struct quic_tx_packet *pkt; |
| 1685 | int64_t largest_acked_pn; |
| 1686 | unsigned int loss_time_limit, time_sent; |
| 1687 | |
| 1688 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1689 | largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1690 | node = eb64_next(node); |
| 1691 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1692 | break; |
| 1693 | |
| 1694 | time_sent = pkt->time_sent; |
| 1695 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1696 | if (tick_is_le(time_sent, now_ms) || |
| 1697 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1698 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1699 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1700 | } |
| 1701 | else { |
Frédéric Lécaille | dc90c07 | 2021-12-27 18:15:27 +0100 | [diff] [blame] | 1702 | if (tick_isset(pktns->tx.loss_time)) |
| 1703 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1704 | else |
| 1705 | pktns->tx.loss_time = loss_time_limit; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1710 | TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1714 | * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1715 | * if the largest acked packet was newly acked and if there was at least one newly |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1716 | * acked ack-eliciting packet. |
| 1717 | * Return 1, if succeeded, 0 if not. |
| 1718 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1719 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1720 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1721 | struct quic_enc_level *qel, |
| 1722 | unsigned int *rtt_sample, |
| 1723 | const unsigned char **pos, const unsigned char *end) |
| 1724 | { |
| 1725 | struct quic_ack *ack = &frm->ack; |
| 1726 | uint64_t smallest, largest; |
| 1727 | struct eb_root *pkts; |
| 1728 | struct eb64_node *largest_node; |
| 1729 | unsigned int time_sent, pkt_flags; |
| 1730 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1731 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1732 | |
| 1733 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1734 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1735 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1736 | goto err; |
| 1737 | } |
| 1738 | |
| 1739 | if (ack->first_ack_range > ack->largest_ack) { |
| 1740 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1741 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1742 | goto err; |
| 1743 | } |
| 1744 | |
| 1745 | largest = ack->largest_ack; |
| 1746 | smallest = largest - ack->first_ack_range; |
| 1747 | pkts = &qel->pktns->tx.pkts; |
| 1748 | pkt_flags = 0; |
| 1749 | largest_node = NULL; |
| 1750 | time_sent = 0; |
| 1751 | |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1752 | if ((int64_t)ack->largest_ack > HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1753 | largest_node = eb64_lookup(pkts, largest); |
| 1754 | if (!largest_node) { |
| 1755 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1756 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1757 | } |
| 1758 | else { |
| 1759 | time_sent = eb64_entry(&largest_node->node, |
| 1760 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1761 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1765 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1766 | do { |
| 1767 | uint64_t gap, ack_range; |
| 1768 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1769 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1770 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1771 | if (!ack->ack_range_num--) |
| 1772 | break; |
| 1773 | |
| 1774 | if (!quic_dec_int(&gap, pos, end)) |
| 1775 | goto err; |
| 1776 | |
| 1777 | if (smallest < gap + 2) { |
| 1778 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1779 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1780 | goto err; |
| 1781 | } |
| 1782 | |
| 1783 | largest = smallest - gap - 2; |
| 1784 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1785 | goto err; |
| 1786 | |
| 1787 | if (largest < ack_range) { |
| 1788 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1789 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1790 | goto err; |
| 1791 | } |
| 1792 | |
| 1793 | /* Do not use this node anymore. */ |
| 1794 | largest_node = NULL; |
| 1795 | /* Next range */ |
| 1796 | smallest = largest - ack_range; |
| 1797 | |
| 1798 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1799 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1800 | } while (1); |
| 1801 | |
| 1802 | /* Flag this packet number space as having received an ACK. */ |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 1803 | HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1804 | |
| 1805 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1806 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1807 | HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1811 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1812 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1813 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1814 | qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1815 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1816 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 1817 | if (quic_peer_validated_addr(qc)) |
| 1818 | qc->path->loss.pto_count = 0; |
| 1819 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | |
| 1823 | return 1; |
| 1824 | |
| 1825 | err: |
| 1826 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1827 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1828 | return 0; |
| 1829 | } |
| 1830 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 1831 | /* This function gives the detail of the SSL error. It is used only |
| 1832 | * if the debug mode and the verbose mode are activated. It dump all |
| 1833 | * the SSL error until the stack was empty. |
| 1834 | */ |
| 1835 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 1836 | { |
| 1837 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 1838 | while (1) { |
| 1839 | unsigned long ret; |
| 1840 | |
| 1841 | ret = ERR_get_error(); |
| 1842 | if (!ret) |
| 1843 | return; |
| 1844 | |
| 1845 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 1846 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1851 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 1852 | const char **str, int *len); |
| 1853 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1854 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 1855 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1856 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1857 | * Return 1 if succeeded, 0 if not. |
| 1858 | */ |
| 1859 | static inline int qc_provide_cdata(struct quic_enc_level *el, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1860 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1861 | const unsigned char *data, size_t len, |
| 1862 | struct quic_rx_packet *pkt, |
| 1863 | struct quic_rx_crypto_frm *cf) |
| 1864 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1865 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 1866 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1867 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1868 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 1869 | qc = ctx->qc; |
| 1870 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1871 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 1872 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1873 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 1874 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1875 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1876 | goto err; |
| 1877 | } |
| 1878 | |
| 1879 | el->rx.crypto.offset += len; |
| 1880 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 1881 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1882 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1883 | state = HA_ATOMIC_LOAD(&qc->state); |
| 1884 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1885 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 1886 | if (ssl_err != 1) { |
| 1887 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1888 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1889 | TRACE_PROTO("SSL handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1890 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1891 | goto out; |
| 1892 | } |
| 1893 | |
| 1894 | TRACE_DEVEL("SSL handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1895 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 1896 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 1897 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1898 | goto err; |
| 1899 | } |
| 1900 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1901 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 1902 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1903 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1904 | else |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1905 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1906 | } else { |
| 1907 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 1908 | if (ssl_err != 1) { |
| 1909 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1910 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1911 | TRACE_DEVEL("SSL post handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1912 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1913 | goto out; |
| 1914 | } |
| 1915 | |
| 1916 | TRACE_DEVEL("SSL post handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1917 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1918 | goto err; |
| 1919 | } |
| 1920 | |
| 1921 | TRACE_PROTO("SSL post handshake succeeded", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1922 | QUIC_EV_CONN_HDSHK, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1923 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 1924 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1925 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1926 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1927 | return 1; |
| 1928 | |
| 1929 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1930 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1931 | return 0; |
| 1932 | } |
| 1933 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1934 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 1935 | * <pkt> RX packet. |
| 1936 | * Return it if succeeded, NULL if not. |
| 1937 | */ |
| 1938 | static inline |
| 1939 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 1940 | struct quic_rx_packet *pkt) |
| 1941 | { |
| 1942 | struct quic_rx_strm_frm *frm; |
| 1943 | |
| 1944 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 1945 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1946 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1947 | frm->len = stream_frm->len; |
| 1948 | frm->data = stream_frm->data; |
| 1949 | frm->pkt = pkt; |
| 1950 | } |
| 1951 | |
| 1952 | return frm; |
| 1953 | } |
| 1954 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1955 | /* Copy as most as possible STREAM data from <strm_frm> into <strm> stream. |
Frédéric Lécaille | 3fe7df8 | 2021-12-15 15:32:55 +0100 | [diff] [blame] | 1956 | * Also update <strm_frm> frame to reflect the data which have been consumed. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1957 | */ |
| 1958 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 1959 | { |
| 1960 | size_t ret; |
| 1961 | |
| 1962 | ret = 0; |
| 1963 | while (strm_frm->len) { |
| 1964 | size_t try; |
| 1965 | |
| 1966 | try = b_contig_space(buf); |
| 1967 | if (!try) |
| 1968 | break; |
| 1969 | |
| 1970 | if (try > strm_frm->len) |
| 1971 | try = strm_frm->len; |
| 1972 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1973 | strm_frm->len -= try; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1974 | strm_frm->offset.key += try; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1975 | b_add(buf, try); |
| 1976 | ret += try; |
| 1977 | } |
| 1978 | |
| 1979 | return ret; |
| 1980 | } |
| 1981 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 1982 | /* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer. |
| 1983 | * Also update <strm_frm> frame to reflect the data which have been consumed. |
| 1984 | */ |
| 1985 | static size_t qc_rx_strm_frm_cpy(struct buffer *buf, |
| 1986 | struct quic_rx_strm_frm *strm_frm) |
| 1987 | { |
| 1988 | size_t ret; |
| 1989 | |
| 1990 | ret = 0; |
| 1991 | while (strm_frm->len) { |
| 1992 | size_t try; |
| 1993 | |
| 1994 | try = b_contig_space(buf); |
| 1995 | if (!try) |
| 1996 | break; |
| 1997 | |
| 1998 | if (try > strm_frm->len) |
| 1999 | try = strm_frm->len; |
| 2000 | memcpy(b_tail(buf), strm_frm->data, try); |
| 2001 | strm_frm->len -= try; |
| 2002 | strm_frm->offset_node.key += try; |
| 2003 | b_add(buf, try); |
| 2004 | ret += try; |
| 2005 | } |
| 2006 | |
| 2007 | return ret; |
| 2008 | } |
| 2009 | |
| 2010 | /* Process as much as possible RX STREAM frames received for <qcs> */ |
| 2011 | static size_t qc_treat_rx_strm_frms(struct qcs *qcs) |
| 2012 | { |
| 2013 | int total; |
| 2014 | struct eb64_node *frm_node; |
| 2015 | |
| 2016 | total = 0; |
| 2017 | frm_node = eb64_first(&qcs->rx.frms); |
| 2018 | while (frm_node) { |
| 2019 | int ret; |
| 2020 | struct quic_rx_strm_frm *frm; |
| 2021 | |
| 2022 | frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node); |
| 2023 | if (frm->offset_node.key != qcs->rx.offset) |
| 2024 | break; |
| 2025 | |
| 2026 | ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm); |
| 2027 | qcs->rx.offset += ret; |
| 2028 | total += ret; |
| 2029 | if (frm->len) { |
| 2030 | /* If there is remaining data in this frame |
| 2031 | * this is because the destination buffer is full. |
| 2032 | */ |
| 2033 | break; |
| 2034 | } |
| 2035 | |
| 2036 | frm_node = eb64_next(frm_node); |
| 2037 | quic_rx_packet_refdec(frm->pkt); |
| 2038 | eb64_delete(&frm->offset_node); |
| 2039 | pool_free(pool_head_quic_rx_strm_frm, frm); |
| 2040 | } |
| 2041 | |
| 2042 | return total; |
| 2043 | } |
| 2044 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2045 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2046 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2047 | * If not, the STREAM frame is stored to be treated again later. |
| 2048 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2049 | * Return 1 if succeeded, 0 if not. |
| 2050 | */ |
| 2051 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2052 | struct quic_stream *strm_frm, |
| 2053 | struct quic_conn *qc) |
| 2054 | { |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2055 | int total; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2056 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2057 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2058 | struct quic_rx_strm_frm *frm; |
| 2059 | |
| 2060 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2061 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2062 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2063 | return 0; |
| 2064 | } |
| 2065 | |
| 2066 | strm = eb64_entry(&strm_node->node, struct qcs, by_id); |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2067 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2068 | size_t diff; |
| 2069 | |
| 2070 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2071 | TRACE_PROTO("Already received STREAM data", |
| 2072 | QUIC_EV_CONN_PSTRM, qc); |
| 2073 | goto out; |
| 2074 | } |
| 2075 | |
| 2076 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2077 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2078 | strm_frm->offset.key = strm->rx.offset; |
| 2079 | strm_frm->len -= diff; |
| 2080 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2081 | } |
| 2082 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2083 | total = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2084 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2085 | int ret; |
| 2086 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2087 | if (!qc_get_buf(strm, &strm->rx.buf)) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2088 | goto store_frm; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2089 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2090 | |
| 2091 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2092 | total += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2093 | strm->rx.offset += ret; |
| 2094 | } |
| 2095 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2096 | total += qc_treat_rx_strm_frms(strm); |
| 2097 | if (total && qc->qcc->app_ops->decode_qcs(strm, strm_frm->fin, qc->qcc->ctx) < 0) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2098 | TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2099 | return 0; |
| 2100 | } |
| 2101 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2102 | if (!strm_frm->len) |
| 2103 | goto out; |
| 2104 | |
| 2105 | store_frm: |
| 2106 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2107 | if (!frm) { |
| 2108 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2109 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2110 | return 0; |
| 2111 | } |
| 2112 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2113 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2114 | quic_rx_packet_refinc(pkt); |
| 2115 | |
| 2116 | out: |
| 2117 | return 1; |
| 2118 | } |
| 2119 | |
| 2120 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2121 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2122 | * If not, the STREAM frame is stored to be treated again later. |
| 2123 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2124 | * Return 1 if succeeded, 0 if not. |
| 2125 | */ |
| 2126 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2127 | struct quic_stream *strm_frm, |
| 2128 | struct quic_conn *qc) |
| 2129 | { |
| 2130 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2131 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2132 | struct quic_rx_strm_frm *frm; |
| 2133 | size_t strm_frm_len; |
| 2134 | |
| 2135 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2136 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2137 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2138 | return 0; |
| 2139 | } |
| 2140 | |
| 2141 | strm = eb64_entry(&strm_node->node, struct qcs, by_id); |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2142 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2143 | size_t diff; |
| 2144 | |
| 2145 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2146 | TRACE_PROTO("Already received STREAM data", |
| 2147 | QUIC_EV_CONN_PSTRM, qc); |
| 2148 | goto out; |
| 2149 | } |
| 2150 | |
| 2151 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2152 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2153 | strm_frm->offset.key = strm->rx.offset; |
| 2154 | strm_frm->len -= diff; |
| 2155 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2159 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2160 | int ret; |
| 2161 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 2162 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2163 | goto store_frm; |
| 2164 | |
| 2165 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 2166 | * of bytes copied. |
| 2167 | */ |
| 2168 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 2169 | /* Inform the application of the arrival of this new stream */ |
| 2170 | if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2171 | TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2172 | return 0; |
| 2173 | } |
| 2174 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2175 | if (ret) |
| 2176 | qcs_notify_recv(strm); |
| 2177 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2178 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2179 | } |
| 2180 | /* Take this frame into an account for the stream flow control */ |
| 2181 | strm->rx.offset += strm_frm_len; |
| 2182 | /* It all the data were provided to the application, there is no need to |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2183 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2184 | */ |
| 2185 | if (!strm_frm->len) |
| 2186 | goto out; |
| 2187 | |
| 2188 | store_frm: |
| 2189 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2190 | if (!frm) { |
| 2191 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2192 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2193 | return 0; |
| 2194 | } |
| 2195 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2196 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2197 | quic_rx_packet_refinc(pkt); |
| 2198 | |
| 2199 | out: |
| 2200 | return 1; |
| 2201 | } |
| 2202 | |
| 2203 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2204 | struct quic_stream *strm_frm, |
| 2205 | struct quic_conn *qc) |
| 2206 | { |
| 2207 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2208 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2209 | else |
| 2210 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2211 | } |
| 2212 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2213 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2214 | static void qc_prep_fast_retrans(struct quic_enc_level *qel, |
| 2215 | struct quic_conn *qc) |
| 2216 | { |
| 2217 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2218 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2219 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2220 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2221 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2222 | pkts = &qel->pktns->tx.pkts; |
| 2223 | node = eb64_first(pkts); |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2224 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2225 | while (node) { |
| 2226 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2227 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2228 | break; |
| 2229 | node = eb64_next(node); |
| 2230 | } |
| 2231 | |
| 2232 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2233 | return; |
| 2234 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 2235 | qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms); |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | /* Prepare a fast retransmission during handshake after a client |
| 2239 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2240 | * up to two datagrams of Initial packets if did not receive all Initial packets |
| 2241 | * and resend them coalescing with others (Handshake here). |
| 2242 | * (Listener only). |
| 2243 | */ |
| 2244 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc) |
| 2245 | { |
| 2246 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2247 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2248 | struct quic_frame *frm, *frmbak; |
| 2249 | |
| 2250 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2251 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2252 | struct quic_enc_level *qel = iqel; |
| 2253 | struct eb_root *pkts; |
| 2254 | struct eb64_node *node; |
| 2255 | struct quic_tx_packet *pkt; |
| 2256 | struct list *tmp = &itmp; |
| 2257 | |
| 2258 | start: |
| 2259 | pkt = NULL; |
| 2260 | pkts = &qel->pktns->tx.pkts; |
| 2261 | node = eb64_first(pkts); |
| 2262 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2263 | while (node) { |
| 2264 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2265 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2266 | break; |
| 2267 | node = eb64_next(node); |
| 2268 | } |
| 2269 | |
| 2270 | if (!pkt) |
| 2271 | goto end; |
| 2272 | |
| 2273 | qel->pktns->tx.pto_probe += 1; |
| 2274 | requeue: |
| 2275 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 2276 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2277 | LIST_DELETE(&frm->list); |
| 2278 | LIST_APPEND(tmp, &frm->list); |
| 2279 | } |
| 2280 | |
| 2281 | if (qel == iqel) { |
| 2282 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2283 | pkt = pkt->next; |
| 2284 | tmp = &htmp; |
| 2285 | hqel->pktns->tx.pto_probe += 1; |
| 2286 | goto requeue; |
| 2287 | } |
| 2288 | |
| 2289 | qel = hqel; |
| 2290 | tmp = &htmp; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2291 | goto start; |
| 2292 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2293 | |
| 2294 | end: |
| 2295 | LIST_SPLICE(&iqel->pktns->tx.frms, &itmp); |
| 2296 | LIST_SPLICE(&hqel->pktns->tx.frms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2297 | } |
| 2298 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2299 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2300 | * as I/O handler context and <qel> as encryption level. |
| 2301 | * Returns 1 if succeeded, 0 if failed. |
| 2302 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2303 | static int qc_parse_pkt_frms(struct quic_rx_packet *pkt, struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2304 | struct quic_enc_level *qel) |
| 2305 | { |
| 2306 | struct quic_frame frm; |
| 2307 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2308 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2309 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2310 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2311 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2312 | /* Skip the AAD */ |
| 2313 | pos = pkt->data + pkt->aad_len; |
| 2314 | end = pkt->data + pkt->len; |
| 2315 | |
| 2316 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2317 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2318 | goto err; |
| 2319 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2320 | TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2321 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2322 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2323 | break; |
| 2324 | case QUIC_FT_PING: |
| 2325 | break; |
| 2326 | case QUIC_FT_ACK: |
| 2327 | { |
| 2328 | unsigned int rtt_sample; |
| 2329 | |
| 2330 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2331 | if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2332 | goto err; |
| 2333 | |
| 2334 | if (rtt_sample) { |
| 2335 | unsigned int ack_delay; |
| 2336 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2337 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2338 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ? |
| 2339 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2340 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2341 | quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc); |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2342 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2343 | break; |
| 2344 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2345 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2346 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2347 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2348 | { |
| 2349 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2350 | |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 2351 | if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) { |
| 2352 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2353 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2354 | |
| 2355 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2356 | cfdebug.len = frm.crypto.len; |
| 2357 | TRACE_PROTO("CRYPTO data discarded", |
| 2358 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2359 | break; |
| 2360 | } |
| 2361 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2362 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2363 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2364 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2365 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2366 | |
| 2367 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2368 | cfdebug.len = frm.crypto.len; |
| 2369 | /* Nothing to do */ |
| 2370 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2371 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2372 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2373 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2374 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2375 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2376 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2377 | else { |
| 2378 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2379 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2380 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2381 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2382 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2383 | cfdebug.len = frm.crypto.len; |
| 2384 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2385 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2386 | frm.crypto.len -= diff; |
| 2387 | frm.crypto.data += diff; |
| 2388 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2389 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2390 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2391 | |
| 2392 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2393 | /* XXX TO DO: <cf> is used only for the traces. */ |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2394 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2395 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2396 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2397 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2398 | if (!qc_provide_cdata(qel, ctx, |
| 2399 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2400 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2401 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2402 | |
| 2403 | break; |
| 2404 | } |
| 2405 | |
| 2406 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2407 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2408 | if (!cf) { |
| 2409 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2410 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2411 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2412 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2413 | |
| 2414 | cf->offset_node.key = frm.crypto.offset; |
| 2415 | cf->len = frm.crypto.len; |
| 2416 | cf->data = frm.crypto.data; |
| 2417 | cf->pkt = pkt; |
| 2418 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2419 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2420 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2421 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2422 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2423 | { |
| 2424 | struct quic_stream *stream = &frm.stream; |
| 2425 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2426 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2427 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2428 | goto err; |
| 2429 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2430 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2431 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2432 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2433 | goto err; |
| 2434 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2435 | break; |
| 2436 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2437 | case QUIC_FT_MAX_DATA: |
| 2438 | case QUIC_FT_MAX_STREAM_DATA: |
| 2439 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2440 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2441 | case QUIC_FT_DATA_BLOCKED: |
| 2442 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2443 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2444 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2445 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2446 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2447 | break; |
| 2448 | case QUIC_FT_CONNECTION_CLOSE: |
| 2449 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Amaury Denoyelle | 5154e7a | 2021-12-08 14:51:04 +0100 | [diff] [blame] | 2450 | /* warn the mux to close the connection */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2451 | qc->qcc->flags |= QC_CF_CC_RECV; |
| 2452 | tasklet_wakeup(qc->qcc->wait_event.tasklet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2453 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2454 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2455 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2456 | goto err; |
| 2457 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2458 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2459 | break; |
| 2460 | default: |
| 2461 | goto err; |
| 2462 | } |
| 2463 | } |
| 2464 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2465 | if (fast_retrans) |
| 2466 | qc_prep_hdshk_fast_retrans(qc); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2467 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2468 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2469 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2470 | * be discarded. |
| 2471 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2472 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2473 | int state = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2474 | |
| 2475 | if (state >= QUIC_HS_ST_SERVER_INITIAL) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2476 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2477 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2478 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2479 | qc_set_timer(ctx->qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2480 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 2481 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2482 | if (state < QUIC_HS_ST_SERVER_HANDSHAKE) |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2483 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2484 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2485 | } |
| 2486 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2487 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2488 | return 1; |
| 2489 | |
| 2490 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2491 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2492 | return 0; |
| 2493 | } |
| 2494 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2495 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2496 | * buffer. This is the responsibility of the caller to check there is enough |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2497 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2498 | * This function must be called only after having built a correct datagram. |
| 2499 | * Always succeeds. |
| 2500 | */ |
| 2501 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2502 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2503 | { |
| 2504 | write_u16(cb_wr(cbuf), dglen); |
| 2505 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2506 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2507 | } |
| 2508 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2509 | /* Prepare as much as possible packets into <qr> ring buffer for |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2510 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2511 | * several packets in the same datagram. A header made of two fields is added |
| 2512 | * to each datagram: the datagram length followed by the address of the first |
| 2513 | * packet in this datagram. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2514 | * Returns 1 if succeeded, or 0 if something wrong happened. |
| 2515 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 2516 | static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr, |
| 2517 | enum quic_tls_enc_level tel, |
| 2518 | enum quic_tls_enc_level next_tel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2519 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2520 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2521 | struct cbuf *cbuf; |
| 2522 | unsigned char *end_buf, *end, *pos, *spos; |
| 2523 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2524 | /* length of datagrams */ |
| 2525 | uint16_t dglen; |
| 2526 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2527 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2528 | /* Each datagram is prepended with its length followed by the |
| 2529 | * address of the first packet in the datagram. |
| 2530 | */ |
| 2531 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2532 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2533 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2534 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2535 | total = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2536 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2537 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2538 | padding = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2539 | qel = &qc->els[tel]; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2540 | cbuf = qr->cbuf; |
| 2541 | spos = pos = cb_wr(cbuf); |
| 2542 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2543 | * to ensure there is enough room to mark the end of prepared |
| 2544 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2545 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2546 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2547 | first_pkt = prv_pkt = NULL; |
| 2548 | while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2549 | int err, probe, ack, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2550 | enum quic_pkt_type pkt_type; |
| 2551 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2552 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2553 | probe = ack = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 2554 | cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 2555 | if (!cc) { |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 2556 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2557 | ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2558 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2559 | /* Do not build any more packet if the TX secrets are not available or |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2560 | * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2561 | * and if there is no more packets to send upon PTO expiration |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2562 | * and if there is no more CRYPTO data available or in flight |
| 2563 | * congestion control limit is reached for prepared data |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2564 | */ |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2565 | if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) || |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2566 | (!cc && !ack && !probe && |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2567 | (LIST_ISEMPTY(&qel->pktns->tx.frms) || |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2568 | qc->path->prep_in_flight >= qc->path->cwnd))) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2569 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2570 | /* Set the current datagram as prepared into <cbuf> if |
| 2571 | * the was already a correct packet which was previously written. |
| 2572 | */ |
| 2573 | if (prv_pkt) |
| 2574 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2575 | break; |
| 2576 | } |
| 2577 | |
| 2578 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2579 | if (!prv_pkt) { |
| 2580 | /* Leave room for the datagram header */ |
| 2581 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2582 | if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) { |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 2583 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2584 | } |
| 2585 | else { |
| 2586 | end = pos + qc->path->mtu; |
| 2587 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2588 | } |
| 2589 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2590 | cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2591 | pkt_type, ack, probe, cc, &err); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2592 | /* Restore the PTO dgrams counter if a packet could not be built */ |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2593 | if (err < 0) { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2594 | if (ack) |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2595 | HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2596 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2597 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2598 | case -2: |
| 2599 | goto err; |
| 2600 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2601 | /* If there was already a correct packet present, set the |
| 2602 | * current datagram as prepared into <cbuf>. |
| 2603 | */ |
| 2604 | if (prv_pkt) { |
| 2605 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2606 | goto stop_build; |
| 2607 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2608 | goto out; |
| 2609 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2610 | break; |
| 2611 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2612 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2613 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2614 | if (!cur_pkt) |
| 2615 | goto err; |
| 2616 | |
| 2617 | total += cur_pkt->len; |
| 2618 | /* keep trace of the first packet in the datagram */ |
| 2619 | if (!first_pkt) |
| 2620 | first_pkt = cur_pkt; |
| 2621 | /* Attach the current one to the previous one */ |
| 2622 | if (prv_pkt) |
| 2623 | prv_pkt->next = cur_pkt; |
| 2624 | /* Let's say we have to build a new dgram */ |
| 2625 | prv_pkt = NULL; |
| 2626 | dglen += cur_pkt->len; |
| 2627 | /* Client: discard the Initial encryption keys as soon as |
| 2628 | * a handshake packet could be built. |
| 2629 | */ |
| 2630 | if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL && |
| 2631 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2632 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2633 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 2634 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2635 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2636 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 2637 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns); |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2638 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE); |
| 2639 | } |
| 2640 | /* If the data for the current encryption level have all been sent, |
| 2641 | * select the next level. |
| 2642 | */ |
| 2643 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2644 | (LIST_ISEMPTY(&qel->pktns->tx.frms))) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2645 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2646 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2647 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 2648 | tel = next_tel; |
| 2649 | qel = &qc->els[tel]; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2650 | if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2651 | /* If there is data for the next level, do not |
| 2652 | * consume a datagram. |
| 2653 | */ |
| 2654 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2655 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2656 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2657 | /* If we have to build a new datagram, set the current datagram as |
| 2658 | * prepared into <cbuf>. |
| 2659 | */ |
| 2660 | if (!prv_pkt) { |
| 2661 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2662 | first_pkt = NULL; |
| 2663 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2664 | padding = 0; |
| 2665 | } |
| 2666 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 2667 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2668 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2669 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | stop_build: |
| 2674 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2675 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
| 2676 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2677 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2678 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2679 | if (cb_contig_space(cbuf) >= sizeof(uint16_t)) { |
| 2680 | if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) { |
| 2681 | /* Mark the end of contiguous data for the reader */ |
| 2682 | write_u16(cb_wr(cbuf), 0); |
| 2683 | cb_add(cbuf, sizeof(uint16_t)); |
| 2684 | } |
| 2685 | } |
| 2686 | |
| 2687 | if (rd && rd <= cbuf->wr) { |
| 2688 | cb_wr_reset(cbuf); |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2689 | /* Let's try to reuse this buffer */ |
| 2690 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2695 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2696 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2697 | |
| 2698 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2699 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2700 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | /* Send the QUIC packets which have been prepared for QUIC connections |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2704 | * from <qr> ring buffer with <ctx> as I/O handler context. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2705 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2706 | int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2707 | { |
| 2708 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2709 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2710 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2711 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2712 | cbuf = qr->cbuf; |
| 2713 | while (cb_contig_data(cbuf)) { |
| 2714 | unsigned char *pos; |
| 2715 | struct buffer tmpbuf = { }; |
| 2716 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 2717 | uint16_t dglen; |
| 2718 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2719 | unsigned int time_sent; |
| 2720 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2721 | pos = cb_rd(cbuf); |
| 2722 | dglen = read_u16(pos); |
| 2723 | /* End of prepared datagrams. |
| 2724 | * Reset the reader index only if in front of the writer index. |
| 2725 | */ |
| 2726 | if (!dglen) { |
| 2727 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 2728 | |
| 2729 | if (wr && wr < cbuf->rd) { |
| 2730 | cb_rd_reset(cbuf); |
| 2731 | continue; |
| 2732 | } |
| 2733 | break; |
| 2734 | } |
| 2735 | |
| 2736 | pos += sizeof dglen; |
| 2737 | first_pkt = read_ptr(pos); |
| 2738 | pos += sizeof first_pkt; |
| 2739 | tmpbuf.area = (char *)pos; |
| 2740 | tmpbuf.size = tmpbuf.data = dglen; |
| 2741 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2742 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2743 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2744 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2745 | if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx, |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2746 | &tmpbuf, tmpbuf.data, 0) <= 0) { |
| 2747 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2748 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2749 | break; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2750 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2751 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2752 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2753 | qc->tx.bytes += tmpbuf.data; |
| 2754 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2755 | |
| 2756 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 2757 | pkt->time_sent = time_sent; |
| 2758 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 2759 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 2760 | qc->path->ifae_pkts++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2761 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2762 | qc->path->in_flight += pkt->in_flight_len; |
| 2763 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 2764 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2765 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2766 | TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2767 | next_pkt = pkt->next; |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 2768 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2769 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2770 | } |
| 2771 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2772 | |
| 2773 | return 1; |
| 2774 | } |
| 2775 | |
| 2776 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 2777 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 2778 | * a HANDSHAKE_DONE frame. |
| 2779 | * Return 1 if succeeded, 0 if not. |
| 2780 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2781 | static int quic_build_post_handshake_frames(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2782 | { |
| 2783 | int i; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2784 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2785 | struct quic_frame *frm; |
| 2786 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2787 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2788 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 2789 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2790 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 2791 | if (!frm) |
| 2792 | return 0; |
| 2793 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2794 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2795 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2796 | } |
| 2797 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2798 | for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2799 | struct quic_connection_id *cid; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2800 | struct listener *l = __objt_listener(qc->conn->target); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2801 | |
| 2802 | frm = pool_alloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2803 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2804 | goto err; |
| 2805 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2806 | cid = new_quic_cid(&qc->cids, qc, i); |
| 2807 | if (!cid) |
| 2808 | goto err; |
| 2809 | |
| 2810 | /* insert the allocated CID in the receiver tree */ |
| 2811 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2812 | ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len); |
| 2813 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2814 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2815 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2816 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2817 | } |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 2818 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2819 | |
| 2820 | return 1; |
| 2821 | |
| 2822 | err: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2823 | return 0; |
| 2824 | } |
| 2825 | |
| 2826 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2827 | void free_quic_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2828 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2829 | struct eb64_node *n; |
| 2830 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2831 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2832 | n = eb64_first(&arngs->root); |
| 2833 | while (n) { |
| 2834 | struct eb64_node *next; |
| 2835 | |
| 2836 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 2837 | next = eb64_next(n); |
| 2838 | eb64_delete(n); |
| 2839 | free(ar); |
| 2840 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2841 | } |
| 2842 | } |
| 2843 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2844 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 2845 | * descending order. |
| 2846 | */ |
| 2847 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 2848 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2849 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2850 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2851 | } |
| 2852 | |
| 2853 | |
| 2854 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 2855 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2856 | * Returns 1 if succeeded, 0 if not (no more element to remove). |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2857 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2858 | static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2859 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2860 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2861 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2862 | last = eb64_last(&arngs->root); |
| 2863 | while (last && arngs->enc_sz > limit) { |
| 2864 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2865 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2866 | prev = eb64_prev(last); |
| 2867 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2868 | return 0; |
| 2869 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2870 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 2871 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 2872 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 2873 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 2874 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 2875 | --arngs->sz; |
| 2876 | eb64_delete(last); |
| 2877 | pool_free(pool_head_quic_arng, last); |
| 2878 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | return 1; |
| 2882 | } |
| 2883 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2884 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 2885 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 2886 | { |
| 2887 | struct eb64_node *node, *next; |
| 2888 | struct quic_arng_node *ar, *ar_next; |
| 2889 | |
| 2890 | node = eb64_last(&arngs->root); |
| 2891 | if (!node) |
| 2892 | return; |
| 2893 | |
| 2894 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2895 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 2896 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 2897 | |
| 2898 | while ((next = eb64_prev(node))) { |
| 2899 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 2900 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 2901 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 2902 | node = next; |
| 2903 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2904 | } |
| 2905 | } |
| 2906 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2907 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 2908 | * Returns the ack range node which has been inserted if succeeded, NULL if not. |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2909 | */ |
| 2910 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2911 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 2912 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2913 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2914 | struct quic_arng_node *new_ar; |
| 2915 | |
| 2916 | new_ar = pool_alloc(pool_head_quic_arng); |
| 2917 | if (new_ar) { |
| 2918 | new_ar->first.key = ar->first; |
| 2919 | new_ar->last = ar->last; |
| 2920 | eb64_insert(&arngs->root, &new_ar->first); |
| 2921 | arngs->sz++; |
| 2922 | } |
| 2923 | |
| 2924 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | /* Update <arngs> tree of ACK ranges with <ar> as new ACK range value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2928 | * Note that this function computes the number of bytes required to encode |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2929 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2930 | * |
| 2931 | * Descending order |
| 2932 | * -------------> |
| 2933 | * range1 range2 |
| 2934 | * ..........|--------|..............|--------| |
| 2935 | * ^ ^ ^ ^ |
| 2936 | * | | | | |
| 2937 | * last1 first1 last2 first2 |
| 2938 | * ..........+--------+--------------+--------+...... |
| 2939 | * diff1 gap12 diff2 |
| 2940 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2941 | * To encode the previous list of ranges we must encode integers as follows in |
| 2942 | * descending order: |
| 2943 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2944 | * with diff1 = last1 - first1 |
| 2945 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2946 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2947 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2948 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2949 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 2950 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2951 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2952 | struct eb64_node *le; |
| 2953 | struct quic_arng_node *new_node; |
| 2954 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2955 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2956 | new = NULL; |
| 2957 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2958 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2959 | if (!new_node) |
| 2960 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2961 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2962 | goto out; |
| 2963 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2964 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2965 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 2966 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2967 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2968 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2969 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 2970 | |
| 2971 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2972 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2973 | else { |
| 2974 | struct quic_arng_node *le_ar = |
| 2975 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2976 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2977 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 2978 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2979 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2980 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2981 | if (le_ar->last + 1 >= ar->first) { |
| 2982 | le_ar->last = ar->last; |
| 2983 | new = le; |
| 2984 | new_node = le_ar; |
| 2985 | } |
| 2986 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2987 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2988 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2989 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 2990 | |
| 2991 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2992 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2993 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2994 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2995 | /* Verify that the new inserted node does not overlap the nodes |
| 2996 | * which follow it. |
| 2997 | */ |
| 2998 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2999 | struct eb64_node *next; |
| 3000 | struct quic_arng_node *next_node; |
| 3001 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3002 | while ((next = eb64_next(new))) { |
| 3003 | next_node = |
| 3004 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3005 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3006 | break; |
| 3007 | |
| 3008 | if (next_node->last > new_node->last) |
| 3009 | new_node->last = next_node->last; |
| 3010 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3011 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3012 | /* Decrement the size of these ranges. */ |
| 3013 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3014 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3015 | } |
| 3016 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3017 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3018 | quic_arngs_set_enc_sz(arngs); |
| 3019 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3020 | return 1; |
| 3021 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3022 | /* Remove the header protection of packets at <el> encryption level. |
| 3023 | * Always succeeds. |
| 3024 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3025 | static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3026 | { |
| 3027 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3028 | struct quic_rx_packet *pqpkt; |
| 3029 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3030 | struct quic_enc_level *app_qel; |
| 3031 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3032 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3033 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3034 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 3035 | if (el == app_qel && qc_is_listener(qc) && |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3036 | HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3037 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3038 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3039 | goto out; |
| 3040 | } |
| 3041 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3042 | mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3043 | if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3044 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3045 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3046 | TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3047 | /* XXX TO DO XXX */ |
| 3048 | } |
| 3049 | else { |
| 3050 | /* The AAD includes the packet number field */ |
| 3051 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3052 | /* Store the packet into the tree of packets to decrypt. */ |
| 3053 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3054 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3055 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3056 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3057 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3058 | TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3059 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3060 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3061 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3065 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3069 | * Return 1 if succeeded, 0 if not. |
| 3070 | */ |
| 3071 | static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3072 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3073 | { |
| 3074 | struct eb64_node *node; |
| 3075 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3076 | node = eb64_first(&el->rx.crypto.frms); |
| 3077 | while (node) { |
| 3078 | struct quic_rx_crypto_frm *cf; |
| 3079 | |
| 3080 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 3081 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3082 | break; |
| 3083 | |
| 3084 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3085 | goto err; |
| 3086 | |
| 3087 | node = eb64_next(node); |
| 3088 | quic_rx_packet_refdec(cf->pkt); |
| 3089 | eb64_delete(&cf->offset_node); |
| 3090 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3091 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3092 | return 1; |
| 3093 | |
| 3094 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3095 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3096 | return 0; |
| 3097 | } |
| 3098 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3099 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3100 | * This is the caller responsibility to check that <cur_el> is different of <next_el> |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3101 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3102 | * Return 1 if succeeded, 0 if not. |
| 3103 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3104 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3105 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3106 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3107 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3108 | int64_t largest_pn = -1; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3109 | struct quic_conn *qc = ctx->conn->qc; |
| 3110 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3111 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3112 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3113 | qel = cur_el; |
| 3114 | next_tel: |
| 3115 | if (!qel) |
| 3116 | goto out; |
| 3117 | |
| 3118 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3119 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3120 | while (node) { |
| 3121 | struct quic_rx_packet *pkt; |
| 3122 | |
| 3123 | pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3124 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3125 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3126 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3127 | /* Drop the packet */ |
| 3128 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3129 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3130 | } |
| 3131 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3132 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3133 | /* Drop the packet */ |
| 3134 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3135 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3136 | } |
| 3137 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3138 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3139 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3140 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING && |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3141 | (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack)) |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 3142 | HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3143 | if (pkt->pn > largest_pn) |
| 3144 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3145 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3146 | if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3147 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3148 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3149 | } |
| 3150 | } |
| 3151 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3152 | eb64_delete(&pkt->pn_node); |
| 3153 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3154 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3155 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3156 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3157 | /* Update the largest packet number. */ |
| 3158 | if (largest_pn != -1) |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3159 | HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn); |
| 3160 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3161 | goto err; |
| 3162 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3163 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3164 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3165 | qel = next_el; |
| 3166 | goto next_tel; |
| 3167 | } |
| 3168 | |
| 3169 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3170 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3171 | return 1; |
| 3172 | |
| 3173 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3174 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3175 | return 0; |
| 3176 | } |
| 3177 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3178 | /* QUIC connection packet handler task. */ |
| 3179 | struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3180 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3181 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3182 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3183 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3184 | enum quic_tls_enc_level tel, next_tel; |
| 3185 | struct quic_enc_level *qel, *next_qel; |
| 3186 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3187 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3188 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3189 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3190 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3191 | qc = ctx->qc; |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3192 | TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3193 | qr = NULL; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3194 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3195 | TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3196 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3197 | HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT); |
| 3198 | /* The I/O handler has been woken up by the dgram listener |
| 3199 | * after the anti-amplification was reached. |
| 3200 | */ |
| 3201 | qc_set_timer(qc); |
| 3202 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3203 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3204 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3205 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3206 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3207 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3208 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA])); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3209 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3210 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3211 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3212 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3213 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3214 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3215 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3216 | } |
| 3217 | else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3218 | goto err; |
| 3219 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3220 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3221 | next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3222 | |
| 3223 | next_level: |
| 3224 | tls_ctx = &qel->tls_ctx; |
| 3225 | |
| 3226 | /* If the header protection key for this level has been derived, |
| 3227 | * remove the packet header protections. |
| 3228 | */ |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3229 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3230 | (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3231 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3232 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3233 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3234 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3235 | if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3236 | goto err; |
| 3237 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3238 | if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) && |
| 3239 | (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3240 | qel = next_qel; |
| 3241 | next_qel = NULL; |
| 3242 | goto next_level; |
| 3243 | } |
| 3244 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3245 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3246 | if (st >= QUIC_HS_ST_COMPLETE) { |
| 3247 | if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) && |
| 3248 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3249 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3250 | |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3251 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags & |
| 3252 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3253 | /* Discard the Handshake keys. */ |
| 3254 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3255 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3256 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3257 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3258 | qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 3259 | qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 3263 | /* There may be remaining handshake to build (acks) */ |
| 3264 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3265 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3266 | } |
| 3267 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3268 | if (!qr) |
| 3269 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
Frédéric Lécaille | bec186d | 2022-01-12 15:32:55 +0100 | [diff] [blame] | 3270 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 3271 | * be considered. |
| 3272 | */ |
| 3273 | if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0)) |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 3274 | goto err; |
| 3275 | ret = qc_prep_pkts(qc, qr, tel, next_tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3276 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3277 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3278 | else if (ret == 0) |
| 3279 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3280 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3281 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3282 | goto err; |
| 3283 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3284 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3285 | /* Check if there is something to do for the next level. |
| 3286 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3287 | if (next_qel && next_qel != qel && |
| 3288 | (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) && |
Frédéric Lécaille | 7d807c9 | 2021-12-06 08:56:38 +0100 | [diff] [blame] | 3289 | (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3290 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3291 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3292 | goto next_level; |
| 3293 | } |
| 3294 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3295 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3296 | TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3297 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3298 | |
| 3299 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3300 | if (qr) |
| 3301 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3302 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3303 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3304 | } |
| 3305 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3306 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3307 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 3308 | { |
| 3309 | int i; |
| 3310 | |
| 3311 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 3312 | if (qel->tx.crypto.bufs[i]) { |
| 3313 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 3314 | qel->tx.crypto.bufs[i] = NULL; |
| 3315 | } |
| 3316 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3317 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3321 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3322 | * Returns 1 if succeeded, 0 if not. |
| 3323 | */ |
| 3324 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 3325 | enum quic_tls_enc_level level) |
| 3326 | { |
| 3327 | struct quic_enc_level *qel; |
| 3328 | |
| 3329 | qel = &qc->els[level]; |
| 3330 | qel->level = quic_to_ssl_enc_level(level); |
| 3331 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 3332 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 3333 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 3334 | qel->tls_ctx.rx.flags = 0; |
| 3335 | qel->tls_ctx.tx.flags = 0; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 3336 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3337 | |
| 3338 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3339 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3340 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 3341 | qel->rx.crypto.offset = 0; |
| 3342 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3343 | |
| 3344 | /* Allocate only one buffer. */ |
| 3345 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 3346 | if (!qel->tx.crypto.bufs) |
| 3347 | goto err; |
| 3348 | |
| 3349 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 3350 | if (!qel->tx.crypto.bufs[0]) |
| 3351 | goto err; |
| 3352 | |
| 3353 | qel->tx.crypto.bufs[0]->sz = 0; |
| 3354 | qel->tx.crypto.nb_buf = 1; |
| 3355 | |
| 3356 | qel->tx.crypto.sz = 0; |
| 3357 | qel->tx.crypto.offset = 0; |
| 3358 | |
| 3359 | return 1; |
| 3360 | |
| 3361 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3362 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3363 | return 0; |
| 3364 | } |
| 3365 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3366 | /* Increment the <qc> refcount. |
| 3367 | * |
| 3368 | * This operation must be conducted when manipulating the quic_conn outside of |
| 3369 | * the connection pinned thread. These threads can only retrieve the connection |
| 3370 | * in the CID tree, so this function must be conducted under the CID lock. |
| 3371 | */ |
| 3372 | static inline void quic_conn_take(struct quic_conn *qc) |
| 3373 | { |
| 3374 | HA_ATOMIC_INC(&qc->refcount); |
| 3375 | } |
| 3376 | |
| 3377 | /* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the |
Ilya Shipitsin | 37d3e38 | 2022-01-07 14:46:15 +0500 | [diff] [blame] | 3378 | * subtraction, the quic_conn is freed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3379 | */ |
| 3380 | static void quic_conn_drop(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3381 | { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3382 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3383 | int i; |
| 3384 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3385 | if (!qc) |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3386 | return; |
| 3387 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame^] | 3388 | if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1)) |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3389 | return; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3390 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3391 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
Amaury Denoyelle | 9ab2fb3 | 2022-01-12 14:54:23 +0100 | [diff] [blame] | 3392 | if (conn_ctx) |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3393 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3394 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3395 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3396 | quic_conn_enc_level_uninit(&qc->els[i]); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3397 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3398 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 3399 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3400 | TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3401 | } |
| 3402 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame^] | 3403 | /* Release the quic_conn <qc>. It will decrement its refcount so that the |
| 3404 | * connection will be freed once all threads have finished to work with it. The |
| 3405 | * connection is removed from the CIDs tree and thus cannot be found by other |
| 3406 | * threads after it. |
| 3407 | * |
| 3408 | * Do not use <qc> after it as it may be freed. This function must only be |
| 3409 | * called by the thread responsible of the quic_conn tasklet. |
| 3410 | */ |
| 3411 | static void quic_conn_release(struct quic_conn *qc) |
| 3412 | { |
| 3413 | /* remove the connection from receiver cids trees */ |
| 3414 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
| 3415 | ebmb_delete(&qc->odcid_node); |
| 3416 | ebmb_delete(&qc->scid_node); |
| 3417 | free_quic_conn_cids(qc); |
| 3418 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
| 3419 | |
| 3420 | quic_conn_drop(qc); |
| 3421 | } |
| 3422 | |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3423 | void quic_close(struct connection *conn, void *xprt_ctx) |
| 3424 | { |
| 3425 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
| 3426 | struct quic_conn *qc = conn_ctx->conn->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3427 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3428 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3429 | /* This task must be deleted by the connection-pinned thread. */ |
| 3430 | if (qc->timer_task) { |
| 3431 | task_destroy(qc->timer_task); |
| 3432 | qc->timer_task = NULL; |
| 3433 | } |
| 3434 | |
Amaury Denoyelle | 9ab2fb3 | 2022-01-12 14:54:23 +0100 | [diff] [blame] | 3435 | if (conn_ctx->wait_event.tasklet) { |
| 3436 | tasklet_free(conn_ctx->wait_event.tasklet); |
| 3437 | conn_ctx->wait_event.tasklet = NULL; |
| 3438 | } |
| 3439 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3440 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 9c4da93 | 2022-01-21 14:54:58 +0100 | [diff] [blame] | 3441 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame^] | 3442 | /* TODO for now release the quic_conn on notification by the upper |
| 3443 | * layer. It could be useful to delay it if there is remaining data to |
| 3444 | * send or data to be acked. |
| 3445 | */ |
| 3446 | quic_conn_release(qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3447 | } |
| 3448 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3449 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3450 | static struct task *process_timer(struct task *task, void *ctx, unsigned int state) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3451 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3452 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3453 | struct quic_conn *qc; |
| 3454 | struct quic_pktns *pktns; |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3455 | int i, st; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3456 | |
| 3457 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3458 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3459 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3460 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3461 | task->expire = TICK_ETERNITY; |
| 3462 | pktns = quic_loss_pktns(qc); |
| 3463 | if (tick_isset(pktns->tx.loss_time)) { |
| 3464 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 3465 | |
| 3466 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 3467 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3468 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 3469 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3470 | goto out; |
| 3471 | } |
| 3472 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3473 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3474 | if (qc->path->in_flight) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3475 | pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL); |
Frédéric Lécaille | 0fa553d | 2022-01-17 14:26:12 +0100 | [diff] [blame] | 3476 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 3477 | pktns->tx.pto_probe = 1; |
| 3478 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
| 3479 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1; |
| 3480 | } |
| 3481 | else { |
| 3482 | pktns->tx.pto_probe = 2; |
| 3483 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3484 | } |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3485 | else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3486 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3487 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3488 | |
| 3489 | if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3490 | hel->pktns->tx.pto_probe = 1; |
| 3491 | if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3492 | iel->pktns->tx.pto_probe = 1; |
| 3493 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3494 | |
| 3495 | for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 3496 | int j; |
| 3497 | |
| 3498 | if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc)) |
| 3499 | continue; |
| 3500 | |
| 3501 | for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++) |
| 3502 | qc_prep_fast_retrans(&qc->els[i], qc); |
| 3503 | } |
| 3504 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3505 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3506 | qc->path->loss.pto_count++; |
| 3507 | |
| 3508 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3509 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3510 | |
| 3511 | return task; |
| 3512 | } |
| 3513 | |
| 3514 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 3515 | * connections used to identify the first Initial packets of client connecting |
| 3516 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 3517 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 3518 | * <scid> is the source connection ID with <scid_len> as length. |
| 3519 | * Returns 1 if succeeded, 0 if not. |
| 3520 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3521 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3522 | unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3523 | unsigned char *scid, size_t scid_len, int server, void *owner) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3524 | { |
| 3525 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3526 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3527 | /* Initial CID. */ |
| 3528 | struct quic_connection_id *icid; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3529 | char *buf_area; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3530 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3531 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3532 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3533 | qc = pool_zalloc(pool_head_quic_conn); |
| 3534 | if (!qc) { |
| 3535 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 3536 | goto err; |
| 3537 | } |
| 3538 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3539 | HA_ATOMIC_STORE(&qc->refcount, 0); |
| 3540 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3541 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 3542 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3543 | TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3544 | goto err; |
| 3545 | } |
| 3546 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3547 | qc->cids = EB_ROOT; |
| 3548 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3549 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3550 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3551 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 3552 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3553 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL); |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3554 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3555 | qc->odcid.len = dcid_len; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3556 | qc->odcid.addrlen = dcid_addr_len; |
| 3557 | memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3558 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 3559 | /* copy the packet SCID to reuse it as DCID for sending */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3560 | if (scid_len) |
| 3561 | memcpy(qc->dcid.data, scid, scid_len); |
| 3562 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 3563 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3564 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3565 | } |
| 3566 | /* QUIC Client (outgoing connection to servers) */ |
| 3567 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3568 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3569 | if (dcid_len) |
| 3570 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 3571 | qc->dcid.len = dcid_len; |
| 3572 | } |
| 3573 | |
| 3574 | /* Initialize the output buffer */ |
| 3575 | qc->obuf.pos = qc->obuf.data; |
| 3576 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3577 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3578 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3579 | TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3580 | goto err; |
| 3581 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3582 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3583 | /* insert the allocated CID in the receiver tree */ |
| 3584 | if (server) { |
| 3585 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3586 | ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len); |
| 3587 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3588 | } |
| 3589 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3590 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 3591 | qc->scid = icid->cid; |
| 3592 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3593 | /* Packet number spaces initialization. */ |
| 3594 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 3595 | quic_pktns_init(&qc->pktns[i]); |
| 3596 | /* QUIC encryption level context initialization. */ |
| 3597 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3598 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3599 | TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3600 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3601 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3602 | /* Initialize the packet number space. */ |
| 3603 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 3604 | } |
| 3605 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 3606 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 3607 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 3608 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 3609 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3610 | /* TX part. */ |
| 3611 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3612 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 3613 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 3614 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3615 | /* RX part. */ |
| 3616 | qc->rx.bytes = 0; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3617 | qc->rx.nb_ack_eliciting = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3618 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 3619 | HA_RWLOCK_INIT(&qc->rx.buf_rwlock); |
| 3620 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 3621 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3622 | TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 3623 | goto err; |
| 3624 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3625 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3626 | /* XXX TO DO: Only one path at this time. */ |
| 3627 | qc->path = &qc->paths[0]; |
| 3628 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 3629 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3630 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3631 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3632 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3633 | |
| 3634 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3635 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3636 | quic_conn_drop(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3637 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | /* Initialize the timer task of <qc> QUIC connection. |
| 3641 | * Returns 1 if succeeded, 0 if not. |
| 3642 | */ |
| 3643 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 3644 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 3645 | /* Attach this task to the same thread ID used for the connection */ |
| 3646 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3647 | if (!qc->timer_task) |
| 3648 | return 0; |
| 3649 | |
| 3650 | qc->timer = TICK_ETERNITY; |
| 3651 | qc->timer_task->process = process_timer; |
| 3652 | qc->timer_task->context = qc->conn->xprt_ctx; |
| 3653 | |
| 3654 | return 1; |
| 3655 | } |
| 3656 | |
| 3657 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 3658 | * past one byte of this buffer. |
| 3659 | */ |
| 3660 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 3661 | struct quic_rx_packet *pkt) |
| 3662 | { |
| 3663 | unsigned char dcid_len, scid_len; |
| 3664 | |
| 3665 | /* Version */ |
| 3666 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 3667 | return 0; |
| 3668 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3669 | /* Destination Connection ID Length */ |
| 3670 | dcid_len = *(*buf)++; |
| 3671 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 3672 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 3673 | /* XXX MUST BE DROPPED */ |
| 3674 | return 0; |
| 3675 | |
| 3676 | if (dcid_len) { |
| 3677 | /* Check that the length of this received DCID matches the CID lengths |
| 3678 | * of our implementation for non Initials packets only. |
| 3679 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3680 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 3681 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3682 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3683 | return 0; |
| 3684 | |
| 3685 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 3686 | } |
| 3687 | |
| 3688 | pkt->dcid.len = dcid_len; |
| 3689 | *buf += dcid_len; |
| 3690 | |
| 3691 | /* Source Connection ID Length */ |
| 3692 | scid_len = *(*buf)++; |
| 3693 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 3694 | /* XXX MUST BE DROPPED */ |
| 3695 | return 0; |
| 3696 | |
| 3697 | if (scid_len) |
| 3698 | memcpy(pkt->scid.data, *buf, scid_len); |
| 3699 | pkt->scid.len = scid_len; |
| 3700 | *buf += scid_len; |
| 3701 | |
| 3702 | return 1; |
| 3703 | } |
| 3704 | |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3705 | /* If the header protection of <pkt> packet attached to <qc> connection with <ctx> |
| 3706 | * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated |
| 3707 | * encryption level matching with the packet type. <*qel> may be null if not found. |
| 3708 | * Note that <ctx> may be null (for Initial packets). |
| 3709 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3710 | static int qc_pkt_may_rm_hp(struct quic_conn *qc, struct quic_rx_packet *pkt, |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3711 | struct quic_enc_level **qel) |
| 3712 | { |
| 3713 | enum quic_tls_enc_level tel; |
| 3714 | |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3715 | tel = quic_packet_type_enc_level(pkt->type); |
| 3716 | if (tel == QUIC_TLS_ENC_LEVEL_NONE) { |
| 3717 | *qel = NULL; |
| 3718 | return 0; |
| 3719 | } |
| 3720 | |
| 3721 | *qel = &qc->els[tel]; |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3722 | if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3723 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3724 | |
| 3725 | if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) && |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3726 | (tel != QUIC_TLS_ENC_LEVEL_APP || |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3727 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)) |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3728 | return 1; |
| 3729 | |
| 3730 | return 0; |
| 3731 | } |
| 3732 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3733 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 3734 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 3735 | { |
| 3736 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 3737 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3738 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3739 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 3740 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | /* Try to remove the header protection of <pkt> QUIC packet attached to <qc> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3744 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 3745 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 3746 | * the packet first byte. |
| 3747 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 3748 | * Returns 1 if succeeded, 0 if not. |
| 3749 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3750 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 3751 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3752 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3753 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3754 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3755 | { |
| 3756 | unsigned char *pn = NULL; /* Packet number field */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3757 | struct quic_enc_level *qel; |
| 3758 | /* Only for traces. */ |
| 3759 | struct quic_rx_packet *qpkt_trace; |
| 3760 | |
| 3761 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3762 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3763 | /* The packet number is here. This is also the start minus |
| 3764 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 3765 | * protection. |
| 3766 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3767 | pn = buf; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3768 | if (qc_pkt_may_rm_hp(qc, pkt, &qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3769 | /* Note that the following function enables us to unprotect the packet |
| 3770 | * number and its length subsequently used to decrypt the entire |
| 3771 | * packets. |
| 3772 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3773 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 3774 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 3775 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3776 | goto err; |
| 3777 | } |
| 3778 | |
| 3779 | /* The AAD includes the packet number field found at <pn>. */ |
| 3780 | pkt->aad_len = pn - beg + pkt->pnl; |
| 3781 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3782 | } |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3783 | else if (qel) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3784 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 3785 | /* If the packet number space has been discarded, this packet |
| 3786 | * will be not parsed. |
| 3787 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3788 | TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3789 | goto out; |
| 3790 | } |
| 3791 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3792 | TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3793 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3794 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 3795 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3796 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3797 | else { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3798 | TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3799 | goto err; |
| 3800 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3801 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3802 | *el = qel; |
| 3803 | /* No reference counter incrementation here!!! */ |
| 3804 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 3805 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 3806 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 3807 | b_add(&qc->rx.buf, pkt->len); |
| 3808 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3809 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3810 | return 1; |
| 3811 | |
| 3812 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3813 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3814 | return 0; |
| 3815 | } |
| 3816 | |
| 3817 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 3818 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 3819 | */ |
| 3820 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 3821 | unsigned char byte0, int *long_header) |
| 3822 | { |
| 3823 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 3824 | pkt->type = |
| 3825 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 3826 | *long_header = 1; |
| 3827 | } |
| 3828 | else { |
| 3829 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 3830 | *long_header = 0; |
| 3831 | } |
| 3832 | } |
| 3833 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3834 | /* |
| 3835 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 3836 | */ |
| 3837 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 3838 | { |
| 3839 | int j = 0, version; |
| 3840 | |
| 3841 | do { |
| 3842 | version = quic_supported_version[j]; |
| 3843 | if (version == pkt->version) |
| 3844 | return 1; |
| 3845 | |
| 3846 | version = quic_supported_version[++j]; |
| 3847 | } while(version); |
| 3848 | |
| 3849 | return 0; |
| 3850 | } |
| 3851 | |
Frédéric Lécaille | c5c69a0 | 2021-10-20 17:24:42 +0200 | [diff] [blame] | 3852 | __attribute__((unused)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3853 | static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end, |
| 3854 | struct quic_rx_packet *pkt, |
| 3855 | struct quic_dgram_ctx *dgram_ctx, |
| 3856 | struct sockaddr_storage *saddr) |
| 3857 | { |
| 3858 | unsigned char *beg; |
| 3859 | uint64_t len; |
| 3860 | struct quic_conn *qc; |
| 3861 | struct eb_root *cids; |
| 3862 | struct ebmb_node *node; |
| 3863 | struct connection *srv_conn; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3864 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3865 | int long_header; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3866 | size_t b_cspace; |
| 3867 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3868 | |
| 3869 | qc = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 3870 | qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3871 | TRACE_ENTER(QUIC_EV_CONN_SPKT); |
| 3872 | if (end <= *buf) |
| 3873 | goto err; |
| 3874 | |
| 3875 | /* Fixed bit */ |
| 3876 | if (!(**buf & QUIC_PACKET_FIXED_BIT)) |
| 3877 | /* XXX TO BE DISCARDED */ |
| 3878 | goto err; |
| 3879 | |
| 3880 | srv_conn = dgram_ctx->owner; |
| 3881 | beg = *buf; |
| 3882 | /* Header form */ |
| 3883 | qc_parse_hd_form(pkt, *(*buf)++, &long_header); |
| 3884 | if (long_header) { |
| 3885 | size_t cid_lookup_len; |
| 3886 | |
| 3887 | if (!quic_packet_read_long_header(buf, end, pkt)) |
| 3888 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3889 | |
| 3890 | /* unsupported QUIC version */ |
| 3891 | if (!qc_pkt_is_supported_version(pkt)) { |
| 3892 | TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT); |
| 3893 | goto err; |
| 3894 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3895 | |
| 3896 | /* For Initial packets, and for servers (QUIC clients connections), |
| 3897 | * there is no Initial connection IDs storage. |
| 3898 | */ |
| 3899 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3900 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
| 3901 | cid_lookup_len = pkt->dcid.len; |
| 3902 | } |
| 3903 | else { |
| 3904 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3905 | cid_lookup_len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len); |
| 3909 | if (!node) |
| 3910 | goto err; |
| 3911 | |
| 3912 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
| 3913 | |
| 3914 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3915 | qc->dcid.len = pkt->scid.len; |
| 3916 | if (pkt->scid.len) |
| 3917 | memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len); |
| 3918 | } |
| 3919 | |
| 3920 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3921 | uint64_t token_len; |
| 3922 | |
| 3923 | if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len) |
| 3924 | goto err; |
| 3925 | |
| 3926 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 3927 | * A server which sends an Initial packet must not set the token. |
| 3928 | * So, a client which receives an Initial packet with a token |
| 3929 | * MUST discard the packet or generate a connection error with |
| 3930 | * PROTOCOL_VIOLATION as type. |
| 3931 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 3932 | */ |
| 3933 | pkt->token_len = token_len; |
| 3934 | } |
| 3935 | } |
| 3936 | else { |
| 3937 | /* XXX TO DO: Short header XXX */ |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3938 | if (end - *buf < QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3939 | goto err; |
| 3940 | |
| 3941 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3942 | node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3943 | if (!node) |
| 3944 | goto err; |
| 3945 | |
| 3946 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3947 | *buf += QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3948 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3949 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3950 | /* Only packets packets with long headers and not RETRY or VERSION as type |
| 3951 | * have a length field. |
| 3952 | */ |
| 3953 | if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) { |
| 3954 | if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len) |
| 3955 | goto err; |
| 3956 | |
| 3957 | pkt->len = len; |
| 3958 | } |
| 3959 | else if (!long_header) { |
| 3960 | /* A short packet is the last one of an UDP datagram. */ |
| 3961 | pkt->len = end - *buf; |
| 3962 | } |
| 3963 | |
| 3964 | conn_ctx = qc->conn->xprt_ctx; |
| 3965 | |
| 3966 | /* Increase the total length of this packet by the header length. */ |
| 3967 | pkt->len += *buf - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3968 | |
| 3969 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 3970 | * they must have the same DCID. |
| 3971 | * |
| 3972 | * This check must be done after the final update to pkt.len to |
| 3973 | * properly drop the packet on failure. |
| 3974 | */ |
| 3975 | if (!dgram_ctx->dcid.len) { |
| 3976 | memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len); |
| 3977 | } |
| 3978 | else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3979 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3980 | goto err; |
| 3981 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3982 | dgram_ctx->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3983 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3984 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3985 | b_cspace = b_contig_space(&qc->rx.buf); |
| 3986 | if (b_cspace < pkt->len) { |
| 3987 | /* Let us consume the remaining contiguous space. */ |
| 3988 | b_add(&qc->rx.buf, b_cspace); |
| 3989 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 3990 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3991 | TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3992 | goto err; |
| 3993 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3994 | } |
| 3995 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3996 | if (!qc_try_rm_hp(qc, pkt, *buf, beg, end, &qel)) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3997 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3998 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3999 | goto err; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4000 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4001 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4002 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 4003 | if (pkt->aad_len) |
| 4004 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4005 | /* Wake the tasklet of the QUIC connection packet handler. */ |
| 4006 | if (conn_ctx) |
| 4007 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4008 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4009 | TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4010 | |
| 4011 | return pkt->len; |
| 4012 | |
| 4013 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4014 | TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4015 | return -1; |
| 4016 | } |
| 4017 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4018 | /* |
| 4019 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 4020 | * address <addr>. |
| 4021 | * Implementation of RFC9000 6. Version Negotiation |
| 4022 | * |
| 4023 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 4024 | * |
| 4025 | * Returns 0 on success else non-zero |
| 4026 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4027 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 4028 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4029 | { |
| 4030 | char buf[256]; |
| 4031 | int i = 0, j, version; |
| 4032 | const socklen_t addrlen = get_addr_len(addr); |
| 4033 | |
| 4034 | /* |
| 4035 | * header form |
| 4036 | * long header, fixed bit to 0 for Version Negotiation |
| 4037 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4038 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 4039 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4040 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4041 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4042 | /* null version for Version Negotiation */ |
| 4043 | buf[i++] = '\x00'; |
| 4044 | buf[i++] = '\x00'; |
| 4045 | buf[i++] = '\x00'; |
| 4046 | buf[i++] = '\x00'; |
| 4047 | |
| 4048 | /* source connection id */ |
| 4049 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4050 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4051 | i += pkt->scid.len; |
| 4052 | |
| 4053 | /* destination connection id */ |
| 4054 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4055 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4056 | i += pkt->dcid.len; |
| 4057 | |
| 4058 | /* supported version */ |
| 4059 | j = 0; |
| 4060 | do { |
| 4061 | version = htonl(quic_supported_version[j]); |
| 4062 | memcpy(&buf[i], &version, sizeof(version)); |
| 4063 | i += sizeof(version); |
| 4064 | |
| 4065 | version = quic_supported_version[++j]; |
| 4066 | } while (version); |
| 4067 | |
| 4068 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4069 | return 1; |
| 4070 | |
| 4071 | return 0; |
| 4072 | } |
| 4073 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4074 | /* Generate the token to be used in Retry packets. The token is written to |
| 4075 | * <buf> which is expected to be <len> bytes. |
| 4076 | * |
| 4077 | * Various parameters are expected to be encoded in the token. For now, only |
| 4078 | * the DCID from <pkt> is stored. This is useful to implement a stateless Retry |
| 4079 | * as this CID must be repeated by the server in the transport parameters. |
| 4080 | * |
| 4081 | * TODO add the client address to validate the token origin. |
| 4082 | * |
| 4083 | * Returns the length of the encoded token or 0 on error. |
| 4084 | */ |
| 4085 | static int generate_retry_token(unsigned char *buf, unsigned char len, |
| 4086 | struct quic_rx_packet *pkt) |
| 4087 | { |
| 4088 | const size_t token_len = 1 + pkt->dcid.len; |
| 4089 | unsigned char i = 0; |
| 4090 | |
| 4091 | if (token_len > len) |
| 4092 | return 0; |
| 4093 | |
| 4094 | buf[i++] = pkt->dcid.len; |
| 4095 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 4096 | i += pkt->dcid.len; |
| 4097 | |
| 4098 | return i; |
| 4099 | } |
| 4100 | |
| 4101 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 4102 | * the Initial <pkt> packet. |
| 4103 | * |
| 4104 | * Returns 0 on success else non-zero. |
| 4105 | */ |
| 4106 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 4107 | struct quic_rx_packet *pkt) |
| 4108 | { |
| 4109 | unsigned char buf[128]; |
| 4110 | int i = 0, token_len; |
| 4111 | const socklen_t addrlen = get_addr_len(addr); |
| 4112 | struct quic_cid scid; |
| 4113 | |
| 4114 | /* long header + fixed bit + packet type 0x3 */ |
| 4115 | buf[i++] = 0xf0; |
| 4116 | /* version */ |
| 4117 | buf[i++] = 0x00; |
| 4118 | buf[i++] = 0x00; |
| 4119 | buf[i++] = 0x00; |
| 4120 | buf[i++] = 0x01; |
| 4121 | |
| 4122 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 4123 | buf[i++] = pkt->scid.len; |
| 4124 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 4125 | i += pkt->scid.len; |
| 4126 | |
| 4127 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 4128 | scid.len = QUIC_HAP_CID_LEN; |
| 4129 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 4130 | return 1; |
| 4131 | |
| 4132 | buf[i++] = scid.len; |
| 4133 | memcpy(&buf[i], scid.data, scid.len); |
| 4134 | i += scid.len; |
| 4135 | |
| 4136 | /* token */ |
| 4137 | if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt))) |
| 4138 | return 1; |
| 4139 | i += token_len; |
| 4140 | |
| 4141 | /* token integrity tag */ |
| 4142 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 4143 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 4144 | pkt->dcid.len, buf, i)) { |
| 4145 | return 1; |
| 4146 | } |
| 4147 | |
| 4148 | i += QUIC_TLS_TAG_LEN; |
| 4149 | |
| 4150 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4151 | return 1; |
| 4152 | |
| 4153 | return 0; |
| 4154 | } |
| 4155 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4156 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 4157 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 4158 | * concatenated to the <pkt> DCID field. |
| 4159 | * |
| 4160 | * Returns the instance or NULL if not found. |
| 4161 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4162 | static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4163 | struct listener *l, |
| 4164 | struct sockaddr_storage *saddr) |
| 4165 | { |
| 4166 | struct quic_conn *qc = NULL; |
| 4167 | struct ebmb_node *node; |
| 4168 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4169 | /* set if the quic_conn is found in the second DCID tree */ |
| 4170 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4171 | |
| 4172 | HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 4173 | |
| 4174 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 4175 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 4176 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 4177 | /* DCIDs of first packets coming from multiple clients may have |
| 4178 | * the same values. Let's distinguish them by concatenating the |
| 4179 | * socket addresses. |
| 4180 | */ |
| 4181 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 4182 | node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data, |
| 4183 | pkt->dcid.len + pkt->dcid.addrlen); |
| 4184 | if (node) { |
| 4185 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 4186 | goto end; |
| 4187 | } |
| 4188 | } |
| 4189 | |
| 4190 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 4191 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 4192 | * used. |
| 4193 | */ |
| 4194 | node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len); |
| 4195 | if (!node) |
| 4196 | goto end; |
| 4197 | |
| 4198 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 4199 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4200 | found_in_dcid = 1; |
| 4201 | |
| 4202 | end: |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4203 | if (qc) |
| 4204 | quic_conn_take(qc); |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4205 | HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4206 | |
| 4207 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 4208 | * If already done, this is a noop. |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4209 | * |
| 4210 | * node.leaf_p is first checked to avoid unnecessary locking. |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4211 | */ |
Amaury Denoyelle | c6fab98 | 2021-12-23 16:27:56 +0100 | [diff] [blame] | 4212 | if (qc && found_in_dcid && qc->odcid_node.node.leaf_p) { |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4213 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 4214 | ebmb_delete(&qc->odcid_node); |
| 4215 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 4216 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4217 | |
| 4218 | return qc; |
| 4219 | } |
| 4220 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4221 | /* Parse the Retry token from buffer <token> whose size is <token_len>. This |
| 4222 | * will extract the parameters stored in the token : <odcid>. |
| 4223 | * |
| 4224 | * Returns 0 on success else non-zero. |
| 4225 | */ |
| 4226 | static int parse_retry_token(const unsigned char *token, uint64_t token_len, |
| 4227 | struct quic_cid *odcid) |
| 4228 | { |
| 4229 | uint64_t odcid_len; |
| 4230 | |
| 4231 | if (!quic_dec_int(&odcid_len, &token, token + token_len)) |
| 4232 | return 1; |
| 4233 | |
| 4234 | memcpy(odcid->data, token, odcid_len); |
| 4235 | odcid->len = odcid_len; |
| 4236 | |
| 4237 | return 0; |
| 4238 | } |
| 4239 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4240 | static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4241 | struct quic_rx_packet *pkt, |
| 4242 | struct quic_dgram_ctx *dgram_ctx, |
| 4243 | struct sockaddr_storage *saddr) |
| 4244 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4245 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4246 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4247 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4248 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4249 | int long_header = 0, io_cb_wakeup = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4250 | size_t b_cspace; |
| 4251 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4252 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4253 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4254 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4255 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 4256 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 4257 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 4258 | /* This ist only to please to traces and distinguish the |
| 4259 | * packet with parsed packet number from others. |
| 4260 | */ |
| 4261 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4262 | if (end <= buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4263 | goto err; |
| 4264 | |
| 4265 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4266 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4267 | /* XXX TO BE DISCARDED */ |
| 4268 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4269 | goto err; |
| 4270 | } |
| 4271 | |
| 4272 | l = dgram_ctx->owner; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4273 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4274 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4275 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4276 | uint64_t len; |
| 4277 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4278 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4279 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4280 | goto err; |
| 4281 | } |
| 4282 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4283 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 4284 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 4285 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4286 | goto err; |
| 4287 | } |
| 4288 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4289 | /* RFC9000 6. Version Negotiation */ |
| 4290 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4291 | /* unsupported version, send Negotiation packet */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4292 | if (send_version_negotiation(l->rx.fd, saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4293 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 4294 | goto err; |
| 4295 | } |
| 4296 | |
| 4297 | TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4298 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4299 | } |
| 4300 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4301 | /* For Initial packets, and for servers (QUIC clients connections), |
| 4302 | * there is no Initial connection IDs storage. |
| 4303 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4304 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4305 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4306 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4307 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 4308 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4309 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4310 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4311 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4312 | |
| 4313 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 4314 | * A server which sends an Initial packet must not set the token. |
| 4315 | * So, a client which receives an Initial packet with a token |
| 4316 | * MUST discard the packet or generate a connection error with |
| 4317 | * PROTOCOL_VIOLATION as type. |
| 4318 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 4319 | */ |
| 4320 | pkt->token_len = token_len; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4321 | |
| 4322 | /* TODO Retry should be automatically activated if |
| 4323 | * suspect network usage is detected. |
| 4324 | */ |
| 4325 | if (!token_len && l->bind_conf->quic_force_retry) { |
| 4326 | TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT); |
| 4327 | if (send_retry(l->rx.fd, saddr, pkt)) { |
| 4328 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 4329 | goto err; |
| 4330 | } |
| 4331 | |
| 4332 | goto err; |
| 4333 | } |
| 4334 | else { |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4335 | pkt->token = buf; |
| 4336 | buf += pkt->token_len; |
| 4337 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4338 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4339 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4340 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4341 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4342 | goto err; |
| 4343 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4344 | } |
| 4345 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4346 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 4347 | end - buf < len) { |
| 4348 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4349 | goto err; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4350 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4351 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4352 | payload = buf; |
| 4353 | pkt->len = len + payload - beg; |
| 4354 | |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4355 | qc = retrieve_qc_conn_from_cid(pkt, l, saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4356 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4357 | int ipv4; |
| 4358 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4359 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4360 | const unsigned char *salt = initial_salt_v1; |
| 4361 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4362 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4363 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 4364 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4365 | goto err; |
| 4366 | } |
| 4367 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4368 | pkt->saddr = *saddr; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4369 | ipv4 = saddr->ss_family == AF_INET; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4370 | qc = qc_new_conn(pkt->version, ipv4, |
| 4371 | pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4372 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4373 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4374 | goto err; |
| 4375 | |
| 4376 | odcid = &qc->rx.params.original_destination_connection_id; |
| 4377 | /* Copy the transport parameters. */ |
| 4378 | qc->rx.params = l->bind_conf->quic_params; |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4379 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4380 | /* Copy original_destination_connection_id transport parameter. */ |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4381 | if (pkt->token_len) { |
| 4382 | if (parse_retry_token(pkt->token, pkt->token_len, odcid)) { |
| 4383 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 4384 | goto err; |
| 4385 | } |
Amaury Denoyelle | c3b6f4d | 2022-01-11 12:03:09 +0100 | [diff] [blame] | 4386 | /* Copy retry_source_connection_id transport parameter. */ |
| 4387 | quic_cid_cpy(&qc->rx.params.retry_source_connection_id, |
| 4388 | &pkt->dcid); |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4389 | } |
| 4390 | else { |
| 4391 | memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len); |
| 4392 | odcid->len = pkt->dcid.len; |
| 4393 | } |
| 4394 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4395 | /* Copy the initial source connection ID. */ |
| 4396 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4397 | qc->enc_params_len = |
| 4398 | quic_transport_params_encode(qc->enc_params, |
| 4399 | qc->enc_params + sizeof qc->enc_params, |
| 4400 | &qc->rx.params, 1); |
| 4401 | if (!qc->enc_params_len) |
| 4402 | goto err; |
| 4403 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4404 | /* NOTE: the socket address has been concatenated to the destination ID |
| 4405 | * chosen by the client for Initial packets. |
| 4406 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4407 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4408 | salt = initial_salt_draft_29; |
| 4409 | salt_len = sizeof initial_salt_draft_29; |
| 4410 | } |
| 4411 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4412 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4413 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4414 | goto err; |
| 4415 | } |
| 4416 | |
Frédéric Lécaille | b0006ee | 2021-11-03 19:01:31 +0100 | [diff] [blame] | 4417 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4418 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4419 | n = ebmb_insert(&l->rx.odcids, &qc->odcid_node, |
| 4420 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4421 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4422 | /* If the insertion failed, it means that another |
| 4423 | * thread has already allocated a QUIC connection for |
| 4424 | * the same CID. Liberate our allocated connection. |
| 4425 | */ |
| 4426 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4427 | qc_to_purge = qc; |
| 4428 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4429 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 4430 | pkt->qc = qc; |
| 4431 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4432 | |
| 4433 | quic_conn_take(qc); |
| 4434 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 4435 | |
| 4436 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4437 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 4438 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4439 | MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list); |
| 4440 | /* Try to accept a new connection. */ |
| 4441 | listener_accept(l); |
| 4442 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4443 | else { |
| 4444 | quic_conn_drop(qc_to_purge); |
| 4445 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4446 | } |
| 4447 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4448 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4449 | } |
| 4450 | } |
| 4451 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4452 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4453 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4454 | goto err; |
| 4455 | } |
| 4456 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4457 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4458 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4459 | buf += QUIC_HAP_CID_LEN; |
| 4460 | |
| 4461 | /* A short packet is the last one of a UDP datagram. */ |
| 4462 | payload = buf; |
| 4463 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4464 | |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4465 | qc = retrieve_qc_conn_from_cid(pkt, l, saddr); |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4466 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4467 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4468 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
| 4469 | goto err; |
| 4470 | } |
| 4471 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4472 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4473 | } |
| 4474 | |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4475 | |
| 4476 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4477 | * they must have the same DCID. |
| 4478 | * |
| 4479 | * This check must be done after the final update to pkt.len to |
| 4480 | * properly drop the packet on failure. |
| 4481 | */ |
| 4482 | if (!dgram_ctx->dcid.len) { |
| 4483 | memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len); |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4484 | if (!quic_peer_validated_addr(qc) && |
| 4485 | HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
| 4486 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 4487 | QUIC_EV_CONN_LPKT, qc); |
| 4488 | /* Reset the anti-amplification bit. It will be set again |
| 4489 | * when sending the next packet if reached again. |
| 4490 | */ |
| 4491 | HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT); |
| 4492 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT); |
| 4493 | io_cb_wakeup = 1; |
| 4494 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4495 | } |
| 4496 | else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4497 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4498 | goto err; |
| 4499 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4500 | dgram_ctx->qc = qc; |
| 4501 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4502 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4503 | if (HA_ATOMIC_LOAD(&qc->err_code)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4504 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4505 | goto out; |
| 4506 | } |
| 4507 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4508 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4509 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4510 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4511 | b_cspace = b_contig_space(&qc->rx.buf); |
| 4512 | if (b_cspace < pkt->len) { |
| 4513 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4514 | if (b_cspace) { |
| 4515 | b_putchr(&qc->rx.buf, 0x00); |
| 4516 | b_cspace--; |
| 4517 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4518 | b_add(&qc->rx.buf, b_cspace); |
| 4519 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 4520 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4521 | TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len); |
Frédéric Lécaille | 91ac6c3 | 2021-12-17 16:11:54 +0100 | [diff] [blame] | 4522 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4523 | goto err; |
| 4524 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4525 | } |
| 4526 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4527 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4528 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4529 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 4530 | goto err; |
| 4531 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4532 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4533 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4534 | TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4535 | if (pkt->aad_len) |
| 4536 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4537 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 4538 | /* Wake up the connection packet handler task from here only if all |
| 4539 | * the contexts have been initialized, especially the mux context |
| 4540 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 4541 | * will start it if these contexts for the connection are not already |
| 4542 | * initialized. |
| 4543 | */ |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 4544 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
Frédéric Lécaille | b80b20c | 2022-01-12 17:46:56 +0100 | [diff] [blame] | 4545 | if (conn_ctx && HA_ATOMIC_LOAD(&qc->qcc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4546 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4547 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4548 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4549 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4550 | if (qc) |
| 4551 | quic_conn_drop(qc); |
| 4552 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4553 | return pkt->len; |
| 4554 | |
| 4555 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4556 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 4557 | * This cannot be done by this thread. |
| 4558 | */ |
| 4559 | if (io_cb_wakeup) { |
| 4560 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
| 4561 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 4562 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4563 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 4564 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4565 | if (!pkt->len) |
| 4566 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 4567 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4568 | qc ? qc : NULL, pkt); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4569 | |
| 4570 | if (qc) |
| 4571 | quic_conn_drop(qc); |
| 4572 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4573 | return -1; |
| 4574 | } |
| 4575 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4576 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 4577 | * Return 1 if enough room to build this header, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4578 | */ |
| 4579 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 4580 | int type, size_t pn_len, struct quic_conn *conn) |
| 4581 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4582 | if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4583 | return 0; |
| 4584 | |
| 4585 | /* #0 byte flags */ |
| 4586 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 4587 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 4588 | /* Version */ |
| 4589 | quic_write_uint32(buf, end, conn->version); |
| 4590 | *(*buf)++ = conn->dcid.len; |
| 4591 | /* Destination connection ID */ |
| 4592 | if (conn->dcid.len) { |
| 4593 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4594 | *buf += conn->dcid.len; |
| 4595 | } |
| 4596 | /* Source connection ID */ |
| 4597 | *(*buf)++ = conn->scid.len; |
| 4598 | if (conn->scid.len) { |
| 4599 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 4600 | *buf += conn->scid.len; |
| 4601 | } |
| 4602 | |
| 4603 | return 1; |
| 4604 | } |
| 4605 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4606 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 4607 | * Return 1 if enough room to build this header, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4608 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4609 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 4610 | size_t pn_len, struct quic_conn *conn, |
| 4611 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4612 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4613 | if (end - *buf < 1 + conn->dcid.len) |
| 4614 | return 0; |
| 4615 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4616 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 4617 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 4618 | ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4619 | /* Destination connection ID */ |
| 4620 | if (conn->dcid.len) { |
| 4621 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4622 | *buf += conn->dcid.len; |
| 4623 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4624 | |
| 4625 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 4629 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 4630 | * with <aead> as AEAD cipher and <key> as secret key. |
| 4631 | * Returns 1 if succeeded or 0 if failed. |
| 4632 | */ |
| 4633 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 4634 | const EVP_CIPHER *aead, const unsigned char *key) |
| 4635 | { |
| 4636 | int i, ret, outlen; |
| 4637 | EVP_CIPHER_CTX *ctx; |
| 4638 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 4639 | * and at most 4 bytes for the packet number |
| 4640 | */ |
| 4641 | unsigned char mask[5] = {0}; |
| 4642 | |
| 4643 | ret = 0; |
| 4644 | ctx = EVP_CIPHER_CTX_new(); |
| 4645 | if (!ctx) |
| 4646 | return 0; |
| 4647 | |
| 4648 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 4649 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 4650 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 4651 | goto out; |
| 4652 | |
| 4653 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 4654 | for (i = 0; i < pnlen; i++) |
| 4655 | pn[i] ^= mask[i + 1]; |
| 4656 | |
| 4657 | ret = 1; |
| 4658 | |
| 4659 | out: |
| 4660 | EVP_CIPHER_CTX_free(ctx); |
| 4661 | |
| 4662 | return ret; |
| 4663 | } |
| 4664 | |
| 4665 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 4666 | * ACK ranges if needed to a value below <limit> in bytes. |
| 4667 | * Return 1 if succeeded, 0 if not. |
| 4668 | */ |
| 4669 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 4670 | { |
| 4671 | size_t room, ack_delay_sz; |
| 4672 | |
| 4673 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 4674 | /* A frame is made of 1 byte for the frame type. */ |
| 4675 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4676 | if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4677 | return 0; |
| 4678 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4679 | return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4680 | } |
| 4681 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4682 | /* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames |
| 4683 | * for <qel> encryption level to be encoded in a buffer with <room> as available room, |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4684 | * and <*len> the packet Length field initialized with the number of bytes already present |
| 4685 | * in this buffer which must be taken into an account for the Length packet field value. |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4686 | * <headlen> is the number of bytes already present in this packet before building frames. |
| 4687 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4688 | * Update consequently <*len> to reflect the size of these frames built |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4689 | * by this function. Also attach these frames to <l> frame list. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4690 | * Return 1 if succeeded, 0 if not. |
| 4691 | */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4692 | static inline int qc_build_frms(struct list *l, |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4693 | size_t room, size_t *len, size_t headlen, |
| 4694 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4695 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4696 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4697 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4698 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4699 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4700 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 4701 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4702 | return 0; |
| 4703 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4704 | /* If we are not probing we must take into an account the congestion |
| 4705 | * control window. |
| 4706 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 4707 | if (!qel->pktns->tx.pto_probe) { |
| 4708 | size_t remain = quic_path_prep_data(qc->path); |
| 4709 | |
| 4710 | if (headlen > remain) |
| 4711 | return 0; |
| 4712 | |
| 4713 | room = QUIC_MIN(room, remain - headlen); |
| 4714 | } |
| 4715 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4716 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4717 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4718 | list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4719 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4720 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4721 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4722 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4723 | break; |
| 4724 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4725 | switch (cf->type) { |
| 4726 | case QUIC_FT_CRYPTO: |
| 4727 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4728 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4729 | /* Compute the length of this CRYPTO frame header */ |
| 4730 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 4731 | /* Compute the data length of this CRyPTO frame. */ |
| 4732 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 4733 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4734 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4735 | if (!dlen) |
| 4736 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4737 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4738 | /* CRYPTO frame length. */ |
| 4739 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 4740 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4741 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4742 | /* Add the CRYPTO data length and its encoded length to the packet |
| 4743 | * length and the length of this length. |
| 4744 | */ |
| 4745 | *len += flen; |
| 4746 | room -= flen; |
| 4747 | if (dlen == cf->crypto.len) { |
| 4748 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4749 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4750 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4751 | } |
| 4752 | else { |
| 4753 | struct quic_frame *new_cf; |
| 4754 | |
| 4755 | new_cf = pool_alloc(pool_head_quic_frame); |
| 4756 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4757 | TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4758 | return 0; |
| 4759 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4760 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4761 | new_cf->type = QUIC_FT_CRYPTO; |
| 4762 | new_cf->crypto.len = dlen; |
| 4763 | new_cf->crypto.offset = cf->crypto.offset; |
| 4764 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4765 | LIST_APPEND(l, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4766 | /* Consume <dlen> bytes of the current frame. */ |
| 4767 | cf->crypto.len -= dlen; |
| 4768 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4769 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4770 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4771 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4772 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4773 | /* Note that these frames are accepted in short packets only without |
| 4774 | * "Length" packet field. Here, <*len> is used only to compute the |
| 4775 | * sum of the lengths of the already built frames for this packet. |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 4776 | * |
| 4777 | * Compute the length of this STREAM frame "header" made a all the field |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4778 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 4779 | */ |
| 4780 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4781 | ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4782 | /* Compute the data length of this STREAM frame. */ |
| 4783 | avail_room = room - hlen - *len; |
| 4784 | if ((ssize_t)avail_room <= 0) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4785 | break; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4786 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 4787 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4788 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4789 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 4790 | dlen = max_available_room(avail_room, &dlen_sz); |
| 4791 | if (dlen > cf->stream.len) { |
| 4792 | dlen = cf->stream.len; |
| 4793 | } |
| 4794 | dlen_sz = quic_int_getsize(dlen); |
| 4795 | flen = hlen + dlen_sz + dlen; |
| 4796 | } |
| 4797 | else { |
| 4798 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 4799 | flen = hlen + dlen; |
| 4800 | } |
| 4801 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4802 | QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4803 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4804 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4805 | /* Add the STREAM data length and its encoded length to the packet |
| 4806 | * length and the length of this length. |
| 4807 | */ |
| 4808 | *len += flen; |
| 4809 | room -= flen; |
| 4810 | if (dlen == cf->stream.len) { |
| 4811 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4812 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4813 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4814 | } |
| 4815 | else { |
| 4816 | struct quic_frame *new_cf; |
| 4817 | |
| 4818 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 4819 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4820 | TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4821 | return 0; |
| 4822 | } |
| 4823 | |
| 4824 | new_cf->type = cf->type; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4825 | new_cf->stream.qcs = cf->stream.qcs; |
| 4826 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4827 | new_cf->stream.id = cf->stream.id; |
| 4828 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 4829 | new_cf->stream.offset = cf->stream.offset; |
| 4830 | new_cf->stream.len = dlen; |
| 4831 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 4832 | /* FIN bit reset */ |
| 4833 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 4834 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4835 | LIST_APPEND(l, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4836 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 4837 | /* Consume <dlen> bytes of the current frame. */ |
| 4838 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4839 | cf->stream.offset.key += dlen; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4840 | cf->stream.data += dlen; |
| 4841 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4842 | break; |
| 4843 | |
| 4844 | default: |
| 4845 | flen = qc_frm_len(cf); |
| 4846 | BUG_ON(!flen); |
| 4847 | if (flen > room) |
| 4848 | continue; |
| 4849 | |
| 4850 | *len += flen; |
| 4851 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4852 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4853 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4854 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4855 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4856 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4857 | } |
| 4858 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4859 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4860 | } |
| 4861 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4862 | /* This function builds a clear packet from <pkt> information (its type) |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4863 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 4864 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 4865 | * filling the buffer with as much frames as possible. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4866 | * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4867 | * reserved so that to ensure there is enough room to build this AEAD TAG after |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4868 | * having returned from this function. |
| 4869 | * This function also updates the value of <buf_pn> pointer to point to the packet |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4870 | * number field in this packet. <pn_len> will also have the packet number |
| 4871 | * length as value. |
| 4872 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4873 | * Return 1 if succeeded (enough room to buile this packet), O if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4874 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4875 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 4876 | size_t dglen, struct quic_tx_packet *pkt, |
| 4877 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 4878 | int ack, int padding, int cc, int probe, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4879 | struct quic_enc_level *qel, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4880 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4881 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4882 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4883 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 4884 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4885 | struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, }; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4886 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4887 | int64_t largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4888 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4889 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4890 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4891 | /* Length field value with CRYPTO frames if present. */ |
| 4892 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4893 | beg = pos; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4894 | /* When not probing and not acking, and no immediate close is required, |
| 4895 | * reduce the size of this buffer to respect |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4896 | * the congestion controller window. So, we do not limit the size of this |
| 4897 | * packet if we have an ACK frame to send because an ACK frame is not |
| 4898 | * ack-eliciting. This size will be limited if we have ack-eliciting |
| 4899 | * frames to send from qel->pktns->tx.frms. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4900 | */ |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 4901 | if (!probe && !ack && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4902 | size_t path_room; |
| 4903 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4904 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4905 | if (end - beg > path_room) |
| 4906 | end = beg + path_room; |
| 4907 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4908 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4909 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 4910 | * length field if any. |
| 4911 | */ |
| 4912 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 4913 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 4914 | goto no_room; |
| 4915 | |
| 4916 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | e1aa0d3 | 2021-08-03 16:03:09 +0200 | [diff] [blame] | 4917 | largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4918 | /* packet number length */ |
| 4919 | *pn_len = quic_packet_number_length(pn, largest_acked_pn); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4920 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4921 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4922 | !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) || |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4923 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4924 | !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc))) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4925 | goto no_room; |
| 4926 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4927 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 4928 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4929 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4930 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4931 | /* Build an ACK frame if required. */ |
| 4932 | ack_frm_len = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4933 | if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4934 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4935 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4936 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 4937 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 4938 | * that from here, we do not know if we will have to send a PING frame. |
| 4939 | * This will be decided after having computed the ack-eliciting frames |
| 4940 | * to be added to this packet. |
| 4941 | */ |
| 4942 | ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - 1 - *pn_len - pos); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4943 | if (!ack_frm_len) |
| 4944 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4945 | } |
| 4946 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4947 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4948 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4949 | len_frms = 0; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4950 | if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4951 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4952 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4953 | /* Initialize the length of the frames built below to <len>. |
| 4954 | * If any frame could be successfully built by qc_build_frms(), |
| 4955 | * we will have len_frms > len. |
| 4956 | */ |
| 4957 | len_frms = len; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4958 | if (!qc_build_frms(&frm_list, end - pos, &len_frms, pos - beg, qel, qc)) { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4959 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4960 | qc, NULL, NULL, &room); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4961 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4962 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4963 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4964 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 4965 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 4966 | * for the encryption tag. It must be taken into an account for the length |
| 4967 | * of this packet. |
| 4968 | */ |
| 4969 | if (len_frms) |
| 4970 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 4971 | else |
| 4972 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4973 | /* CONNECTION_CLOSE frame */ |
| 4974 | if (cc) { |
| 4975 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4976 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4977 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4978 | len += qc_frm_len(&cc_frm); |
| 4979 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4980 | add_ping_frm = 0; |
| 4981 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4982 | len_sz = quic_int_getsize(len); |
| 4983 | /* Add this packet size to <dglen> */ |
| 4984 | dglen += head_len + len_sz + len; |
| 4985 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 4986 | /* This is a maximum padding size */ |
| 4987 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 4988 | /* The length field value is of this packet is <len> + <padding_len> |
| 4989 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 4990 | * <len_sz>. So, let's deduce the difference between these to packet |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4991 | * sizes from <padding_len>. |
| 4992 | */ |
| 4993 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 4994 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4995 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4996 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4997 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4998 | /* If we cannot send a frame, we send a PING frame. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4999 | add_ping_frm = 1; |
| 5000 | len += 1; |
| 5001 | } |
| 5002 | /* If there is no frame at all to follow, add at least a PADDING frame. */ |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5003 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5004 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 5005 | } |
| 5006 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5007 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 5008 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5009 | |
| 5010 | /* Packet number field address. */ |
| 5011 | *buf_pn = pos; |
| 5012 | |
| 5013 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5014 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 5015 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5016 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5017 | if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5018 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5019 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5020 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5021 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 5022 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5023 | |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5024 | list_for_each_entry(cf, &frm_list, list) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5025 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5026 | ssize_t room = end - pos; |
| 5027 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5028 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5029 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5030 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5031 | } |
| 5032 | } |
| 5033 | |
| 5034 | /* Build a PING frame if needed. */ |
| 5035 | if (add_ping_frm) { |
| 5036 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5037 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5038 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5039 | } |
| 5040 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5041 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5042 | if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5043 | goto no_room; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5044 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5045 | /* Build a PADDING frame if needed. */ |
| 5046 | if (padding_len) { |
| 5047 | frm.type = QUIC_FT_PADDING; |
| 5048 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5049 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5050 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5051 | } |
| 5052 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5053 | /* If this packet is ack-eliciting and we are probing let's |
| 5054 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5055 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5056 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 5057 | qel->pktns->tx.pto_probe) |
| 5058 | qel->pktns->tx.pto_probe--; |
| 5059 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5060 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5061 | LIST_SPLICE(&pkt->frms, &frm_list); |
| 5062 | TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5063 | |
| 5064 | return 1; |
| 5065 | |
| 5066 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5067 | /* Replace the pre-built frames which could not be add to this packet */ |
| 5068 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5069 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5070 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5071 | } |
| 5072 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5073 | static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5074 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5075 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5076 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5077 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 5078 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5079 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5080 | pkt->next = NULL; |
| 5081 | pkt->refcnt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5082 | } |
| 5083 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5084 | /* Free <pkt> TX packet which has not already attached to any tree. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5085 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 5086 | { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 5087 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5088 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5089 | if (!pkt) |
| 5090 | return; |
| 5091 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5092 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5093 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 5094 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5095 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5096 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5097 | } |
| 5098 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5099 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 5100 | * type for <qc> QUIC connection from <qel> encryption level. |
| 5101 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 5102 | * -1 if there was not enough room to build a packet. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5103 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5104 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 5105 | const unsigned char *buf_end, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5106 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5107 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 5108 | int pkt_type, int ack, int probe, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5109 | { |
| 5110 | /* The pointer to the packet number field. */ |
| 5111 | unsigned char *buf_pn; |
| 5112 | unsigned char *beg, *end, *payload; |
| 5113 | int64_t pn; |
| 5114 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5115 | struct quic_tls_ctx *tls_ctx; |
| 5116 | struct quic_tx_packet *pkt; |
| 5117 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5118 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5119 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5120 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 5121 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5122 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5123 | *err = -2; |
| 5124 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5125 | } |
| 5126 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5127 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5128 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5129 | pn_len = 0; |
| 5130 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5131 | |
| 5132 | pn = qel->pktns->tx.next_pn + 1; |
| 5133 | if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 5134 | ack, padding, cc, probe, qel, qc)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5135 | *err = -1; |
| 5136 | goto err; |
| 5137 | } |
| 5138 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5139 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5140 | payload = buf_pn + pn_len; |
| 5141 | payload_len = end - payload; |
| 5142 | aad_len = payload - beg; |
| 5143 | |
| 5144 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 5145 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5146 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5147 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5148 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5149 | |
| 5150 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5151 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5152 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 5153 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5154 | TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5155 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5156 | goto err; |
| 5157 | } |
| 5158 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5159 | /* Consume a packet number */ |
| 5160 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 5161 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 41a0760 | 2022-01-04 16:57:37 +0100 | [diff] [blame] | 5162 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes) |
| 5163 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5164 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 5165 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5166 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 5167 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5168 | /* Set the packet in fligth length for in flight packet only. */ |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5169 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5170 | pkt->in_flight_len = pkt->len; |
| 5171 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5172 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5173 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5174 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5175 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5176 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5177 | |
| 5178 | err: |
| 5179 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5180 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5181 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5182 | } |
| 5183 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 5184 | /* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>. |
| 5185 | * Return the number of bytes which have been copied. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5186 | */ |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 5187 | static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx, |
| 5188 | struct buffer *buf, size_t count, int flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5189 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5190 | size_t try, done = 0; |
| 5191 | |
| 5192 | if (!conn_ctrl_ready(conn)) |
| 5193 | return 0; |
| 5194 | |
| 5195 | if (!fd_recv_ready(conn->handle.fd)) |
| 5196 | return 0; |
| 5197 | |
| 5198 | conn->flags &= ~CO_FL_WAIT_ROOM; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5199 | |
| 5200 | /* read the largest possible block. For this, we perform only one call |
| 5201 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 5202 | * in which case we accept to do it once again. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5203 | */ |
| 5204 | while (count > 0) { |
| 5205 | try = b_contig_space(buf); |
| 5206 | if (!try) |
| 5207 | break; |
| 5208 | |
| 5209 | if (try > count) |
| 5210 | try = count; |
| 5211 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 5212 | b_add(buf, try); |
| 5213 | done += try; |
| 5214 | count -= try; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5215 | } |
| 5216 | |
| 5217 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 5218 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5219 | |
| 5220 | leave: |
| 5221 | return done; |
| 5222 | |
| 5223 | read0: |
| 5224 | conn_sock_read0(conn); |
| 5225 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5226 | |
| 5227 | /* Now a final check for a possible asynchronous low-level error |
| 5228 | * report. This can happen when a connection receives a reset |
| 5229 | * after a shutdown, both POLL_HUP and POLL_ERR are queued, and |
| 5230 | * we might have come from there by just checking POLL_HUP instead |
| 5231 | * of recv()'s return value 0, so we have no way to tell there was |
| 5232 | * an error without checking. |
| 5233 | */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 5234 | if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5235 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 5236 | goto leave; |
| 5237 | } |
| 5238 | |
| 5239 | |
| 5240 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 5241 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 5242 | * other pending data for example, but this flag is ignored at the moment. |
| 5243 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5244 | * a second call may be performed. The connection's flags are updated with |
| 5245 | * whatever special event is detected (error, empty). The caller is responsible |
| 5246 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5247 | * function does not call the connection's polling update function, so the caller |
| 5248 | * is responsible for this. It's up to the caller to update the buffer's contents |
| 5249 | * based on the return value. |
| 5250 | */ |
| 5251 | static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags) |
| 5252 | { |
| 5253 | ssize_t ret; |
| 5254 | size_t try, done; |
| 5255 | int send_flag; |
| 5256 | |
| 5257 | if (!conn_ctrl_ready(conn)) |
| 5258 | return 0; |
| 5259 | |
| 5260 | if (!fd_send_ready(conn->handle.fd)) |
| 5261 | return 0; |
| 5262 | |
| 5263 | done = 0; |
| 5264 | /* send the largest possible block. For this we perform only one call |
| 5265 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5266 | * in which case we accept to do it once again. |
| 5267 | */ |
| 5268 | while (count) { |
| 5269 | try = b_contig_data(buf, done); |
| 5270 | if (try > count) |
| 5271 | try = count; |
| 5272 | |
| 5273 | send_flag = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 5274 | if (try < count || flags & CO_SFL_MSG_MORE) |
| 5275 | send_flag |= MSG_MORE; |
| 5276 | |
| 5277 | ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag, |
| 5278 | (struct sockaddr *)conn->dst, get_addr_len(conn->dst)); |
| 5279 | if (ret > 0) { |
| 5280 | count -= ret; |
| 5281 | done += ret; |
| 5282 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5283 | /* A send succeeded, so we can consider ourself connected */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5284 | conn->flags |= CO_FL_WAIT_L4L6; |
| 5285 | /* if the system buffer is full, don't insist */ |
| 5286 | if (ret < try) |
| 5287 | break; |
| 5288 | } |
| 5289 | else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) { |
| 5290 | /* nothing written, we need to poll for write first */ |
| 5291 | fd_cant_send(conn->handle.fd); |
| 5292 | break; |
| 5293 | } |
| 5294 | else if (errno != EINTR) { |
| 5295 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 5296 | break; |
| 5297 | } |
| 5298 | } |
| 5299 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 5300 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5301 | |
| 5302 | if (done > 0) { |
| 5303 | /* we count the total bytes sent, and the send rate for 32-byte |
| 5304 | * blocks. The reason for the latter is that freq_ctr are |
| 5305 | * limited to 4GB and that it's not enough per second. |
| 5306 | */ |
| 5307 | _HA_ATOMIC_ADD(&global.out_bytes, done); |
| 5308 | update_freq_ctr(&global.out_32bps, (done + 16) / 32); |
| 5309 | } |
| 5310 | return done; |
| 5311 | } |
| 5312 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5313 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 5314 | * event subscriber <es> is not allowed to change from a previous call as long |
| 5315 | * as at least one event is still subscribed. The <event_type> must only be a |
| 5316 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 5317 | */ |
| 5318 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5319 | { |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5320 | struct qcc *qcc = conn->qc->qcc; |
| 5321 | |
| 5322 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 5323 | BUG_ON(qcc->subs && qcc->subs != es); |
| 5324 | |
| 5325 | es->events |= event_type; |
| 5326 | qcc->subs = es; |
| 5327 | |
| 5328 | if (event_type & SUB_RETRY_RECV) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5329 | TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5330 | |
| 5331 | if (event_type & SUB_RETRY_SEND) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5332 | TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc); |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5333 | |
| 5334 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5335 | } |
| 5336 | |
| 5337 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5338 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5339 | * subscribe() call. It always returns zero. |
| 5340 | */ |
| 5341 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5342 | { |
| 5343 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 5344 | } |
| 5345 | |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 5346 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 5347 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 5348 | * parameters of this session. |
| 5349 | * This is the responsibility of the caller to check the validity of all the |
| 5350 | * pointers passed as parameter to this function. |
| 5351 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 5352 | * CO_ER_SSL_NO_MEM. |
| 5353 | */ |
| 5354 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 5355 | unsigned char *params, size_t params_len) |
| 5356 | { |
| 5357 | int retry; |
| 5358 | |
| 5359 | retry = 1; |
| 5360 | retry: |
| 5361 | *ssl = SSL_new(ssl_ctx); |
| 5362 | if (!*ssl) { |
| 5363 | if (!retry--) |
| 5364 | goto err; |
| 5365 | |
| 5366 | pool_gc(NULL); |
| 5367 | goto retry; |
| 5368 | } |
| 5369 | |
| 5370 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 5371 | !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) || |
| 5372 | !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) { |
| 5373 | goto err; |
| 5374 | |
| 5375 | SSL_free(*ssl); |
| 5376 | *ssl = NULL; |
| 5377 | if (!retry--) |
| 5378 | goto err; |
| 5379 | |
| 5380 | pool_gc(NULL); |
| 5381 | goto retry; |
| 5382 | } |
| 5383 | |
| 5384 | return 0; |
| 5385 | |
| 5386 | err: |
| 5387 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 5388 | return -1; |
| 5389 | } |
| 5390 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5391 | /* Initialize a QUIC connection (quic_conn struct) to be attached to <conn> |
| 5392 | * connection with <xprt_ctx> as address of the xprt context. |
| 5393 | * Returns 1 if succeeded, 0 if not. |
| 5394 | */ |
| 5395 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 5396 | { |
Amaury Denoyelle | 9979d0d | 2021-12-23 16:32:24 +0100 | [diff] [blame] | 5397 | struct ssl_sock_ctx *ctx = NULL; |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5398 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5399 | |
| 5400 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 5401 | |
| 5402 | if (*xprt_ctx) |
| 5403 | goto out; |
| 5404 | |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5405 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5406 | if (!ctx) { |
| 5407 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 5408 | goto err; |
| 5409 | } |
| 5410 | |
| 5411 | ctx->wait_event.tasklet = tasklet_new(); |
| 5412 | if (!ctx->wait_event.tasklet) { |
| 5413 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 5414 | goto err; |
| 5415 | } |
| 5416 | |
| 5417 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 5418 | ctx->wait_event.tasklet->context = ctx; |
| 5419 | ctx->wait_event.events = 0; |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5420 | HA_ATOMIC_STORE(&ctx->conn, conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5421 | ctx->subs = NULL; |
| 5422 | ctx->xprt_ctx = NULL; |
| 5423 | |
| 5424 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 5425 | if (objt_server(conn->target)) { |
| 5426 | /* Server */ |
| 5427 | struct server *srv = __objt_server(conn->target); |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 5428 | unsigned char dcid[QUIC_HAP_CID_LEN]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5429 | int ssl_err, ipv4; |
| 5430 | |
| 5431 | ssl_err = SSL_ERROR_NONE; |
| 5432 | if (RAND_bytes(dcid, sizeof dcid) != 1) |
| 5433 | goto err; |
| 5434 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5435 | ipv4 = conn->dst->ss_family == AF_INET; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5436 | qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 5437 | dcid, sizeof dcid, 0, NULL, 0, 0, srv); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5438 | if (qc == NULL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5439 | goto err; |
| 5440 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 5441 | ctx->qc = qc; |
| 5442 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 5443 | /* Insert our SCID, the connection ID for the QUIC client. */ |
| 5444 | ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len); |
| 5445 | |
| 5446 | conn->qc = qc; |
| 5447 | qc->conn = conn; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 5448 | if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1, |
| 5449 | dcid, sizeof dcid, 0)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5450 | goto err; |
| 5451 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5452 | qc->rx.params = srv->quic_params; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5453 | /* Copy the initial source connection ID. */ |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5454 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 5455 | qc->enc_params_len = |
| 5456 | quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params, |
| 5457 | &qc->rx.params, 0); |
| 5458 | if (!qc->enc_params_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5459 | goto err; |
| 5460 | |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 5461 | if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl, |
| 5462 | qc->enc_params, qc->enc_params_len) == -1) |
| 5463 | goto err; |
| 5464 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 5465 | SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5466 | SSL_set_connect_state(ctx->ssl); |
| 5467 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 5468 | if (ssl_err != 1) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 5469 | int st; |
| 5470 | |
| 5471 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5472 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 5473 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5474 | TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5475 | } |
| 5476 | else { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5477 | TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5478 | goto err; |
| 5479 | } |
| 5480 | } |
| 5481 | } |
| 5482 | else if (objt_listener(conn->target)) { |
| 5483 | /* Listener */ |
| 5484 | struct bind_conf *bc = __objt_listener(conn->target)->bind_conf; |
| 5485 | |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5486 | qc = ctx->conn->qc; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 5487 | ctx->qc = qc; |
| 5488 | |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 5489 | qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid); |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 5490 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 5491 | qc->enc_params, qc->enc_params_len) == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5492 | goto err; |
| 5493 | |
Frédéric Lécaille | ad3c07a | 2021-12-14 19:23:43 +0100 | [diff] [blame] | 5494 | /* Enabling 0-RTT */ |
| 5495 | if (bc->ssl_conf.early_data) |
| 5496 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 5497 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5498 | SSL_set_accept_state(ctx->ssl); |
| 5499 | } |
| 5500 | |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 5501 | HA_ATOMIC_STORE(xprt_ctx, ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5502 | |
| 5503 | /* Leave init state and start handshake */ |
| 5504 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5505 | |
| 5506 | out: |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5507 | HA_ATOMIC_STORE(&qc->xprt_ctx, ctx); |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5508 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5509 | |
| 5510 | return 0; |
| 5511 | |
| 5512 | err: |
Willy Tarreau | 7deb28c | 2021-05-10 07:40:27 +0200 | [diff] [blame] | 5513 | if (ctx && ctx->wait_event.tasklet) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5514 | tasklet_free(ctx->wait_event.tasklet); |
| 5515 | pool_free(pool_head_quic_conn_ctx, ctx); |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 5516 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5517 | return -1; |
| 5518 | } |
| 5519 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5520 | /* Start the QUIC transport layer */ |
| 5521 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 5522 | { |
| 5523 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5524 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5525 | |
| 5526 | qc = conn->qc; |
| 5527 | if (!quic_conn_init_timer(qc)) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5528 | TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5529 | return 0; |
| 5530 | } |
| 5531 | |
| 5532 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 5533 | return 1; |
| 5534 | } |
| 5535 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5536 | /* transport-layer operations for QUIC connections. */ |
| 5537 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 5538 | .close = quic_close, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5539 | .snd_buf = quic_conn_from_buf, |
| 5540 | .rcv_buf = quic_conn_to_buf, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5541 | .subscribe = quic_conn_subscribe, |
| 5542 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5543 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5544 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5545 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 5546 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 5547 | .get_alpn = ssl_sock_get_alpn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5548 | .name = "QUIC", |
| 5549 | }; |
| 5550 | |
| 5551 | __attribute__((constructor)) |
| 5552 | static void __quic_conn_init(void) |
| 5553 | { |
| 5554 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 5555 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 5556 | } |
| 5557 | |
| 5558 | __attribute__((destructor)) |
| 5559 | static void __quic_conn_deinit(void) |
| 5560 | { |
| 5561 | BIO_meth_free(ha_quic_meth); |
| 5562 | } |
| 5563 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5564 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 5565 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5566 | * Return the number of bytes read if succeeded, -1 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5567 | */ |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5568 | static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5569 | struct sockaddr_storage *saddr, qpkt_read_func *func) |
| 5570 | { |
| 5571 | unsigned char *pos; |
| 5572 | const unsigned char *end; |
| 5573 | struct quic_dgram_ctx dgram_ctx = { |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5574 | .qc = NULL, |
| 5575 | .dcid.len = 0, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5576 | .owner = owner, |
| 5577 | }; |
| 5578 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5579 | pos = (unsigned char *)b_head(buf); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5580 | end = pos + len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5581 | do { |
| 5582 | int ret; |
| 5583 | struct quic_rx_packet *pkt; |
| 5584 | |
Willy Tarreau | e449893 | 2021-03-22 21:13:05 +0100 | [diff] [blame] | 5585 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5586 | if (!pkt) |
| 5587 | goto err; |
| 5588 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5589 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5590 | ret = func(pos, end, pkt, &dgram_ctx, saddr); |
| 5591 | pos += pkt->len; |
Frédéric Lécaille | 310d1bd | 2021-09-22 15:10:49 +0200 | [diff] [blame] | 5592 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5593 | if (ret == -1) |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 5594 | /* If the packet length could not be found, we cannot continue. */ |
| 5595 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5596 | } while (pos < end); |
| 5597 | |
| 5598 | /* Increasing the received bytes counter by the UDP datagram length |
| 5599 | * if this datagram could be associated to a connection. |
| 5600 | */ |
| 5601 | if (dgram_ctx.qc) |
| 5602 | dgram_ctx.qc->rx.bytes += len; |
| 5603 | |
Amaury Denoyelle | ce340fe | 2022-01-11 14:20:46 +0100 | [diff] [blame] | 5604 | return pos - (unsigned char *)b_head(buf); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5605 | |
| 5606 | err: |
| 5607 | return -1; |
| 5608 | } |
| 5609 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5610 | ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5611 | struct sockaddr_storage *saddr) |
| 5612 | { |
| 5613 | return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv); |
| 5614 | } |
| 5615 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5616 | /* Function to automatically activate QUIC traces on stdout. |
| 5617 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 5618 | * Main use for now is in the docker image for QUIC interop testing. |
| 5619 | */ |
| 5620 | static void quic_init_stdout_traces(void) |
| 5621 | { |
| 5622 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 5623 | trace_quic.sink = sink_find("stdout"); |
| 5624 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5625 | trace_quic.state = TRACE_STATE_RUNNING; |
| 5626 | #endif |
| 5627 | } |
| 5628 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 5629 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5630 | /* |
| 5631 | * Local variables: |
| 5632 | * c-indent-level: 8 |
| 5633 | * c-basic-offset: 8 |
| 5634 | * End: |
| 5635 | */ |