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> |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame^] | 47 | #include <haproxy/quic_sock.h> |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 48 | #include <haproxy/cbuf.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 49 | #include <haproxy/quic_tls.h> |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 50 | #include <haproxy/sink.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 51 | #include <haproxy/ssl_sock.h> |
| 52 | #include <haproxy/stream_interface.h> |
| 53 | #include <haproxy/task.h> |
| 54 | #include <haproxy/trace.h> |
| 55 | #include <haproxy/xprt_quic.h> |
| 56 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 57 | /* list of supported QUIC versions by this implementation */ |
| 58 | static int quic_supported_version[] = { |
| 59 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 60 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 61 | |
| 62 | /* placeholder, do not add entry after this */ |
| 63 | 0x0 |
| 64 | }; |
| 65 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 66 | /* This is the values of some QUIC transport parameters when absent. |
| 67 | * Should be used to initialize any transport parameters (local or remote) |
| 68 | * before updating them with customized values. |
| 69 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 70 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 71 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 72 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 73 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 74 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /* trace source and events */ |
| 78 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 79 | const struct trace_source *src, |
| 80 | const struct ist where, const struct ist func, |
| 81 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 82 | |
| 83 | static const struct trace_event quic_trace_events[] = { |
| 84 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 85 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 86 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 87 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 89 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 90 | { .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] | 91 | { .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] | 92 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 93 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 94 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 95 | { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" }, |
| 96 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 97 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 100 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 101 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 102 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 103 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 104 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 105 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 106 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 107 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 108 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 109 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 110 | { .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] | 111 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 112 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 113 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 114 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 115 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 116 | { .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] | 117 | { .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] | 118 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 119 | { .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] | 120 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 121 | { .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] | 122 | { /* end */ } |
| 123 | }; |
| 124 | |
| 125 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 126 | /* arg1 */ { /* already used by the connection */ }, |
| 127 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 128 | /* arg3 */ { }, |
| 129 | /* arg4 */ { } |
| 130 | }; |
| 131 | |
| 132 | static const struct name_desc quic_trace_decoding[] = { |
| 133 | #define QUIC_VERB_CLEAN 1 |
| 134 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 135 | { /* end */ } |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | struct trace_source trace_quic = { |
| 140 | .name = IST("quic"), |
| 141 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 142 | .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] | 143 | .default_cb = quic_trace, |
| 144 | .known_events = quic_trace_events, |
| 145 | .lockon_args = quic_trace_lockon_args, |
| 146 | .decoding = quic_trace_decoding, |
| 147 | .report_events = ~0, /* report everything by default */ |
| 148 | }; |
| 149 | |
| 150 | #define TRACE_SOURCE &trace_quic |
| 151 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 152 | |
| 153 | static BIO_METHOD *ha_quic_meth; |
| 154 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 155 | 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] | 156 | 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] | 157 | 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] | 158 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 159 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 160 | 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] | 161 | DECLARE_POOL(pool_head_quic_connection_id, |
| 162 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 163 | 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] | 164 | 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] | 165 | 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] | 166 | 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] | 167 | 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] | 168 | 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] | 169 | 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] | 170 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 171 | 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] | 172 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 173 | 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] | 174 | 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] | 175 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 176 | /* Only for debug purpose */ |
| 177 | struct enc_debug_info { |
| 178 | unsigned char *payload; |
| 179 | size_t payload_len; |
| 180 | unsigned char *aad; |
| 181 | size_t aad_len; |
| 182 | uint64_t pn; |
| 183 | }; |
| 184 | |
| 185 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 186 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 187 | unsigned char *payload, size_t payload_len, |
| 188 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 189 | { |
| 190 | edi->payload = payload; |
| 191 | edi->payload_len = payload_len; |
| 192 | edi->aad = aad; |
| 193 | edi->aad_len = aad_len; |
| 194 | edi->pn = pn; |
| 195 | } |
| 196 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 197 | /* Trace callback for QUIC. |
| 198 | * These traces always expect that arg1, if non-null, is of type connection. |
| 199 | */ |
| 200 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 201 | const struct ist where, const struct ist func, |
| 202 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 203 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 204 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 205 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 206 | if (qc) { |
| 207 | const struct quic_tls_secrets *secs; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 208 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 209 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 210 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 211 | chunk_appendf(&trace_buf, "\n odcid"); |
| 212 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 213 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 214 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 215 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 216 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 220 | const enum ssl_encryption_level_t *level = a2; |
| 221 | const size_t *len = a3; |
| 222 | |
| 223 | if (level) { |
| 224 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 225 | |
| 226 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 227 | } |
| 228 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 229 | 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] | 230 | } |
| 231 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 232 | /* Initial read & write secrets. */ |
| 233 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 234 | const unsigned char *rx_sec = a2; |
| 235 | const unsigned char *tx_sec = a3; |
| 236 | |
| 237 | secs = &qc->els[level].tls_ctx.rx; |
| 238 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 239 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 240 | if (rx_sec) |
| 241 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 242 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 243 | } |
| 244 | secs = &qc->els[level].tls_ctx.tx; |
| 245 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 246 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 247 | if (tx_sec) |
| 248 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 249 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 250 | } |
| 251 | } |
| 252 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 253 | const enum ssl_encryption_level_t *level = a2; |
| 254 | const unsigned char *secret = a3; |
| 255 | const size_t *secret_len = a4; |
| 256 | |
| 257 | if (level) { |
| 258 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 259 | |
| 260 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 261 | if (secret && secret_len) |
| 262 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 263 | secs = &qc->els[lvl].tls_ctx.rx; |
| 264 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 265 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 270 | const enum ssl_encryption_level_t *level = a2; |
| 271 | const unsigned char *secret = a3; |
| 272 | const size_t *secret_len = a4; |
| 273 | |
| 274 | if (level) { |
| 275 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 276 | |
| 277 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 278 | if (secret && secret_len) |
| 279 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 280 | secs = &qc->els[lvl].tls_ctx.tx; |
| 281 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 282 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 283 | } |
| 284 | |
| 285 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 286 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 287 | 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] | 288 | const struct quic_tx_packet *pkt = a2; |
| 289 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 290 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 291 | |
| 292 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 293 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 294 | 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] | 295 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 296 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 297 | (unsigned long long)qc->path->cwnd, |
| 298 | (unsigned long long)qc->path->prep_in_flight, |
| 299 | (unsigned long long)qc->path->in_flight, |
| 300 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 301 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 302 | } |
| 303 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 304 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 305 | if (pkt->pn_node.key != (uint64_t)-1) |
| 306 | 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] | 307 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 308 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 309 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 310 | (unsigned long long)qc->rx.bytes, |
| 311 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (room) { |
| 315 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 316 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 317 | (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] | 318 | } |
| 319 | } |
| 320 | |
| 321 | if (mask & QUIC_EV_CONN_HDSHK) { |
| 322 | const enum quic_handshake_state *state = a2; |
| 323 | const int *err = a3; |
| 324 | |
| 325 | if (state) |
| 326 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 327 | if (err) |
| 328 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 329 | } |
| 330 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 331 | 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] | 332 | const struct quic_rx_packet *pkt = a2; |
| 333 | const unsigned long *pktlen = a3; |
| 334 | const SSL *ssl = a4; |
| 335 | |
| 336 | if (pkt) { |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 337 | chunk_appendf(&trace_buf, " pkt@%p el=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 338 | pkt, quic_packet_type_enc_level_char(pkt->type)); |
| 339 | if (pkt->pnl) |
| 340 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 341 | (unsigned long long)pkt->pn); |
| 342 | if (pkt->token_len) |
| 343 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 344 | (unsigned long long)pkt->token_len); |
| 345 | if (pkt->aad_len) |
| 346 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 347 | (unsigned long long)pkt->aad_len); |
| 348 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 349 | pkt->flags, (unsigned long long)pkt->len); |
| 350 | } |
| 351 | if (pktlen) |
| 352 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 353 | if (ssl) { |
| 354 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 355 | chunk_appendf(&trace_buf, " el=%c", |
| 356 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 361 | const struct quic_rx_packet *pkt = a2; |
| 362 | const struct quic_rx_crypto_frm *cf = a3; |
| 363 | const SSL *ssl = a4; |
| 364 | |
| 365 | if (pkt) |
| 366 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 367 | quic_packet_type_enc_level_char(pkt->type), |
| 368 | (unsigned long long)pkt->pn); |
| 369 | if (cf) |
| 370 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 371 | (unsigned long long)cf->offset_node.key, |
| 372 | (unsigned long long)cf->len); |
| 373 | if (ssl) { |
| 374 | 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] | 375 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 376 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 377 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 378 | |
| 379 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 380 | 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] | 381 | } |
| 382 | |
| 383 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 384 | const struct quic_frame *frm = a2; |
| 385 | |
| 386 | if (frm) |
| 387 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 388 | } |
| 389 | |
| 390 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 391 | const struct quic_enc_level *qel = a2; |
| 392 | |
| 393 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 394 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 395 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 396 | " 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] | 397 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 546186b | 2021-08-03 14:25:36 +0200 | [diff] [blame] | 398 | quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)), |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 399 | !!(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] | 400 | (unsigned long long)qc->path->cwnd, |
| 401 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 402 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 403 | (unsigned long long)pktns->tx.in_flight, |
| 404 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 408 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 409 | const struct enc_debug_info *edi = a2; |
| 410 | |
| 411 | if (edi) |
| 412 | chunk_appendf(&trace_buf, |
| 413 | " payload=@%p payload_len=%llu" |
| 414 | " aad=@%p aad_len=%llu pn=%llu", |
| 415 | edi->payload, (unsigned long long)edi->payload_len, |
| 416 | edi->aad, (unsigned long long)edi->aad_len, |
| 417 | (unsigned long long)edi->pn); |
| 418 | } |
| 419 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 420 | if (mask & QUIC_EV_CONN_RMHP) { |
| 421 | const struct quic_rx_packet *pkt = a2; |
| 422 | |
| 423 | if (pkt) { |
| 424 | const int *ret = a3; |
| 425 | |
| 426 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 427 | if (ret && *ret) |
| 428 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 429 | pkt->pnl, (unsigned long long)pkt->pn); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 434 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 435 | const unsigned long *val1 = a3; |
| 436 | const unsigned long *val2 = a4; |
| 437 | |
| 438 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 439 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 440 | if (val1) |
| 441 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 442 | if (val2) |
| 443 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 444 | } |
| 445 | |
| 446 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 447 | const unsigned int *rtt_sample = a2; |
| 448 | const unsigned int *ack_delay = a3; |
| 449 | const struct quic_loss *ql = a4; |
| 450 | |
| 451 | if (rtt_sample) |
| 452 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 453 | if (ack_delay) |
| 454 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 455 | if (ql) |
| 456 | chunk_appendf(&trace_buf, |
| 457 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 458 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 459 | } |
| 460 | if (mask & QUIC_EV_CONN_CC) { |
| 461 | const struct quic_cc_event *ev = a2; |
| 462 | const struct quic_cc *cc = a3; |
| 463 | |
| 464 | if (a2) |
| 465 | quic_cc_event_trace(&trace_buf, ev); |
| 466 | if (a3) |
| 467 | quic_cc_state_trace(&trace_buf, cc); |
| 468 | } |
| 469 | |
| 470 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 471 | const struct quic_pktns *pktns = a2; |
| 472 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 473 | |
| 474 | if (pktns) { |
| 475 | chunk_appendf(&trace_buf, " pktns=%s", |
| 476 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 477 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 478 | if (pktns->tx.loss_time) |
| 479 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 480 | 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] | 481 | } |
| 482 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 483 | struct quic_tx_packet *pkt; |
| 484 | |
| 485 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 486 | list_for_each_entry(pkt, lost_pkts, list) |
| 487 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | 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] | 492 | const struct quic_pktns *pktns = a2; |
| 493 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 494 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 495 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 496 | if (ifae_pkts) |
| 497 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 498 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 499 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 500 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 501 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 502 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 503 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 504 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 505 | if (pktns->tx.in_flight) |
| 506 | 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] | 507 | if (pktns->tx.loss_time) |
| 508 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 509 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 510 | } |
| 511 | if (mask & QUIC_EV_CONN_SPTO) { |
| 512 | if (pktns->tx.time_of_last_eliciting) |
| 513 | chunk_appendf(&trace_buf, " tole=%dms", |
| 514 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 515 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 516 | 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] | 517 | } |
| 518 | } |
| 519 | |
| 520 | if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) { |
| 521 | chunk_appendf(&trace_buf, |
| 522 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 527 | const struct quic_tx_packet *pkt = a2; |
| 528 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 529 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 530 | (unsigned long long)qc->path->cwnd, |
| 531 | (unsigned long long)qc->path->prep_in_flight, |
| 532 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 533 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 534 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 535 | (unsigned long)pkt->pn_node.key, |
| 536 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 537 | 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] | 538 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 539 | } |
| 540 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 541 | |
| 542 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 543 | const uint8_t *alert = a2; |
| 544 | const enum ssl_encryption_level_t *level = a3; |
| 545 | |
| 546 | if (alert) |
| 547 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 548 | if (level) |
| 549 | chunk_appendf(&trace_buf, " el=%c", |
| 550 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 551 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 552 | |
| 553 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 554 | const size_t *sz1 = a2; |
| 555 | const size_t *sz2 = a3; |
| 556 | const size_t *sz3 = a4; |
| 557 | |
| 558 | if (sz1) |
| 559 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 560 | if (sz2) |
| 561 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 562 | if (sz3) |
| 563 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 564 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 565 | |
| 566 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 567 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 568 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 569 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 570 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 571 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 572 | } |
| 573 | if (mask & QUIC_EV_CONN_LPKT) { |
| 574 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 575 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 576 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 577 | if (pkt) { |
| 578 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 579 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 580 | if (pkt->pn_node.key != (uint64_t)-1) |
| 581 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 582 | } |
| 583 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 584 | if (len) |
| 585 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | } |
| 589 | |
| 590 | /* 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] | 591 | 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] | 592 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 593 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 594 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 595 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 596 | return 1; |
| 597 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 598 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 599 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 600 | if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) || |
| 601 | (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] | 602 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 603 | return 1; |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 609 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 610 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 611 | 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] | 612 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 613 | struct quic_pktns *pktns; |
| 614 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 615 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 616 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 617 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 618 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 619 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 620 | pktns = quic_loss_pktns(qc); |
| 621 | if (tick_isset(pktns->tx.loss_time)) { |
| 622 | qc->timer = pktns->tx.loss_time; |
| 623 | goto out; |
| 624 | } |
| 625 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 626 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 627 | * cancelled for a server which reached the anti-amplification limit. |
| 628 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 629 | if (!quic_peer_validated_addr(qc) && |
| 630 | (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 631 | 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] | 632 | qc->timer = TICK_ETERNITY; |
| 633 | goto out; |
| 634 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 635 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 636 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 637 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 638 | /* Timer cancellation. */ |
| 639 | qc->timer = TICK_ETERNITY; |
| 640 | goto out; |
| 641 | } |
| 642 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 643 | handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE; |
| 644 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 645 | if (tick_isset(pto)) |
| 646 | qc->timer = pto; |
| 647 | out: |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 648 | if (qc->timer_task && qc->timer != TICK_ETERNITY) |
Amaury Denoyelle | 336f6fd | 2021-10-05 14:42:25 +0200 | [diff] [blame] | 649 | task_schedule(qc->timer_task, qc->timer); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 650 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 651 | } |
| 652 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 653 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 654 | * connection. |
| 655 | * Return 1 if succeeded, 0 if not. |
| 656 | */ |
| 657 | static int quic_tls_key_update(struct quic_conn *qc) |
| 658 | { |
| 659 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 660 | struct quic_tls_secrets *rx, *tx; |
| 661 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 662 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 663 | |
| 664 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 665 | rx = &tls_ctx->rx; |
| 666 | tx = &tls_ctx->tx; |
| 667 | nxt_rx = &qc->ku.nxt_rx; |
| 668 | nxt_tx = &qc->ku.nxt_tx; |
| 669 | |
| 670 | /* Prepare new RX secrets */ |
| 671 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 672 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 673 | 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] | 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 678 | nxt_rx->key, nxt_rx->keylen, |
| 679 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 680 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 681 | 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] | 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | /* Prepare new TX secrets */ |
| 686 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 687 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 688 | 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] | 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 693 | nxt_tx->key, nxt_tx->keylen, |
| 694 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 695 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 696 | 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] | 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | return 1; |
| 701 | } |
| 702 | |
| 703 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 704 | * Must be used after having updated them. |
| 705 | * Always succeeds. |
| 706 | */ |
| 707 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 708 | { |
| 709 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 710 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 711 | |
| 712 | /* Rotate the RX secrets */ |
| 713 | curr_secret = tls_ctx->rx.secret; |
| 714 | curr_iv = tls_ctx->rx.iv; |
| 715 | curr_key = tls_ctx->rx.key; |
| 716 | |
| 717 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 718 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 719 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 720 | |
| 721 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 722 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 723 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 724 | |
| 725 | qc->ku.prv_rx.secret = curr_secret; |
| 726 | qc->ku.prv_rx.iv = curr_iv; |
| 727 | qc->ku.prv_rx.key = curr_key; |
| 728 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 729 | |
| 730 | /* Update the TX secrets */ |
| 731 | curr_secret = tls_ctx->tx.secret; |
| 732 | curr_iv = tls_ctx->tx.iv; |
| 733 | curr_key = tls_ctx->tx.key; |
| 734 | |
| 735 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 736 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 737 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 738 | |
| 739 | qc->ku.nxt_tx.secret = curr_secret; |
| 740 | qc->ku.nxt_tx.iv = curr_iv; |
| 741 | qc->ku.nxt_tx.key = curr_key; |
| 742 | } |
| 743 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 744 | #ifndef OPENSSL_IS_BORINGSSL |
| 745 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 746 | const uint8_t *read_secret, |
| 747 | const uint8_t *write_secret, size_t secret_len) |
| 748 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 749 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 750 | struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 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 | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 754 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, 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); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 756 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 757 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, 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 | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 762 | TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, 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 | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 776 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, 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 | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 788 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, 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: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 794 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 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 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 802 | if (!quic_transport_params_store(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) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 807 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 808 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 809 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 810 | |
| 811 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 812 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 813 | TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 814 | goto err; |
| 815 | } |
| 816 | |
| 817 | memcpy(rx->secret, read_secret, secret_len); |
| 818 | rx->secretlen = secret_len; |
| 819 | memcpy(tx->secret, write_secret, secret_len); |
| 820 | tx->secretlen = secret_len; |
| 821 | /* Initialize all the secret keys lengths */ |
| 822 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 823 | /* Prepare the next key update */ |
| 824 | if (!quic_tls_key_update(qc)) |
| 825 | goto err; |
| 826 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 827 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 828 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 829 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 830 | |
| 831 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 832 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 833 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 834 | } |
| 835 | #else |
| 836 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 837 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 838 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 839 | */ |
| 840 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 841 | const SSL_CIPHER *cipher, |
| 842 | const uint8_t *secret, size_t secret_len) |
| 843 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 844 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 845 | struct quic_tls_ctx *tls_ctx = |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 846 | &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 847 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 848 | TRACE_ENTER(QUIC_EV_CONN_RSEC, qc); |
| 849 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 850 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 851 | goto out; |
| 852 | } |
| 853 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 854 | tls_ctx->rx.aead = tls_aead(cipher); |
| 855 | tls_ctx->rx.md = tls_md(cipher); |
| 856 | tls_ctx->rx.hp = tls_hp(cipher); |
| 857 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 858 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 859 | goto err; |
| 860 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 861 | 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] | 862 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 863 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 864 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 865 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 866 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 867 | goto err; |
| 868 | } |
| 869 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 870 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 871 | const unsigned char *buf; |
| 872 | size_t buflen; |
| 873 | |
| 874 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 875 | if (!buflen) |
| 876 | goto err; |
| 877 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 878 | if (!quic_transport_params_store(qc, 1, buf, buf + buflen)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 879 | goto err; |
| 880 | } |
| 881 | |
| 882 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 883 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 884 | TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 885 | |
| 886 | return 1; |
| 887 | |
| 888 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 889 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 894 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 895 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 896 | */ |
| 897 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 898 | const SSL_CIPHER *cipher, |
| 899 | const uint8_t *secret, size_t secret_len) |
| 900 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 901 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 902 | struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 903 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 904 | TRACE_ENTER(QUIC_EV_CONN_WSEC, qc); |
| 905 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 906 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 907 | goto out; |
| 908 | } |
| 909 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 910 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 911 | goto err; |
| 912 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 913 | tls_ctx->tx.aead = tls_aead(cipher); |
| 914 | tls_ctx->tx.md = tls_md(cipher); |
| 915 | tls_ctx->tx.hp = tls_hp(cipher); |
| 916 | |
| 917 | 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] | 918 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 919 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 920 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 921 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 922 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 923 | goto err; |
| 924 | } |
| 925 | |
| 926 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 927 | TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 928 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 929 | return 1; |
| 930 | |
| 931 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 932 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | #endif |
| 936 | |
| 937 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 938 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 939 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 940 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 941 | * store all the data. |
| 942 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 943 | */ |
| 944 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 945 | const unsigned char *data, size_t len) |
| 946 | { |
| 947 | struct quic_crypto_buf **qcb; |
| 948 | /* The remaining byte to store in CRYPTO buffers. */ |
| 949 | size_t cf_offset, cf_len, *nb_buf; |
| 950 | unsigned char *pos; |
| 951 | |
| 952 | nb_buf = &qel->tx.crypto.nb_buf; |
| 953 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 954 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 955 | cf_len = len; |
| 956 | |
| 957 | while (len) { |
| 958 | size_t to_copy, room; |
| 959 | |
| 960 | pos = (*qcb)->data + (*qcb)->sz; |
| 961 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 962 | to_copy = len > room ? room : len; |
| 963 | if (to_copy) { |
| 964 | memcpy(pos, data, to_copy); |
| 965 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 966 | qel->tx.crypto.sz += to_copy; |
| 967 | (*qcb)->sz += to_copy; |
| 968 | pos += to_copy; |
| 969 | len -= to_copy; |
| 970 | data += to_copy; |
| 971 | } |
| 972 | else { |
| 973 | struct quic_crypto_buf **tmp; |
| 974 | |
| 975 | tmp = realloc(qel->tx.crypto.bufs, |
| 976 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 977 | if (tmp) { |
| 978 | qel->tx.crypto.bufs = tmp; |
| 979 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 980 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 981 | if (!*qcb) |
| 982 | return 0; |
| 983 | |
| 984 | (*qcb)->sz = 0; |
| 985 | ++*nb_buf; |
| 986 | } |
| 987 | else { |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 994 | * have been buffered. |
| 995 | */ |
| 996 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 997 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 998 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 999 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 1000 | /* There is at most one CRYPTO frame in this packet number |
| 1001 | * space. Let's look for it. |
| 1002 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1003 | 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] | 1004 | if (frm->type != QUIC_FT_CRYPTO) |
| 1005 | continue; |
| 1006 | |
| 1007 | /* Found */ |
| 1008 | found = frm; |
| 1009 | break; |
| 1010 | } |
| 1011 | |
| 1012 | if (found) { |
| 1013 | found->crypto.len += cf_len; |
| 1014 | } |
| 1015 | else { |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1016 | frm = pool_alloc(pool_head_quic_frame); |
| 1017 | if (!frm) |
| 1018 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1019 | |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1020 | frm->type = QUIC_FT_CRYPTO; |
| 1021 | frm->crypto.offset = cf_offset; |
| 1022 | frm->crypto.len = cf_len; |
| 1023 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1024 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1025 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | return len == 0; |
| 1029 | } |
| 1030 | |
| 1031 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1032 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1033 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1034 | { |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1035 | 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] | 1036 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1037 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1038 | } |
| 1039 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1040 | /* Set the application for <qc> QUIC connection. |
| 1041 | * Return 1 if succeeded, 0 if not. |
| 1042 | */ |
| 1043 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1044 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1045 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1046 | qc->app_ops = &h3_ops; |
| 1047 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1048 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1049 | else |
| 1050 | return 0; |
| 1051 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1052 | return 1; |
| 1053 | } |
| 1054 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1055 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1056 | * wants to provide the QUIC layer with CRYPTO data. |
| 1057 | * Returns 1 if succeeded, 0 if not. |
| 1058 | */ |
| 1059 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1060 | const uint8_t *data, size_t len) |
| 1061 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1062 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1063 | enum quic_tls_enc_level tel; |
| 1064 | struct quic_enc_level *qel; |
| 1065 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1066 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1067 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1068 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 1069 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1070 | goto out; |
| 1071 | } |
| 1072 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1073 | tel = ssl_to_quic_enc_level(level); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1074 | qel = &qc->els[tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1075 | |
| 1076 | if (tel == -1) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1077 | TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1078 | goto err; |
| 1079 | } |
| 1080 | |
| 1081 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1082 | TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1083 | goto err; |
| 1084 | } |
| 1085 | |
| 1086 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1087 | qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1088 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1089 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1090 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1091 | return 1; |
| 1092 | |
| 1093 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1094 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | int ha_quic_flush_flight(SSL *ssl) |
| 1099 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1100 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1101 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1102 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1103 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1104 | |
| 1105 | return 1; |
| 1106 | } |
| 1107 | |
| 1108 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1109 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1110 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1111 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1112 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1113 | quic_set_tls_alert(qc, alert); |
| 1114 | HA_ATOMIC_STORE(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1115 | return 1; |
| 1116 | } |
| 1117 | |
| 1118 | /* QUIC TLS methods */ |
| 1119 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1120 | #ifdef OPENSSL_IS_BORINGSSL |
| 1121 | .set_read_secret = ha_set_rsec, |
| 1122 | .set_write_secret = ha_set_wsec, |
| 1123 | #else |
| 1124 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1125 | #endif |
| 1126 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1127 | .flush_flight = ha_quic_flush_flight, |
| 1128 | .send_alert = ha_quic_send_alert, |
| 1129 | }; |
| 1130 | |
| 1131 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1132 | * Returns an error count. |
| 1133 | */ |
| 1134 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1135 | { |
| 1136 | struct proxy *curproxy = bind_conf->frontend; |
| 1137 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1138 | int cfgerr = 0; |
| 1139 | |
| 1140 | #if 0 |
| 1141 | /* XXX Did not manage to use this. */ |
| 1142 | const char *ciphers = |
| 1143 | "TLS_AES_128_GCM_SHA256:" |
| 1144 | "TLS_AES_256_GCM_SHA384:" |
| 1145 | "TLS_CHACHA20_POLY1305_SHA256:" |
| 1146 | "TLS_AES_128_CCM_SHA256"; |
| 1147 | #endif |
Frédéric Lécaille | 4b1fddc | 2021-07-01 17:09:05 +0200 | [diff] [blame] | 1148 | 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] | 1149 | long options = |
| 1150 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1151 | SSL_OP_SINGLE_ECDH_USE | |
| 1152 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1153 | SSL_CTX *ctx; |
| 1154 | |
| 1155 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1156 | bind_conf->initial_ctx = ctx; |
| 1157 | |
| 1158 | SSL_CTX_set_options(ctx, options); |
| 1159 | #if 0 |
| 1160 | if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) { |
| 1161 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' " |
| 1162 | "for bind '%s' at [%s:%d].\n", |
| 1163 | curproxy->id, ciphers, |
| 1164 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1165 | cfgerr++; |
| 1166 | } |
| 1167 | #endif |
| 1168 | |
| 1169 | if (SSL_CTX_set1_curves_list(ctx, groups) != 1) { |
| 1170 | ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' " |
| 1171 | "for bind '%s' at [%s:%d].\n", |
| 1172 | curproxy->id, groups, |
| 1173 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1174 | cfgerr++; |
| 1175 | } |
| 1176 | |
| 1177 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1178 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1179 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1180 | SSL_CTX_set_default_verify_paths(ctx); |
| 1181 | |
| 1182 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1183 | #ifdef OPENSSL_IS_BORINGSSL |
| 1184 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1185 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1186 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1187 | if (bind_conf->ssl_conf.early_data) { |
| 1188 | 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] | 1189 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1190 | } |
| 1191 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1192 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1193 | #else |
| 1194 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1195 | #endif |
| 1196 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1197 | #endif |
| 1198 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1199 | |
| 1200 | return cfgerr; |
| 1201 | } |
| 1202 | |
| 1203 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1204 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1205 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1206 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1207 | */ |
| 1208 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1209 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1210 | { |
| 1211 | uint64_t expected_pn = largest_pn + 1; |
| 1212 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1213 | uint64_t pn_hwin = pn_win / 2; |
| 1214 | uint64_t pn_mask = pn_win - 1; |
| 1215 | uint64_t candidate_pn; |
| 1216 | |
| 1217 | |
| 1218 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1219 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1220 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1221 | candidate_pn + pn_hwin <= expected_pn) |
| 1222 | return candidate_pn + pn_win; |
| 1223 | |
| 1224 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1225 | return candidate_pn - pn_win; |
| 1226 | |
| 1227 | return candidate_pn; |
| 1228 | } |
| 1229 | |
| 1230 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1231 | * cryptographic context. |
| 1232 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1233 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1234 | * <end> points to one byte past the end of this packet. |
| 1235 | * Returns 1 if succeeded, 0 if not. |
| 1236 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1237 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1238 | 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] | 1239 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1240 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1241 | { |
| 1242 | int ret, outlen, i, pnlen; |
| 1243 | uint64_t packet_number; |
| 1244 | uint32_t truncated_pn = 0; |
| 1245 | unsigned char mask[5] = {0}; |
| 1246 | unsigned char *sample; |
| 1247 | EVP_CIPHER_CTX *cctx; |
| 1248 | unsigned char *hp_key; |
| 1249 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1250 | /* Check there is enough data in this packet. */ |
| 1251 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1252 | 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] | 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | cctx = EVP_CIPHER_CTX_new(); |
| 1257 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1258 | 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] | 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | ret = 0; |
| 1263 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1264 | |
| 1265 | hp_key = tls_ctx->rx.hp_key; |
| 1266 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1267 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1268 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1269 | 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] | 1270 | goto out; |
| 1271 | } |
| 1272 | |
| 1273 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1274 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1275 | for (i = 0; i < pnlen; i++) { |
| 1276 | pn[i] ^= mask[i + 1]; |
| 1277 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1278 | } |
| 1279 | |
| 1280 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1281 | /* Store remaining information for this unprotected header */ |
| 1282 | pkt->pn = packet_number; |
| 1283 | pkt->pnl = pnlen; |
| 1284 | |
| 1285 | ret = 1; |
| 1286 | |
| 1287 | out: |
| 1288 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1289 | |
| 1290 | return ret; |
| 1291 | } |
| 1292 | |
| 1293 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1294 | * address, with <payload_len> as payload length, <aad> as address of |
| 1295 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1296 | * context. |
| 1297 | * Returns 1 if succeeded, 0 if not. |
| 1298 | */ |
| 1299 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1300 | 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] | 1301 | 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] | 1302 | { |
| 1303 | unsigned char iv[12]; |
| 1304 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1305 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1306 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1307 | |
| 1308 | 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] | 1309 | 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] | 1310 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1314 | tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1315 | 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] | 1316 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1320 | |
| 1321 | err: |
| 1322 | 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] | 1323 | 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] | 1324 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1328 | * Returns 1 if succeeded, 0 if not. |
| 1329 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1330 | 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] | 1331 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1332 | int ret, kp_changed; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1333 | unsigned char iv[12]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1334 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1335 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1336 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1337 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1338 | |
| 1339 | kp_changed = 0; |
| 1340 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1341 | /* The two tested bits are not at the same position, |
| 1342 | * this is why they are first both inversed. |
| 1343 | */ |
| 1344 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1345 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1346 | /* The lowest packet number of a previous key phase |
| 1347 | * cannot be null if it really stores previous key phase |
| 1348 | * secrets. |
| 1349 | */ |
| 1350 | if (!pkt->qc->ku.prv_rx.pn) |
| 1351 | return 0; |
| 1352 | |
| 1353 | rx_iv = pkt->qc->ku.prv_rx.iv; |
| 1354 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1355 | } |
| 1356 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1357 | /* Next key phase */ |
| 1358 | kp_changed = 1; |
| 1359 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
| 1360 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1361 | } |
| 1362 | } |
| 1363 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1364 | |
| 1365 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1366 | return 0; |
| 1367 | |
| 1368 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1369 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1370 | tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1371 | if (!ret) |
| 1372 | return 0; |
| 1373 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1374 | /* Update the keys only if the packet decryption succeeded. */ |
| 1375 | if (kp_changed) { |
| 1376 | quic_tls_rotate_keys(pkt->qc); |
| 1377 | /* Toggle the Key Phase bit */ |
| 1378 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1379 | /* Store the lowest packet number received for the current key phase */ |
| 1380 | tls_ctx->rx.pn = pkt->pn; |
| 1381 | /* Prepare the next key update */ |
| 1382 | if (!quic_tls_key_update(pkt->qc)) |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1386 | /* Update the packet length (required to parse the frames). */ |
| 1387 | pkt->len = pkt->aad_len + ret; |
| 1388 | |
| 1389 | return 1; |
| 1390 | } |
| 1391 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1392 | /* Remove from <qcs> stream the acknowledged frames. |
| 1393 | * Never fails. |
| 1394 | */ |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1395 | static int qcs_try_to_consume(struct qcs *qcs) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1396 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1397 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1398 | struct eb64_node *frm_node; |
| 1399 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1400 | ret = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1401 | frm_node = eb64_first(&qcs->tx.acked_frms); |
| 1402 | while (frm_node) { |
| 1403 | struct quic_stream *strm; |
| 1404 | |
| 1405 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
| 1406 | if (strm->offset.key != qcs->tx.ack_offset) |
| 1407 | break; |
| 1408 | |
| 1409 | b_del(strm->buf, strm->len); |
| 1410 | qcs->tx.ack_offset += strm->len; |
| 1411 | frm_node = eb64_next(frm_node); |
| 1412 | eb64_delete(&strm->offset); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1413 | ret = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1414 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1415 | |
| 1416 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1417 | } |
| 1418 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1419 | /* 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] | 1420 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1421 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1422 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1423 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1424 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1425 | 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] | 1426 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1427 | switch (frm->type) { |
| 1428 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1429 | { |
| 1430 | struct qcs *qcs = frm->stream.qcs; |
| 1431 | struct quic_stream *strm = &frm->stream; |
| 1432 | |
| 1433 | if (qcs->tx.ack_offset == strm->offset.key) { |
| 1434 | b_del(strm->buf, strm->len); |
| 1435 | qcs->tx.ack_offset += strm->len; |
| 1436 | LIST_DELETE(&frm->list); |
| 1437 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1438 | stream_acked = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1439 | } |
| 1440 | else { |
| 1441 | eb64_insert(&qcs->tx.acked_frms, &strm->offset); |
| 1442 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1443 | stream_acked |= qcs_try_to_consume(qcs); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1444 | } |
| 1445 | break; |
| 1446 | default: |
| 1447 | LIST_DELETE(&frm->list); |
| 1448 | pool_free(pool_head_quic_frame, frm); |
| 1449 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1450 | |
| 1451 | if (stream_acked) { |
| 1452 | struct qcc *qcc = qc->qcc; |
| 1453 | |
| 1454 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1455 | tasklet_wakeup(qcc->subs->tasklet); |
| 1456 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1457 | if (!qcc->subs->events) |
| 1458 | qcc->subs = NULL; |
| 1459 | } |
| 1460 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1464 | * deallocating them, and their TX frames. |
| 1465 | * Returns the last node reached to be used for the next range. |
| 1466 | * May be NULL if <largest> node could not be found. |
| 1467 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1468 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1469 | struct eb_root *pkts, |
| 1470 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1471 | struct list *newly_acked_pkts, |
| 1472 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1473 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1474 | { |
| 1475 | struct eb64_node *node; |
| 1476 | struct quic_tx_packet *pkt; |
| 1477 | |
| 1478 | if (largest_node) |
| 1479 | node = largest_node; |
| 1480 | else { |
| 1481 | node = eb64_lookup(pkts, largest); |
| 1482 | while (!node && largest > smallest) { |
| 1483 | node = eb64_lookup(pkts, --largest); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1488 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1489 | |
| 1490 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1491 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1492 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1493 | 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] | 1494 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1495 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1496 | node = eb64_prev(node); |
| 1497 | eb64_delete(&pkt->pn_node); |
| 1498 | } |
| 1499 | |
| 1500 | return node; |
| 1501 | } |
| 1502 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1503 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1504 | * 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] | 1505 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1506 | static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1507 | struct list *pkt_frm_list, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1508 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1509 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1510 | struct quic_frame *frm, *frmbak; |
| 1511 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1512 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1513 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1514 | LIST_DELETE(&frm->list); |
| 1515 | 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] | 1516 | LIST_APPEND(&tmp, &frm->list); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1517 | } |
| 1518 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1519 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1520 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1521 | |
| 1522 | /* Free the TX packets of <pkts> list */ |
| 1523 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1524 | { |
| 1525 | struct quic_tx_packet *pkt, *tmp; |
| 1526 | |
| 1527 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1528 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1529 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1530 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | /* Send a packet loss event nofification to the congestion controller |
| 1535 | * attached to <qc> connection with <lost_bytes> the number of lost bytes, |
| 1536 | * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet |
| 1537 | * at <now_us> current time. |
| 1538 | * Always succeeds. |
| 1539 | */ |
| 1540 | static inline void qc_cc_loss_event(struct quic_conn *qc, |
| 1541 | unsigned int lost_bytes, |
| 1542 | unsigned int newest_time_sent, |
| 1543 | unsigned int period, |
| 1544 | unsigned int now_us) |
| 1545 | { |
| 1546 | struct quic_cc_event ev = { |
| 1547 | .type = QUIC_CC_EVT_LOSS, |
| 1548 | .loss.now_ms = now_ms, |
| 1549 | .loss.max_ack_delay = qc->max_ack_delay, |
| 1550 | .loss.lost_bytes = lost_bytes, |
| 1551 | .loss.newest_time_sent = newest_time_sent, |
| 1552 | .loss.period = period, |
| 1553 | }; |
| 1554 | |
| 1555 | quic_cc_event(&qc->path->cc, &ev); |
| 1556 | } |
| 1557 | |
| 1558 | /* Send a packet ack event nofication for each newly acked packet of |
| 1559 | * <newly_acked_pkts> list and free them. |
| 1560 | * Always succeeds. |
| 1561 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1562 | 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] | 1563 | struct list *newly_acked_pkts) |
| 1564 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1565 | struct quic_tx_packet *pkt, *tmp; |
| 1566 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1567 | |
| 1568 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1569 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1570 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1571 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1572 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1573 | ev.ack.acked = pkt->in_flight_len; |
| 1574 | ev.ack.time_sent = pkt->time_sent; |
| 1575 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1576 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1577 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1578 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | } |
| 1582 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1583 | /* Release all the frames attached to <pktns> packet number space */ |
| 1584 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1585 | { |
| 1586 | struct quic_frame *frm, *frmbak; |
| 1587 | |
| 1588 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1589 | LIST_DELETE(&frm->list); |
| 1590 | pool_free(pool_head_quic_frame, frm); |
| 1591 | } |
| 1592 | } |
| 1593 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1594 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1595 | * their TX frames. |
| 1596 | * Send a packet loss event to the congestion controller if |
| 1597 | * in flight packet have been lost. |
| 1598 | * Also frees the packet in <pkts> list. |
| 1599 | * Never fails. |
| 1600 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1601 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1602 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1603 | struct list *pkts, |
| 1604 | uint64_t now_us) |
| 1605 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1606 | 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] | 1607 | uint64_t lost_bytes; |
| 1608 | |
| 1609 | lost_bytes = 0; |
| 1610 | oldest_lost = newest_lost = NULL; |
| 1611 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1612 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1613 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1614 | lost_bytes += pkt->in_flight_len; |
| 1615 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1616 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1617 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1618 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1619 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1620 | qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1621 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1622 | if (!oldest_lost) { |
| 1623 | oldest_lost = newest_lost = pkt; |
| 1624 | } |
| 1625 | else { |
| 1626 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1627 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1628 | newest_lost = pkt; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | if (lost_bytes) { |
| 1633 | /* Sent a packet loss event to the congestion controller. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1634 | 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] | 1635 | 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] | 1636 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1637 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1638 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1643 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1644 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1645 | * space if any not deemed lost. |
| 1646 | * Should be called after having received an ACK frame with newly acknowledged |
| 1647 | * packets or when the the loss detection timer has expired. |
| 1648 | * Always succeeds. |
| 1649 | */ |
| 1650 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1651 | struct quic_conn *qc, |
| 1652 | struct list *lost_pkts) |
| 1653 | { |
| 1654 | struct eb_root *pkts; |
| 1655 | struct eb64_node *node; |
| 1656 | struct quic_loss *ql; |
| 1657 | unsigned int loss_delay; |
| 1658 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1659 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1660 | pkts = &pktns->tx.pkts; |
| 1661 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1662 | if (eb_is_empty(pkts)) |
| 1663 | goto out; |
| 1664 | |
| 1665 | ql = &qc->path->loss; |
| 1666 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
| 1667 | loss_delay += loss_delay >> 3; |
| 1668 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1669 | |
| 1670 | node = eb64_first(pkts); |
| 1671 | while (node) { |
| 1672 | struct quic_tx_packet *pkt; |
| 1673 | int64_t largest_acked_pn; |
| 1674 | unsigned int loss_time_limit, time_sent; |
| 1675 | |
| 1676 | 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] | 1677 | 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] | 1678 | node = eb64_next(node); |
| 1679 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1680 | break; |
| 1681 | |
| 1682 | time_sent = pkt->time_sent; |
| 1683 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1684 | if (tick_is_le(time_sent, now_ms) || |
| 1685 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1686 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1687 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1688 | } |
| 1689 | else { |
Frédéric Lécaille | dc90c07 | 2021-12-27 18:15:27 +0100 | [diff] [blame] | 1690 | if (tick_isset(pktns->tx.loss_time)) |
| 1691 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1692 | else |
| 1693 | pktns->tx.loss_time = loss_time_limit; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1698 | 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] | 1699 | } |
| 1700 | |
| 1701 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1702 | * 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] | 1703 | * 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] | 1704 | * acked ack-eliciting packet. |
| 1705 | * Return 1, if succeeded, 0 if not. |
| 1706 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1707 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1708 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1709 | struct quic_enc_level *qel, |
| 1710 | unsigned int *rtt_sample, |
| 1711 | const unsigned char **pos, const unsigned char *end) |
| 1712 | { |
| 1713 | struct quic_ack *ack = &frm->ack; |
| 1714 | uint64_t smallest, largest; |
| 1715 | struct eb_root *pkts; |
| 1716 | struct eb64_node *largest_node; |
| 1717 | unsigned int time_sent, pkt_flags; |
| 1718 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1719 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1720 | |
| 1721 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1722 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1723 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1724 | goto err; |
| 1725 | } |
| 1726 | |
| 1727 | if (ack->first_ack_range > ack->largest_ack) { |
| 1728 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1729 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1730 | goto err; |
| 1731 | } |
| 1732 | |
| 1733 | largest = ack->largest_ack; |
| 1734 | smallest = largest - ack->first_ack_range; |
| 1735 | pkts = &qel->pktns->tx.pkts; |
| 1736 | pkt_flags = 0; |
| 1737 | largest_node = NULL; |
| 1738 | time_sent = 0; |
| 1739 | |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1740 | 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] | 1741 | largest_node = eb64_lookup(pkts, largest); |
| 1742 | if (!largest_node) { |
| 1743 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1744 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1745 | } |
| 1746 | else { |
| 1747 | time_sent = eb64_entry(&largest_node->node, |
| 1748 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1749 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1753 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1754 | do { |
| 1755 | uint64_t gap, ack_range; |
| 1756 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1757 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1758 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1759 | if (!ack->ack_range_num--) |
| 1760 | break; |
| 1761 | |
| 1762 | if (!quic_dec_int(&gap, pos, end)) |
| 1763 | goto err; |
| 1764 | |
| 1765 | if (smallest < gap + 2) { |
| 1766 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1767 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1768 | goto err; |
| 1769 | } |
| 1770 | |
| 1771 | largest = smallest - gap - 2; |
| 1772 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1773 | goto err; |
| 1774 | |
| 1775 | if (largest < ack_range) { |
| 1776 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1777 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1778 | goto err; |
| 1779 | } |
| 1780 | |
| 1781 | /* Do not use this node anymore. */ |
| 1782 | largest_node = NULL; |
| 1783 | /* Next range */ |
| 1784 | smallest = largest - ack_range; |
| 1785 | |
| 1786 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1787 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1788 | } while (1); |
| 1789 | |
| 1790 | /* 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] | 1791 | 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] | 1792 | |
| 1793 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1794 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1795 | 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] | 1796 | } |
| 1797 | |
| 1798 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1799 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1800 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1801 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1802 | 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] | 1803 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1804 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 1805 | if (quic_peer_validated_addr(qc)) |
| 1806 | qc->path->loss.pto_count = 0; |
| 1807 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | |
| 1811 | return 1; |
| 1812 | |
| 1813 | err: |
| 1814 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1815 | 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] | 1816 | return 0; |
| 1817 | } |
| 1818 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 1819 | /* This function gives the detail of the SSL error. It is used only |
| 1820 | * if the debug mode and the verbose mode are activated. It dump all |
| 1821 | * the SSL error until the stack was empty. |
| 1822 | */ |
| 1823 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 1824 | { |
| 1825 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 1826 | while (1) { |
| 1827 | unsigned long ret; |
| 1828 | |
| 1829 | ret = ERR_get_error(); |
| 1830 | if (!ret) |
| 1831 | return; |
| 1832 | |
| 1833 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 1834 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1839 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 1840 | const char **str, int *len); |
| 1841 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1842 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 1843 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1844 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1845 | * Return 1 if succeeded, 0 if not. |
| 1846 | */ |
| 1847 | 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] | 1848 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1849 | const unsigned char *data, size_t len, |
| 1850 | struct quic_rx_packet *pkt, |
| 1851 | struct quic_rx_crypto_frm *cf) |
| 1852 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1853 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 1854 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1855 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1856 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 1857 | qc = ctx->qc; |
| 1858 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1859 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 1860 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1861 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 1862 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1863 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1864 | goto err; |
| 1865 | } |
| 1866 | |
| 1867 | el->rx.crypto.offset += len; |
| 1868 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 1869 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1870 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1871 | state = HA_ATOMIC_LOAD(&qc->state); |
| 1872 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1873 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 1874 | if (ssl_err != 1) { |
| 1875 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1876 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1877 | TRACE_PROTO("SSL handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1878 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1879 | goto out; |
| 1880 | } |
| 1881 | |
| 1882 | TRACE_DEVEL("SSL handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1883 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 1884 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 1885 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1886 | goto err; |
| 1887 | } |
| 1888 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1889 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame^] | 1890 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1891 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame^] | 1892 | /* The connection is ready to be accepted. */ |
| 1893 | quic_accept_push_qc(qc); |
| 1894 | } |
| 1895 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1896 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame^] | 1897 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1898 | } else { |
| 1899 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 1900 | if (ssl_err != 1) { |
| 1901 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1902 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1903 | TRACE_DEVEL("SSL post handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1904 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1905 | goto out; |
| 1906 | } |
| 1907 | |
| 1908 | TRACE_DEVEL("SSL post handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1909 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1910 | goto err; |
| 1911 | } |
| 1912 | |
| 1913 | TRACE_PROTO("SSL post handshake succeeded", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1914 | QUIC_EV_CONN_HDSHK, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1915 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 1916 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1917 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1918 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1919 | return 1; |
| 1920 | |
| 1921 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1922 | 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] | 1923 | return 0; |
| 1924 | } |
| 1925 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1926 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 1927 | * <pkt> RX packet. |
| 1928 | * Return it if succeeded, NULL if not. |
| 1929 | */ |
| 1930 | static inline |
| 1931 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 1932 | struct quic_rx_packet *pkt) |
| 1933 | { |
| 1934 | struct quic_rx_strm_frm *frm; |
| 1935 | |
| 1936 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 1937 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1938 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1939 | frm->len = stream_frm->len; |
| 1940 | frm->data = stream_frm->data; |
| 1941 | frm->pkt = pkt; |
| 1942 | } |
| 1943 | |
| 1944 | return frm; |
| 1945 | } |
| 1946 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1947 | /* 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] | 1948 | * 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] | 1949 | */ |
| 1950 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 1951 | { |
| 1952 | size_t ret; |
| 1953 | |
| 1954 | ret = 0; |
| 1955 | while (strm_frm->len) { |
| 1956 | size_t try; |
| 1957 | |
| 1958 | try = b_contig_space(buf); |
| 1959 | if (!try) |
| 1960 | break; |
| 1961 | |
| 1962 | if (try > strm_frm->len) |
| 1963 | try = strm_frm->len; |
| 1964 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1965 | strm_frm->len -= try; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1966 | strm_frm->offset.key += try; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1967 | b_add(buf, try); |
| 1968 | ret += try; |
| 1969 | } |
| 1970 | |
| 1971 | return ret; |
| 1972 | } |
| 1973 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 1974 | /* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer. |
| 1975 | * Also update <strm_frm> frame to reflect the data which have been consumed. |
| 1976 | */ |
| 1977 | static size_t qc_rx_strm_frm_cpy(struct buffer *buf, |
| 1978 | struct quic_rx_strm_frm *strm_frm) |
| 1979 | { |
| 1980 | size_t ret; |
| 1981 | |
| 1982 | ret = 0; |
| 1983 | while (strm_frm->len) { |
| 1984 | size_t try; |
| 1985 | |
| 1986 | try = b_contig_space(buf); |
| 1987 | if (!try) |
| 1988 | break; |
| 1989 | |
| 1990 | if (try > strm_frm->len) |
| 1991 | try = strm_frm->len; |
| 1992 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1993 | strm_frm->len -= try; |
| 1994 | strm_frm->offset_node.key += try; |
| 1995 | b_add(buf, try); |
| 1996 | ret += try; |
| 1997 | } |
| 1998 | |
| 1999 | return ret; |
| 2000 | } |
| 2001 | |
| 2002 | /* Process as much as possible RX STREAM frames received for <qcs> */ |
| 2003 | static size_t qc_treat_rx_strm_frms(struct qcs *qcs) |
| 2004 | { |
| 2005 | int total; |
| 2006 | struct eb64_node *frm_node; |
| 2007 | |
| 2008 | total = 0; |
| 2009 | frm_node = eb64_first(&qcs->rx.frms); |
| 2010 | while (frm_node) { |
| 2011 | int ret; |
| 2012 | struct quic_rx_strm_frm *frm; |
| 2013 | |
| 2014 | frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node); |
| 2015 | if (frm->offset_node.key != qcs->rx.offset) |
| 2016 | break; |
| 2017 | |
| 2018 | ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm); |
| 2019 | qcs->rx.offset += ret; |
| 2020 | total += ret; |
| 2021 | if (frm->len) { |
| 2022 | /* If there is remaining data in this frame |
| 2023 | * this is because the destination buffer is full. |
| 2024 | */ |
| 2025 | break; |
| 2026 | } |
| 2027 | |
| 2028 | frm_node = eb64_next(frm_node); |
| 2029 | quic_rx_packet_refdec(frm->pkt); |
| 2030 | eb64_delete(&frm->offset_node); |
| 2031 | pool_free(pool_head_quic_rx_strm_frm, frm); |
| 2032 | } |
| 2033 | |
| 2034 | return total; |
| 2035 | } |
| 2036 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2037 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2038 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2039 | * If not, the STREAM frame is stored to be treated again later. |
| 2040 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2041 | * Return 1 if succeeded, 0 if not. |
| 2042 | */ |
| 2043 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2044 | struct quic_stream *strm_frm, |
| 2045 | struct quic_conn *qc) |
| 2046 | { |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2047 | int total; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2048 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2049 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2050 | struct quic_rx_strm_frm *frm; |
| 2051 | |
| 2052 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2053 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2054 | 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] | 2055 | return 0; |
| 2056 | } |
| 2057 | |
| 2058 | 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] | 2059 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2060 | size_t diff; |
| 2061 | |
| 2062 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2063 | TRACE_PROTO("Already received STREAM data", |
| 2064 | QUIC_EV_CONN_PSTRM, qc); |
| 2065 | goto out; |
| 2066 | } |
| 2067 | |
| 2068 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2069 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2070 | strm_frm->offset.key = strm->rx.offset; |
| 2071 | strm_frm->len -= diff; |
| 2072 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2073 | } |
| 2074 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2075 | total = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2076 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2077 | int ret; |
| 2078 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2079 | if (!qc_get_buf(strm, &strm->rx.buf)) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2080 | goto store_frm; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2081 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2082 | |
| 2083 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2084 | total += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2085 | strm->rx.offset += ret; |
| 2086 | } |
| 2087 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2088 | total += qc_treat_rx_strm_frms(strm); |
| 2089 | 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] | 2090 | TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2091 | return 0; |
| 2092 | } |
| 2093 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2094 | if (!strm_frm->len) |
| 2095 | goto out; |
| 2096 | |
| 2097 | store_frm: |
| 2098 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2099 | if (!frm) { |
| 2100 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2101 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2102 | return 0; |
| 2103 | } |
| 2104 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2105 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2106 | quic_rx_packet_refinc(pkt); |
| 2107 | |
| 2108 | out: |
| 2109 | return 1; |
| 2110 | } |
| 2111 | |
| 2112 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2113 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2114 | * If not, the STREAM frame is stored to be treated again later. |
| 2115 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2116 | * Return 1 if succeeded, 0 if not. |
| 2117 | */ |
| 2118 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2119 | struct quic_stream *strm_frm, |
| 2120 | struct quic_conn *qc) |
| 2121 | { |
| 2122 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2123 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2124 | struct quic_rx_strm_frm *frm; |
| 2125 | size_t strm_frm_len; |
| 2126 | |
| 2127 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2128 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2129 | 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] | 2130 | return 0; |
| 2131 | } |
| 2132 | |
| 2133 | 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] | 2134 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2135 | size_t diff; |
| 2136 | |
| 2137 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2138 | TRACE_PROTO("Already received STREAM data", |
| 2139 | QUIC_EV_CONN_PSTRM, qc); |
| 2140 | goto out; |
| 2141 | } |
| 2142 | |
| 2143 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2144 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2145 | strm_frm->offset.key = strm->rx.offset; |
| 2146 | strm_frm->len -= diff; |
| 2147 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2151 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2152 | int ret; |
| 2153 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 2154 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2155 | goto store_frm; |
| 2156 | |
| 2157 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 2158 | * of bytes copied. |
| 2159 | */ |
| 2160 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 2161 | /* Inform the application of the arrival of this new stream */ |
| 2162 | 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] | 2163 | 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] | 2164 | return 0; |
| 2165 | } |
| 2166 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2167 | if (ret) |
| 2168 | qcs_notify_recv(strm); |
| 2169 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2170 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2171 | } |
| 2172 | /* Take this frame into an account for the stream flow control */ |
| 2173 | strm->rx.offset += strm_frm_len; |
| 2174 | /* 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] | 2175 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2176 | */ |
| 2177 | if (!strm_frm->len) |
| 2178 | goto out; |
| 2179 | |
| 2180 | store_frm: |
| 2181 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2182 | if (!frm) { |
| 2183 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2184 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2185 | return 0; |
| 2186 | } |
| 2187 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2188 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2189 | quic_rx_packet_refinc(pkt); |
| 2190 | |
| 2191 | out: |
| 2192 | return 1; |
| 2193 | } |
| 2194 | |
| 2195 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2196 | struct quic_stream *strm_frm, |
| 2197 | struct quic_conn *qc) |
| 2198 | { |
| 2199 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2200 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2201 | else |
| 2202 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2203 | } |
| 2204 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2205 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2206 | static void qc_prep_fast_retrans(struct quic_enc_level *qel, |
| 2207 | struct quic_conn *qc) |
| 2208 | { |
| 2209 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2210 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2211 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2212 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2213 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2214 | pkts = &qel->pktns->tx.pkts; |
| 2215 | node = eb64_first(pkts); |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2216 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2217 | while (node) { |
| 2218 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2219 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2220 | break; |
| 2221 | node = eb64_next(node); |
| 2222 | } |
| 2223 | |
| 2224 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2225 | return; |
| 2226 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 2227 | 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] | 2228 | } |
| 2229 | |
| 2230 | /* Prepare a fast retransmission during handshake after a client |
| 2231 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2232 | * up to two datagrams of Initial packets if did not receive all Initial packets |
| 2233 | * and resend them coalescing with others (Handshake here). |
| 2234 | * (Listener only). |
| 2235 | */ |
| 2236 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc) |
| 2237 | { |
| 2238 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2239 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2240 | struct quic_frame *frm, *frmbak; |
| 2241 | |
| 2242 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2243 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2244 | struct quic_enc_level *qel = iqel; |
| 2245 | struct eb_root *pkts; |
| 2246 | struct eb64_node *node; |
| 2247 | struct quic_tx_packet *pkt; |
| 2248 | struct list *tmp = &itmp; |
| 2249 | |
| 2250 | start: |
| 2251 | pkt = NULL; |
| 2252 | pkts = &qel->pktns->tx.pkts; |
| 2253 | node = eb64_first(pkts); |
| 2254 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2255 | while (node) { |
| 2256 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2257 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2258 | break; |
| 2259 | node = eb64_next(node); |
| 2260 | } |
| 2261 | |
| 2262 | if (!pkt) |
| 2263 | goto end; |
| 2264 | |
| 2265 | qel->pktns->tx.pto_probe += 1; |
| 2266 | requeue: |
| 2267 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 2268 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2269 | LIST_DELETE(&frm->list); |
| 2270 | LIST_APPEND(tmp, &frm->list); |
| 2271 | } |
| 2272 | |
| 2273 | if (qel == iqel) { |
| 2274 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2275 | pkt = pkt->next; |
| 2276 | tmp = &htmp; |
| 2277 | hqel->pktns->tx.pto_probe += 1; |
| 2278 | goto requeue; |
| 2279 | } |
| 2280 | |
| 2281 | qel = hqel; |
| 2282 | tmp = &htmp; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2283 | goto start; |
| 2284 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2285 | |
| 2286 | end: |
| 2287 | LIST_SPLICE(&iqel->pktns->tx.frms, &itmp); |
| 2288 | LIST_SPLICE(&hqel->pktns->tx.frms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2289 | } |
| 2290 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2291 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2292 | * as I/O handler context and <qel> as encryption level. |
| 2293 | * Returns 1 if succeeded, 0 if failed. |
| 2294 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2295 | 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] | 2296 | struct quic_enc_level *qel) |
| 2297 | { |
| 2298 | struct quic_frame frm; |
| 2299 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2300 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2301 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2302 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2303 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2304 | /* Skip the AAD */ |
| 2305 | pos = pkt->data + pkt->aad_len; |
| 2306 | end = pkt->data + pkt->len; |
| 2307 | |
| 2308 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2309 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2310 | goto err; |
| 2311 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2312 | 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] | 2313 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2314 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2315 | break; |
| 2316 | case QUIC_FT_PING: |
| 2317 | break; |
| 2318 | case QUIC_FT_ACK: |
| 2319 | { |
| 2320 | unsigned int rtt_sample; |
| 2321 | |
| 2322 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2323 | 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] | 2324 | goto err; |
| 2325 | |
| 2326 | if (rtt_sample) { |
| 2327 | unsigned int ack_delay; |
| 2328 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2329 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2330 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ? |
| 2331 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2332 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2333 | 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] | 2334 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2335 | break; |
| 2336 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2337 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2338 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2339 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2340 | { |
| 2341 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2342 | |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 2343 | if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) { |
| 2344 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2345 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2346 | |
| 2347 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2348 | cfdebug.len = frm.crypto.len; |
| 2349 | TRACE_PROTO("CRYPTO data discarded", |
| 2350 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2351 | break; |
| 2352 | } |
| 2353 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2354 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2355 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2356 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2357 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2358 | |
| 2359 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2360 | cfdebug.len = frm.crypto.len; |
| 2361 | /* Nothing to do */ |
| 2362 | TRACE_PROTO("Already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2363 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2364 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2365 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2366 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2367 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2368 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2369 | else { |
| 2370 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2371 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2372 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2373 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2374 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2375 | cfdebug.len = frm.crypto.len; |
| 2376 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2377 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2378 | frm.crypto.len -= diff; |
| 2379 | frm.crypto.data += diff; |
| 2380 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2381 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2382 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2383 | |
| 2384 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2385 | /* 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] | 2386 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2387 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2388 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2389 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2390 | if (!qc_provide_cdata(qel, ctx, |
| 2391 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2392 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2393 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2394 | |
| 2395 | break; |
| 2396 | } |
| 2397 | |
| 2398 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2399 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2400 | if (!cf) { |
| 2401 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2402 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2403 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2404 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2405 | |
| 2406 | cf->offset_node.key = frm.crypto.offset; |
| 2407 | cf->len = frm.crypto.len; |
| 2408 | cf->data = frm.crypto.data; |
| 2409 | cf->pkt = pkt; |
| 2410 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2411 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2412 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2413 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2414 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2415 | { |
| 2416 | struct quic_stream *stream = &frm.stream; |
| 2417 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2418 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2419 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2420 | goto err; |
| 2421 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2422 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2423 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2424 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2425 | goto err; |
| 2426 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2427 | break; |
| 2428 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2429 | case QUIC_FT_MAX_DATA: |
| 2430 | case QUIC_FT_MAX_STREAM_DATA: |
| 2431 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2432 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2433 | case QUIC_FT_DATA_BLOCKED: |
| 2434 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2435 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2436 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2437 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2438 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2439 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2440 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2441 | break; |
| 2442 | case QUIC_FT_CONNECTION_CLOSE: |
| 2443 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Amaury Denoyelle | 5154e7a | 2021-12-08 14:51:04 +0100 | [diff] [blame] | 2444 | /* warn the mux to close the connection */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2445 | qc->qcc->flags |= QC_CF_CC_RECV; |
| 2446 | tasklet_wakeup(qc->qcc->wait_event.tasklet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2447 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2448 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2449 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2450 | goto err; |
| 2451 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2452 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2453 | break; |
| 2454 | default: |
| 2455 | goto err; |
| 2456 | } |
| 2457 | } |
| 2458 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2459 | if (fast_retrans) |
| 2460 | qc_prep_hdshk_fast_retrans(qc); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2461 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2462 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2463 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2464 | * be discarded. |
| 2465 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2466 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2467 | int state = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2468 | |
| 2469 | if (state >= QUIC_HS_ST_SERVER_INITIAL) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2470 | 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] | 2471 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2472 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2473 | qc_set_timer(ctx->qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2474 | 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] | 2475 | 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] | 2476 | if (state < QUIC_HS_ST_SERVER_HANDSHAKE) |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2477 | 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] | 2478 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2479 | } |
| 2480 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2481 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2482 | return 1; |
| 2483 | |
| 2484 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2485 | 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] | 2486 | return 0; |
| 2487 | } |
| 2488 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2489 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2490 | * 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] | 2491 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2492 | * This function must be called only after having built a correct datagram. |
| 2493 | * Always succeeds. |
| 2494 | */ |
| 2495 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2496 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2497 | { |
| 2498 | write_u16(cb_wr(cbuf), dglen); |
| 2499 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2500 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2501 | } |
| 2502 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2503 | /* 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] | 2504 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2505 | * several packets in the same datagram. A header made of two fields is added |
| 2506 | * to each datagram: the datagram length followed by the address of the first |
| 2507 | * packet in this datagram. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2508 | * Returns 1 if succeeded, or 0 if something wrong happened. |
| 2509 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 2510 | static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr, |
| 2511 | enum quic_tls_enc_level tel, |
| 2512 | enum quic_tls_enc_level next_tel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2513 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2514 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2515 | struct cbuf *cbuf; |
| 2516 | unsigned char *end_buf, *end, *pos, *spos; |
| 2517 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2518 | /* length of datagrams */ |
| 2519 | uint16_t dglen; |
| 2520 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2521 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2522 | /* Each datagram is prepended with its length followed by the |
| 2523 | * address of the first packet in the datagram. |
| 2524 | */ |
| 2525 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2526 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2527 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2528 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2529 | total = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2530 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2531 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2532 | padding = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2533 | qel = &qc->els[tel]; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2534 | cbuf = qr->cbuf; |
| 2535 | spos = pos = cb_wr(cbuf); |
| 2536 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2537 | * to ensure there is enough room to mark the end of prepared |
| 2538 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2539 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2540 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2541 | first_pkt = prv_pkt = NULL; |
| 2542 | 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] | 2543 | int err, probe, ack, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2544 | enum quic_pkt_type pkt_type; |
| 2545 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2546 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2547 | probe = ack = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 2548 | cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 2549 | if (!cc) { |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 2550 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2551 | 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] | 2552 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2553 | /* 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] | 2554 | * 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] | 2555 | * 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] | 2556 | * and if there is no more CRYPTO data available or in flight |
| 2557 | * congestion control limit is reached for prepared data |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2558 | */ |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2559 | 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] | 2560 | (!cc && !ack && !probe && |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2561 | (LIST_ISEMPTY(&qel->pktns->tx.frms) || |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2562 | qc->path->prep_in_flight >= qc->path->cwnd))) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2563 | 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] | 2564 | /* Set the current datagram as prepared into <cbuf> if |
| 2565 | * the was already a correct packet which was previously written. |
| 2566 | */ |
| 2567 | if (prv_pkt) |
| 2568 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2569 | /* Let's select the next encryption level */ |
| 2570 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 2571 | tel = next_tel; |
| 2572 | qel = &qc->els[tel]; |
| 2573 | /* Build a new datagram */ |
| 2574 | prv_pkt = NULL; |
| 2575 | continue; |
| 2576 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2577 | break; |
| 2578 | } |
| 2579 | |
| 2580 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2581 | if (!prv_pkt) { |
| 2582 | /* Leave room for the datagram header */ |
| 2583 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2584 | 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] | 2585 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2586 | } |
| 2587 | else { |
| 2588 | end = pos + qc->path->mtu; |
| 2589 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2590 | } |
| 2591 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2592 | 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] | 2593 | pkt_type, ack, probe, cc, &err); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2594 | /* 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] | 2595 | if (err < 0) { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2596 | if (ack) |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2597 | 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] | 2598 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2599 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2600 | case -2: |
| 2601 | goto err; |
| 2602 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2603 | /* If there was already a correct packet present, set the |
| 2604 | * current datagram as prepared into <cbuf>. |
| 2605 | */ |
| 2606 | if (prv_pkt) { |
| 2607 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2608 | goto stop_build; |
| 2609 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2610 | goto out; |
| 2611 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2612 | break; |
| 2613 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2614 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2615 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2616 | if (!cur_pkt) |
| 2617 | goto err; |
| 2618 | |
| 2619 | total += cur_pkt->len; |
| 2620 | /* keep trace of the first packet in the datagram */ |
| 2621 | if (!first_pkt) |
| 2622 | first_pkt = cur_pkt; |
| 2623 | /* Attach the current one to the previous one */ |
| 2624 | if (prv_pkt) |
| 2625 | prv_pkt->next = cur_pkt; |
| 2626 | /* Let's say we have to build a new dgram */ |
| 2627 | prv_pkt = NULL; |
| 2628 | dglen += cur_pkt->len; |
| 2629 | /* Client: discard the Initial encryption keys as soon as |
| 2630 | * a handshake packet could be built. |
| 2631 | */ |
| 2632 | if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL && |
| 2633 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2634 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2635 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 2636 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2637 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2638 | 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] | 2639 | 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] | 2640 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE); |
| 2641 | } |
| 2642 | /* If the data for the current encryption level have all been sent, |
| 2643 | * select the next level. |
| 2644 | */ |
| 2645 | 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] | 2646 | (LIST_ISEMPTY(&qel->pktns->tx.frms))) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2647 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2648 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2649 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 2650 | tel = next_tel; |
| 2651 | qel = &qc->els[tel]; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2652 | if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2653 | /* If there is data for the next level, do not |
| 2654 | * consume a datagram. |
| 2655 | */ |
| 2656 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2657 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2658 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2659 | /* If we have to build a new datagram, set the current datagram as |
| 2660 | * prepared into <cbuf>. |
| 2661 | */ |
| 2662 | if (!prv_pkt) { |
| 2663 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2664 | first_pkt = NULL; |
| 2665 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2666 | padding = 0; |
| 2667 | } |
| 2668 | 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] | 2669 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2670 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2671 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | stop_build: |
| 2676 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2677 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
| 2678 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2679 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2680 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2681 | if (cb_contig_space(cbuf) >= sizeof(uint16_t)) { |
| 2682 | if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) { |
| 2683 | /* Mark the end of contiguous data for the reader */ |
| 2684 | write_u16(cb_wr(cbuf), 0); |
| 2685 | cb_add(cbuf, sizeof(uint16_t)); |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | if (rd && rd <= cbuf->wr) { |
| 2690 | cb_wr_reset(cbuf); |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2691 | /* Let's try to reuse this buffer */ |
| 2692 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2697 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2698 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2699 | |
| 2700 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2701 | 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] | 2702 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | /* 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] | 2706 | * 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] | 2707 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2708 | 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] | 2709 | { |
| 2710 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2711 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2712 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2713 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2714 | cbuf = qr->cbuf; |
| 2715 | while (cb_contig_data(cbuf)) { |
| 2716 | unsigned char *pos; |
| 2717 | struct buffer tmpbuf = { }; |
| 2718 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 2719 | uint16_t dglen; |
| 2720 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2721 | unsigned int time_sent; |
| 2722 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2723 | pos = cb_rd(cbuf); |
| 2724 | dglen = read_u16(pos); |
| 2725 | /* End of prepared datagrams. |
| 2726 | * Reset the reader index only if in front of the writer index. |
| 2727 | */ |
| 2728 | if (!dglen) { |
| 2729 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 2730 | |
| 2731 | if (wr && wr < cbuf->rd) { |
| 2732 | cb_rd_reset(cbuf); |
| 2733 | continue; |
| 2734 | } |
| 2735 | break; |
| 2736 | } |
| 2737 | |
| 2738 | pos += sizeof dglen; |
| 2739 | first_pkt = read_ptr(pos); |
| 2740 | pos += sizeof first_pkt; |
| 2741 | tmpbuf.area = (char *)pos; |
| 2742 | tmpbuf.size = tmpbuf.data = dglen; |
| 2743 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2744 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2745 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2746 | quic_tx_packet_refinc(pkt); |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 2747 | if (ctx->xprt->snd_buf(NULL, qc->xprt_ctx, |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 2748 | &tmpbuf, tmpbuf.data, 0) <= 0) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2749 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2750 | quic_tx_packet_refdec(pkt); |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 2751 | break; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2752 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2753 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2754 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2755 | qc->tx.bytes += tmpbuf.data; |
| 2756 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2757 | |
| 2758 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 2759 | pkt->time_sent = time_sent; |
| 2760 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 2761 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 2762 | qc->path->ifae_pkts++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2763 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2764 | qc->path->in_flight += pkt->in_flight_len; |
| 2765 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 2766 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2767 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2768 | 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] | 2769 | next_pkt = pkt->next; |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 2770 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2771 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2772 | } |
| 2773 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2774 | |
| 2775 | return 1; |
| 2776 | } |
| 2777 | |
| 2778 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 2779 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 2780 | * a HANDSHAKE_DONE frame. |
| 2781 | * Return 1 if succeeded, 0 if not. |
| 2782 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2783 | 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] | 2784 | { |
| 2785 | int i; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2786 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2787 | struct quic_frame *frm; |
| 2788 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2789 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2790 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 2791 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2792 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 2793 | if (!frm) |
| 2794 | return 0; |
| 2795 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2796 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2797 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2798 | } |
| 2799 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2800 | 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] | 2801 | struct quic_connection_id *cid; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 2802 | struct listener *l = qc->li; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2803 | |
| 2804 | frm = pool_alloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2805 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2806 | goto err; |
| 2807 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2808 | cid = new_quic_cid(&qc->cids, qc, i); |
| 2809 | if (!cid) |
| 2810 | goto err; |
| 2811 | |
| 2812 | /* insert the allocated CID in the receiver tree */ |
| 2813 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2814 | ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len); |
| 2815 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2816 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2817 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2818 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2819 | } |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 2820 | 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] | 2821 | |
| 2822 | return 1; |
| 2823 | |
| 2824 | err: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2825 | return 0; |
| 2826 | } |
| 2827 | |
| 2828 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2829 | void free_quic_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2830 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2831 | struct eb64_node *n; |
| 2832 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2833 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2834 | n = eb64_first(&arngs->root); |
| 2835 | while (n) { |
| 2836 | struct eb64_node *next; |
| 2837 | |
| 2838 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 2839 | next = eb64_next(n); |
| 2840 | eb64_delete(n); |
| 2841 | free(ar); |
| 2842 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2846 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 2847 | * descending order. |
| 2848 | */ |
| 2849 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 2850 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2851 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2852 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | |
| 2856 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 2857 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2858 | * 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] | 2859 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2860 | 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] | 2861 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2862 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2863 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2864 | last = eb64_last(&arngs->root); |
| 2865 | while (last && arngs->enc_sz > limit) { |
| 2866 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2867 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2868 | prev = eb64_prev(last); |
| 2869 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2870 | return 0; |
| 2871 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2872 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 2873 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 2874 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 2875 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 2876 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 2877 | --arngs->sz; |
| 2878 | eb64_delete(last); |
| 2879 | pool_free(pool_head_quic_arng, last); |
| 2880 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | return 1; |
| 2884 | } |
| 2885 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2886 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 2887 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 2888 | { |
| 2889 | struct eb64_node *node, *next; |
| 2890 | struct quic_arng_node *ar, *ar_next; |
| 2891 | |
| 2892 | node = eb64_last(&arngs->root); |
| 2893 | if (!node) |
| 2894 | return; |
| 2895 | |
| 2896 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2897 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 2898 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 2899 | |
| 2900 | while ((next = eb64_prev(node))) { |
| 2901 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 2902 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 2903 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 2904 | node = next; |
| 2905 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2906 | } |
| 2907 | } |
| 2908 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2909 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 2910 | * 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] | 2911 | */ |
| 2912 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2913 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 2914 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2915 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2916 | struct quic_arng_node *new_ar; |
| 2917 | |
| 2918 | new_ar = pool_alloc(pool_head_quic_arng); |
| 2919 | if (new_ar) { |
| 2920 | new_ar->first.key = ar->first; |
| 2921 | new_ar->last = ar->last; |
| 2922 | eb64_insert(&arngs->root, &new_ar->first); |
| 2923 | arngs->sz++; |
| 2924 | } |
| 2925 | |
| 2926 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | /* 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] | 2930 | * 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] | 2931 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2932 | * |
| 2933 | * Descending order |
| 2934 | * -------------> |
| 2935 | * range1 range2 |
| 2936 | * ..........|--------|..............|--------| |
| 2937 | * ^ ^ ^ ^ |
| 2938 | * | | | | |
| 2939 | * last1 first1 last2 first2 |
| 2940 | * ..........+--------+--------------+--------+...... |
| 2941 | * diff1 gap12 diff2 |
| 2942 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2943 | * To encode the previous list of ranges we must encode integers as follows in |
| 2944 | * descending order: |
| 2945 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2946 | * with diff1 = last1 - first1 |
| 2947 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2948 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2949 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2950 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2951 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 2952 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2953 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2954 | struct eb64_node *le; |
| 2955 | struct quic_arng_node *new_node; |
| 2956 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2957 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2958 | new = NULL; |
| 2959 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2960 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2961 | if (!new_node) |
| 2962 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2963 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2964 | goto out; |
| 2965 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2966 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2967 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 2968 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2969 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2970 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2971 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 2972 | |
| 2973 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2974 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2975 | else { |
| 2976 | struct quic_arng_node *le_ar = |
| 2977 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2978 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2979 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 2980 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2981 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2982 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2983 | if (le_ar->last + 1 >= ar->first) { |
| 2984 | le_ar->last = ar->last; |
| 2985 | new = le; |
| 2986 | new_node = le_ar; |
| 2987 | } |
| 2988 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2989 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2990 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2991 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 2992 | |
| 2993 | new = &new_node->first; |
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 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2996 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2997 | /* Verify that the new inserted node does not overlap the nodes |
| 2998 | * which follow it. |
| 2999 | */ |
| 3000 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3001 | struct eb64_node *next; |
| 3002 | struct quic_arng_node *next_node; |
| 3003 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3004 | while ((next = eb64_next(new))) { |
| 3005 | next_node = |
| 3006 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3007 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3008 | break; |
| 3009 | |
| 3010 | if (next_node->last > new_node->last) |
| 3011 | new_node->last = next_node->last; |
| 3012 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3013 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3014 | /* Decrement the size of these ranges. */ |
| 3015 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3016 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3017 | } |
| 3018 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3019 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3020 | quic_arngs_set_enc_sz(arngs); |
| 3021 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3022 | return 1; |
| 3023 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3024 | /* Remove the header protection of packets at <el> encryption level. |
| 3025 | * Always succeeds. |
| 3026 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3027 | 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] | 3028 | { |
| 3029 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3030 | struct quic_rx_packet *pqpkt; |
| 3031 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3032 | struct quic_enc_level *app_qel; |
| 3033 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3034 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3035 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3036 | /* 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] | 3037 | if (el == app_qel && qc_is_listener(qc) && |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3038 | HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3039 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3040 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3041 | goto out; |
| 3042 | } |
| 3043 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3044 | 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] | 3045 | 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] | 3046 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3047 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3048 | 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] | 3049 | /* XXX TO DO XXX */ |
| 3050 | } |
| 3051 | else { |
| 3052 | /* The AAD includes the packet number field */ |
| 3053 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3054 | /* Store the packet into the tree of packets to decrypt. */ |
| 3055 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3056 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3057 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3058 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3059 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3060 | 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] | 3061 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3062 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3063 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3067 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3068 | } |
| 3069 | |
| 3070 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3071 | * Return 1 if succeeded, 0 if not. |
| 3072 | */ |
| 3073 | 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] | 3074 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3075 | { |
| 3076 | struct eb64_node *node; |
| 3077 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3078 | node = eb64_first(&el->rx.crypto.frms); |
| 3079 | while (node) { |
| 3080 | struct quic_rx_crypto_frm *cf; |
| 3081 | |
| 3082 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 3083 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3084 | break; |
| 3085 | |
| 3086 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3087 | goto err; |
| 3088 | |
| 3089 | node = eb64_next(node); |
| 3090 | quic_rx_packet_refdec(cf->pkt); |
| 3091 | eb64_delete(&cf->offset_node); |
| 3092 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3093 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3094 | return 1; |
| 3095 | |
| 3096 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3097 | 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] | 3098 | return 0; |
| 3099 | } |
| 3100 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3101 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3102 | * 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] | 3103 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3104 | * Return 1 if succeeded, 0 if not. |
| 3105 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3106 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3107 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3108 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3109 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3110 | int64_t largest_pn = -1; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3111 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3112 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3113 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3114 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3115 | qel = cur_el; |
| 3116 | next_tel: |
| 3117 | if (!qel) |
| 3118 | goto out; |
| 3119 | |
| 3120 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3121 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3122 | while (node) { |
| 3123 | struct quic_rx_packet *pkt; |
| 3124 | |
| 3125 | 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] | 3126 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3127 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3128 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3129 | /* Drop the packet */ |
| 3130 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3131 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3132 | } |
| 3133 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3134 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3135 | /* Drop the packet */ |
| 3136 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3137 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3138 | } |
| 3139 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3140 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3141 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3142 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING && |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3143 | (!(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] | 3144 | 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] | 3145 | if (pkt->pn > largest_pn) |
| 3146 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3147 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3148 | 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] | 3149 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3150 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3151 | } |
| 3152 | } |
| 3153 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3154 | eb64_delete(&pkt->pn_node); |
| 3155 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3156 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3157 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3158 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3159 | /* Update the largest packet number. */ |
| 3160 | if (largest_pn != -1) |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3161 | HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn); |
| 3162 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3163 | goto err; |
| 3164 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3165 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3166 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3167 | qel = next_el; |
| 3168 | goto next_tel; |
| 3169 | } |
| 3170 | |
| 3171 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3172 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3173 | return 1; |
| 3174 | |
| 3175 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3176 | 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] | 3177 | return 0; |
| 3178 | } |
| 3179 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3180 | /* Check if it's possible to remove header protection for packets related to |
| 3181 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3182 | * |
| 3183 | * Return true if the operation is possible else false. |
| 3184 | */ |
| 3185 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3186 | { |
| 3187 | enum quic_tls_enc_level tel; |
| 3188 | |
| 3189 | if (!qel) |
| 3190 | return 0; |
| 3191 | |
| 3192 | tel = ssl_to_quic_enc_level(qel->level); |
| 3193 | |
| 3194 | /* check if tls secrets are available */ |
| 3195 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) |
| 3196 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
| 3197 | |
| 3198 | if (!(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) |
| 3199 | return 0; |
| 3200 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3201 | /* check if the connection layer is ready before using app level */ |
| 3202 | if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->mux_state != QC_MUX_READY) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3203 | return 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3204 | |
| 3205 | return 1; |
| 3206 | } |
| 3207 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3208 | /* QUIC connection packet handler task. */ |
| 3209 | 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] | 3210 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3211 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3212 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3213 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3214 | enum quic_tls_enc_level tel, next_tel; |
| 3215 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3216 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3217 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3218 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3219 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3220 | qc = ctx->qc; |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3221 | TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3222 | qr = NULL; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3223 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3224 | TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3225 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3226 | HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT); |
| 3227 | /* The I/O handler has been woken up by the dgram listener |
| 3228 | * after the anti-amplification was reached. |
| 3229 | */ |
| 3230 | qc_set_timer(qc); |
| 3231 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3232 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3233 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3234 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3235 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3236 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3237 | 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] | 3238 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3239 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3240 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3241 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3242 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3243 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3244 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3245 | } |
| 3246 | 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] | 3247 | goto err; |
| 3248 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3249 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3250 | 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] | 3251 | |
| 3252 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3253 | /* Treat packets waiting for header packet protection decryption */ |
| 3254 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3255 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3256 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3257 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3258 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3259 | 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] | 3260 | goto err; |
| 3261 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3262 | if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) && |
| 3263 | (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3264 | qel = next_qel; |
| 3265 | next_qel = NULL; |
| 3266 | goto next_level; |
| 3267 | } |
| 3268 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3269 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3270 | if (st >= QUIC_HS_ST_COMPLETE) { |
| 3271 | if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) && |
| 3272 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3273 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3274 | |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3275 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags & |
| 3276 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3277 | /* Discard the Handshake keys. */ |
| 3278 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3279 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3280 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3281 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3282 | 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] | 3283 | 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] | 3284 | } |
| 3285 | |
| 3286 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 3287 | /* There may be remaining handshake to build (acks) */ |
| 3288 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3289 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3290 | } |
| 3291 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3292 | if (!qr) |
| 3293 | 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] | 3294 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 3295 | * be considered. |
| 3296 | */ |
| 3297 | 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] | 3298 | goto err; |
| 3299 | ret = qc_prep_pkts(qc, qr, tel, next_tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3300 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3301 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3302 | else if (ret == 0) |
| 3303 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3304 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3305 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3306 | goto err; |
| 3307 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3308 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3309 | /* Check if there is something to do for the next level. |
| 3310 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3311 | if (next_qel && next_qel != qel && |
| 3312 | (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] | 3313 | (!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] | 3314 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3315 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3316 | goto next_level; |
| 3317 | } |
| 3318 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3319 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3320 | TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3321 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3322 | |
| 3323 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3324 | if (qr) |
| 3325 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3326 | 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] | 3327 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3328 | } |
| 3329 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3330 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3331 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 3332 | { |
| 3333 | int i; |
| 3334 | |
| 3335 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 3336 | if (qel->tx.crypto.bufs[i]) { |
| 3337 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 3338 | qel->tx.crypto.bufs[i] = NULL; |
| 3339 | } |
| 3340 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3341 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3345 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3346 | * Returns 1 if succeeded, 0 if not. |
| 3347 | */ |
| 3348 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 3349 | enum quic_tls_enc_level level) |
| 3350 | { |
| 3351 | struct quic_enc_level *qel; |
| 3352 | |
| 3353 | qel = &qc->els[level]; |
| 3354 | qel->level = quic_to_ssl_enc_level(level); |
| 3355 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 3356 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 3357 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 3358 | qel->tls_ctx.rx.flags = 0; |
| 3359 | qel->tls_ctx.tx.flags = 0; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 3360 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3361 | |
| 3362 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3363 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3364 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 3365 | qel->rx.crypto.offset = 0; |
| 3366 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3367 | |
| 3368 | /* Allocate only one buffer. */ |
| 3369 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 3370 | if (!qel->tx.crypto.bufs) |
| 3371 | goto err; |
| 3372 | |
| 3373 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 3374 | if (!qel->tx.crypto.bufs[0]) |
| 3375 | goto err; |
| 3376 | |
| 3377 | qel->tx.crypto.bufs[0]->sz = 0; |
| 3378 | qel->tx.crypto.nb_buf = 1; |
| 3379 | |
| 3380 | qel->tx.crypto.sz = 0; |
| 3381 | qel->tx.crypto.offset = 0; |
| 3382 | |
| 3383 | return 1; |
| 3384 | |
| 3385 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3386 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3387 | return 0; |
| 3388 | } |
| 3389 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3390 | /* Increment the <qc> refcount. |
| 3391 | * |
| 3392 | * This operation must be conducted when manipulating the quic_conn outside of |
| 3393 | * the connection pinned thread. These threads can only retrieve the connection |
| 3394 | * in the CID tree, so this function must be conducted under the CID lock. |
| 3395 | */ |
| 3396 | static inline void quic_conn_take(struct quic_conn *qc) |
| 3397 | { |
| 3398 | HA_ATOMIC_INC(&qc->refcount); |
| 3399 | } |
| 3400 | |
| 3401 | /* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the |
Ilya Shipitsin | 37d3e38 | 2022-01-07 14:46:15 +0500 | [diff] [blame] | 3402 | * subtraction, the quic_conn is freed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3403 | */ |
| 3404 | static void quic_conn_drop(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3405 | { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3406 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3407 | int i; |
| 3408 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3409 | if (!qc) |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3410 | return; |
| 3411 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3412 | if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1)) |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3413 | return; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3414 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3415 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3416 | if (conn_ctx) { |
| 3417 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3418 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3419 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3420 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3421 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3422 | quic_conn_enc_level_uninit(&qc->els[i]); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3423 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3424 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 3425 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3426 | 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] | 3427 | } |
| 3428 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3429 | /* Release the quic_conn <qc>. It will decrement its refcount so that the |
| 3430 | * connection will be freed once all threads have finished to work with it. The |
| 3431 | * connection is removed from the CIDs tree and thus cannot be found by other |
Amaury Denoyelle | 760da3b | 2022-01-20 17:43:02 +0100 | [diff] [blame] | 3432 | * threads after it. The connection tasklet is killed. |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3433 | * |
| 3434 | * Do not use <qc> after it as it may be freed. This function must only be |
| 3435 | * called by the thread responsible of the quic_conn tasklet. |
| 3436 | */ |
| 3437 | static void quic_conn_release(struct quic_conn *qc) |
| 3438 | { |
Amaury Denoyelle | 760da3b | 2022-01-20 17:43:02 +0100 | [diff] [blame] | 3439 | struct ssl_sock_ctx *conn_ctx; |
| 3440 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3441 | /* remove the connection from receiver cids trees */ |
| 3442 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
| 3443 | ebmb_delete(&qc->odcid_node); |
| 3444 | ebmb_delete(&qc->scid_node); |
| 3445 | free_quic_conn_cids(qc); |
| 3446 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock); |
| 3447 | |
Amaury Denoyelle | 760da3b | 2022-01-20 17:43:02 +0100 | [diff] [blame] | 3448 | /* Kill the tasklet. Do not use tasklet_free as this is not thread safe |
| 3449 | * as other threads may call tasklet_wakeup after this. |
| 3450 | */ |
| 3451 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
| 3452 | if (conn_ctx) |
| 3453 | tasklet_kill(conn_ctx->wait_event.tasklet); |
| 3454 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3455 | quic_conn_drop(qc); |
| 3456 | } |
| 3457 | |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3458 | void quic_close(struct connection *conn, void *xprt_ctx) |
| 3459 | { |
| 3460 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3461 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3462 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3463 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3464 | /* This task must be deleted by the connection-pinned thread. */ |
| 3465 | if (qc->timer_task) { |
| 3466 | task_destroy(qc->timer_task); |
| 3467 | qc->timer_task = NULL; |
| 3468 | } |
| 3469 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3470 | /* Next application data can be dropped. */ |
| 3471 | qc->mux_state = QC_MUX_RELEASED; |
| 3472 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3473 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 9c4da93 | 2022-01-21 14:54:58 +0100 | [diff] [blame] | 3474 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3475 | /* TODO for now release the quic_conn on notification by the upper |
| 3476 | * layer. It could be useful to delay it if there is remaining data to |
| 3477 | * send or data to be acked. |
| 3478 | */ |
| 3479 | quic_conn_release(qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3480 | } |
| 3481 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3482 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3483 | 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] | 3484 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3485 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3486 | struct quic_conn *qc; |
| 3487 | struct quic_pktns *pktns; |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3488 | int i, st; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3489 | |
| 3490 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3491 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3492 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3493 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3494 | task->expire = TICK_ETERNITY; |
| 3495 | pktns = quic_loss_pktns(qc); |
| 3496 | if (tick_isset(pktns->tx.loss_time)) { |
| 3497 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 3498 | |
| 3499 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 3500 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3501 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 3502 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3503 | goto out; |
| 3504 | } |
| 3505 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3506 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3507 | if (qc->path->in_flight) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3508 | 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] | 3509 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 3510 | pktns->tx.pto_probe = 1; |
| 3511 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
| 3512 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1; |
| 3513 | } |
| 3514 | else { |
| 3515 | pktns->tx.pto_probe = 2; |
| 3516 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3517 | } |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3518 | 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] | 3519 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3520 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3521 | |
| 3522 | if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3523 | hel->pktns->tx.pto_probe = 1; |
| 3524 | if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3525 | iel->pktns->tx.pto_probe = 1; |
| 3526 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3527 | |
| 3528 | for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
| 3529 | int j; |
| 3530 | |
| 3531 | if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc)) |
| 3532 | continue; |
| 3533 | |
| 3534 | for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++) |
| 3535 | qc_prep_fast_retrans(&qc->els[i], qc); |
| 3536 | } |
| 3537 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3538 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3539 | qc->path->loss.pto_count++; |
| 3540 | |
| 3541 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3542 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3543 | |
| 3544 | return task; |
| 3545 | } |
| 3546 | |
| 3547 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 3548 | * connections used to identify the first Initial packets of client connecting |
| 3549 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 3550 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 3551 | * <scid> is the source connection ID with <scid_len> as length. |
| 3552 | * Returns 1 if succeeded, 0 if not. |
| 3553 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3554 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3555 | 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] | 3556 | 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] | 3557 | { |
| 3558 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3559 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3560 | /* Initial CID. */ |
| 3561 | struct quic_connection_id *icid; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3562 | char *buf_area; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3563 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3564 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3565 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3566 | qc = pool_zalloc(pool_head_quic_conn); |
| 3567 | if (!qc) { |
| 3568 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 3569 | goto err; |
| 3570 | } |
| 3571 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3572 | HA_ATOMIC_STORE(&qc->refcount, 0); |
| 3573 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3574 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 3575 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3576 | 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] | 3577 | goto err; |
| 3578 | } |
| 3579 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3580 | qc->cids = EB_ROOT; |
| 3581 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3582 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3583 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3584 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 3585 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3586 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL); |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3587 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3588 | qc->odcid.len = dcid_len; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3589 | qc->odcid.addrlen = dcid_addr_len; |
| 3590 | 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] | 3591 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 3592 | /* 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] | 3593 | if (scid_len) |
| 3594 | memcpy(qc->dcid.data, scid, scid_len); |
| 3595 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 3596 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3597 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3598 | } |
| 3599 | /* QUIC Client (outgoing connection to servers) */ |
| 3600 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3601 | 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] | 3602 | if (dcid_len) |
| 3603 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 3604 | qc->dcid.len = dcid_len; |
| 3605 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3606 | qc->mux_state = QC_MUX_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3607 | |
| 3608 | /* Initialize the output buffer */ |
| 3609 | qc->obuf.pos = qc->obuf.data; |
| 3610 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3611 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3612 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3613 | 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] | 3614 | goto err; |
| 3615 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3616 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3617 | /* insert the allocated CID in the receiver tree */ |
| 3618 | if (server) { |
| 3619 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3620 | ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len); |
| 3621 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3622 | } |
| 3623 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3624 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 3625 | qc->scid = icid->cid; |
| 3626 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3627 | /* Packet number spaces initialization. */ |
| 3628 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 3629 | quic_pktns_init(&qc->pktns[i]); |
| 3630 | /* QUIC encryption level context initialization. */ |
| 3631 | 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] | 3632 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3633 | 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] | 3634 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3635 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3636 | /* Initialize the packet number space. */ |
| 3637 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 3638 | } |
| 3639 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 3640 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 3641 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 3642 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 3643 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3644 | /* TX part. */ |
| 3645 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3646 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 3647 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 3648 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3649 | /* RX part. */ |
| 3650 | qc->rx.bytes = 0; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3651 | qc->rx.nb_ack_eliciting = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3652 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 3653 | HA_RWLOCK_INIT(&qc->rx.buf_rwlock); |
| 3654 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 3655 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3656 | 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] | 3657 | goto err; |
| 3658 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3659 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3660 | /* XXX TO DO: Only one path at this time. */ |
| 3661 | qc->path = &qc->paths[0]; |
| 3662 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 3663 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame^] | 3664 | /* required to use MTLIST_IN_LIST */ |
| 3665 | MT_LIST_INIT(&qc->accept_list); |
| 3666 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3667 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3668 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3669 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3670 | |
| 3671 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3672 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3673 | quic_conn_drop(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3674 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | /* Initialize the timer task of <qc> QUIC connection. |
| 3678 | * Returns 1 if succeeded, 0 if not. |
| 3679 | */ |
| 3680 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 3681 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 3682 | /* Attach this task to the same thread ID used for the connection */ |
| 3683 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3684 | if (!qc->timer_task) |
| 3685 | return 0; |
| 3686 | |
| 3687 | qc->timer = TICK_ETERNITY; |
| 3688 | qc->timer_task->process = process_timer; |
| 3689 | qc->timer_task->context = qc->conn->xprt_ctx; |
| 3690 | |
| 3691 | return 1; |
| 3692 | } |
| 3693 | |
| 3694 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 3695 | * past one byte of this buffer. |
| 3696 | */ |
| 3697 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 3698 | struct quic_rx_packet *pkt) |
| 3699 | { |
| 3700 | unsigned char dcid_len, scid_len; |
| 3701 | |
| 3702 | /* Version */ |
| 3703 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 3704 | return 0; |
| 3705 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3706 | /* Destination Connection ID Length */ |
| 3707 | dcid_len = *(*buf)++; |
| 3708 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 3709 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 3710 | /* XXX MUST BE DROPPED */ |
| 3711 | return 0; |
| 3712 | |
| 3713 | if (dcid_len) { |
| 3714 | /* Check that the length of this received DCID matches the CID lengths |
| 3715 | * of our implementation for non Initials packets only. |
| 3716 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3717 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 3718 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3719 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3720 | return 0; |
| 3721 | |
| 3722 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 3723 | } |
| 3724 | |
| 3725 | pkt->dcid.len = dcid_len; |
| 3726 | *buf += dcid_len; |
| 3727 | |
| 3728 | /* Source Connection ID Length */ |
| 3729 | scid_len = *(*buf)++; |
| 3730 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 3731 | /* XXX MUST BE DROPPED */ |
| 3732 | return 0; |
| 3733 | |
| 3734 | if (scid_len) |
| 3735 | memcpy(pkt->scid.data, *buf, scid_len); |
| 3736 | pkt->scid.len = scid_len; |
| 3737 | *buf += scid_len; |
| 3738 | |
| 3739 | return 1; |
| 3740 | } |
| 3741 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3742 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 3743 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 3744 | { |
| 3745 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 3746 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3747 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3748 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 3749 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | /* 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] | 3753 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 3754 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 3755 | * the packet first byte. |
| 3756 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 3757 | * Returns 1 if succeeded, 0 if not. |
| 3758 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3759 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 3760 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3761 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3762 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3763 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3764 | { |
| 3765 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3766 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3767 | struct quic_enc_level *qel; |
| 3768 | /* Only for traces. */ |
| 3769 | struct quic_rx_packet *qpkt_trace; |
| 3770 | |
| 3771 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3772 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3773 | /* The packet number is here. This is also the start minus |
| 3774 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 3775 | * protection. |
| 3776 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3777 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3778 | |
| 3779 | tel = quic_packet_type_enc_level(pkt->type); |
| 3780 | qel = &qc->els[tel]; |
| 3781 | |
| 3782 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3783 | /* Note that the following function enables us to unprotect the packet |
| 3784 | * number and its length subsequently used to decrypt the entire |
| 3785 | * packets. |
| 3786 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3787 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 3788 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 3789 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3790 | goto err; |
| 3791 | } |
| 3792 | |
| 3793 | /* The AAD includes the packet number field found at <pn>. */ |
| 3794 | pkt->aad_len = pn - beg + pkt->pnl; |
| 3795 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3796 | } |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3797 | else if (qel) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3798 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 3799 | /* If the packet number space has been discarded, this packet |
| 3800 | * will be not parsed. |
| 3801 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3802 | 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] | 3803 | goto out; |
| 3804 | } |
| 3805 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3806 | 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] | 3807 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3808 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 3809 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3810 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3811 | else { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3812 | 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] | 3813 | goto err; |
| 3814 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3815 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3816 | *el = qel; |
| 3817 | /* No reference counter incrementation here!!! */ |
| 3818 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 3819 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 3820 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 3821 | b_add(&qc->rx.buf, pkt->len); |
| 3822 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3823 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3824 | return 1; |
| 3825 | |
| 3826 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3827 | 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] | 3828 | return 0; |
| 3829 | } |
| 3830 | |
| 3831 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 3832 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 3833 | */ |
| 3834 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 3835 | unsigned char byte0, int *long_header) |
| 3836 | { |
| 3837 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 3838 | pkt->type = |
| 3839 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 3840 | *long_header = 1; |
| 3841 | } |
| 3842 | else { |
| 3843 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 3844 | *long_header = 0; |
| 3845 | } |
| 3846 | } |
| 3847 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3848 | /* |
| 3849 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 3850 | */ |
| 3851 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 3852 | { |
| 3853 | int j = 0, version; |
| 3854 | |
| 3855 | do { |
| 3856 | version = quic_supported_version[j]; |
| 3857 | if (version == pkt->version) |
| 3858 | return 1; |
| 3859 | |
| 3860 | version = quic_supported_version[++j]; |
| 3861 | } while(version); |
| 3862 | |
| 3863 | return 0; |
| 3864 | } |
| 3865 | |
Frédéric Lécaille | c5c69a0 | 2021-10-20 17:24:42 +0200 | [diff] [blame] | 3866 | __attribute__((unused)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3867 | static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end, |
| 3868 | struct quic_rx_packet *pkt, |
| 3869 | struct quic_dgram_ctx *dgram_ctx, |
| 3870 | struct sockaddr_storage *saddr) |
| 3871 | { |
| 3872 | unsigned char *beg; |
| 3873 | uint64_t len; |
| 3874 | struct quic_conn *qc; |
| 3875 | struct eb_root *cids; |
| 3876 | struct ebmb_node *node; |
| 3877 | struct connection *srv_conn; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3878 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3879 | int long_header; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3880 | size_t b_cspace; |
| 3881 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3882 | |
| 3883 | qc = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 3884 | qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3885 | TRACE_ENTER(QUIC_EV_CONN_SPKT); |
| 3886 | if (end <= *buf) |
| 3887 | goto err; |
| 3888 | |
| 3889 | /* Fixed bit */ |
| 3890 | if (!(**buf & QUIC_PACKET_FIXED_BIT)) |
| 3891 | /* XXX TO BE DISCARDED */ |
| 3892 | goto err; |
| 3893 | |
| 3894 | srv_conn = dgram_ctx->owner; |
| 3895 | beg = *buf; |
| 3896 | /* Header form */ |
| 3897 | qc_parse_hd_form(pkt, *(*buf)++, &long_header); |
| 3898 | if (long_header) { |
| 3899 | size_t cid_lookup_len; |
| 3900 | |
| 3901 | if (!quic_packet_read_long_header(buf, end, pkt)) |
| 3902 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3903 | |
| 3904 | /* unsupported QUIC version */ |
| 3905 | if (!qc_pkt_is_supported_version(pkt)) { |
| 3906 | TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT); |
| 3907 | goto err; |
| 3908 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3909 | |
| 3910 | /* For Initial packets, and for servers (QUIC clients connections), |
| 3911 | * there is no Initial connection IDs storage. |
| 3912 | */ |
| 3913 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3914 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
| 3915 | cid_lookup_len = pkt->dcid.len; |
| 3916 | } |
| 3917 | else { |
| 3918 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3919 | cid_lookup_len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len); |
| 3923 | if (!node) |
| 3924 | goto err; |
| 3925 | |
| 3926 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
| 3927 | |
| 3928 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3929 | qc->dcid.len = pkt->scid.len; |
| 3930 | if (pkt->scid.len) |
| 3931 | memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len); |
| 3932 | } |
| 3933 | |
| 3934 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3935 | uint64_t token_len; |
| 3936 | |
| 3937 | if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len) |
| 3938 | goto err; |
| 3939 | |
| 3940 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 3941 | * A server which sends an Initial packet must not set the token. |
| 3942 | * So, a client which receives an Initial packet with a token |
| 3943 | * MUST discard the packet or generate a connection error with |
| 3944 | * PROTOCOL_VIOLATION as type. |
| 3945 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 3946 | */ |
| 3947 | pkt->token_len = token_len; |
| 3948 | } |
| 3949 | } |
| 3950 | else { |
| 3951 | /* XXX TO DO: Short header XXX */ |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3952 | if (end - *buf < QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3953 | goto err; |
| 3954 | |
| 3955 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3956 | node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3957 | if (!node) |
| 3958 | goto err; |
| 3959 | |
| 3960 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3961 | *buf += QUIC_HAP_CID_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3962 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3963 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3964 | /* Only packets packets with long headers and not RETRY or VERSION as type |
| 3965 | * have a length field. |
| 3966 | */ |
| 3967 | if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) { |
| 3968 | if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len) |
| 3969 | goto err; |
| 3970 | |
| 3971 | pkt->len = len; |
| 3972 | } |
| 3973 | else if (!long_header) { |
| 3974 | /* A short packet is the last one of an UDP datagram. */ |
| 3975 | pkt->len = end - *buf; |
| 3976 | } |
| 3977 | |
| 3978 | conn_ctx = qc->conn->xprt_ctx; |
| 3979 | |
| 3980 | /* Increase the total length of this packet by the header length. */ |
| 3981 | pkt->len += *buf - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3982 | |
| 3983 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 3984 | * they must have the same DCID. |
| 3985 | * |
| 3986 | * This check must be done after the final update to pkt.len to |
| 3987 | * properly drop the packet on failure. |
| 3988 | */ |
| 3989 | if (!dgram_ctx->dcid.len) { |
| 3990 | memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len); |
| 3991 | } |
| 3992 | 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] | 3993 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3994 | goto err; |
| 3995 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 3996 | dgram_ctx->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3997 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3998 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3999 | b_cspace = b_contig_space(&qc->rx.buf); |
| 4000 | if (b_cspace < pkt->len) { |
| 4001 | /* Let us consume the remaining contiguous space. */ |
| 4002 | b_add(&qc->rx.buf, b_cspace); |
| 4003 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 4004 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4005 | 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] | 4006 | goto err; |
| 4007 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4008 | } |
| 4009 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4010 | 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] | 4011 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4012 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4013 | goto err; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4014 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4015 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4016 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 4017 | if (pkt->aad_len) |
| 4018 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4019 | /* Wake the tasklet of the QUIC connection packet handler. */ |
| 4020 | if (conn_ctx) |
| 4021 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4022 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4023 | TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4024 | |
| 4025 | return pkt->len; |
| 4026 | |
| 4027 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4028 | 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] | 4029 | return -1; |
| 4030 | } |
| 4031 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4032 | /* |
| 4033 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 4034 | * address <addr>. |
| 4035 | * Implementation of RFC9000 6. Version Negotiation |
| 4036 | * |
| 4037 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 4038 | * |
| 4039 | * Returns 0 on success else non-zero |
| 4040 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4041 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 4042 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4043 | { |
| 4044 | char buf[256]; |
| 4045 | int i = 0, j, version; |
| 4046 | const socklen_t addrlen = get_addr_len(addr); |
| 4047 | |
| 4048 | /* |
| 4049 | * header form |
| 4050 | * long header, fixed bit to 0 for Version Negotiation |
| 4051 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4052 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 4053 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4054 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 4055 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4056 | /* null version for Version Negotiation */ |
| 4057 | buf[i++] = '\x00'; |
| 4058 | buf[i++] = '\x00'; |
| 4059 | buf[i++] = '\x00'; |
| 4060 | buf[i++] = '\x00'; |
| 4061 | |
| 4062 | /* source connection id */ |
| 4063 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4064 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4065 | i += pkt->scid.len; |
| 4066 | |
| 4067 | /* destination connection id */ |
| 4068 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 4069 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4070 | i += pkt->dcid.len; |
| 4071 | |
| 4072 | /* supported version */ |
| 4073 | j = 0; |
| 4074 | do { |
| 4075 | version = htonl(quic_supported_version[j]); |
| 4076 | memcpy(&buf[i], &version, sizeof(version)); |
| 4077 | i += sizeof(version); |
| 4078 | |
| 4079 | version = quic_supported_version[++j]; |
| 4080 | } while (version); |
| 4081 | |
| 4082 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4083 | return 1; |
| 4084 | |
| 4085 | return 0; |
| 4086 | } |
| 4087 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4088 | /* Generate the token to be used in Retry packets. The token is written to |
| 4089 | * <buf> which is expected to be <len> bytes. |
| 4090 | * |
| 4091 | * Various parameters are expected to be encoded in the token. For now, only |
| 4092 | * the DCID from <pkt> is stored. This is useful to implement a stateless Retry |
| 4093 | * as this CID must be repeated by the server in the transport parameters. |
| 4094 | * |
| 4095 | * TODO add the client address to validate the token origin. |
| 4096 | * |
| 4097 | * Returns the length of the encoded token or 0 on error. |
| 4098 | */ |
| 4099 | static int generate_retry_token(unsigned char *buf, unsigned char len, |
| 4100 | struct quic_rx_packet *pkt) |
| 4101 | { |
| 4102 | const size_t token_len = 1 + pkt->dcid.len; |
| 4103 | unsigned char i = 0; |
| 4104 | |
| 4105 | if (token_len > len) |
| 4106 | return 0; |
| 4107 | |
| 4108 | buf[i++] = pkt->dcid.len; |
| 4109 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 4110 | i += pkt->dcid.len; |
| 4111 | |
| 4112 | return i; |
| 4113 | } |
| 4114 | |
| 4115 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 4116 | * the Initial <pkt> packet. |
| 4117 | * |
| 4118 | * Returns 0 on success else non-zero. |
| 4119 | */ |
| 4120 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 4121 | struct quic_rx_packet *pkt) |
| 4122 | { |
| 4123 | unsigned char buf[128]; |
| 4124 | int i = 0, token_len; |
| 4125 | const socklen_t addrlen = get_addr_len(addr); |
| 4126 | struct quic_cid scid; |
| 4127 | |
| 4128 | /* long header + fixed bit + packet type 0x3 */ |
| 4129 | buf[i++] = 0xf0; |
| 4130 | /* version */ |
| 4131 | buf[i++] = 0x00; |
| 4132 | buf[i++] = 0x00; |
| 4133 | buf[i++] = 0x00; |
| 4134 | buf[i++] = 0x01; |
| 4135 | |
| 4136 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 4137 | buf[i++] = pkt->scid.len; |
| 4138 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 4139 | i += pkt->scid.len; |
| 4140 | |
| 4141 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 4142 | scid.len = QUIC_HAP_CID_LEN; |
| 4143 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 4144 | return 1; |
| 4145 | |
| 4146 | buf[i++] = scid.len; |
| 4147 | memcpy(&buf[i], scid.data, scid.len); |
| 4148 | i += scid.len; |
| 4149 | |
| 4150 | /* token */ |
| 4151 | if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt))) |
| 4152 | return 1; |
| 4153 | i += token_len; |
| 4154 | |
| 4155 | /* token integrity tag */ |
| 4156 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 4157 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 4158 | pkt->dcid.len, buf, i)) { |
| 4159 | return 1; |
| 4160 | } |
| 4161 | |
| 4162 | i += QUIC_TLS_TAG_LEN; |
| 4163 | |
| 4164 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4165 | return 1; |
| 4166 | |
| 4167 | return 0; |
| 4168 | } |
| 4169 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4170 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 4171 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 4172 | * concatenated to the <pkt> DCID field. |
| 4173 | * |
| 4174 | * Returns the instance or NULL if not found. |
| 4175 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4176 | 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] | 4177 | struct listener *l, |
| 4178 | struct sockaddr_storage *saddr) |
| 4179 | { |
| 4180 | struct quic_conn *qc = NULL; |
| 4181 | struct ebmb_node *node; |
| 4182 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4183 | /* set if the quic_conn is found in the second DCID tree */ |
| 4184 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4185 | |
| 4186 | HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 4187 | |
| 4188 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 4189 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 4190 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 4191 | /* DCIDs of first packets coming from multiple clients may have |
| 4192 | * the same values. Let's distinguish them by concatenating the |
| 4193 | * socket addresses. |
| 4194 | */ |
| 4195 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 4196 | node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data, |
| 4197 | pkt->dcid.len + pkt->dcid.addrlen); |
| 4198 | if (node) { |
| 4199 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 4200 | goto end; |
| 4201 | } |
| 4202 | } |
| 4203 | |
| 4204 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 4205 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 4206 | * used. |
| 4207 | */ |
| 4208 | node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len); |
| 4209 | if (!node) |
| 4210 | goto end; |
| 4211 | |
| 4212 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 4213 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4214 | found_in_dcid = 1; |
| 4215 | |
| 4216 | end: |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4217 | if (qc) |
| 4218 | quic_conn_take(qc); |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4219 | HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4220 | |
| 4221 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 4222 | * If already done, this is a noop. |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4223 | * |
| 4224 | * node.leaf_p is first checked to avoid unnecessary locking. |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4225 | */ |
Amaury Denoyelle | c6fab98 | 2021-12-23 16:27:56 +0100 | [diff] [blame] | 4226 | if (qc && found_in_dcid && qc->odcid_node.node.leaf_p) { |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4227 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 4228 | ebmb_delete(&qc->odcid_node); |
| 4229 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 4230 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4231 | |
| 4232 | return qc; |
| 4233 | } |
| 4234 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4235 | /* Parse the Retry token from buffer <token> whose size is <token_len>. This |
| 4236 | * will extract the parameters stored in the token : <odcid>. |
| 4237 | * |
| 4238 | * Returns 0 on success else non-zero. |
| 4239 | */ |
| 4240 | static int parse_retry_token(const unsigned char *token, uint64_t token_len, |
| 4241 | struct quic_cid *odcid) |
| 4242 | { |
| 4243 | uint64_t odcid_len; |
| 4244 | |
| 4245 | if (!quic_dec_int(&odcid_len, &token, token + token_len)) |
| 4246 | return 1; |
| 4247 | |
| 4248 | memcpy(odcid->data, token, odcid_len); |
| 4249 | odcid->len = odcid_len; |
| 4250 | |
| 4251 | return 0; |
| 4252 | } |
| 4253 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4254 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 4255 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 4256 | * parameters of this session. |
| 4257 | * This is the responsibility of the caller to check the validity of all the |
| 4258 | * pointers passed as parameter to this function. |
| 4259 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 4260 | * CO_ER_SSL_NO_MEM. |
| 4261 | */ |
| 4262 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 4263 | unsigned char *params, size_t params_len) |
| 4264 | { |
| 4265 | int retry; |
| 4266 | |
| 4267 | retry = 1; |
| 4268 | retry: |
| 4269 | *ssl = SSL_new(ssl_ctx); |
| 4270 | if (!*ssl) { |
| 4271 | if (!retry--) |
| 4272 | goto err; |
| 4273 | |
| 4274 | pool_gc(NULL); |
| 4275 | goto retry; |
| 4276 | } |
| 4277 | |
| 4278 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 4279 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) || |
| 4280 | !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) { |
| 4281 | goto err; |
| 4282 | |
| 4283 | SSL_free(*ssl); |
| 4284 | *ssl = NULL; |
| 4285 | if (!retry--) |
| 4286 | goto err; |
| 4287 | |
| 4288 | pool_gc(NULL); |
| 4289 | goto retry; |
| 4290 | } |
| 4291 | |
| 4292 | return 0; |
| 4293 | |
| 4294 | err: |
| 4295 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 4296 | return -1; |
| 4297 | } |
| 4298 | |
| 4299 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 4300 | * used to process <qc> received packets. The allocated context is stored in |
| 4301 | * <qc.xprt_ctx>. |
| 4302 | * |
| 4303 | * Returns 0 on success else non-zero. |
| 4304 | */ |
| 4305 | int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 4306 | { |
| 4307 | struct bind_conf *bc = qc->li->bind_conf; |
| 4308 | struct ssl_sock_ctx *ctx = NULL; |
| 4309 | |
| 4310 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 4311 | if (!ctx) |
| 4312 | goto err; |
| 4313 | |
| 4314 | ctx->wait_event.tasklet = tasklet_new(); |
| 4315 | if (!ctx->wait_event.tasklet) |
| 4316 | goto err; |
| 4317 | |
| 4318 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 4319 | ctx->wait_event.tasklet->context = ctx; |
| 4320 | ctx->wait_event.events = 0; |
| 4321 | ctx->subs = NULL; |
| 4322 | ctx->xprt_ctx = NULL; |
| 4323 | ctx->qc = qc; |
| 4324 | |
| 4325 | /* Set tasklet tid based on the SCID selected by us for this |
| 4326 | * connection. The upper layer will also be binded on the same thread. |
| 4327 | */ |
| 4328 | qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid); |
| 4329 | |
| 4330 | if (qc_is_listener(qc)) { |
| 4331 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 4332 | qc->enc_params, qc->enc_params_len) == -1) { |
| 4333 | goto err; |
| 4334 | } |
| 4335 | |
| 4336 | /* Enabling 0-RTT */ |
| 4337 | if (bc->ssl_conf.early_data) |
| 4338 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 4339 | |
| 4340 | SSL_set_accept_state(ctx->ssl); |
| 4341 | } |
| 4342 | |
| 4343 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 4344 | |
| 4345 | /* Store the allocated context in <qc>. */ |
| 4346 | HA_ATOMIC_STORE(&qc->xprt_ctx, ctx); |
| 4347 | |
| 4348 | return 0; |
| 4349 | |
| 4350 | err: |
| 4351 | if (ctx && ctx->wait_event.tasklet) |
| 4352 | tasklet_free(ctx->wait_event.tasklet); |
| 4353 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 4354 | |
| 4355 | return 1; |
| 4356 | } |
| 4357 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4358 | 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] | 4359 | struct quic_rx_packet *pkt, |
| 4360 | struct quic_dgram_ctx *dgram_ctx, |
| 4361 | struct sockaddr_storage *saddr) |
| 4362 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4363 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4364 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4365 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4366 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4367 | int long_header = 0, io_cb_wakeup = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4368 | size_t b_cspace; |
| 4369 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4370 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4371 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4372 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4373 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 4374 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 4375 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 4376 | /* This ist only to please to traces and distinguish the |
| 4377 | * packet with parsed packet number from others. |
| 4378 | */ |
| 4379 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4380 | if (end <= buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4381 | goto err; |
| 4382 | |
| 4383 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4384 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4385 | /* XXX TO BE DISCARDED */ |
| 4386 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4387 | goto err; |
| 4388 | } |
| 4389 | |
| 4390 | l = dgram_ctx->owner; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4391 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4392 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4393 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4394 | uint64_t len; |
| 4395 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4396 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4397 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4398 | goto err; |
| 4399 | } |
| 4400 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4401 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 4402 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 4403 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4404 | goto err; |
| 4405 | } |
| 4406 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4407 | /* RFC9000 6. Version Negotiation */ |
| 4408 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4409 | /* unsupported version, send Negotiation packet */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4410 | if (send_version_negotiation(l->rx.fd, saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4411 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 4412 | goto err; |
| 4413 | } |
| 4414 | |
| 4415 | 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] | 4416 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4417 | } |
| 4418 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4419 | /* For Initial packets, and for servers (QUIC clients connections), |
| 4420 | * there is no Initial connection IDs storage. |
| 4421 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4422 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4423 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4424 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4425 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 4426 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4427 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4428 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4429 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4430 | |
| 4431 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 4432 | * A server which sends an Initial packet must not set the token. |
| 4433 | * So, a client which receives an Initial packet with a token |
| 4434 | * MUST discard the packet or generate a connection error with |
| 4435 | * PROTOCOL_VIOLATION as type. |
| 4436 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 4437 | */ |
| 4438 | pkt->token_len = token_len; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4439 | |
| 4440 | /* TODO Retry should be automatically activated if |
| 4441 | * suspect network usage is detected. |
| 4442 | */ |
| 4443 | if (!token_len && l->bind_conf->quic_force_retry) { |
| 4444 | TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT); |
| 4445 | if (send_retry(l->rx.fd, saddr, pkt)) { |
| 4446 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 4447 | goto err; |
| 4448 | } |
| 4449 | |
| 4450 | goto err; |
| 4451 | } |
| 4452 | else { |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4453 | pkt->token = buf; |
| 4454 | buf += pkt->token_len; |
| 4455 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4456 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4457 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4458 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4459 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4460 | goto err; |
| 4461 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4462 | } |
| 4463 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4464 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 4465 | end - buf < len) { |
| 4466 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4467 | goto err; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4468 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4469 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4470 | payload = buf; |
| 4471 | pkt->len = len + payload - beg; |
| 4472 | |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4473 | qc = retrieve_qc_conn_from_cid(pkt, l, saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4474 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4475 | int ipv4; |
| 4476 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4477 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4478 | const unsigned char *salt = initial_salt_v1; |
| 4479 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4480 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4481 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 4482 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4483 | goto err; |
| 4484 | } |
| 4485 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4486 | pkt->saddr = *saddr; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4487 | ipv4 = saddr->ss_family == AF_INET; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4488 | qc = qc_new_conn(pkt->version, ipv4, |
| 4489 | pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4490 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4491 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4492 | goto err; |
| 4493 | |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 4494 | memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr)); |
| 4495 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4496 | odcid = &qc->rx.params.original_destination_connection_id; |
| 4497 | /* Copy the transport parameters. */ |
| 4498 | qc->rx.params = l->bind_conf->quic_params; |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4499 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4500 | /* Copy original_destination_connection_id transport parameter. */ |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4501 | if (pkt->token_len) { |
| 4502 | if (parse_retry_token(pkt->token, pkt->token_len, odcid)) { |
| 4503 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 4504 | goto err; |
| 4505 | } |
Amaury Denoyelle | c3b6f4d | 2022-01-11 12:03:09 +0100 | [diff] [blame] | 4506 | /* Copy retry_source_connection_id transport parameter. */ |
| 4507 | quic_cid_cpy(&qc->rx.params.retry_source_connection_id, |
| 4508 | &pkt->dcid); |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4509 | } |
| 4510 | else { |
| 4511 | memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len); |
| 4512 | odcid->len = pkt->dcid.len; |
| 4513 | } |
| 4514 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4515 | /* Copy the initial source connection ID. */ |
| 4516 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4517 | qc->enc_params_len = |
| 4518 | quic_transport_params_encode(qc->enc_params, |
| 4519 | qc->enc_params + sizeof qc->enc_params, |
| 4520 | &qc->rx.params, 1); |
| 4521 | if (!qc->enc_params_len) |
| 4522 | goto err; |
| 4523 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4524 | if (qc_conn_alloc_ssl_ctx(qc)) |
| 4525 | goto err; |
| 4526 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4527 | /* NOTE: the socket address has been concatenated to the destination ID |
| 4528 | * chosen by the client for Initial packets. |
| 4529 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4530 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4531 | salt = initial_salt_draft_29; |
| 4532 | salt_len = sizeof initial_salt_draft_29; |
| 4533 | } |
| 4534 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4535 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4536 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4537 | goto err; |
| 4538 | } |
| 4539 | |
Frédéric Lécaille | b0006ee | 2021-11-03 19:01:31 +0100 | [diff] [blame] | 4540 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4541 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4542 | n = ebmb_insert(&l->rx.odcids, &qc->odcid_node, |
| 4543 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4544 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4545 | /* If the insertion failed, it means that another |
| 4546 | * thread has already allocated a QUIC connection for |
| 4547 | * the same CID. Liberate our allocated connection. |
| 4548 | */ |
| 4549 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4550 | qc_to_purge = qc; |
| 4551 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4552 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 4553 | pkt->qc = qc; |
| 4554 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4555 | |
| 4556 | quic_conn_take(qc); |
| 4557 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 4558 | |
| 4559 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4560 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 4561 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4562 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4563 | else { |
| 4564 | quic_conn_drop(qc_to_purge); |
| 4565 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4566 | } |
| 4567 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4568 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4569 | } |
| 4570 | } |
| 4571 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4572 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4573 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4574 | goto err; |
| 4575 | } |
| 4576 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4577 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4578 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4579 | buf += QUIC_HAP_CID_LEN; |
| 4580 | |
| 4581 | /* A short packet is the last one of a UDP datagram. */ |
| 4582 | payload = buf; |
| 4583 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4584 | |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4585 | qc = retrieve_qc_conn_from_cid(pkt, l, saddr); |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4586 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4587 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4588 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
| 4589 | goto err; |
| 4590 | } |
| 4591 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4592 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4593 | } |
| 4594 | |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4595 | |
| 4596 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4597 | * they must have the same DCID. |
| 4598 | * |
| 4599 | * This check must be done after the final update to pkt.len to |
| 4600 | * properly drop the packet on failure. |
| 4601 | */ |
| 4602 | if (!dgram_ctx->dcid.len) { |
| 4603 | 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] | 4604 | if (!quic_peer_validated_addr(qc) && |
| 4605 | HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
| 4606 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 4607 | QUIC_EV_CONN_LPKT, qc); |
| 4608 | /* Reset the anti-amplification bit. It will be set again |
| 4609 | * when sending the next packet if reached again. |
| 4610 | */ |
| 4611 | HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT); |
| 4612 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT); |
| 4613 | io_cb_wakeup = 1; |
| 4614 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4615 | } |
| 4616 | 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] | 4617 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4618 | goto err; |
| 4619 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4620 | dgram_ctx->qc = qc; |
| 4621 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4622 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4623 | if (HA_ATOMIC_LOAD(&qc->err_code)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4624 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4625 | goto out; |
| 4626 | } |
| 4627 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4628 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4629 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4630 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4631 | b_cspace = b_contig_space(&qc->rx.buf); |
| 4632 | if (b_cspace < pkt->len) { |
| 4633 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4634 | if (b_cspace) { |
| 4635 | b_putchr(&qc->rx.buf, 0x00); |
| 4636 | b_cspace--; |
| 4637 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4638 | b_add(&qc->rx.buf, b_cspace); |
| 4639 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 4640 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4641 | 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] | 4642 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4643 | goto err; |
| 4644 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4645 | } |
| 4646 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4647 | 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] | 4648 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4649 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 4650 | goto err; |
| 4651 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4652 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4653 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4654 | 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] | 4655 | if (pkt->aad_len) |
| 4656 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4657 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 4658 | /* Wake up the connection packet handler task from here only if all |
| 4659 | * the contexts have been initialized, especially the mux context |
| 4660 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 4661 | * will start it if these contexts for the connection are not already |
| 4662 | * initialized. |
| 4663 | */ |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 4664 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame^] | 4665 | if (conn_ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4666 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4667 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4668 | 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] | 4669 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4670 | if (qc) |
| 4671 | quic_conn_drop(qc); |
| 4672 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4673 | return pkt->len; |
| 4674 | |
| 4675 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4676 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 4677 | * This cannot be done by this thread. |
| 4678 | */ |
| 4679 | if (io_cb_wakeup) { |
| 4680 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
| 4681 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 4682 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4683 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 4684 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4685 | if (!pkt->len) |
| 4686 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 4687 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4688 | qc ? qc : NULL, pkt); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4689 | |
| 4690 | if (qc) |
| 4691 | quic_conn_drop(qc); |
| 4692 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4693 | return -1; |
| 4694 | } |
| 4695 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4696 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 4697 | * 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] | 4698 | */ |
| 4699 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 4700 | int type, size_t pn_len, struct quic_conn *conn) |
| 4701 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4702 | 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] | 4703 | return 0; |
| 4704 | |
| 4705 | /* #0 byte flags */ |
| 4706 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 4707 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 4708 | /* Version */ |
| 4709 | quic_write_uint32(buf, end, conn->version); |
| 4710 | *(*buf)++ = conn->dcid.len; |
| 4711 | /* Destination connection ID */ |
| 4712 | if (conn->dcid.len) { |
| 4713 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4714 | *buf += conn->dcid.len; |
| 4715 | } |
| 4716 | /* Source connection ID */ |
| 4717 | *(*buf)++ = conn->scid.len; |
| 4718 | if (conn->scid.len) { |
| 4719 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 4720 | *buf += conn->scid.len; |
| 4721 | } |
| 4722 | |
| 4723 | return 1; |
| 4724 | } |
| 4725 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4726 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 4727 | * 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] | 4728 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4729 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 4730 | size_t pn_len, struct quic_conn *conn, |
| 4731 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4732 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4733 | if (end - *buf < 1 + conn->dcid.len) |
| 4734 | return 0; |
| 4735 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4736 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 4737 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 4738 | ((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] | 4739 | /* Destination connection ID */ |
| 4740 | if (conn->dcid.len) { |
| 4741 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4742 | *buf += conn->dcid.len; |
| 4743 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4744 | |
| 4745 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 4749 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 4750 | * with <aead> as AEAD cipher and <key> as secret key. |
| 4751 | * Returns 1 if succeeded or 0 if failed. |
| 4752 | */ |
| 4753 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 4754 | const EVP_CIPHER *aead, const unsigned char *key) |
| 4755 | { |
| 4756 | int i, ret, outlen; |
| 4757 | EVP_CIPHER_CTX *ctx; |
| 4758 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 4759 | * and at most 4 bytes for the packet number |
| 4760 | */ |
| 4761 | unsigned char mask[5] = {0}; |
| 4762 | |
| 4763 | ret = 0; |
| 4764 | ctx = EVP_CIPHER_CTX_new(); |
| 4765 | if (!ctx) |
| 4766 | return 0; |
| 4767 | |
| 4768 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 4769 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 4770 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 4771 | goto out; |
| 4772 | |
| 4773 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 4774 | for (i = 0; i < pnlen; i++) |
| 4775 | pn[i] ^= mask[i + 1]; |
| 4776 | |
| 4777 | ret = 1; |
| 4778 | |
| 4779 | out: |
| 4780 | EVP_CIPHER_CTX_free(ctx); |
| 4781 | |
| 4782 | return ret; |
| 4783 | } |
| 4784 | |
| 4785 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 4786 | * ACK ranges if needed to a value below <limit> in bytes. |
| 4787 | * Return 1 if succeeded, 0 if not. |
| 4788 | */ |
| 4789 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 4790 | { |
| 4791 | size_t room, ack_delay_sz; |
| 4792 | |
| 4793 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 4794 | /* A frame is made of 1 byte for the frame type. */ |
| 4795 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4796 | 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] | 4797 | return 0; |
| 4798 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4799 | 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] | 4800 | } |
| 4801 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4802 | /* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames |
| 4803 | * 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] | 4804 | * and <*len> the packet Length field initialized with the number of bytes already present |
| 4805 | * 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] | 4806 | * <headlen> is the number of bytes already present in this packet before building frames. |
| 4807 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4808 | * 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] | 4809 | * 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] | 4810 | * Return 1 if succeeded, 0 if not. |
| 4811 | */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4812 | static inline int qc_build_frms(struct list *l, |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4813 | size_t room, size_t *len, size_t headlen, |
| 4814 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4815 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4816 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4817 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4818 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4819 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4820 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 4821 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4822 | return 0; |
| 4823 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4824 | /* If we are not probing we must take into an account the congestion |
| 4825 | * control window. |
| 4826 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 4827 | if (!qel->pktns->tx.pto_probe) { |
| 4828 | size_t remain = quic_path_prep_data(qc->path); |
| 4829 | |
| 4830 | if (headlen > remain) |
| 4831 | return 0; |
| 4832 | |
| 4833 | room = QUIC_MIN(room, remain - headlen); |
| 4834 | } |
| 4835 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4836 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4837 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4838 | 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] | 4839 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4840 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4841 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4842 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4843 | break; |
| 4844 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4845 | switch (cf->type) { |
| 4846 | case QUIC_FT_CRYPTO: |
| 4847 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4848 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4849 | /* Compute the length of this CRYPTO frame header */ |
| 4850 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 4851 | /* Compute the data length of this CRyPTO frame. */ |
| 4852 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 4853 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4854 | 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] | 4855 | if (!dlen) |
| 4856 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4857 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4858 | /* CRYPTO frame length. */ |
| 4859 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 4860 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4861 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4862 | /* Add the CRYPTO data length and its encoded length to the packet |
| 4863 | * length and the length of this length. |
| 4864 | */ |
| 4865 | *len += flen; |
| 4866 | room -= flen; |
| 4867 | if (dlen == cf->crypto.len) { |
| 4868 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4869 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4870 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4871 | } |
| 4872 | else { |
| 4873 | struct quic_frame *new_cf; |
| 4874 | |
| 4875 | new_cf = pool_alloc(pool_head_quic_frame); |
| 4876 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4877 | 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] | 4878 | return 0; |
| 4879 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4880 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4881 | new_cf->type = QUIC_FT_CRYPTO; |
| 4882 | new_cf->crypto.len = dlen; |
| 4883 | new_cf->crypto.offset = cf->crypto.offset; |
| 4884 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4885 | LIST_APPEND(l, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4886 | /* Consume <dlen> bytes of the current frame. */ |
| 4887 | cf->crypto.len -= dlen; |
| 4888 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4889 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4890 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4891 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4892 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4893 | /* Note that these frames are accepted in short packets only without |
| 4894 | * "Length" packet field. Here, <*len> is used only to compute the |
| 4895 | * 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] | 4896 | * |
| 4897 | * 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] | 4898 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 4899 | */ |
| 4900 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4901 | ((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] | 4902 | /* Compute the data length of this STREAM frame. */ |
| 4903 | avail_room = room - hlen - *len; |
| 4904 | if ((ssize_t)avail_room <= 0) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4905 | break; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4906 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 4907 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4908 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4909 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 4910 | dlen = max_available_room(avail_room, &dlen_sz); |
| 4911 | if (dlen > cf->stream.len) { |
| 4912 | dlen = cf->stream.len; |
| 4913 | } |
| 4914 | dlen_sz = quic_int_getsize(dlen); |
| 4915 | flen = hlen + dlen_sz + dlen; |
| 4916 | } |
| 4917 | else { |
| 4918 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 4919 | flen = hlen + dlen; |
| 4920 | } |
| 4921 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4922 | 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] | 4923 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4924 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4925 | /* Add the STREAM data length and its encoded length to the packet |
| 4926 | * length and the length of this length. |
| 4927 | */ |
| 4928 | *len += flen; |
| 4929 | room -= flen; |
| 4930 | if (dlen == cf->stream.len) { |
| 4931 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4932 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4933 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4934 | } |
| 4935 | else { |
| 4936 | struct quic_frame *new_cf; |
| 4937 | |
| 4938 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 4939 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4940 | 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] | 4941 | return 0; |
| 4942 | } |
| 4943 | |
| 4944 | new_cf->type = cf->type; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4945 | new_cf->stream.qcs = cf->stream.qcs; |
| 4946 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4947 | new_cf->stream.id = cf->stream.id; |
| 4948 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 4949 | new_cf->stream.offset = cf->stream.offset; |
| 4950 | new_cf->stream.len = dlen; |
| 4951 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 4952 | /* FIN bit reset */ |
| 4953 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 4954 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4955 | LIST_APPEND(l, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4956 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 4957 | /* Consume <dlen> bytes of the current frame. */ |
| 4958 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4959 | cf->stream.offset.key += dlen; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4960 | cf->stream.data += dlen; |
| 4961 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4962 | break; |
| 4963 | |
| 4964 | default: |
| 4965 | flen = qc_frm_len(cf); |
| 4966 | BUG_ON(!flen); |
| 4967 | if (flen > room) |
| 4968 | continue; |
| 4969 | |
| 4970 | *len += flen; |
| 4971 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4972 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4973 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4974 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4975 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4976 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4977 | } |
| 4978 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4979 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4980 | } |
| 4981 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4982 | /* 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] | 4983 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 4984 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 4985 | * filling the buffer with as much frames as possible. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4986 | * 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] | 4987 | * 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] | 4988 | * having returned from this function. |
| 4989 | * 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] | 4990 | * number field in this packet. <pn_len> will also have the packet number |
| 4991 | * length as value. |
| 4992 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4993 | * 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] | 4994 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4995 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 4996 | size_t dglen, struct quic_tx_packet *pkt, |
| 4997 | 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] | 4998 | int ack, int padding, int cc, int probe, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4999 | struct quic_enc_level *qel, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5000 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5001 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5002 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5003 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 5004 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5005 | 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] | 5006 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5007 | int64_t largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5008 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5009 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5010 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5011 | /* Length field value with CRYPTO frames if present. */ |
| 5012 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5013 | beg = pos; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 5014 | /* When not probing and not acking, and no immediate close is required, |
| 5015 | * reduce the size of this buffer to respect |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5016 | * the congestion controller window. So, we do not limit the size of this |
| 5017 | * packet if we have an ACK frame to send because an ACK frame is not |
| 5018 | * ack-eliciting. This size will be limited if we have ack-eliciting |
| 5019 | * frames to send from qel->pktns->tx.frms. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5020 | */ |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 5021 | if (!probe && !ack && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5022 | size_t path_room; |
| 5023 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5024 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5025 | if (end - beg > path_room) |
| 5026 | end = beg + path_room; |
| 5027 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5028 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5029 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 5030 | * length field if any. |
| 5031 | */ |
| 5032 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 5033 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 5034 | goto no_room; |
| 5035 | |
| 5036 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | e1aa0d3 | 2021-08-03 16:03:09 +0200 | [diff] [blame] | 5037 | 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] | 5038 | /* packet number length */ |
| 5039 | *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] | 5040 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5041 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5042 | !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] | 5043 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5044 | !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] | 5045 | goto no_room; |
| 5046 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5047 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 5048 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5049 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5050 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5051 | /* Build an ACK frame if required. */ |
| 5052 | ack_frm_len = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 5053 | 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] | 5054 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 5055 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5056 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 5057 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 5058 | * that from here, we do not know if we will have to send a PING frame. |
| 5059 | * This will be decided after having computed the ack-eliciting frames |
| 5060 | * to be added to this packet. |
| 5061 | */ |
| 5062 | 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] | 5063 | if (!ack_frm_len) |
| 5064 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5065 | } |
| 5066 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5067 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5068 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 5069 | len_frms = 0; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 5070 | if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5071 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 5072 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5073 | /* Initialize the length of the frames built below to <len>. |
| 5074 | * If any frame could be successfully built by qc_build_frms(), |
| 5075 | * we will have len_frms > len. |
| 5076 | */ |
| 5077 | len_frms = len; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5078 | 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] | 5079 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5080 | qc, NULL, NULL, &room); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5081 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5082 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5083 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5084 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 5085 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 5086 | * for the encryption tag. It must be taken into an account for the length |
| 5087 | * of this packet. |
| 5088 | */ |
| 5089 | if (len_frms) |
| 5090 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 5091 | else |
| 5092 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5093 | /* CONNECTION_CLOSE frame */ |
| 5094 | if (cc) { |
| 5095 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5096 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5097 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5098 | len += qc_frm_len(&cc_frm); |
| 5099 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5100 | add_ping_frm = 0; |
| 5101 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5102 | len_sz = quic_int_getsize(len); |
| 5103 | /* Add this packet size to <dglen> */ |
| 5104 | dglen += head_len + len_sz + len; |
| 5105 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 5106 | /* This is a maximum padding size */ |
| 5107 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 5108 | /* The length field value is of this packet is <len> + <padding_len> |
| 5109 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 5110 | * <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] | 5111 | * sizes from <padding_len>. |
| 5112 | */ |
| 5113 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 5114 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5115 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5116 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5117 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5118 | /* 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] | 5119 | add_ping_frm = 1; |
| 5120 | len += 1; |
| 5121 | } |
| 5122 | /* 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] | 5123 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5124 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 5125 | } |
| 5126 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5127 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 5128 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5129 | |
| 5130 | /* Packet number field address. */ |
| 5131 | *buf_pn = pos; |
| 5132 | |
| 5133 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5134 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 5135 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5136 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5137 | 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] | 5138 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5139 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5140 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5141 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 5142 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5143 | |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5144 | list_for_each_entry(cf, &frm_list, list) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5145 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5146 | ssize_t room = end - pos; |
| 5147 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5148 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5149 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 5150 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5151 | } |
| 5152 | } |
| 5153 | |
| 5154 | /* Build a PING frame if needed. */ |
| 5155 | if (add_ping_frm) { |
| 5156 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5157 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5158 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5159 | } |
| 5160 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5161 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5162 | 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] | 5163 | goto no_room; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5164 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5165 | /* Build a PADDING frame if needed. */ |
| 5166 | if (padding_len) { |
| 5167 | frm.type = QUIC_FT_PADDING; |
| 5168 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5169 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5170 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5171 | } |
| 5172 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5173 | /* If this packet is ack-eliciting and we are probing let's |
| 5174 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5175 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5176 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 5177 | qel->pktns->tx.pto_probe) |
| 5178 | qel->pktns->tx.pto_probe--; |
| 5179 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5180 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5181 | LIST_SPLICE(&pkt->frms, &frm_list); |
| 5182 | 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] | 5183 | |
| 5184 | return 1; |
| 5185 | |
| 5186 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5187 | /* Replace the pre-built frames which could not be add to this packet */ |
| 5188 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5189 | 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] | 5190 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5191 | } |
| 5192 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5193 | 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] | 5194 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5195 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5196 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5197 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 5198 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5199 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5200 | pkt->next = NULL; |
| 5201 | pkt->refcnt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5202 | } |
| 5203 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5204 | /* 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] | 5205 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 5206 | { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 5207 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5208 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5209 | if (!pkt) |
| 5210 | return; |
| 5211 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5212 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5213 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 5214 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5215 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5216 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5217 | } |
| 5218 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5219 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 5220 | * type for <qc> QUIC connection from <qel> encryption level. |
| 5221 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 5222 | * -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] | 5223 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5224 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 5225 | const unsigned char *buf_end, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5226 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5227 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 5228 | 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] | 5229 | { |
| 5230 | /* The pointer to the packet number field. */ |
| 5231 | unsigned char *buf_pn; |
| 5232 | unsigned char *beg, *end, *payload; |
| 5233 | int64_t pn; |
| 5234 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5235 | struct quic_tls_ctx *tls_ctx; |
| 5236 | struct quic_tx_packet *pkt; |
| 5237 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5238 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5239 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5240 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 5241 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5242 | 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] | 5243 | *err = -2; |
| 5244 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5245 | } |
| 5246 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5247 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5248 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5249 | pn_len = 0; |
| 5250 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5251 | |
| 5252 | pn = qel->pktns->tx.next_pn + 1; |
| 5253 | 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] | 5254 | ack, padding, cc, probe, qel, qc)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5255 | *err = -1; |
| 5256 | goto err; |
| 5257 | } |
| 5258 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5259 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5260 | payload = buf_pn + pn_len; |
| 5261 | payload_len = end - payload; |
| 5262 | aad_len = payload - beg; |
| 5263 | |
| 5264 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 5265 | 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] | 5266 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5267 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5268 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5269 | |
| 5270 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5271 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5272 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 5273 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5274 | 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] | 5275 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5276 | goto err; |
| 5277 | } |
| 5278 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5279 | /* Consume a packet number */ |
| 5280 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 5281 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 41a0760 | 2022-01-04 16:57:37 +0100 | [diff] [blame] | 5282 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes) |
| 5283 | 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] | 5284 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 5285 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5286 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 5287 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5288 | /* 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] | 5289 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5290 | pkt->in_flight_len = pkt->len; |
| 5291 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5292 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5293 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5294 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5295 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5296 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5297 | |
| 5298 | err: |
| 5299 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5300 | 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] | 5301 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5302 | } |
| 5303 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 5304 | /* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>. |
| 5305 | * Return the number of bytes which have been copied. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5306 | */ |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 5307 | static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx, |
| 5308 | struct buffer *buf, size_t count, int flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5309 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5310 | size_t try, done = 0; |
| 5311 | |
| 5312 | if (!conn_ctrl_ready(conn)) |
| 5313 | return 0; |
| 5314 | |
| 5315 | if (!fd_recv_ready(conn->handle.fd)) |
| 5316 | return 0; |
| 5317 | |
| 5318 | conn->flags &= ~CO_FL_WAIT_ROOM; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5319 | |
| 5320 | /* read the largest possible block. For this, we perform only one call |
| 5321 | * 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] | 5322 | * 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] | 5323 | */ |
| 5324 | while (count > 0) { |
| 5325 | try = b_contig_space(buf); |
| 5326 | if (!try) |
| 5327 | break; |
| 5328 | |
| 5329 | if (try > count) |
| 5330 | try = count; |
| 5331 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 5332 | b_add(buf, try); |
| 5333 | done += try; |
| 5334 | count -= try; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5335 | } |
| 5336 | |
| 5337 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 5338 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5339 | |
| 5340 | leave: |
| 5341 | return done; |
| 5342 | |
| 5343 | read0: |
| 5344 | conn_sock_read0(conn); |
| 5345 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5346 | |
| 5347 | /* Now a final check for a possible asynchronous low-level error |
| 5348 | * report. This can happen when a connection receives a reset |
| 5349 | * after a shutdown, both POLL_HUP and POLL_ERR are queued, and |
| 5350 | * we might have come from there by just checking POLL_HUP instead |
| 5351 | * of recv()'s return value 0, so we have no way to tell there was |
| 5352 | * an error without checking. |
| 5353 | */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 5354 | 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] | 5355 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 5356 | goto leave; |
| 5357 | } |
| 5358 | |
| 5359 | |
| 5360 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 5361 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 5362 | * other pending data for example, but this flag is ignored at the moment. |
| 5363 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5364 | * a second call may be performed. The connection's flags are updated with |
| 5365 | * whatever special event is detected (error, empty). The caller is responsible |
| 5366 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5367 | * function does not call the connection's polling update function, so the caller |
| 5368 | * is responsible for this. It's up to the caller to update the buffer's contents |
| 5369 | * based on the return value. |
| 5370 | */ |
| 5371 | static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags) |
| 5372 | { |
| 5373 | ssize_t ret; |
| 5374 | size_t try, done; |
| 5375 | int send_flag; |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 5376 | struct quic_conn *qc = ((struct ssl_sock_ctx *)xprt_ctx)->qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5377 | |
| 5378 | done = 0; |
| 5379 | /* send the largest possible block. For this we perform only one call |
| 5380 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5381 | * in which case we accept to do it once again. |
| 5382 | */ |
| 5383 | while (count) { |
| 5384 | try = b_contig_data(buf, done); |
| 5385 | if (try > count) |
| 5386 | try = count; |
| 5387 | |
| 5388 | send_flag = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 5389 | if (try < count || flags & CO_SFL_MSG_MORE) |
| 5390 | send_flag |= MSG_MORE; |
| 5391 | |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 5392 | ret = sendto(qc->li->rx.fd, b_peek(buf, done), try, send_flag, |
| 5393 | (struct sockaddr *)&qc->peer_addr, get_addr_len(&qc->peer_addr)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5394 | if (ret > 0) { |
| 5395 | count -= ret; |
| 5396 | done += ret; |
| 5397 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5398 | if (ret < try) |
| 5399 | break; |
| 5400 | } |
| 5401 | else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) { |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 5402 | ABORT_NOW(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5403 | } |
| 5404 | else if (errno != EINTR) { |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 5405 | ABORT_NOW(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5406 | } |
| 5407 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5408 | |
| 5409 | if (done > 0) { |
| 5410 | /* we count the total bytes sent, and the send rate for 32-byte |
| 5411 | * blocks. The reason for the latter is that freq_ctr are |
| 5412 | * limited to 4GB and that it's not enough per second. |
| 5413 | */ |
| 5414 | _HA_ATOMIC_ADD(&global.out_bytes, done); |
| 5415 | update_freq_ctr(&global.out_32bps, (done + 16) / 32); |
| 5416 | } |
| 5417 | return done; |
| 5418 | } |
| 5419 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5420 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 5421 | * event subscriber <es> is not allowed to change from a previous call as long |
| 5422 | * as at least one event is still subscribed. The <event_type> must only be a |
| 5423 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 5424 | */ |
| 5425 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5426 | { |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5427 | struct qcc *qcc = conn->qc->qcc; |
| 5428 | |
| 5429 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 5430 | BUG_ON(qcc->subs && qcc->subs != es); |
| 5431 | |
| 5432 | es->events |= event_type; |
| 5433 | qcc->subs = es; |
| 5434 | |
| 5435 | if (event_type & SUB_RETRY_RECV) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5436 | 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] | 5437 | |
| 5438 | if (event_type & SUB_RETRY_SEND) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5439 | 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] | 5440 | |
| 5441 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5442 | } |
| 5443 | |
| 5444 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5445 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5446 | * subscribe() call. It always returns zero. |
| 5447 | */ |
| 5448 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5449 | { |
| 5450 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 5451 | } |
| 5452 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5453 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 5454 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5455 | */ |
| 5456 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 5457 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5458 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5459 | |
| 5460 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 5461 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5462 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5463 | if (*xprt_ctx) |
| 5464 | goto out; |
| 5465 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5466 | HA_ATOMIC_STORE(xprt_ctx, conn->qc->xprt_ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5467 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5468 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5469 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5470 | |
| 5471 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5472 | } |
| 5473 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5474 | /* Start the QUIC transport layer */ |
| 5475 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 5476 | { |
| 5477 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5478 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5479 | |
| 5480 | qc = conn->qc; |
| 5481 | if (!quic_conn_init_timer(qc)) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5482 | 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] | 5483 | return 0; |
| 5484 | } |
| 5485 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame^] | 5486 | quic_mux_transport_params_update(qc->qcc); |
| 5487 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 5488 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
| 5489 | return 0; |
| 5490 | } |
| 5491 | |
| 5492 | /* mux-quic can now be considered ready. */ |
| 5493 | qc->mux_state = QC_MUX_READY; |
| 5494 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5495 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 5496 | return 1; |
| 5497 | } |
| 5498 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5499 | /* transport-layer operations for QUIC connections. */ |
| 5500 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 5501 | .close = quic_close, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5502 | .snd_buf = quic_conn_from_buf, |
| 5503 | .rcv_buf = quic_conn_to_buf, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5504 | .subscribe = quic_conn_subscribe, |
| 5505 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5506 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5507 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5508 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 5509 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 5510 | .get_alpn = ssl_sock_get_alpn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5511 | .name = "QUIC", |
| 5512 | }; |
| 5513 | |
| 5514 | __attribute__((constructor)) |
| 5515 | static void __quic_conn_init(void) |
| 5516 | { |
| 5517 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 5518 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 5519 | } |
| 5520 | |
| 5521 | __attribute__((destructor)) |
| 5522 | static void __quic_conn_deinit(void) |
| 5523 | { |
| 5524 | BIO_meth_free(ha_quic_meth); |
| 5525 | } |
| 5526 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5527 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 5528 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5529 | * 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] | 5530 | */ |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5531 | 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] | 5532 | struct sockaddr_storage *saddr, qpkt_read_func *func) |
| 5533 | { |
| 5534 | unsigned char *pos; |
| 5535 | const unsigned char *end; |
| 5536 | struct quic_dgram_ctx dgram_ctx = { |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 5537 | .qc = NULL, |
| 5538 | .dcid.len = 0, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5539 | .owner = owner, |
| 5540 | }; |
| 5541 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5542 | pos = (unsigned char *)b_head(buf); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5543 | end = pos + len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5544 | do { |
| 5545 | int ret; |
| 5546 | struct quic_rx_packet *pkt; |
| 5547 | |
Willy Tarreau | e449893 | 2021-03-22 21:13:05 +0100 | [diff] [blame] | 5548 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5549 | if (!pkt) |
| 5550 | goto err; |
| 5551 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5552 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5553 | ret = func(pos, end, pkt, &dgram_ctx, saddr); |
| 5554 | pos += pkt->len; |
Frédéric Lécaille | 310d1bd | 2021-09-22 15:10:49 +0200 | [diff] [blame] | 5555 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 5556 | if (ret == -1) |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 5557 | /* If the packet length could not be found, we cannot continue. */ |
| 5558 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5559 | } while (pos < end); |
| 5560 | |
| 5561 | /* Increasing the received bytes counter by the UDP datagram length |
| 5562 | * if this datagram could be associated to a connection. |
| 5563 | */ |
| 5564 | if (dgram_ctx.qc) |
| 5565 | dgram_ctx.qc->rx.bytes += len; |
| 5566 | |
Amaury Denoyelle | ce340fe | 2022-01-11 14:20:46 +0100 | [diff] [blame] | 5567 | return pos - (unsigned char *)b_head(buf); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5568 | |
| 5569 | err: |
| 5570 | return -1; |
| 5571 | } |
| 5572 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5573 | 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] | 5574 | struct sockaddr_storage *saddr) |
| 5575 | { |
| 5576 | return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv); |
| 5577 | } |
| 5578 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5579 | /* Function to automatically activate QUIC traces on stdout. |
| 5580 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 5581 | * Main use for now is in the docker image for QUIC interop testing. |
| 5582 | */ |
| 5583 | static void quic_init_stdout_traces(void) |
| 5584 | { |
| 5585 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 5586 | trace_quic.sink = sink_find("stdout"); |
| 5587 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5588 | trace_quic.state = TRACE_STATE_RUNNING; |
| 5589 | #endif |
| 5590 | } |
| 5591 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 5592 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5593 | /* |
| 5594 | * Local variables: |
| 5595 | * c-indent-level: 8 |
| 5596 | * c-basic-offset: 8 |
| 5597 | * End: |
| 5598 | */ |