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> |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 49 | #include <haproxy/proto_quic.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 50 | #include <haproxy/quic_tls.h> |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 51 | #include <haproxy/sink.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 52 | #include <haproxy/ssl_sock.h> |
| 53 | #include <haproxy/stream_interface.h> |
| 54 | #include <haproxy/task.h> |
| 55 | #include <haproxy/trace.h> |
| 56 | #include <haproxy/xprt_quic.h> |
| 57 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 58 | /* list of supported QUIC versions by this implementation */ |
| 59 | static int quic_supported_version[] = { |
| 60 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 61 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 62 | |
| 63 | /* placeholder, do not add entry after this */ |
| 64 | 0x0 |
| 65 | }; |
| 66 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 67 | /* This is the values of some QUIC transport parameters when absent. |
| 68 | * Should be used to initialize any transport parameters (local or remote) |
| 69 | * before updating them with customized values. |
| 70 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 71 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 72 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 73 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 74 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 75 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* trace source and events */ |
| 79 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 80 | const struct trace_source *src, |
| 81 | const struct ist where, const struct ist func, |
| 82 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 83 | |
| 84 | static const struct trace_event quic_trace_events[] = { |
| 85 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 86 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 87 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 89 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 90 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 91 | { .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] | 92 | { .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] | 93 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 94 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 95 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 96 | { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" }, |
| 97 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 100 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 101 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 102 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 103 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 104 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 105 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 106 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 107 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 108 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 109 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 110 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 111 | { .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] | 112 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 113 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 114 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 115 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 116 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 117 | { .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] | 118 | { .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] | 119 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 120 | { .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] | 121 | { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" }, |
| 122 | { .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] | 123 | { /* end */ } |
| 124 | }; |
| 125 | |
| 126 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 127 | /* arg1 */ { /* already used by the connection */ }, |
| 128 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 129 | /* arg3 */ { }, |
| 130 | /* arg4 */ { } |
| 131 | }; |
| 132 | |
| 133 | static const struct name_desc quic_trace_decoding[] = { |
| 134 | #define QUIC_VERB_CLEAN 1 |
| 135 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 136 | { /* end */ } |
| 137 | }; |
| 138 | |
| 139 | |
| 140 | struct trace_source trace_quic = { |
| 141 | .name = IST("quic"), |
| 142 | .desc = "QUIC xprt", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 143 | .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] | 144 | .default_cb = quic_trace, |
| 145 | .known_events = quic_trace_events, |
| 146 | .lockon_args = quic_trace_lockon_args, |
| 147 | .decoding = quic_trace_decoding, |
| 148 | .report_events = ~0, /* report everything by default */ |
| 149 | }; |
| 150 | |
| 151 | #define TRACE_SOURCE &trace_quic |
| 152 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 153 | |
| 154 | static BIO_METHOD *ha_quic_meth; |
| 155 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 156 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_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 | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 163 | DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 164 | 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] | 165 | 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] | 166 | 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] | 167 | 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] | 168 | 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] | 169 | 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] | 170 | 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] | 171 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 172 | 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] | 173 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 174 | 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] | 175 | 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] | 176 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 177 | /* Only for debug purpose */ |
| 178 | struct enc_debug_info { |
| 179 | unsigned char *payload; |
| 180 | size_t payload_len; |
| 181 | unsigned char *aad; |
| 182 | size_t aad_len; |
| 183 | uint64_t pn; |
| 184 | }; |
| 185 | |
| 186 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 187 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 188 | unsigned char *payload, size_t payload_len, |
| 189 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 190 | { |
| 191 | edi->payload = payload; |
| 192 | edi->payload_len = payload_len; |
| 193 | edi->aad = aad; |
| 194 | edi->aad_len = aad_len; |
| 195 | edi->pn = pn; |
| 196 | } |
| 197 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 198 | /* Trace callback for QUIC. |
| 199 | * These traces always expect that arg1, if non-null, is of type connection. |
| 200 | */ |
| 201 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 202 | const struct ist where, const struct ist func, |
| 203 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 204 | { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 205 | const struct quic_conn *qc = a1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 206 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 207 | if (qc) { |
| 208 | const struct quic_tls_secrets *secs; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 209 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 210 | chunk_appendf(&trace_buf, " : qc@%p", qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 211 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 212 | chunk_appendf(&trace_buf, "\n odcid"); |
| 213 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 214 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 215 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 216 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 217 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 221 | const enum ssl_encryption_level_t *level = a2; |
| 222 | const size_t *len = a3; |
| 223 | |
| 224 | if (level) { |
| 225 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 226 | |
| 227 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 228 | } |
| 229 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 230 | 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] | 231 | } |
| 232 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 233 | /* Initial read & write secrets. */ |
| 234 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 235 | const unsigned char *rx_sec = a2; |
| 236 | const unsigned char *tx_sec = a3; |
| 237 | |
| 238 | secs = &qc->els[level].tls_ctx.rx; |
| 239 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 240 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 241 | if (rx_sec) |
| 242 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 243 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 244 | } |
| 245 | secs = &qc->els[level].tls_ctx.tx; |
| 246 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 247 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 248 | if (tx_sec) |
| 249 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 250 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 251 | } |
| 252 | } |
| 253 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 254 | const enum ssl_encryption_level_t *level = a2; |
| 255 | const unsigned char *secret = a3; |
| 256 | const size_t *secret_len = a4; |
| 257 | |
| 258 | if (level) { |
| 259 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 260 | |
| 261 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 262 | if (secret && secret_len) |
| 263 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 264 | secs = &qc->els[lvl].tls_ctx.rx; |
| 265 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 266 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 271 | const enum ssl_encryption_level_t *level = a2; |
| 272 | const unsigned char *secret = a3; |
| 273 | const size_t *secret_len = a4; |
| 274 | |
| 275 | if (level) { |
| 276 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 277 | |
| 278 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 279 | if (secret && secret_len) |
| 280 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 281 | secs = &qc->els[lvl].tls_ctx.tx; |
| 282 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 283 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 284 | } |
| 285 | |
| 286 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 287 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 288 | 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] | 289 | const struct quic_tx_packet *pkt = a2; |
| 290 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 291 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 292 | |
| 293 | if (qel) { |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 294 | const struct quic_pktns *pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 295 | 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] | 296 | "if=%llu pp=%u", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 297 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 298 | (unsigned long long)qc->path->cwnd, |
| 299 | (unsigned long long)qc->path->prep_in_flight, |
| 300 | (unsigned long long)qc->path->in_flight, |
| 301 | (unsigned long long)pktns->tx.in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 302 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 303 | } |
| 304 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 305 | const struct quic_frame *frm; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 306 | if (pkt->pn_node.key != (uint64_t)-1) |
| 307 | 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] | 308 | list_for_each_entry(frm, &pkt->frms, list) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 309 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 8b6ea17 | 2022-01-17 10:51:43 +0100 | [diff] [blame] | 310 | chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu", |
| 311 | (unsigned long long)qc->rx.bytes, |
| 312 | (unsigned long long)qc->tx.bytes); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | if (room) { |
| 316 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 317 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 318 | (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] | 319 | } |
| 320 | } |
| 321 | |
| 322 | if (mask & QUIC_EV_CONN_HDSHK) { |
| 323 | const enum quic_handshake_state *state = a2; |
| 324 | const int *err = a3; |
| 325 | |
| 326 | if (state) |
| 327 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 328 | if (err) |
| 329 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 330 | } |
| 331 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 332 | 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] | 333 | const struct quic_rx_packet *pkt = a2; |
| 334 | const unsigned long *pktlen = a3; |
| 335 | const SSL *ssl = a4; |
| 336 | |
| 337 | if (pkt) { |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 338 | chunk_appendf(&trace_buf, " pkt@%p el=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 339 | pkt, quic_packet_type_enc_level_char(pkt->type)); |
| 340 | if (pkt->pnl) |
| 341 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 342 | (unsigned long long)pkt->pn); |
| 343 | if (pkt->token_len) |
| 344 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 345 | (unsigned long long)pkt->token_len); |
| 346 | if (pkt->aad_len) |
| 347 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 348 | (unsigned long long)pkt->aad_len); |
| 349 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 350 | pkt->flags, (unsigned long long)pkt->len); |
| 351 | } |
| 352 | if (pktlen) |
| 353 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 354 | if (ssl) { |
| 355 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 356 | chunk_appendf(&trace_buf, " el=%c", |
| 357 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 362 | const struct quic_rx_packet *pkt = a2; |
| 363 | const struct quic_rx_crypto_frm *cf = a3; |
| 364 | const SSL *ssl = a4; |
| 365 | |
| 366 | if (pkt) |
| 367 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 368 | quic_packet_type_enc_level_char(pkt->type), |
| 369 | (unsigned long long)pkt->pn); |
| 370 | if (cf) |
| 371 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 372 | (unsigned long long)cf->offset_node.key, |
| 373 | (unsigned long long)cf->len); |
| 374 | if (ssl) { |
| 375 | 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] | 376 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 377 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 378 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 379 | |
| 380 | if (qc->err_code) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 381 | 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] | 382 | } |
| 383 | |
| 384 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 385 | const struct quic_frame *frm = a2; |
| 386 | |
| 387 | if (frm) |
| 388 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 389 | } |
| 390 | |
| 391 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 392 | const struct quic_enc_level *qel = a2; |
| 393 | |
| 394 | if (qel) { |
Frédéric Lécaille | dd51da5 | 2021-12-29 15:36:25 +0100 | [diff] [blame] | 395 | const struct quic_pktns *pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 396 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 397 | " 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] | 398 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 546186b | 2021-08-03 14:25:36 +0200 | [diff] [blame] | 399 | quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)), |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 400 | !!(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] | 401 | (unsigned long long)qc->path->cwnd, |
| 402 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 403 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 404 | (unsigned long long)pktns->tx.in_flight, |
| 405 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 409 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 410 | const struct enc_debug_info *edi = a2; |
| 411 | |
| 412 | if (edi) |
| 413 | chunk_appendf(&trace_buf, |
| 414 | " payload=@%p payload_len=%llu" |
| 415 | " aad=@%p aad_len=%llu pn=%llu", |
| 416 | edi->payload, (unsigned long long)edi->payload_len, |
| 417 | edi->aad, (unsigned long long)edi->aad_len, |
| 418 | (unsigned long long)edi->pn); |
| 419 | } |
| 420 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 421 | if (mask & QUIC_EV_CONN_RMHP) { |
| 422 | const struct quic_rx_packet *pkt = a2; |
| 423 | |
| 424 | if (pkt) { |
| 425 | const int *ret = a3; |
| 426 | |
| 427 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 428 | if (ret && *ret) |
| 429 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 430 | pkt->pnl, (unsigned long long)pkt->pn); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 435 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 436 | const unsigned long *val1 = a3; |
| 437 | const unsigned long *val2 = a4; |
| 438 | |
| 439 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 440 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 441 | if (val1) |
| 442 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 443 | if (val2) |
| 444 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 445 | } |
| 446 | |
| 447 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 448 | const unsigned int *rtt_sample = a2; |
| 449 | const unsigned int *ack_delay = a3; |
| 450 | const struct quic_loss *ql = a4; |
| 451 | |
| 452 | if (rtt_sample) |
| 453 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 454 | if (ack_delay) |
| 455 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 456 | if (ql) |
| 457 | chunk_appendf(&trace_buf, |
| 458 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 459 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 460 | } |
| 461 | if (mask & QUIC_EV_CONN_CC) { |
| 462 | const struct quic_cc_event *ev = a2; |
| 463 | const struct quic_cc *cc = a3; |
| 464 | |
| 465 | if (a2) |
| 466 | quic_cc_event_trace(&trace_buf, ev); |
| 467 | if (a3) |
| 468 | quic_cc_state_trace(&trace_buf, cc); |
| 469 | } |
| 470 | |
| 471 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 472 | const struct quic_pktns *pktns = a2; |
| 473 | const struct list *lost_pkts = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 474 | |
| 475 | if (pktns) { |
| 476 | chunk_appendf(&trace_buf, " pktns=%s", |
| 477 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 478 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 479 | if (pktns->tx.loss_time) |
| 480 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 481 | 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] | 482 | } |
| 483 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 484 | struct quic_tx_packet *pkt; |
| 485 | |
| 486 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 487 | list_for_each_entry(pkt, lost_pkts, list) |
| 488 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | 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] | 493 | const struct quic_pktns *pktns = a2; |
| 494 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 495 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 496 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 497 | if (ifae_pkts) |
| 498 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 499 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 500 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 501 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 502 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 503 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 504 | pktns->tx.pto_probe); |
Frédéric Lécaille | 22cfd83 | 2021-12-27 17:42:51 +0100 | [diff] [blame] | 505 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) { |
| 506 | if (pktns->tx.in_flight) |
| 507 | 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] | 508 | if (pktns->tx.loss_time) |
| 509 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 510 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 511 | } |
| 512 | if (mask & QUIC_EV_CONN_SPTO) { |
| 513 | if (pktns->tx.time_of_last_eliciting) |
| 514 | chunk_appendf(&trace_buf, " tole=%dms", |
| 515 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 516 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 517 | 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] | 518 | } |
| 519 | } |
| 520 | |
| 521 | if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) { |
| 522 | chunk_appendf(&trace_buf, |
| 523 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 528 | const struct quic_tx_packet *pkt = a2; |
| 529 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 530 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 531 | (unsigned long long)qc->path->cwnd, |
| 532 | (unsigned long long)qc->path->prep_in_flight, |
| 533 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 534 | if (pkt) { |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 535 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 536 | (unsigned long)pkt->pn_node.key, |
| 537 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 538 | 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] | 539 | (unsigned long long)pkt->in_flight_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 540 | } |
| 541 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 542 | |
| 543 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 544 | const uint8_t *alert = a2; |
| 545 | const enum ssl_encryption_level_t *level = a3; |
| 546 | |
| 547 | if (alert) |
| 548 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 549 | if (level) |
| 550 | chunk_appendf(&trace_buf, " el=%c", |
| 551 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 552 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 553 | |
| 554 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 555 | const size_t *sz1 = a2; |
| 556 | const size_t *sz2 = a3; |
| 557 | const size_t *sz3 = a4; |
| 558 | |
| 559 | if (sz1) |
| 560 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 561 | if (sz2) |
| 562 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 563 | if (sz3) |
| 564 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 565 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 566 | |
| 567 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 568 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 569 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 570 | if (frm) |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 571 | chunk_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 572 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 573 | } |
| 574 | if (mask & QUIC_EV_CONN_LPKT) { |
| 575 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 576 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 577 | |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 578 | if (pkt) { |
| 579 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s", |
| 580 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short"); |
| 581 | if (pkt->pn_node.key != (uint64_t)-1) |
| 582 | chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key); |
| 583 | } |
| 584 | |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 585 | if (len) |
| 586 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | } |
| 590 | |
| 591 | /* 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] | 592 | 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] | 593 | { |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 594 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 595 | |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 596 | if (!qc_is_listener(qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 597 | return 1; |
| 598 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 599 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 600 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 601 | if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) || |
| 602 | (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] | 603 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 604 | return 1; |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 610 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 611 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 612 | 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] | 613 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 614 | struct quic_pktns *pktns; |
| 615 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 616 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 617 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 618 | TRACE_ENTER(QUIC_EV_CONN_STIMER, qc, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 619 | NULL, NULL, &qc->path->ifae_pkts); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 620 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 621 | pktns = quic_loss_pktns(qc); |
| 622 | if (tick_isset(pktns->tx.loss_time)) { |
| 623 | qc->timer = pktns->tx.loss_time; |
| 624 | goto out; |
| 625 | } |
| 626 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 627 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 628 | * cancelled for a server which reached the anti-amplification limit. |
| 629 | */ |
Frédéric Lécaille | 078634d | 2022-01-04 16:59:42 +0100 | [diff] [blame] | 630 | if (!quic_peer_validated_addr(qc) && |
| 631 | (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 632 | 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] | 633 | qc->timer = TICK_ETERNITY; |
| 634 | goto out; |
| 635 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 636 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 637 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 638 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 639 | /* Timer cancellation. */ |
| 640 | qc->timer = TICK_ETERNITY; |
| 641 | goto out; |
| 642 | } |
| 643 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 644 | handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE; |
| 645 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 646 | if (tick_isset(pto)) |
| 647 | qc->timer = pto; |
| 648 | out: |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 649 | if (qc->timer_task && qc->timer != TICK_ETERNITY) |
Amaury Denoyelle | 336f6fd | 2021-10-05 14:42:25 +0200 | [diff] [blame] | 650 | task_schedule(qc->timer_task, qc->timer); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 651 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Frédéric Lécaille | a7973a6 | 2021-11-30 11:10:36 +0100 | [diff] [blame] | 654 | /* Derive new keys and ivs required for Key Update feature for <qc> QUIC |
| 655 | * connection. |
| 656 | * Return 1 if succeeded, 0 if not. |
| 657 | */ |
| 658 | static int quic_tls_key_update(struct quic_conn *qc) |
| 659 | { |
| 660 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 661 | struct quic_tls_secrets *rx, *tx; |
| 662 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 663 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 664 | |
| 665 | tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 666 | rx = &tls_ctx->rx; |
| 667 | tx = &tls_ctx->tx; |
| 668 | nxt_rx = &qc->ku.nxt_rx; |
| 669 | nxt_tx = &qc->ku.nxt_tx; |
| 670 | |
| 671 | /* Prepare new RX secrets */ |
| 672 | if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen, |
| 673 | rx->secret, rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 674 | 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] | 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, |
| 679 | nxt_rx->key, nxt_rx->keylen, |
| 680 | nxt_rx->iv, nxt_rx->ivlen, NULL, 0, |
| 681 | nxt_rx->secret, nxt_rx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 682 | 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] | 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | /* Prepare new TX secrets */ |
| 687 | if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen, |
| 688 | tx->secret, tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 689 | 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] | 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, |
| 694 | nxt_tx->key, nxt_tx->keylen, |
| 695 | nxt_tx->iv, nxt_tx->ivlen, NULL, 0, |
| 696 | nxt_tx->secret, nxt_tx->secretlen)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 697 | 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] | 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | return 1; |
| 702 | } |
| 703 | |
| 704 | /* Rotate the Key Update information for <qc> QUIC connection. |
| 705 | * Must be used after having updated them. |
| 706 | * Always succeeds. |
| 707 | */ |
| 708 | static void quic_tls_rotate_keys(struct quic_conn *qc) |
| 709 | { |
| 710 | struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx; |
| 711 | unsigned char *curr_secret, *curr_iv, *curr_key; |
| 712 | |
| 713 | /* Rotate the RX secrets */ |
| 714 | curr_secret = tls_ctx->rx.secret; |
| 715 | curr_iv = tls_ctx->rx.iv; |
| 716 | curr_key = tls_ctx->rx.key; |
| 717 | |
| 718 | tls_ctx->rx.secret = qc->ku.nxt_rx.secret; |
| 719 | tls_ctx->rx.iv = qc->ku.nxt_rx.iv; |
| 720 | tls_ctx->rx.key = qc->ku.nxt_rx.key; |
| 721 | |
| 722 | qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret; |
| 723 | qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv; |
| 724 | qc->ku.nxt_rx.key = qc->ku.prv_rx.key; |
| 725 | |
| 726 | qc->ku.prv_rx.secret = curr_secret; |
| 727 | qc->ku.prv_rx.iv = curr_iv; |
| 728 | qc->ku.prv_rx.key = curr_key; |
| 729 | qc->ku.prv_rx.pn = tls_ctx->rx.pn; |
| 730 | |
| 731 | /* Update the TX secrets */ |
| 732 | curr_secret = tls_ctx->tx.secret; |
| 733 | curr_iv = tls_ctx->tx.iv; |
| 734 | curr_key = tls_ctx->tx.key; |
| 735 | |
| 736 | tls_ctx->tx.secret = qc->ku.nxt_tx.secret; |
| 737 | tls_ctx->tx.iv = qc->ku.nxt_tx.iv; |
| 738 | tls_ctx->tx.key = qc->ku.nxt_tx.key; |
| 739 | |
| 740 | qc->ku.nxt_tx.secret = curr_secret; |
| 741 | qc->ku.nxt_tx.iv = curr_iv; |
| 742 | qc->ku.nxt_tx.key = curr_key; |
| 743 | } |
| 744 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 745 | #ifndef OPENSSL_IS_BORINGSSL |
| 746 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 747 | const uint8_t *read_secret, |
| 748 | const uint8_t *write_secret, size_t secret_len) |
| 749 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 750 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 751 | 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] | 752 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 753 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 754 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 755 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 756 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 757 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 758 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 759 | goto out; |
| 760 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 761 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 762 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 763 | 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] | 764 | goto err; |
| 765 | } |
| 766 | |
| 767 | rx = &tls_ctx->rx; |
| 768 | tx = &tls_ctx->tx; |
| 769 | |
| 770 | rx->aead = tx->aead = tls_aead(cipher); |
| 771 | rx->md = tx->md = tls_md(cipher); |
| 772 | rx->hp = tx->hp = tls_hp(cipher); |
| 773 | |
| 774 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 775 | 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] | 776 | read_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 777 | 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] | 778 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 781 | rx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 61b851d | 2022-01-28 21:38:45 +0100 | [diff] [blame] | 782 | /* Enqueue this connection asap if we could derive O-RTT secrets as |
| 783 | * listener. Note that a listener derives only RX secrets for this |
| 784 | * level. |
| 785 | */ |
| 786 | if (qc_is_listener(qc) && level == ssl_encryption_early_data) |
| 787 | quic_accept_push_qc(qc); |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 788 | |
| 789 | if (!write_secret) |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 790 | goto out; |
Frédéric Lécaille | 4015cbb | 2021-12-14 19:29:34 +0100 | [diff] [blame] | 791 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 792 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 793 | 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] | 794 | write_secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 795 | 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] | 796 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 799 | tx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 800 | if (level == ssl_encryption_application) { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 801 | struct quic_tls_kp *prv_rx = &qc->ku.prv_rx; |
| 802 | struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx; |
| 803 | struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx; |
| 804 | |
| 805 | if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) || |
| 806 | !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 807 | 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] | 808 | goto err; |
| 809 | } |
| 810 | |
| 811 | memcpy(rx->secret, read_secret, secret_len); |
| 812 | rx->secretlen = secret_len; |
| 813 | memcpy(tx->secret, write_secret, secret_len); |
| 814 | tx->secretlen = secret_len; |
| 815 | /* Initialize all the secret keys lengths */ |
| 816 | prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len; |
| 817 | /* Prepare the next key update */ |
| 818 | if (!quic_tls_key_update(qc)) |
| 819 | goto err; |
| 820 | } |
Frédéric Lécaille | ee4508d | 2022-02-14 17:54:04 +0100 | [diff] [blame] | 821 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 822 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 823 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 824 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 825 | |
| 826 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 827 | 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] | 828 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 829 | } |
| 830 | #else |
| 831 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 832 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 833 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 834 | */ |
| 835 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 836 | const SSL_CIPHER *cipher, |
| 837 | const uint8_t *secret, size_t secret_len) |
| 838 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 839 | 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] | 840 | struct quic_tls_ctx *tls_ctx = |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 841 | &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] | 842 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 843 | TRACE_ENTER(QUIC_EV_CONN_RSEC, qc); |
| 844 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 845 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 846 | goto out; |
| 847 | } |
| 848 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 849 | tls_ctx->rx.aead = tls_aead(cipher); |
| 850 | tls_ctx->rx.md = tls_md(cipher); |
| 851 | tls_ctx->rx.hp = tls_hp(cipher); |
| 852 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 853 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 854 | goto err; |
| 855 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 856 | 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] | 857 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 858 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 859 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 860 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 861 | 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] | 862 | goto err; |
| 863 | } |
| 864 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 865 | if (!qc_is_listener(qc) && level == ssl_encryption_application) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 866 | const unsigned char *buf; |
| 867 | size_t buflen; |
| 868 | |
| 869 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 870 | if (!buflen) |
| 871 | goto err; |
| 872 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 873 | 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] | 874 | goto err; |
| 875 | } |
| 876 | |
| 877 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 878 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 879 | 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] | 880 | |
| 881 | return 1; |
| 882 | |
| 883 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 884 | 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] | 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 889 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 890 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 891 | */ |
| 892 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 893 | const SSL_CIPHER *cipher, |
| 894 | const uint8_t *secret, size_t secret_len) |
| 895 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 896 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 897 | 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] | 898 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 899 | TRACE_ENTER(QUIC_EV_CONN_WSEC, qc); |
| 900 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 901 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 902 | goto out; |
| 903 | } |
| 904 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 905 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 906 | goto err; |
| 907 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 908 | tls_ctx->tx.aead = tls_aead(cipher); |
| 909 | tls_ctx->tx.md = tls_md(cipher); |
| 910 | tls_ctx->tx.hp = tls_hp(cipher); |
| 911 | |
| 912 | 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] | 913 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 914 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 915 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 916 | secret, secret_len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 917 | 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] | 918 | goto err; |
| 919 | } |
| 920 | |
| 921 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 922 | 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] | 923 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 924 | return 1; |
| 925 | |
| 926 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 927 | 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] | 928 | return 0; |
| 929 | } |
| 930 | #endif |
| 931 | |
| 932 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 933 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 934 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 935 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 936 | * store all the data. |
| 937 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 938 | */ |
| 939 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 940 | const unsigned char *data, size_t len) |
| 941 | { |
| 942 | struct quic_crypto_buf **qcb; |
| 943 | /* The remaining byte to store in CRYPTO buffers. */ |
| 944 | size_t cf_offset, cf_len, *nb_buf; |
| 945 | unsigned char *pos; |
| 946 | |
| 947 | nb_buf = &qel->tx.crypto.nb_buf; |
| 948 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 949 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 950 | cf_len = len; |
| 951 | |
| 952 | while (len) { |
| 953 | size_t to_copy, room; |
| 954 | |
| 955 | pos = (*qcb)->data + (*qcb)->sz; |
| 956 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 957 | to_copy = len > room ? room : len; |
| 958 | if (to_copy) { |
| 959 | memcpy(pos, data, to_copy); |
| 960 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 961 | qel->tx.crypto.sz += to_copy; |
| 962 | (*qcb)->sz += to_copy; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 963 | len -= to_copy; |
| 964 | data += to_copy; |
| 965 | } |
| 966 | else { |
| 967 | struct quic_crypto_buf **tmp; |
| 968 | |
| 969 | tmp = realloc(qel->tx.crypto.bufs, |
| 970 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 971 | if (tmp) { |
| 972 | qel->tx.crypto.bufs = tmp; |
| 973 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 974 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 975 | if (!*qcb) |
| 976 | return 0; |
| 977 | |
| 978 | (*qcb)->sz = 0; |
| 979 | ++*nb_buf; |
| 980 | } |
| 981 | else { |
| 982 | break; |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 988 | * have been buffered. |
| 989 | */ |
| 990 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 991 | struct quic_frame *frm; |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 992 | struct quic_frame *found = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 993 | |
Frédéric Lécaille | 81cd3c8 | 2022-01-10 18:31:07 +0100 | [diff] [blame] | 994 | /* There is at most one CRYPTO frame in this packet number |
| 995 | * space. Let's look for it. |
| 996 | */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 997 | 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] | 998 | if (frm->type != QUIC_FT_CRYPTO) |
| 999 | continue; |
| 1000 | |
| 1001 | /* Found */ |
| 1002 | found = frm; |
| 1003 | break; |
| 1004 | } |
| 1005 | |
| 1006 | if (found) { |
| 1007 | found->crypto.len += cf_len; |
| 1008 | } |
| 1009 | else { |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1010 | frm = pool_alloc(pool_head_quic_frame); |
| 1011 | if (!frm) |
| 1012 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1013 | |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1014 | frm->type = QUIC_FT_CRYPTO; |
| 1015 | frm->crypto.offset = cf_offset; |
| 1016 | frm->crypto.len = cf_len; |
| 1017 | frm->crypto.qel = qel; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1018 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d4ecf94 | 2022-01-04 23:15:40 +0100 | [diff] [blame] | 1019 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | return len == 0; |
| 1023 | } |
| 1024 | |
| 1025 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1026 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 1027 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 1028 | { |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1029 | 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] | 1030 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1031 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1032 | } |
| 1033 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1034 | /* Set the application for <qc> QUIC connection. |
| 1035 | * Return 1 if succeeded, 0 if not. |
| 1036 | */ |
| 1037 | int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len) |
| 1038 | { |
Amaury Denoyelle | 4b40f19 | 2022-01-19 11:29:25 +0100 | [diff] [blame] | 1039 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) |
| 1040 | qc->app_ops = &h3_ops; |
| 1041 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) |
| 1042 | qc->app_ops = &hq_interop_ops; |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1043 | else |
| 1044 | return 0; |
| 1045 | |
Frédéric Lécaille | b0bd62d | 2021-12-14 19:34:08 +0100 | [diff] [blame] | 1046 | return 1; |
| 1047 | } |
| 1048 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1049 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 1050 | * wants to provide the QUIC layer with CRYPTO data. |
| 1051 | * Returns 1 if succeeded, 0 if not. |
| 1052 | */ |
| 1053 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 1054 | const uint8_t *data, size_t len) |
| 1055 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1056 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1057 | enum quic_tls_enc_level tel; |
| 1058 | struct quic_enc_level *qel; |
| 1059 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1060 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1061 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc); |
| 1062 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 1063 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1064 | goto out; |
| 1065 | } |
| 1066 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1067 | tel = ssl_to_quic_enc_level(level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1068 | if (tel == -1) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1069 | 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] | 1070 | goto err; |
| 1071 | } |
| 1072 | |
Frédéric Lécaille | 3916ca1 | 2022-02-02 14:09:05 +0100 | [diff] [blame] | 1073 | qel = &qc->els[tel]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1074 | if (!quic_crypto_data_cpy(qel, data, len)) { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1075 | 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] | 1076 | goto err; |
| 1077 | } |
| 1078 | |
| 1079 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1080 | qc, &level, &len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1081 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 1082 | out: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1083 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1084 | return 1; |
| 1085 | |
| 1086 | err: |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1087 | 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] | 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | int ha_quic_flush_flight(SSL *ssl) |
| 1092 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1093 | 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] | 1094 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1095 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc); |
| 1096 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1097 | |
| 1098 | return 1; |
| 1099 | } |
| 1100 | |
| 1101 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 1102 | { |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1103 | 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] | 1104 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1105 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level); |
| 1106 | quic_set_tls_alert(qc, alert); |
| 1107 | 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] | 1108 | return 1; |
| 1109 | } |
| 1110 | |
| 1111 | /* QUIC TLS methods */ |
| 1112 | static SSL_QUIC_METHOD ha_quic_method = { |
| 1113 | #ifdef OPENSSL_IS_BORINGSSL |
| 1114 | .set_read_secret = ha_set_rsec, |
| 1115 | .set_write_secret = ha_set_wsec, |
| 1116 | #else |
| 1117 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1118 | #endif |
| 1119 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1120 | .flush_flight = ha_quic_flush_flight, |
| 1121 | .send_alert = ha_quic_send_alert, |
| 1122 | }; |
| 1123 | |
| 1124 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1125 | * Returns an error count. |
| 1126 | */ |
| 1127 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1128 | { |
| 1129 | struct proxy *curproxy = bind_conf->frontend; |
| 1130 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1131 | int cfgerr = 0; |
| 1132 | |
| 1133 | #if 0 |
| 1134 | /* XXX Did not manage to use this. */ |
| 1135 | const char *ciphers = |
| 1136 | "TLS_AES_128_GCM_SHA256:" |
| 1137 | "TLS_AES_256_GCM_SHA384:" |
| 1138 | "TLS_CHACHA20_POLY1305_SHA256:" |
| 1139 | "TLS_AES_128_CCM_SHA256"; |
| 1140 | #endif |
Frédéric Lécaille | 4b1fddc | 2021-07-01 17:09:05 +0200 | [diff] [blame] | 1141 | 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] | 1142 | long options = |
| 1143 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1144 | SSL_OP_SINGLE_ECDH_USE | |
| 1145 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1146 | SSL_CTX *ctx; |
| 1147 | |
| 1148 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1149 | bind_conf->initial_ctx = ctx; |
| 1150 | |
| 1151 | SSL_CTX_set_options(ctx, options); |
| 1152 | #if 0 |
| 1153 | if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) { |
| 1154 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' " |
| 1155 | "for bind '%s' at [%s:%d].\n", |
| 1156 | curproxy->id, ciphers, |
| 1157 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1158 | cfgerr++; |
| 1159 | } |
| 1160 | #endif |
| 1161 | |
| 1162 | if (SSL_CTX_set1_curves_list(ctx, groups) != 1) { |
| 1163 | ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' " |
| 1164 | "for bind '%s' at [%s:%d].\n", |
| 1165 | curproxy->id, groups, |
| 1166 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1167 | cfgerr++; |
| 1168 | } |
| 1169 | |
| 1170 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1171 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1172 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1173 | SSL_CTX_set_default_verify_paths(ctx); |
| 1174 | |
| 1175 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1176 | #ifdef OPENSSL_IS_BORINGSSL |
| 1177 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1178 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1179 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1180 | if (bind_conf->ssl_conf.early_data) { |
| 1181 | 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] | 1182 | SSL_CTX_set_max_early_data(ctx, 0xffffffff); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1183 | } |
| 1184 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1185 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1186 | #else |
| 1187 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1188 | #endif |
| 1189 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1190 | #endif |
| 1191 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1192 | |
| 1193 | return cfgerr; |
| 1194 | } |
| 1195 | |
| 1196 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1197 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1198 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1199 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1200 | */ |
| 1201 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1202 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1203 | { |
| 1204 | uint64_t expected_pn = largest_pn + 1; |
| 1205 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1206 | uint64_t pn_hwin = pn_win / 2; |
| 1207 | uint64_t pn_mask = pn_win - 1; |
| 1208 | uint64_t candidate_pn; |
| 1209 | |
| 1210 | |
| 1211 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1212 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1213 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1214 | candidate_pn + pn_hwin <= expected_pn) |
| 1215 | return candidate_pn + pn_win; |
| 1216 | |
| 1217 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1218 | return candidate_pn - pn_win; |
| 1219 | |
| 1220 | return candidate_pn; |
| 1221 | } |
| 1222 | |
| 1223 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1224 | * cryptographic context. |
| 1225 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1226 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1227 | * <end> points to one byte past the end of this packet. |
| 1228 | * Returns 1 if succeeded, 0 if not. |
| 1229 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1230 | static int qc_do_rm_hp(struct quic_conn *qc, |
| 1231 | 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] | 1232 | int64_t largest_pn, unsigned char *pn, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1233 | unsigned char *byte0, const unsigned char *end) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1234 | { |
| 1235 | int ret, outlen, i, pnlen; |
| 1236 | uint64_t packet_number; |
| 1237 | uint32_t truncated_pn = 0; |
| 1238 | unsigned char mask[5] = {0}; |
| 1239 | unsigned char *sample; |
| 1240 | EVP_CIPHER_CTX *cctx; |
| 1241 | unsigned char *hp_key; |
| 1242 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1243 | /* Check there is enough data in this packet. */ |
| 1244 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1245 | 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] | 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | cctx = EVP_CIPHER_CTX_new(); |
| 1250 | if (!cctx) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1251 | 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] | 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | ret = 0; |
| 1256 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1257 | |
| 1258 | hp_key = tls_ctx->rx.hp_key; |
| 1259 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1260 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1261 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1262 | 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] | 1263 | goto out; |
| 1264 | } |
| 1265 | |
| 1266 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1267 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1268 | for (i = 0; i < pnlen; i++) { |
| 1269 | pn[i] ^= mask[i + 1]; |
| 1270 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1271 | } |
| 1272 | |
| 1273 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1274 | /* Store remaining information for this unprotected header */ |
| 1275 | pkt->pn = packet_number; |
| 1276 | pkt->pnl = pnlen; |
| 1277 | |
| 1278 | ret = 1; |
| 1279 | |
| 1280 | out: |
| 1281 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1282 | |
| 1283 | return ret; |
| 1284 | } |
| 1285 | |
| 1286 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1287 | * address, with <payload_len> as payload length, <aad> as address of |
| 1288 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1289 | * context. |
| 1290 | * Returns 1 if succeeded, 0 if not. |
| 1291 | */ |
| 1292 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1293 | 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] | 1294 | 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] | 1295 | { |
| 1296 | unsigned char iv[12]; |
| 1297 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1298 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1299 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1300 | |
| 1301 | 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] | 1302 | 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] | 1303 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1307 | tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 1308 | 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] | 1309 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1313 | |
| 1314 | err: |
| 1315 | 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] | 1316 | 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] | 1317 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1321 | * Returns 1 if succeeded, 0 if not. |
| 1322 | */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1323 | 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] | 1324 | { |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1325 | int ret, kp_changed; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1326 | unsigned char iv[12]; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1327 | struct quic_tls_ctx *tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1328 | unsigned char *rx_iv = tls_ctx->rx.iv; |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1329 | size_t rx_iv_sz = tls_ctx->rx.ivlen; |
| 1330 | unsigned char *rx_key = tls_ctx->rx.key; |
| 1331 | |
| 1332 | kp_changed = 0; |
| 1333 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) { |
| 1334 | /* The two tested bits are not at the same position, |
| 1335 | * this is why they are first both inversed. |
| 1336 | */ |
| 1337 | if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) { |
| 1338 | if (pkt->pn < tls_ctx->rx.pn) { |
| 1339 | /* The lowest packet number of a previous key phase |
| 1340 | * cannot be null if it really stores previous key phase |
| 1341 | * secrets. |
| 1342 | */ |
| 1343 | if (!pkt->qc->ku.prv_rx.pn) |
| 1344 | return 0; |
| 1345 | |
| 1346 | rx_iv = pkt->qc->ku.prv_rx.iv; |
| 1347 | rx_key = pkt->qc->ku.prv_rx.key; |
| 1348 | } |
| 1349 | else if (pkt->pn > qel->pktns->rx.largest_pn) { |
| 1350 | /* Next key phase */ |
| 1351 | kp_changed = 1; |
| 1352 | rx_iv = pkt->qc->ku.nxt_rx.iv; |
| 1353 | rx_key = pkt->qc->ku.nxt_rx.key; |
| 1354 | } |
| 1355 | } |
| 1356 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1357 | |
| 1358 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1359 | return 0; |
| 1360 | |
| 1361 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1362 | pkt->data, pkt->aad_len, |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1363 | tls_ctx->rx.aead, rx_key, iv); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1364 | if (!ret) |
| 1365 | return 0; |
| 1366 | |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 1367 | /* Update the keys only if the packet decryption succeeded. */ |
| 1368 | if (kp_changed) { |
| 1369 | quic_tls_rotate_keys(pkt->qc); |
| 1370 | /* Toggle the Key Phase bit */ |
| 1371 | tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET; |
| 1372 | /* Store the lowest packet number received for the current key phase */ |
| 1373 | tls_ctx->rx.pn = pkt->pn; |
| 1374 | /* Prepare the next key update */ |
| 1375 | if (!quic_tls_key_update(pkt->qc)) |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1379 | /* Update the packet length (required to parse the frames). */ |
| 1380 | pkt->len = pkt->aad_len + ret; |
| 1381 | |
| 1382 | return 1; |
| 1383 | } |
| 1384 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1385 | /* Remove from <qcs> stream the acknowledged frames. |
| 1386 | * Never fails. |
| 1387 | */ |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1388 | static int qcs_try_to_consume(struct qcs *qcs) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1389 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1390 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1391 | struct eb64_node *frm_node; |
| 1392 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1393 | ret = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1394 | frm_node = eb64_first(&qcs->tx.acked_frms); |
| 1395 | while (frm_node) { |
| 1396 | struct quic_stream *strm; |
| 1397 | |
| 1398 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1399 | if (strm->offset.key > qcs->tx.ack_offset) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1400 | break; |
| 1401 | |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1402 | if (strm->offset.key + strm->len > qcs->tx.ack_offset) { |
| 1403 | const size_t diff = strm->offset.key + strm->len - |
| 1404 | qcs->tx.ack_offset; |
| 1405 | qcs->tx.ack_offset += diff; |
| 1406 | b_del(strm->buf, diff); |
| 1407 | ret = 1; |
| 1408 | } |
| 1409 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1410 | frm_node = eb64_next(frm_node); |
| 1411 | eb64_delete(&strm->offset); |
| 1412 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1413 | |
| 1414 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1417 | /* 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] | 1418 | static inline void qc_treat_acked_tx_frm(struct quic_conn *qc, |
| 1419 | struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1420 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1421 | int stream_acked; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1422 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1423 | 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] | 1424 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1425 | switch (frm->type) { |
| 1426 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1427 | { |
| 1428 | struct qcs *qcs = frm->stream.qcs; |
| 1429 | struct quic_stream *strm = &frm->stream; |
| 1430 | |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1431 | if (strm->offset.key <= qcs->tx.ack_offset) { |
| 1432 | if (strm->offset.key + strm->len > qcs->tx.ack_offset) { |
| 1433 | const size_t diff = strm->offset.key + strm->len - |
| 1434 | qcs->tx.ack_offset; |
| 1435 | qcs->tx.ack_offset += diff; |
| 1436 | b_del(strm->buf, diff); |
| 1437 | stream_acked = 1; |
| 1438 | } |
| 1439 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1440 | LIST_DELETE(&frm->list); |
| 1441 | pool_free(pool_head_quic_frame, frm); |
| 1442 | } |
| 1443 | else { |
| 1444 | eb64_insert(&qcs->tx.acked_frms, &strm->offset); |
| 1445 | } |
Amaury Denoyelle | 119965f | 2022-02-24 17:39:57 +0100 | [diff] [blame] | 1446 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1447 | stream_acked |= qcs_try_to_consume(qcs); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1448 | } |
| 1449 | break; |
| 1450 | default: |
| 1451 | LIST_DELETE(&frm->list); |
| 1452 | pool_free(pool_head_quic_frame, frm); |
| 1453 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1454 | |
| 1455 | if (stream_acked) { |
| 1456 | struct qcc *qcc = qc->qcc; |
| 1457 | |
| 1458 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1459 | tasklet_wakeup(qcc->subs->tasklet); |
| 1460 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1461 | if (!qcc->subs->events) |
| 1462 | qcc->subs = NULL; |
| 1463 | } |
| 1464 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1468 | * deallocating them, and their TX frames. |
| 1469 | * Returns the last node reached to be used for the next range. |
| 1470 | * May be NULL if <largest> node could not be found. |
| 1471 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1472 | static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc, |
| 1473 | struct eb_root *pkts, |
| 1474 | unsigned int *pkt_flags, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1475 | struct list *newly_acked_pkts, |
| 1476 | struct eb64_node *largest_node, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1477 | uint64_t largest, uint64_t smallest) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1478 | { |
| 1479 | struct eb64_node *node; |
| 1480 | struct quic_tx_packet *pkt; |
| 1481 | |
| 1482 | if (largest_node) |
| 1483 | node = largest_node; |
| 1484 | else { |
| 1485 | node = eb64_lookup(pkts, largest); |
| 1486 | while (!node && largest > smallest) { |
| 1487 | node = eb64_lookup(pkts, --largest); |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1492 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1493 | |
| 1494 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1495 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1496 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1497 | 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] | 1498 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1499 | qc_treat_acked_tx_frm(qc, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1500 | node = eb64_prev(node); |
| 1501 | eb64_delete(&pkt->pn_node); |
| 1502 | } |
| 1503 | |
| 1504 | return node; |
| 1505 | } |
| 1506 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1507 | /* Remove all frames from <pkt_frm_list> and reinsert them in the |
| 1508 | * 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] | 1509 | */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1510 | static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc, |
| 1511 | struct list *pkt_frm_list, |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1512 | struct list *pktns_frm_list) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1513 | { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1514 | struct quic_frame *frm, *frmbak; |
| 1515 | struct list tmp = LIST_HEAD_INIT(tmp); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1516 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1517 | list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) { |
| 1518 | LIST_DELETE(&frm->list); |
| 1519 | 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] | 1520 | LIST_APPEND(&tmp, &frm->list); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1521 | } |
| 1522 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 1523 | LIST_SPLICE(pktns_frm_list, &tmp); |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1524 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1525 | |
| 1526 | /* Free the TX packets of <pkts> list */ |
| 1527 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1528 | { |
| 1529 | struct quic_tx_packet *pkt, *tmp; |
| 1530 | |
| 1531 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1532 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1533 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1534 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | /* Send a packet loss event nofification to the congestion controller |
| 1539 | * attached to <qc> connection with <lost_bytes> the number of lost bytes, |
| 1540 | * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet |
| 1541 | * at <now_us> current time. |
| 1542 | * Always succeeds. |
| 1543 | */ |
| 1544 | static inline void qc_cc_loss_event(struct quic_conn *qc, |
| 1545 | unsigned int lost_bytes, |
| 1546 | unsigned int newest_time_sent, |
| 1547 | unsigned int period, |
| 1548 | unsigned int now_us) |
| 1549 | { |
| 1550 | struct quic_cc_event ev = { |
| 1551 | .type = QUIC_CC_EVT_LOSS, |
| 1552 | .loss.now_ms = now_ms, |
| 1553 | .loss.max_ack_delay = qc->max_ack_delay, |
| 1554 | .loss.lost_bytes = lost_bytes, |
| 1555 | .loss.newest_time_sent = newest_time_sent, |
| 1556 | .loss.period = period, |
| 1557 | }; |
| 1558 | |
| 1559 | quic_cc_event(&qc->path->cc, &ev); |
| 1560 | } |
| 1561 | |
| 1562 | /* Send a packet ack event nofication for each newly acked packet of |
| 1563 | * <newly_acked_pkts> list and free them. |
| 1564 | * Always succeeds. |
| 1565 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1566 | 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] | 1567 | struct list *newly_acked_pkts) |
| 1568 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1569 | struct quic_tx_packet *pkt, *tmp; |
| 1570 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1571 | |
| 1572 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1573 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1574 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1575 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1576 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1577 | ev.ack.acked = pkt->in_flight_len; |
| 1578 | ev.ack.time_sent = pkt->time_sent; |
| 1579 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1580 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1581 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1582 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | } |
| 1586 | |
Frédéric Lécaille | e87524d | 2022-01-19 17:48:40 +0100 | [diff] [blame] | 1587 | /* Release all the frames attached to <pktns> packet number space */ |
| 1588 | static inline void qc_release_pktns_frms(struct quic_pktns *pktns) |
| 1589 | { |
| 1590 | struct quic_frame *frm, *frmbak; |
| 1591 | |
| 1592 | list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) { |
| 1593 | LIST_DELETE(&frm->list); |
| 1594 | pool_free(pool_head_quic_frame, frm); |
| 1595 | } |
| 1596 | } |
| 1597 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1598 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1599 | * their TX frames. |
| 1600 | * Send a packet loss event to the congestion controller if |
| 1601 | * in flight packet have been lost. |
| 1602 | * Also frees the packet in <pkts> list. |
| 1603 | * Never fails. |
| 1604 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1605 | static inline void qc_release_lost_pkts(struct quic_conn *qc, |
| 1606 | struct quic_pktns *pktns, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1607 | struct list *pkts, |
| 1608 | uint64_t now_us) |
| 1609 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1610 | 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] | 1611 | uint64_t lost_bytes; |
| 1612 | |
| 1613 | lost_bytes = 0; |
| 1614 | oldest_lost = newest_lost = NULL; |
| 1615 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1616 | struct list tmp = LIST_HEAD_INIT(tmp); |
| 1617 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1618 | lost_bytes += pkt->in_flight_len; |
| 1619 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1620 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1621 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1622 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1623 | /* Treat the frames of this lost packet. */ |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 1624 | qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1625 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1626 | if (!oldest_lost) { |
| 1627 | oldest_lost = newest_lost = pkt; |
| 1628 | } |
| 1629 | else { |
| 1630 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1631 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1632 | newest_lost = pkt; |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | if (lost_bytes) { |
| 1637 | /* Sent a packet loss event to the congestion controller. */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1638 | 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] | 1639 | 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] | 1640 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1641 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1642 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1647 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1648 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1649 | * space if any not deemed lost. |
| 1650 | * Should be called after having received an ACK frame with newly acknowledged |
| 1651 | * packets or when the the loss detection timer has expired. |
| 1652 | * Always succeeds. |
| 1653 | */ |
| 1654 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1655 | struct quic_conn *qc, |
| 1656 | struct list *lost_pkts) |
| 1657 | { |
| 1658 | struct eb_root *pkts; |
| 1659 | struct eb64_node *node; |
| 1660 | struct quic_loss *ql; |
| 1661 | unsigned int loss_delay; |
| 1662 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1663 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1664 | pkts = &pktns->tx.pkts; |
| 1665 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1666 | if (eb_is_empty(pkts)) |
| 1667 | goto out; |
| 1668 | |
| 1669 | ql = &qc->path->loss; |
| 1670 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1671 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1672 | |
| 1673 | node = eb64_first(pkts); |
| 1674 | while (node) { |
| 1675 | struct quic_tx_packet *pkt; |
| 1676 | int64_t largest_acked_pn; |
| 1677 | unsigned int loss_time_limit, time_sent; |
| 1678 | |
| 1679 | 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] | 1680 | 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] | 1681 | node = eb64_next(node); |
| 1682 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1683 | break; |
| 1684 | |
| 1685 | time_sent = pkt->time_sent; |
| 1686 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1687 | if (tick_is_le(time_sent, now_ms) || |
| 1688 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1689 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1690 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1691 | } |
| 1692 | else { |
Frédéric Lécaille | dc90c07 | 2021-12-27 18:15:27 +0100 | [diff] [blame] | 1693 | if (tick_isset(pktns->tx.loss_time)) |
| 1694 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1695 | else |
| 1696 | pktns->tx.loss_time = loss_time_limit; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1701 | 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] | 1702 | } |
| 1703 | |
| 1704 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1705 | * 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] | 1706 | * 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] | 1707 | * acked ack-eliciting packet. |
| 1708 | * Return 1, if succeeded, 0 if not. |
| 1709 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1710 | static inline int qc_parse_ack_frm(struct quic_conn *qc, |
| 1711 | struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1712 | struct quic_enc_level *qel, |
| 1713 | unsigned int *rtt_sample, |
| 1714 | const unsigned char **pos, const unsigned char *end) |
| 1715 | { |
| 1716 | struct quic_ack *ack = &frm->ack; |
| 1717 | uint64_t smallest, largest; |
| 1718 | struct eb_root *pkts; |
| 1719 | struct eb64_node *largest_node; |
| 1720 | unsigned int time_sent, pkt_flags; |
| 1721 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1722 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1723 | |
| 1724 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1725 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1726 | qc, NULL, &ack->largest_ack); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1727 | goto err; |
| 1728 | } |
| 1729 | |
| 1730 | if (ack->first_ack_range > ack->largest_ack) { |
| 1731 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1732 | qc, NULL, &ack->first_ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1733 | goto err; |
| 1734 | } |
| 1735 | |
| 1736 | largest = ack->largest_ack; |
| 1737 | smallest = largest - ack->first_ack_range; |
| 1738 | pkts = &qel->pktns->tx.pkts; |
| 1739 | pkt_flags = 0; |
| 1740 | largest_node = NULL; |
| 1741 | time_sent = 0; |
| 1742 | |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1743 | 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] | 1744 | largest_node = eb64_lookup(pkts, largest); |
| 1745 | if (!largest_node) { |
| 1746 | TRACE_DEVEL("Largest acked packet not found", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1747 | QUIC_EV_CONN_PRSAFRM, qc); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1748 | } |
| 1749 | else { |
| 1750 | time_sent = eb64_entry(&largest_node->node, |
| 1751 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1752 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1756 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1757 | do { |
| 1758 | uint64_t gap, ack_range; |
| 1759 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1760 | qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts, |
| 1761 | largest_node, largest, smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1762 | if (!ack->ack_range_num--) |
| 1763 | break; |
| 1764 | |
| 1765 | if (!quic_dec_int(&gap, pos, end)) |
| 1766 | goto err; |
| 1767 | |
| 1768 | if (smallest < gap + 2) { |
| 1769 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1770 | qc, NULL, &gap, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1771 | goto err; |
| 1772 | } |
| 1773 | |
| 1774 | largest = smallest - gap - 2; |
| 1775 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1776 | goto err; |
| 1777 | |
| 1778 | if (largest < ack_range) { |
| 1779 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1780 | qc, NULL, &largest, &ack_range); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1781 | goto err; |
| 1782 | } |
| 1783 | |
| 1784 | /* Do not use this node anymore. */ |
| 1785 | largest_node = NULL; |
| 1786 | /* Next range */ |
| 1787 | smallest = largest - ack_range; |
| 1788 | |
| 1789 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1790 | qc, NULL, &largest, &smallest); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1791 | } while (1); |
| 1792 | |
| 1793 | /* 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] | 1794 | 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] | 1795 | |
| 1796 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1797 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1798 | 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] | 1799 | } |
| 1800 | |
| 1801 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1802 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1803 | qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1804 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1805 | 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] | 1806 | } |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1807 | qc_treat_newly_acked_pkts(qc, &newly_acked_pkts); |
| 1808 | if (quic_peer_validated_addr(qc)) |
| 1809 | qc->path->loss.pto_count = 0; |
| 1810 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | |
| 1814 | return 1; |
| 1815 | |
| 1816 | err: |
| 1817 | free_quic_tx_pkts(&newly_acked_pkts); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 1818 | 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] | 1819 | return 0; |
| 1820 | } |
| 1821 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 1822 | /* This function gives the detail of the SSL error. It is used only |
| 1823 | * if the debug mode and the verbose mode are activated. It dump all |
| 1824 | * the SSL error until the stack was empty. |
| 1825 | */ |
| 1826 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 1827 | { |
| 1828 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 1829 | while (1) { |
| 1830 | unsigned long ret; |
| 1831 | |
| 1832 | ret = ERR_get_error(); |
| 1833 | if (!ret) |
| 1834 | return; |
| 1835 | |
| 1836 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 1837 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 1838 | } |
| 1839 | } |
| 1840 | } |
| 1841 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1842 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 1843 | const char **str, int *len); |
| 1844 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1845 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 1846 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1847 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1848 | * Return 1 if succeeded, 0 if not. |
| 1849 | */ |
| 1850 | 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] | 1851 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1852 | const unsigned char *data, size_t len, |
| 1853 | struct quic_rx_packet *pkt, |
| 1854 | struct quic_rx_crypto_frm *cf) |
| 1855 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1856 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 1857 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1858 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1859 | ssl_err = SSL_ERROR_NONE; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 1860 | qc = ctx->qc; |
| 1861 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1862 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc); |
| 1863 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1864 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 1865 | TRACE_PROTO("SSL_provide_quic_data() error", |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1866 | QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1867 | goto err; |
| 1868 | } |
| 1869 | |
| 1870 | el->rx.crypto.offset += len; |
| 1871 | TRACE_PROTO("in order CRYPTO data", |
Frédéric Lécaille | e7ff2b2 | 2021-12-22 17:40:38 +0100 | [diff] [blame] | 1872 | QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1873 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1874 | state = HA_ATOMIC_LOAD(&qc->state); |
| 1875 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1876 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 1877 | if (ssl_err != 1) { |
| 1878 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1879 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1880 | TRACE_PROTO("SSL handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1881 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1882 | goto out; |
| 1883 | } |
| 1884 | |
| 1885 | TRACE_DEVEL("SSL handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1886 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 1887 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 1888 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1889 | goto err; |
| 1890 | } |
| 1891 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1892 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 1893 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1894 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 1895 | /* The connection is ready to be accepted. */ |
| 1896 | quic_accept_push_qc(qc); |
| 1897 | } |
| 1898 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1899 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 1900 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1901 | } else { |
| 1902 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 1903 | if (ssl_err != 1) { |
| 1904 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1905 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1906 | TRACE_DEVEL("SSL post handshake", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1907 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1908 | goto out; |
| 1909 | } |
| 1910 | |
| 1911 | TRACE_DEVEL("SSL post handshake error", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1912 | QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1913 | goto err; |
| 1914 | } |
| 1915 | |
| 1916 | TRACE_PROTO("SSL post handshake succeeded", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1917 | QUIC_EV_CONN_HDSHK, qc, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1918 | } |
Amaury Denoyelle | e2288c3 | 2021-12-03 14:44:21 +0100 | [diff] [blame] | 1919 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1920 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1921 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1922 | return 1; |
| 1923 | |
| 1924 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 1925 | 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] | 1926 | return 0; |
| 1927 | } |
| 1928 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1929 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 1930 | * <pkt> RX packet. |
| 1931 | * Return it if succeeded, NULL if not. |
| 1932 | */ |
| 1933 | static inline |
| 1934 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 1935 | struct quic_rx_packet *pkt) |
| 1936 | { |
| 1937 | struct quic_rx_strm_frm *frm; |
| 1938 | |
| 1939 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 1940 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1941 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1942 | frm->len = stream_frm->len; |
| 1943 | frm->data = stream_frm->data; |
| 1944 | frm->pkt = pkt; |
| 1945 | } |
| 1946 | |
| 1947 | return frm; |
| 1948 | } |
| 1949 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1950 | /* 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] | 1951 | * 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] | 1952 | */ |
| 1953 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 1954 | { |
| 1955 | size_t ret; |
| 1956 | |
| 1957 | ret = 0; |
| 1958 | while (strm_frm->len) { |
| 1959 | size_t try; |
| 1960 | |
| 1961 | try = b_contig_space(buf); |
| 1962 | if (!try) |
| 1963 | break; |
| 1964 | |
| 1965 | if (try > strm_frm->len) |
| 1966 | try = strm_frm->len; |
| 1967 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1968 | strm_frm->len -= try; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1969 | strm_frm->offset.key += try; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1970 | b_add(buf, try); |
| 1971 | ret += try; |
| 1972 | } |
| 1973 | |
| 1974 | return ret; |
| 1975 | } |
| 1976 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 1977 | /* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer. |
Amaury Denoyelle | d1c76f2 | 2022-02-21 17:53:38 +0100 | [diff] [blame] | 1978 | * |
| 1979 | * Note that <strm_frm> is not updated as it is implied that the frame may be |
| 1980 | * present in a tree and offset node is used as the key. The caller should |
| 1981 | * update offset/lenght of the frame after the function call. |
| 1982 | * |
| 1983 | * Return the total count of copied bytes. |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 1984 | */ |
| 1985 | static size_t qc_rx_strm_frm_cpy(struct buffer *buf, |
| 1986 | struct quic_rx_strm_frm *strm_frm) |
| 1987 | { |
Amaury Denoyelle | d1c76f2 | 2022-02-21 17:53:38 +0100 | [diff] [blame] | 1988 | size_t flen = strm_frm->len; |
| 1989 | size_t ret = 0; |
| 1990 | size_t try; |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 1991 | |
| 1992 | ret = 0; |
Amaury Denoyelle | d1c76f2 | 2022-02-21 17:53:38 +0100 | [diff] [blame] | 1993 | while (flen && (try = b_contig_space(buf))) { |
| 1994 | if (try > flen) |
| 1995 | try = flen; |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 1996 | |
Amaury Denoyelle | d1c76f2 | 2022-02-21 17:53:38 +0100 | [diff] [blame] | 1997 | memcpy(b_tail(buf), strm_frm->data + ret, try); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 1998 | b_add(buf, try); |
| 1999 | ret += try; |
Amaury Denoyelle | d1c76f2 | 2022-02-21 17:53:38 +0100 | [diff] [blame] | 2000 | flen -= try; |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | return ret; |
| 2004 | } |
| 2005 | |
| 2006 | /* Process as much as possible RX STREAM frames received for <qcs> */ |
| 2007 | static size_t qc_treat_rx_strm_frms(struct qcs *qcs) |
| 2008 | { |
| 2009 | int total; |
| 2010 | struct eb64_node *frm_node; |
| 2011 | |
| 2012 | total = 0; |
| 2013 | frm_node = eb64_first(&qcs->rx.frms); |
| 2014 | while (frm_node) { |
| 2015 | int ret; |
| 2016 | struct quic_rx_strm_frm *frm; |
| 2017 | |
| 2018 | frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node); |
| 2019 | if (frm->offset_node.key != qcs->rx.offset) |
| 2020 | break; |
| 2021 | |
| 2022 | ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm); |
| 2023 | qcs->rx.offset += ret; |
| 2024 | total += ret; |
Amaury Denoyelle | 4323567 | 2022-02-21 19:08:44 +0100 | [diff] [blame] | 2025 | |
| 2026 | BUG_ON(frm->len < ret); |
| 2027 | if (frm->len - ret > 0) { |
| 2028 | /* Remove the frame from the tree before updating the |
| 2029 | * offset field. |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2030 | */ |
Amaury Denoyelle | d1c76f2 | 2022-02-21 17:53:38 +0100 | [diff] [blame] | 2031 | eb64_delete(&frm->offset_node); |
| 2032 | frm->offset_node.key += ret; |
Amaury Denoyelle | 4323567 | 2022-02-21 19:08:44 +0100 | [diff] [blame] | 2033 | frm->data += ret; |
Amaury Denoyelle | d1c76f2 | 2022-02-21 17:53:38 +0100 | [diff] [blame] | 2034 | frm->len -= ret; |
| 2035 | eb64_insert(&qcs->rx.frms, &frm->offset_node); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2036 | break; |
| 2037 | } |
| 2038 | |
| 2039 | frm_node = eb64_next(frm_node); |
| 2040 | quic_rx_packet_refdec(frm->pkt); |
| 2041 | eb64_delete(&frm->offset_node); |
| 2042 | pool_free(pool_head_quic_rx_strm_frm, frm); |
| 2043 | } |
| 2044 | |
| 2045 | return total; |
| 2046 | } |
| 2047 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2048 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 2049 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2050 | * If not, the STREAM frame is stored to be treated again later. |
| 2051 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2052 | * Return 1 if succeeded, 0 if not. |
| 2053 | */ |
| 2054 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 2055 | struct quic_stream *strm_frm, |
| 2056 | struct quic_conn *qc) |
| 2057 | { |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2058 | int total; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2059 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2060 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2061 | struct quic_rx_strm_frm *frm; |
Amaury Denoyelle | 6a2c2f4 | 2022-02-15 10:57:16 +0100 | [diff] [blame] | 2062 | char fin = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2063 | |
| 2064 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2065 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2066 | 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] | 2067 | return 0; |
| 2068 | } |
| 2069 | |
| 2070 | 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] | 2071 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2072 | size_t diff; |
| 2073 | |
| 2074 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2075 | TRACE_PROTO("Already received STREAM data", |
| 2076 | QUIC_EV_CONN_PSTRM, qc); |
| 2077 | goto out; |
| 2078 | } |
| 2079 | |
| 2080 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2081 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2082 | strm_frm->offset.key = strm->rx.offset; |
| 2083 | strm_frm->len -= diff; |
| 2084 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2085 | } |
| 2086 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2087 | total = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2088 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2089 | int ret; |
| 2090 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2091 | if (!qc_get_buf(strm, &strm->rx.buf)) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2092 | goto store_frm; |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2093 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2094 | |
| 2095 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2096 | total += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2097 | strm->rx.offset += ret; |
| 2098 | } |
| 2099 | |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2100 | total += qc_treat_rx_strm_frms(strm); |
Amaury Denoyelle | 6a2c2f4 | 2022-02-15 10:57:16 +0100 | [diff] [blame] | 2101 | /* FIN is set only if all data were copied. */ |
| 2102 | fin = strm_frm->fin && !strm_frm->len; |
| 2103 | if (total && qc->qcc->app_ops->decode_qcs(strm, fin, qc->qcc->ctx) < 0) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 2104 | TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | f1d38cb | 2021-12-20 12:02:13 +0100 | [diff] [blame] | 2105 | return 0; |
| 2106 | } |
| 2107 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2108 | if (!strm_frm->len) |
| 2109 | goto out; |
| 2110 | |
| 2111 | store_frm: |
| 2112 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2113 | if (!frm) { |
| 2114 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2115 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2116 | return 0; |
| 2117 | } |
| 2118 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2119 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2120 | quic_rx_packet_refinc(pkt); |
| 2121 | |
| 2122 | out: |
| 2123 | return 1; |
| 2124 | } |
| 2125 | |
| 2126 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 2127 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 2128 | * If not, the STREAM frame is stored to be treated again later. |
| 2129 | * We rely on the flow control so that not to store too much STREAM frames. |
| 2130 | * Return 1 if succeeded, 0 if not. |
| 2131 | */ |
| 2132 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 2133 | struct quic_stream *strm_frm, |
| 2134 | struct quic_conn *qc) |
| 2135 | { |
| 2136 | struct qcs *strm; |
Frédéric Lécaille | 10250b2 | 2021-12-22 16:13:43 +0100 | [diff] [blame] | 2137 | struct eb64_node *strm_node; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2138 | struct quic_rx_strm_frm *frm; |
| 2139 | size_t strm_frm_len; |
| 2140 | |
| 2141 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 2142 | if (!strm_node) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2143 | 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] | 2144 | return 0; |
| 2145 | } |
| 2146 | |
| 2147 | 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] | 2148 | if (strm_frm->offset.key < strm->rx.offset) { |
| 2149 | size_t diff; |
| 2150 | |
| 2151 | if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) { |
| 2152 | TRACE_PROTO("Already received STREAM data", |
| 2153 | QUIC_EV_CONN_PSTRM, qc); |
| 2154 | goto out; |
| 2155 | } |
| 2156 | |
| 2157 | TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc); |
| 2158 | diff = strm->rx.offset - strm_frm->offset.key; |
| 2159 | strm_frm->offset.key = strm->rx.offset; |
| 2160 | strm_frm->len -= diff; |
| 2161 | strm_frm->data += diff; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2165 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2166 | int ret; |
| 2167 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 2168 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2169 | goto store_frm; |
| 2170 | |
| 2171 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 2172 | * of bytes copied. |
| 2173 | */ |
| 2174 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 2175 | /* Inform the application of the arrival of this new stream */ |
| 2176 | 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] | 2177 | 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] | 2178 | return 0; |
| 2179 | } |
| 2180 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 2181 | if (ret) |
| 2182 | qcs_notify_recv(strm); |
| 2183 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2184 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2185 | } |
| 2186 | /* Take this frame into an account for the stream flow control */ |
| 2187 | strm->rx.offset += strm_frm_len; |
| 2188 | /* 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] | 2189 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2190 | */ |
| 2191 | if (!strm_frm->len) |
| 2192 | goto out; |
| 2193 | |
| 2194 | store_frm: |
| 2195 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2196 | if (!frm) { |
| 2197 | TRACE_PROTO("Could not alloc RX STREAM frame", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2198 | QUIC_EV_CONN_PSTRM, qc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2199 | return 0; |
| 2200 | } |
| 2201 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2202 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2203 | quic_rx_packet_refinc(pkt); |
| 2204 | |
| 2205 | out: |
| 2206 | return 1; |
| 2207 | } |
| 2208 | |
| 2209 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2210 | struct quic_stream *strm_frm, |
| 2211 | struct quic_conn *qc) |
| 2212 | { |
| 2213 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2214 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2215 | else |
| 2216 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2217 | } |
| 2218 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2219 | /* Prepare a fast retransmission from <qel> encryption level */ |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2220 | static void qc_prep_fast_retrans(struct quic_enc_level *qel, |
| 2221 | struct quic_conn *qc) |
| 2222 | { |
| 2223 | struct eb_root *pkts = &qel->pktns->tx.pkts; |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2224 | struct eb64_node *node; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2225 | struct quic_tx_packet *pkt; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2226 | |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2227 | pkt = NULL; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2228 | pkts = &qel->pktns->tx.pkts; |
| 2229 | node = eb64_first(pkts); |
Frédéric Lécaille | f010f0a | 2022-01-06 17:28:05 +0100 | [diff] [blame] | 2230 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2231 | while (node) { |
| 2232 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2233 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2234 | break; |
| 2235 | node = eb64_next(node); |
| 2236 | } |
| 2237 | |
| 2238 | if (!pkt) |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2239 | return; |
| 2240 | |
Frédéric Lécaille | 7065dd0 | 2022-01-14 15:51:52 +0100 | [diff] [blame] | 2241 | 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] | 2242 | } |
| 2243 | |
| 2244 | /* Prepare a fast retransmission during handshake after a client |
| 2245 | * has resent Initial packets. According to the RFC a server may retransmit |
| 2246 | * up to two datagrams of Initial packets if did not receive all Initial packets |
| 2247 | * and resend them coalescing with others (Handshake here). |
| 2248 | * (Listener only). |
| 2249 | */ |
| 2250 | static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc) |
| 2251 | { |
| 2252 | struct list itmp = LIST_HEAD_INIT(itmp); |
| 2253 | struct list htmp = LIST_HEAD_INIT(htmp); |
| 2254 | struct quic_frame *frm, *frmbak; |
| 2255 | |
| 2256 | struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 2257 | struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2258 | struct quic_enc_level *qel = iqel; |
| 2259 | struct eb_root *pkts; |
| 2260 | struct eb64_node *node; |
| 2261 | struct quic_tx_packet *pkt; |
| 2262 | struct list *tmp = &itmp; |
| 2263 | |
| 2264 | start: |
| 2265 | pkt = NULL; |
| 2266 | pkts = &qel->pktns->tx.pkts; |
| 2267 | node = eb64_first(pkts); |
| 2268 | /* Skip the empty packet (they have already been retransmitted) */ |
| 2269 | while (node) { |
| 2270 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 2271 | if (!LIST_ISEMPTY(&pkt->frms)) |
| 2272 | break; |
| 2273 | node = eb64_next(node); |
| 2274 | } |
| 2275 | |
| 2276 | if (!pkt) |
| 2277 | goto end; |
| 2278 | |
| 2279 | qel->pktns->tx.pto_probe += 1; |
| 2280 | requeue: |
| 2281 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
| 2282 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm); |
| 2283 | LIST_DELETE(&frm->list); |
| 2284 | LIST_APPEND(tmp, &frm->list); |
| 2285 | } |
| 2286 | |
| 2287 | if (qel == iqel) { |
| 2288 | if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2289 | pkt = pkt->next; |
| 2290 | tmp = &htmp; |
| 2291 | hqel->pktns->tx.pto_probe += 1; |
| 2292 | goto requeue; |
| 2293 | } |
| 2294 | |
| 2295 | qel = hqel; |
| 2296 | tmp = &htmp; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2297 | goto start; |
| 2298 | } |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2299 | |
| 2300 | end: |
| 2301 | LIST_SPLICE(&iqel->pktns->tx.frms, &itmp); |
| 2302 | LIST_SPLICE(&hqel->pktns->tx.frms, &htmp); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2303 | } |
| 2304 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2305 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2306 | * as I/O handler context and <qel> as encryption level. |
| 2307 | * Returns 1 if succeeded, 0 if failed. |
| 2308 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2309 | 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] | 2310 | struct quic_enc_level *qel) |
| 2311 | { |
| 2312 | struct quic_frame frm; |
| 2313 | const unsigned char *pos, *end; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2314 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2315 | int fast_retrans = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2316 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2317 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2318 | /* Skip the AAD */ |
| 2319 | pos = pkt->data + pkt->aad_len; |
| 2320 | end = pkt->data + pkt->len; |
| 2321 | |
| 2322 | while (pos < end) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2323 | if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2324 | goto err; |
| 2325 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 2326 | 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] | 2327 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2328 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2329 | break; |
| 2330 | case QUIC_FT_PING: |
| 2331 | break; |
| 2332 | case QUIC_FT_ACK: |
| 2333 | { |
| 2334 | unsigned int rtt_sample; |
| 2335 | |
| 2336 | rtt_sample = 0; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2337 | 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] | 2338 | goto err; |
| 2339 | |
| 2340 | if (rtt_sample) { |
| 2341 | unsigned int ack_delay; |
| 2342 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2343 | ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 : |
Frédéric Lécaille | 22576a2 | 2021-12-28 14:27:43 +0100 | [diff] [blame] | 2344 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ? |
| 2345 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) : |
| 2346 | MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc)); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2347 | 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] | 2348 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2349 | break; |
| 2350 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2351 | case QUIC_FT_STOP_SENDING: |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2352 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2353 | case QUIC_FT_CRYPTO: |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2354 | { |
| 2355 | struct quic_rx_crypto_frm *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2356 | |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 2357 | if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) { |
| 2358 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2359 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2360 | |
| 2361 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2362 | cfdebug.len = frm.crypto.len; |
| 2363 | TRACE_PROTO("CRYPTO data discarded", |
| 2364 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
| 2365 | break; |
| 2366 | } |
| 2367 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2368 | if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) { |
| 2369 | if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) { |
| 2370 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 2371 | struct quic_rx_crypto_frm cfdebug = { }; |
| 2372 | |
| 2373 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2374 | cfdebug.len = frm.crypto.len; |
| 2375 | /* Nothing to do */ |
| 2376 | TRACE_PROTO("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 | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2378 | if (qc_is_listener(ctx->qc) && |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2379 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]) |
| 2380 | fast_retrans = 1; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2381 | break; |
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 | else { |
| 2384 | size_t diff = qel->rx.crypto.offset - frm.crypto.offset; |
| 2385 | /* XXX TO DO: <cfdebug> is used only for the traces. */ |
| 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; |
| 2390 | TRACE_PROTO("Partially already received CRYPTO data", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2391 | QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2392 | frm.crypto.len -= diff; |
| 2393 | frm.crypto.data += diff; |
| 2394 | frm.crypto.offset = qel->rx.crypto.offset; |
| 2395 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2396 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2397 | |
| 2398 | if (frm.crypto.offset == qel->rx.crypto.offset) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2399 | /* 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] | 2400 | struct quic_rx_crypto_frm cfdebug = { }; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2401 | |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2402 | cfdebug.offset_node.key = frm.crypto.offset; |
| 2403 | cfdebug.len = frm.crypto.len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2404 | if (!qc_provide_cdata(qel, ctx, |
| 2405 | frm.crypto.data, frm.crypto.len, |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2406 | pkt, &cfdebug)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2407 | goto err; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2408 | |
| 2409 | break; |
| 2410 | } |
| 2411 | |
| 2412 | /* frm.crypto.offset > qel->rx.crypto.offset */ |
| 2413 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2414 | if (!cf) { |
| 2415 | TRACE_DEVEL("CRYPTO frame allocation failed", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2416 | QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2417 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2418 | } |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2419 | |
| 2420 | cf->offset_node.key = frm.crypto.offset; |
| 2421 | cf->len = frm.crypto.len; |
| 2422 | cf->data = frm.crypto.data; |
| 2423 | cf->pkt = pkt; |
| 2424 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2425 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2426 | break; |
Frédéric Lécaille | f9cb3a9 | 2021-12-02 11:25:58 +0100 | [diff] [blame] | 2427 | } |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 2428 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2429 | { |
| 2430 | struct quic_stream *stream = &frm.stream; |
| 2431 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2432 | if (qc_is_listener(ctx->qc)) { |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2433 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2434 | goto err; |
| 2435 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2436 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2437 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2438 | if (!qc_handle_strm_frm(pkt, stream, qc)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2439 | goto err; |
| 2440 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2441 | break; |
| 2442 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2443 | case QUIC_FT_MAX_DATA: |
| 2444 | case QUIC_FT_MAX_STREAM_DATA: |
| 2445 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2446 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2447 | case QUIC_FT_DATA_BLOCKED: |
| 2448 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2449 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2450 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2451 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2452 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | 2cca241 | 2022-01-21 13:55:03 +0100 | [diff] [blame] | 2453 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 2454 | /* XXX TO DO XXX */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2455 | break; |
| 2456 | case QUIC_FT_CONNECTION_CLOSE: |
| 2457 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Amaury Denoyelle | 5154e7a | 2021-12-08 14:51:04 +0100 | [diff] [blame] | 2458 | /* warn the mux to close the connection */ |
Amaury Denoyelle | 4af6595 | 2022-02-15 11:06:15 +0100 | [diff] [blame] | 2459 | if (qc->mux_state == QC_MUX_READY) |
| 2460 | qc->qcc->flags |= QC_CF_CC_RECV; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2461 | tasklet_wakeup(qc->qcc->wait_event.tasklet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2462 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2463 | case QUIC_FT_HANDSHAKE_DONE: |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2464 | if (qc_is_listener(ctx->qc)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2465 | goto err; |
| 2466 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2467 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2468 | break; |
| 2469 | default: |
| 2470 | goto err; |
| 2471 | } |
| 2472 | } |
| 2473 | |
Frédéric Lécaille | 04e63aa | 2022-01-17 18:16:27 +0100 | [diff] [blame] | 2474 | if (fast_retrans) |
| 2475 | qc_prep_hdshk_fast_retrans(qc); |
Frédéric Lécaille | 3bb457c | 2021-12-30 16:14:20 +0100 | [diff] [blame] | 2476 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2477 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2478 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2479 | * be discarded. |
| 2480 | */ |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2481 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2482 | int state = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2483 | |
| 2484 | if (state >= QUIC_HS_ST_SERVER_INITIAL) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2485 | 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] | 2486 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2487 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2488 | qc_set_timer(ctx->qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2489 | 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] | 2490 | 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] | 2491 | if (state < QUIC_HS_ST_SERVER_HANDSHAKE) |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 2492 | 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] | 2493 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2494 | } |
| 2495 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2496 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2497 | return 1; |
| 2498 | |
| 2499 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2500 | 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] | 2501 | return 0; |
| 2502 | } |
| 2503 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2504 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2505 | * 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] | 2506 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2507 | * This function must be called only after having built a correct datagram. |
| 2508 | * Always succeeds. |
| 2509 | */ |
| 2510 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2511 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2512 | { |
| 2513 | write_u16(cb_wr(cbuf), dglen); |
| 2514 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2515 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2516 | } |
| 2517 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2518 | /* 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] | 2519 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2520 | * several packets in the same datagram. A header made of two fields is added |
| 2521 | * to each datagram: the datagram length followed by the address of the first |
| 2522 | * packet in this datagram. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2523 | * Returns 1 if succeeded, or 0 if something wrong happened. |
| 2524 | */ |
Frédéric Lécaille | ee2b8b3 | 2022-01-03 11:14:30 +0100 | [diff] [blame] | 2525 | static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr, |
| 2526 | enum quic_tls_enc_level tel, |
| 2527 | enum quic_tls_enc_level next_tel) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2528 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2529 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2530 | struct cbuf *cbuf; |
| 2531 | unsigned char *end_buf, *end, *pos, *spos; |
| 2532 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2533 | /* length of datagrams */ |
| 2534 | uint16_t dglen; |
| 2535 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2536 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2537 | /* Each datagram is prepended with its length followed by the |
| 2538 | * address of the first packet in the datagram. |
| 2539 | */ |
| 2540 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2541 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2542 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc); |
| 2543 | |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2544 | total = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2545 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2546 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2547 | padding = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2548 | qel = &qc->els[tel]; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2549 | cbuf = qr->cbuf; |
| 2550 | spos = pos = cb_wr(cbuf); |
| 2551 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2552 | * to ensure there is enough room to mark the end of prepared |
| 2553 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2554 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2555 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2556 | first_pkt = prv_pkt = NULL; |
| 2557 | 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] | 2558 | int err, probe, ack, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2559 | enum quic_pkt_type pkt_type; |
| 2560 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2561 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel); |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 2562 | probe = ack = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 2563 | cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 2564 | if (!cc) { |
Frédéric Lécaille | 94fca87 | 2022-01-19 18:54:18 +0100 | [diff] [blame] | 2565 | probe = qel->pktns->tx.pto_probe; |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2566 | 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] | 2567 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2568 | /* 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] | 2569 | * 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] | 2570 | * 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] | 2571 | * and if there is no more CRYPTO data available or in flight |
| 2572 | * congestion control limit is reached for prepared data |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2573 | */ |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2574 | 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] | 2575 | (!cc && !ack && !probe && |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2576 | (LIST_ISEMPTY(&qel->pktns->tx.frms) || |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2577 | qc->path->prep_in_flight >= qc->path->cwnd))) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2578 | 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] | 2579 | /* Set the current datagram as prepared into <cbuf> if |
| 2580 | * the was already a correct packet which was previously written. |
| 2581 | */ |
| 2582 | if (prv_pkt) |
| 2583 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | 39ba1c3 | 2022-01-21 16:52:56 +0100 | [diff] [blame] | 2584 | /* Let's select the next encryption level */ |
| 2585 | if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) { |
| 2586 | tel = next_tel; |
| 2587 | qel = &qc->els[tel]; |
| 2588 | /* Build a new datagram */ |
| 2589 | prv_pkt = NULL; |
| 2590 | continue; |
| 2591 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2592 | break; |
| 2593 | } |
| 2594 | |
| 2595 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2596 | if (!prv_pkt) { |
| 2597 | /* Leave room for the datagram header */ |
| 2598 | pos += dg_headlen; |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 2599 | 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] | 2600 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2601 | } |
| 2602 | else { |
| 2603 | end = pos + qc->path->mtu; |
| 2604 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2605 | } |
| 2606 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2607 | 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] | 2608 | pkt_type, ack, probe, cc, &err); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2609 | /* 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] | 2610 | if (err < 0) { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2611 | if (ack) |
Frédéric Lécaille | 25eeebe | 2021-12-16 11:21:52 +0100 | [diff] [blame] | 2612 | 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] | 2613 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2614 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2615 | case -2: |
| 2616 | goto err; |
| 2617 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2618 | /* If there was already a correct packet present, set the |
| 2619 | * current datagram as prepared into <cbuf>. |
| 2620 | */ |
| 2621 | if (prv_pkt) { |
| 2622 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2623 | goto stop_build; |
| 2624 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2625 | goto out; |
| 2626 | default: |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2627 | break; |
| 2628 | } |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2629 | |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2630 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2631 | if (!cur_pkt) |
| 2632 | goto err; |
| 2633 | |
| 2634 | total += cur_pkt->len; |
| 2635 | /* keep trace of the first packet in the datagram */ |
| 2636 | if (!first_pkt) |
| 2637 | first_pkt = cur_pkt; |
| 2638 | /* Attach the current one to the previous one */ |
| 2639 | if (prv_pkt) |
| 2640 | prv_pkt->next = cur_pkt; |
| 2641 | /* Let's say we have to build a new dgram */ |
| 2642 | prv_pkt = NULL; |
| 2643 | dglen += cur_pkt->len; |
| 2644 | /* Client: discard the Initial encryption keys as soon as |
| 2645 | * a handshake packet could be built. |
| 2646 | */ |
| 2647 | if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL && |
| 2648 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2649 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2650 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 2651 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2652 | qc_set_timer(qc); |
Frédéric Lécaille | a6255f5 | 2022-01-19 17:29:48 +0100 | [diff] [blame] | 2653 | 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] | 2654 | 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] | 2655 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE); |
| 2656 | } |
| 2657 | /* If the data for the current encryption level have all been sent, |
| 2658 | * select the next level. |
| 2659 | */ |
| 2660 | 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] | 2661 | (LIST_ISEMPTY(&qel->pktns->tx.frms))) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2662 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2663 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2664 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 2665 | tel = next_tel; |
| 2666 | qel = &qc->els[tel]; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2667 | if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 6355677 | 2021-12-29 17:18:21 +0100 | [diff] [blame] | 2668 | /* If there is data for the next level, do not |
| 2669 | * consume a datagram. |
| 2670 | */ |
| 2671 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2672 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2673 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2674 | /* If we have to build a new datagram, set the current datagram as |
| 2675 | * prepared into <cbuf>. |
| 2676 | */ |
| 2677 | if (!prv_pkt) { |
| 2678 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2679 | first_pkt = NULL; |
| 2680 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2681 | padding = 0; |
| 2682 | } |
| 2683 | 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] | 2684 | (!qc_is_listener(qc) || |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2685 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2686 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2687 | } |
| 2688 | } |
| 2689 | |
| 2690 | stop_build: |
| 2691 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2692 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
| 2693 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2694 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2695 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2696 | if (cb_contig_space(cbuf) >= sizeof(uint16_t)) { |
| 2697 | if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) { |
| 2698 | /* Mark the end of contiguous data for the reader */ |
| 2699 | write_u16(cb_wr(cbuf), 0); |
| 2700 | cb_add(cbuf, sizeof(uint16_t)); |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | if (rd && rd <= cbuf->wr) { |
| 2705 | cb_wr_reset(cbuf); |
Frédéric Lécaille | 99942d6 | 2022-01-07 14:32:31 +0100 | [diff] [blame] | 2706 | /* Let's try to reuse this buffer */ |
| 2707 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2712 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2713 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2714 | |
| 2715 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2716 | 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] | 2717 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | /* 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] | 2721 | * 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] | 2722 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2723 | 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] | 2724 | { |
| 2725 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2726 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2727 | |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 2728 | qc = ctx->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2729 | cbuf = qr->cbuf; |
| 2730 | while (cb_contig_data(cbuf)) { |
| 2731 | unsigned char *pos; |
| 2732 | struct buffer tmpbuf = { }; |
| 2733 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 2734 | uint16_t dglen; |
| 2735 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2736 | unsigned int time_sent; |
| 2737 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2738 | pos = cb_rd(cbuf); |
| 2739 | dglen = read_u16(pos); |
| 2740 | /* End of prepared datagrams. |
| 2741 | * Reset the reader index only if in front of the writer index. |
| 2742 | */ |
| 2743 | if (!dglen) { |
| 2744 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 2745 | |
| 2746 | if (wr && wr < cbuf->rd) { |
| 2747 | cb_rd_reset(cbuf); |
| 2748 | continue; |
| 2749 | } |
| 2750 | break; |
| 2751 | } |
| 2752 | |
| 2753 | pos += sizeof dglen; |
| 2754 | first_pkt = read_ptr(pos); |
| 2755 | pos += sizeof first_pkt; |
| 2756 | tmpbuf.area = (char *)pos; |
| 2757 | tmpbuf.size = tmpbuf.data = dglen; |
| 2758 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2759 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2760 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2761 | quic_tx_packet_refinc(pkt); |
Amaury Denoyelle | 58a7704 | 2022-02-09 15:43:07 +0100 | [diff] [blame] | 2762 | if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2763 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2764 | quic_tx_packet_refdec(pkt); |
Amaury Denoyelle | 74f2292 | 2022-01-18 16:48:17 +0100 | [diff] [blame] | 2765 | break; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2766 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2767 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2768 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2769 | qc->tx.bytes += tmpbuf.data; |
| 2770 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2771 | |
| 2772 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 2773 | pkt->time_sent = time_sent; |
| 2774 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 2775 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 2776 | qc->path->ifae_pkts++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2777 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2778 | qc->path->in_flight += pkt->in_flight_len; |
| 2779 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 2780 | if (pkt->in_flight_len) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 2781 | qc_set_timer(qc); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 2782 | 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] | 2783 | next_pkt = pkt->next; |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 2784 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2785 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2786 | } |
| 2787 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2788 | |
| 2789 | return 1; |
| 2790 | } |
| 2791 | |
| 2792 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 2793 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 2794 | * a HANDSHAKE_DONE frame. |
| 2795 | * Return 1 if succeeded, 0 if not. |
| 2796 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2797 | 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] | 2798 | { |
| 2799 | int i; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2800 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2801 | struct quic_frame *frm; |
| 2802 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2803 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2804 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 2805 | if (qc_is_listener(qc)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2806 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 2807 | if (!frm) |
| 2808 | return 0; |
| 2809 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2810 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2811 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2812 | } |
| 2813 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2814 | 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] | 2815 | struct quic_connection_id *cid; |
| 2816 | |
| 2817 | frm = pool_alloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2818 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2819 | goto err; |
| 2820 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 2821 | cid = new_quic_cid(&qc->cids, qc, i); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2822 | if (!cid) |
| 2823 | goto err; |
| 2824 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 2825 | /* insert the allocated CID in the receiver datagram handler tree */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 2826 | ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2827 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2828 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 2829 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2830 | } |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 2831 | 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] | 2832 | |
| 2833 | return 1; |
| 2834 | |
| 2835 | err: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2836 | return 0; |
| 2837 | } |
| 2838 | |
| 2839 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2840 | void free_quic_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2841 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2842 | struct eb64_node *n; |
| 2843 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2844 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2845 | n = eb64_first(&arngs->root); |
| 2846 | while (n) { |
| 2847 | struct eb64_node *next; |
| 2848 | |
| 2849 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 2850 | next = eb64_next(n); |
| 2851 | eb64_delete(n); |
| 2852 | free(ar); |
| 2853 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2854 | } |
| 2855 | } |
| 2856 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2857 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 2858 | * descending order. |
| 2859 | */ |
| 2860 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 2861 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2862 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2863 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | |
| 2867 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 2868 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2869 | * 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] | 2870 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2871 | 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] | 2872 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2873 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2874 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2875 | last = eb64_last(&arngs->root); |
| 2876 | while (last && arngs->enc_sz > limit) { |
| 2877 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2878 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2879 | prev = eb64_prev(last); |
| 2880 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2881 | return 0; |
| 2882 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2883 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 2884 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 2885 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 2886 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 2887 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 2888 | --arngs->sz; |
| 2889 | eb64_delete(last); |
| 2890 | pool_free(pool_head_quic_arng, last); |
| 2891 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | return 1; |
| 2895 | } |
| 2896 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2897 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 2898 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 2899 | { |
| 2900 | struct eb64_node *node, *next; |
| 2901 | struct quic_arng_node *ar, *ar_next; |
| 2902 | |
| 2903 | node = eb64_last(&arngs->root); |
| 2904 | if (!node) |
| 2905 | return; |
| 2906 | |
| 2907 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2908 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 2909 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 2910 | |
| 2911 | while ((next = eb64_prev(node))) { |
| 2912 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 2913 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 2914 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 2915 | node = next; |
| 2916 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2917 | } |
| 2918 | } |
| 2919 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2920 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 2921 | * 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] | 2922 | */ |
| 2923 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2924 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 2925 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2926 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2927 | struct quic_arng_node *new_ar; |
| 2928 | |
| 2929 | new_ar = pool_alloc(pool_head_quic_arng); |
| 2930 | if (new_ar) { |
| 2931 | new_ar->first.key = ar->first; |
| 2932 | new_ar->last = ar->last; |
| 2933 | eb64_insert(&arngs->root, &new_ar->first); |
| 2934 | arngs->sz++; |
| 2935 | } |
| 2936 | |
| 2937 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | /* 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] | 2941 | * 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] | 2942 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2943 | * |
| 2944 | * Descending order |
| 2945 | * -------------> |
| 2946 | * range1 range2 |
| 2947 | * ..........|--------|..............|--------| |
| 2948 | * ^ ^ ^ ^ |
| 2949 | * | | | | |
| 2950 | * last1 first1 last2 first2 |
| 2951 | * ..........+--------+--------------+--------+...... |
| 2952 | * diff1 gap12 diff2 |
| 2953 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2954 | * To encode the previous list of ranges we must encode integers as follows in |
| 2955 | * descending order: |
| 2956 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2957 | * with diff1 = last1 - first1 |
| 2958 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2959 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2960 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2961 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2962 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 2963 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2964 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2965 | struct eb64_node *le; |
| 2966 | struct quic_arng_node *new_node; |
| 2967 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2968 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2969 | new = NULL; |
| 2970 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2971 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2972 | if (!new_node) |
| 2973 | return 0; |
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 | goto out; |
| 2976 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2977 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2978 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 2979 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2980 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2981 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2982 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 2983 | |
| 2984 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2985 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2986 | else { |
| 2987 | struct quic_arng_node *le_ar = |
| 2988 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2989 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2990 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 2991 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2992 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2993 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2994 | if (le_ar->last + 1 >= ar->first) { |
| 2995 | le_ar->last = ar->last; |
| 2996 | new = le; |
| 2997 | new_node = le_ar; |
| 2998 | } |
| 2999 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 3000 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3001 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3002 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 3003 | |
| 3004 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3005 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3006 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3007 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3008 | /* Verify that the new inserted node does not overlap the nodes |
| 3009 | * which follow it. |
| 3010 | */ |
| 3011 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3012 | struct eb64_node *next; |
| 3013 | struct quic_arng_node *next_node; |
| 3014 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3015 | while ((next = eb64_next(new))) { |
| 3016 | next_node = |
| 3017 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 3018 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3019 | break; |
| 3020 | |
| 3021 | if (next_node->last > new_node->last) |
| 3022 | new_node->last = next_node->last; |
| 3023 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 3024 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3025 | /* Decrement the size of these ranges. */ |
| 3026 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3027 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3028 | } |
| 3029 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 3030 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3031 | quic_arngs_set_enc_sz(arngs); |
| 3032 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3033 | return 1; |
| 3034 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3035 | /* Remove the header protection of packets at <el> encryption level. |
| 3036 | * Always succeeds. |
| 3037 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3038 | 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] | 3039 | { |
| 3040 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3041 | struct quic_rx_packet *pqpkt; |
| 3042 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3043 | struct quic_enc_level *app_qel; |
| 3044 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3045 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc); |
| 3046 | app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3047 | /* 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] | 3048 | if (el == app_qel && qc_is_listener(qc) && |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3049 | HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3050 | TRACE_PROTO("hp not removed (handshake not completed)", |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3051 | QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3052 | goto out; |
| 3053 | } |
| 3054 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3055 | 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] | 3056 | 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] | 3057 | pqpkt->data + pqpkt->pn_offset, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3058 | pqpkt->data, pqpkt->data + pqpkt->len)) { |
| 3059 | 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] | 3060 | /* XXX TO DO XXX */ |
| 3061 | } |
| 3062 | else { |
| 3063 | /* The AAD includes the packet number field */ |
| 3064 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 3065 | /* Store the packet into the tree of packets to decrypt. */ |
| 3066 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3067 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3068 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 3069 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3070 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3071 | 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] | 3072 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3073 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 3074 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3078 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3079 | } |
| 3080 | |
| 3081 | /* Process all the CRYPTO frame at <el> encryption level. |
| 3082 | * Return 1 if succeeded, 0 if not. |
| 3083 | */ |
| 3084 | 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] | 3085 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3086 | { |
| 3087 | struct eb64_node *node; |
| 3088 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3089 | node = eb64_first(&el->rx.crypto.frms); |
| 3090 | while (node) { |
| 3091 | struct quic_rx_crypto_frm *cf; |
| 3092 | |
| 3093 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 3094 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 3095 | break; |
| 3096 | |
| 3097 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 3098 | goto err; |
| 3099 | |
| 3100 | node = eb64_next(node); |
| 3101 | quic_rx_packet_refdec(cf->pkt); |
| 3102 | eb64_delete(&cf->offset_node); |
| 3103 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 3104 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3105 | return 1; |
| 3106 | |
| 3107 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3108 | 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] | 3109 | return 0; |
| 3110 | } |
| 3111 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3112 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3113 | * 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] | 3114 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3115 | * Return 1 if succeeded, 0 if not. |
| 3116 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3117 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 3118 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3119 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3120 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3121 | int64_t largest_pn = -1; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3122 | struct quic_conn *qc = ctx->qc; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3123 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3124 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3125 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3126 | qel = cur_el; |
| 3127 | next_tel: |
| 3128 | if (!qel) |
| 3129 | goto out; |
| 3130 | |
| 3131 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3132 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3133 | while (node) { |
| 3134 | struct quic_rx_packet *pkt; |
| 3135 | |
| 3136 | 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] | 3137 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3138 | ctx->qc, pkt, NULL, ctx->ssl); |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 3139 | if (!qc_pkt_decrypt(pkt, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3140 | /* Drop the packet */ |
| 3141 | TRACE_PROTO("packet decryption failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3142 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3143 | } |
| 3144 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3145 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3146 | /* Drop the packet */ |
| 3147 | TRACE_PROTO("packet parsing failed -> dropped", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3148 | QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3149 | } |
| 3150 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 3151 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 3152 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3153 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING && |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3154 | (!(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] | 3155 | 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] | 3156 | if (pkt->pn > largest_pn) |
| 3157 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3158 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3159 | 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] | 3160 | TRACE_DEVEL("Could not update ack range list", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3161 | QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3162 | } |
| 3163 | } |
| 3164 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3165 | eb64_delete(&pkt->pn_node); |
| 3166 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3167 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3168 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3169 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 3170 | /* Update the largest packet number. */ |
| 3171 | if (largest_pn != -1) |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3172 | HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn); |
| 3173 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3174 | goto err; |
| 3175 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3176 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3177 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3178 | qel = next_el; |
| 3179 | goto next_tel; |
| 3180 | } |
| 3181 | |
| 3182 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3183 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3184 | return 1; |
| 3185 | |
| 3186 | err: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3187 | 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] | 3188 | return 0; |
| 3189 | } |
| 3190 | |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3191 | /* Check if it's possible to remove header protection for packets related to |
| 3192 | * encryption level <qel>. If <qel> is NULL, assume it's false. |
| 3193 | * |
| 3194 | * Return true if the operation is possible else false. |
| 3195 | */ |
| 3196 | static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel) |
| 3197 | { |
| 3198 | enum quic_tls_enc_level tel; |
| 3199 | |
| 3200 | if (!qel) |
| 3201 | return 0; |
| 3202 | |
| 3203 | tel = ssl_to_quic_enc_level(qel->level); |
| 3204 | |
| 3205 | /* check if tls secrets are available */ |
| 3206 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) |
| 3207 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc); |
| 3208 | |
| 3209 | if (!(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) |
| 3210 | return 0; |
| 3211 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3212 | /* check if the connection layer is ready before using app level */ |
Frédéric Lécaille | 298931d | 2022-01-28 21:41:06 +0100 | [diff] [blame] | 3213 | if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) && |
| 3214 | qc->mux_state != QC_MUX_READY) |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3215 | return 0; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3216 | |
| 3217 | return 1; |
| 3218 | } |
| 3219 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3220 | /* QUIC connection packet handler task. */ |
| 3221 | 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] | 3222 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3223 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3224 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3225 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3226 | enum quic_tls_enc_level tel, next_tel; |
| 3227 | struct quic_enc_level *qel, *next_qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3228 | struct qring *qr; // Tx ring |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3229 | int st, force_ack, zero_rtt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3230 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3231 | ctx = context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3232 | qc = ctx->qc; |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3233 | TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3234 | qr = NULL; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3235 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3236 | TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 3237 | if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) { |
| 3238 | HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT); |
| 3239 | /* The I/O handler has been woken up by the dgram listener |
| 3240 | * after the anti-amplification was reached. |
| 3241 | */ |
| 3242 | qc_set_timer(qc); |
| 3243 | if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms)) |
| 3244 | task_wakeup(qc->timer_task, TASK_WOKEN_MSG); |
| 3245 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3246 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 4137b2d | 2021-12-17 18:24:16 +0100 | [diff] [blame] | 3247 | zero_rtt = st < QUIC_HS_ST_COMPLETE && |
| 3248 | (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) || |
| 3249 | 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] | 3250 | start: |
Frédéric Lécaille | 917a7db | 2022-01-03 17:00:35 +0100 | [diff] [blame] | 3251 | if (st >= QUIC_HS_ST_COMPLETE && |
| 3252 | qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) { |
| 3253 | TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc); |
| 3254 | /* There may be remaining Handshake packets to treat and acknowledge. */ |
| 3255 | tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE; |
| 3256 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
| 3257 | } |
| 3258 | 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] | 3259 | goto err; |
| 3260 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 3261 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 3262 | 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] | 3263 | |
| 3264 | next_level: |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3265 | /* Treat packets waiting for header packet protection decryption */ |
| 3266 | 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] | 3267 | qc_rm_hp_pkts(qc, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3268 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3269 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 3270 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3271 | 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] | 3272 | goto err; |
| 3273 | |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3274 | if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) && |
| 3275 | (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) { |
| 3276 | qel = next_qel; |
| 3277 | next_qel = NULL; |
| 3278 | goto next_level; |
| 3279 | } |
| 3280 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3281 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3282 | if (st >= QUIC_HS_ST_COMPLETE) { |
| 3283 | if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) && |
| 3284 | !quic_build_post_handshake_frames(qc)) |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3285 | goto err; |
Frédéric Lécaille | fee7ba6 | 2021-12-06 12:09:08 +0100 | [diff] [blame] | 3286 | |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3287 | if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags & |
| 3288 | QUIC_FL_TLS_SECRETS_DCD)) { |
| 3289 | /* Discard the Handshake keys. */ |
| 3290 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
| 3291 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc); |
| 3292 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 3293 | qc_set_timer(qc); |
Frédéric Lécaille | de6f7c5 | 2022-01-03 17:25:53 +0100 | [diff] [blame] | 3294 | 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] | 3295 | 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] | 3296 | } |
| 3297 | |
| 3298 | if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) { |
| 3299 | /* There may be remaining handshake to build (acks) */ |
| 3300 | st = QUIC_HS_ST_SERVER_HANDSHAKE; |
| 3301 | } |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3302 | } |
| 3303 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3304 | if (!qr) |
| 3305 | 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] | 3306 | /* A listener does not send any O-RTT packet. O-RTT packet number space must not |
| 3307 | * be considered. |
| 3308 | */ |
| 3309 | 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] | 3310 | goto err; |
| 3311 | ret = qc_prep_pkts(qc, qr, tel, next_tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3312 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3313 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3314 | else if (ret == 0) |
| 3315 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3316 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3317 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3318 | goto err; |
| 3319 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3320 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3321 | /* Check if there is something to do for the next level. |
| 3322 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3323 | if (next_qel && next_qel != qel && |
| 3324 | (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] | 3325 | (!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] | 3326 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 3327 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3328 | goto next_level; |
| 3329 | } |
| 3330 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3331 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3332 | TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 3333 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3334 | |
| 3335 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 3336 | if (qr) |
| 3337 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3338 | 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] | 3339 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3340 | } |
| 3341 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3342 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3343 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 3344 | { |
| 3345 | int i; |
| 3346 | |
| 3347 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 3348 | if (qel->tx.crypto.bufs[i]) { |
| 3349 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 3350 | qel->tx.crypto.bufs[i] = NULL; |
| 3351 | } |
| 3352 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3353 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 3357 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3358 | * Returns 1 if succeeded, 0 if not. |
| 3359 | */ |
| 3360 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 3361 | enum quic_tls_enc_level level) |
| 3362 | { |
| 3363 | struct quic_enc_level *qel; |
| 3364 | |
| 3365 | qel = &qc->els[level]; |
| 3366 | qel->level = quic_to_ssl_enc_level(level); |
| 3367 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 3368 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 3369 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 3370 | qel->tls_ctx.rx.flags = 0; |
| 3371 | qel->tls_ctx.tx.flags = 0; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 3372 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3373 | |
| 3374 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 3375 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 3376 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 3377 | qel->rx.crypto.offset = 0; |
| 3378 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3379 | |
| 3380 | /* Allocate only one buffer. */ |
| 3381 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 3382 | if (!qel->tx.crypto.bufs) |
| 3383 | goto err; |
| 3384 | |
| 3385 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 3386 | if (!qel->tx.crypto.bufs[0]) |
| 3387 | goto err; |
| 3388 | |
| 3389 | qel->tx.crypto.bufs[0]->sz = 0; |
| 3390 | qel->tx.crypto.nb_buf = 1; |
| 3391 | |
| 3392 | qel->tx.crypto.sz = 0; |
| 3393 | qel->tx.crypto.offset = 0; |
| 3394 | |
| 3395 | return 1; |
| 3396 | |
| 3397 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3398 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3399 | return 0; |
| 3400 | } |
| 3401 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3402 | /* Increment the <qc> refcount. |
| 3403 | * |
| 3404 | * This operation must be conducted when manipulating the quic_conn outside of |
| 3405 | * the connection pinned thread. These threads can only retrieve the connection |
| 3406 | * in the CID tree, so this function must be conducted under the CID lock. |
| 3407 | */ |
| 3408 | static inline void quic_conn_take(struct quic_conn *qc) |
| 3409 | { |
| 3410 | HA_ATOMIC_INC(&qc->refcount); |
| 3411 | } |
| 3412 | |
| 3413 | /* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the |
Ilya Shipitsin | 37d3e38 | 2022-01-07 14:46:15 +0500 | [diff] [blame] | 3414 | * subtraction, the quic_conn is freed. |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3415 | */ |
| 3416 | static void quic_conn_drop(struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3417 | { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3418 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3419 | int i; |
| 3420 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3421 | if (!qc) |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3422 | return; |
| 3423 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3424 | if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1)) |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3425 | return; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3426 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3427 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3428 | if (conn_ctx) { |
| 3429 | SSL_free(conn_ctx->ssl); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3430 | pool_free(pool_head_quic_conn_ctx, conn_ctx); |
Amaury Denoyelle | 2d9794b | 2022-01-20 17:43:20 +0100 | [diff] [blame] | 3431 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3432 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3433 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3434 | quic_conn_enc_level_uninit(&qc->els[i]); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3435 | |
Amaury Denoyelle | 67e6cd5 | 2021-12-13 17:07:03 +0100 | [diff] [blame] | 3436 | pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area); |
| 3437 | pool_free(pool_head_quic_conn, qc); |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3438 | 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] | 3439 | } |
| 3440 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3441 | /* Release the quic_conn <qc>. It will decrement its refcount so that the |
| 3442 | * connection will be freed once all threads have finished to work with it. The |
| 3443 | * 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] | 3444 | * threads after it. The connection tasklet is killed. |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3445 | * |
| 3446 | * Do not use <qc> after it as it may be freed. This function must only be |
| 3447 | * called by the thread responsible of the quic_conn tasklet. |
| 3448 | */ |
| 3449 | static void quic_conn_release(struct quic_conn *qc) |
| 3450 | { |
Amaury Denoyelle | 760da3b | 2022-01-20 17:43:02 +0100 | [diff] [blame] | 3451 | struct ssl_sock_ctx *conn_ctx; |
| 3452 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3453 | /* remove the connection from receiver cids trees */ |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3454 | ebmb_delete(&qc->odcid_node); |
| 3455 | ebmb_delete(&qc->scid_node); |
| 3456 | free_quic_conn_cids(qc); |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3457 | |
Amaury Denoyelle | 760da3b | 2022-01-20 17:43:02 +0100 | [diff] [blame] | 3458 | /* Kill the tasklet. Do not use tasklet_free as this is not thread safe |
| 3459 | * as other threads may call tasklet_wakeup after this. |
| 3460 | */ |
| 3461 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
| 3462 | if (conn_ctx) |
| 3463 | tasklet_kill(conn_ctx->wait_event.tasklet); |
| 3464 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3465 | quic_conn_drop(qc); |
| 3466 | } |
| 3467 | |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3468 | void quic_close(struct connection *conn, void *xprt_ctx) |
| 3469 | { |
| 3470 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
Amaury Denoyelle | 29632b8 | 2022-01-18 16:50:58 +0100 | [diff] [blame] | 3471 | struct quic_conn *qc = conn_ctx->qc; |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3472 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3473 | TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 0a29e13 | 2021-12-23 15:06:56 +0100 | [diff] [blame] | 3474 | /* This task must be deleted by the connection-pinned thread. */ |
| 3475 | if (qc->timer_task) { |
| 3476 | task_destroy(qc->timer_task); |
| 3477 | qc->timer_task = NULL; |
| 3478 | } |
| 3479 | |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3480 | /* Next application data can be dropped. */ |
| 3481 | qc->mux_state = QC_MUX_RELEASED; |
| 3482 | |
Frédéric Lécaille | ba85acd | 2022-01-11 14:43:50 +0100 | [diff] [blame] | 3483 | TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc); |
Amaury Denoyelle | 9c4da93 | 2022-01-21 14:54:58 +0100 | [diff] [blame] | 3484 | |
Amaury Denoyelle | 2eb7b30 | 2022-01-20 16:40:36 +0100 | [diff] [blame] | 3485 | /* TODO for now release the quic_conn on notification by the upper |
| 3486 | * layer. It could be useful to delay it if there is remaining data to |
| 3487 | * send or data to be acked. |
| 3488 | */ |
| 3489 | quic_conn_release(qc); |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3490 | } |
| 3491 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3492 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3493 | 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] | 3494 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3495 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3496 | struct quic_conn *qc; |
| 3497 | struct quic_pktns *pktns; |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3498 | int i, st; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3499 | |
| 3500 | conn_ctx = task->context; |
Amaury Denoyelle | c15dd92 | 2021-12-21 11:41:52 +0100 | [diff] [blame] | 3501 | qc = conn_ctx->qc; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3502 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc, |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3503 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3504 | task->expire = TICK_ETERNITY; |
| 3505 | pktns = quic_loss_pktns(qc); |
| 3506 | if (tick_isset(pktns->tx.loss_time)) { |
| 3507 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 3508 | |
| 3509 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 3510 | if (!LIST_ISEMPTY(&lost_pkts)) |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3511 | qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms); |
| 3512 | qc_set_timer(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3513 | goto out; |
| 3514 | } |
| 3515 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3516 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3517 | if (qc->path->in_flight) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3518 | 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] | 3519 | if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) { |
| 3520 | pktns->tx.pto_probe = 1; |
| 3521 | if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) |
| 3522 | qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1; |
| 3523 | } |
| 3524 | else { |
| 3525 | pktns->tx.pto_probe = 2; |
| 3526 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3527 | } |
Frédéric Lécaille | 1aa57d3 | 2022-01-12 09:46:02 +0100 | [diff] [blame] | 3528 | 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] | 3529 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3530 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3531 | |
| 3532 | if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3533 | hel->pktns->tx.pto_probe = 1; |
| 3534 | if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3535 | iel->pktns->tx.pto_probe = 1; |
| 3536 | } |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3537 | |
| 3538 | for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) { |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3539 | if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc)) |
| 3540 | continue; |
| 3541 | |
Frédéric Lécaille | 53c7d8d | 2022-02-15 12:00:55 +0100 | [diff] [blame] | 3542 | qc_prep_fast_retrans(&qc->els[i], qc); |
Frédéric Lécaille | a56054e | 2021-12-31 16:35:28 +0100 | [diff] [blame] | 3543 | } |
| 3544 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3545 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3546 | qc->path->loss.pto_count++; |
| 3547 | |
| 3548 | out: |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 3549 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3550 | |
| 3551 | return task; |
| 3552 | } |
| 3553 | |
| 3554 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 3555 | * connections used to identify the first Initial packets of client connecting |
| 3556 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 3557 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 3558 | * <scid> is the source connection ID with <scid_len> as length. |
| 3559 | * Returns 1 if succeeded, 0 if not. |
| 3560 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3561 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3562 | 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] | 3563 | 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] | 3564 | { |
| 3565 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3566 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3567 | /* Initial CID. */ |
| 3568 | struct quic_connection_id *icid; |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 3569 | char *buf_area = NULL; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3570 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3571 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3572 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3573 | qc = pool_zalloc(pool_head_quic_conn); |
| 3574 | if (!qc) { |
| 3575 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 3576 | goto err; |
| 3577 | } |
| 3578 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3579 | HA_ATOMIC_STORE(&qc->refcount, 0); |
| 3580 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3581 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 3582 | if (!buf_area) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3583 | 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] | 3584 | goto err; |
| 3585 | } |
| 3586 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3587 | qc->cids = EB_ROOT; |
| 3588 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3589 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3590 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3591 | |
Frédéric Lécaille | 2fe8b3b | 2022-01-10 12:10:10 +0100 | [diff] [blame] | 3592 | qc->flags |= QUIC_FL_CONN_LISTENER; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3593 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL); |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3594 | /* Copy the initial DCID with the address. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3595 | qc->odcid.len = dcid_len; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 3596 | qc->odcid.addrlen = dcid_addr_len; |
| 3597 | 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] | 3598 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 3599 | /* 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] | 3600 | if (scid_len) |
| 3601 | memcpy(qc->dcid.data, scid, scid_len); |
| 3602 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 3603 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3604 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3605 | } |
| 3606 | /* QUIC Client (outgoing connection to servers) */ |
| 3607 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3608 | 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] | 3609 | if (dcid_len) |
| 3610 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 3611 | qc->dcid.len = dcid_len; |
| 3612 | } |
Amaury Denoyelle | 0b1f931 | 2022-01-26 09:51:28 +0100 | [diff] [blame] | 3613 | qc->mux_state = QC_MUX_NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3614 | |
| 3615 | /* Initialize the output buffer */ |
| 3616 | qc->obuf.pos = qc->obuf.data; |
| 3617 | |
Amaury Denoyelle | 0442efd | 2022-01-28 16:02:13 +0100 | [diff] [blame] | 3618 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3619 | if (!icid) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3620 | 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] | 3621 | goto err; |
| 3622 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3623 | |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 3624 | /* insert the allocated CID in the receiver datagram handler tree */ |
| 3625 | if (server) |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 3626 | ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3627 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3628 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 3629 | qc->scid = icid->cid; |
| 3630 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3631 | /* Packet number spaces initialization. */ |
| 3632 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 3633 | quic_pktns_init(&qc->pktns[i]); |
| 3634 | /* QUIC encryption level context initialization. */ |
| 3635 | 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] | 3636 | if (!quic_conn_enc_level_init(qc, i)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3637 | 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] | 3638 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3639 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3640 | /* Initialize the packet number space. */ |
| 3641 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 3642 | } |
| 3643 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 3644 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 3645 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 3646 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 3647 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3648 | /* TX part. */ |
| 3649 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3650 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 3651 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 3652 | qc->tx.bytes = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3653 | /* RX part. */ |
| 3654 | qc->rx.bytes = 0; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3655 | qc->rx.nb_ack_eliciting = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3656 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3657 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame] | 3658 | if (!quic_tls_ku_init(qc)) { |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3659 | 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] | 3660 | goto err; |
| 3661 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3662 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3663 | /* XXX TO DO: Only one path at this time. */ |
| 3664 | qc->path = &qc->paths[0]; |
| 3665 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 3666 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 3667 | /* required to use MTLIST_IN_LIST */ |
| 3668 | MT_LIST_INIT(&qc->accept_list); |
| 3669 | |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3670 | TRACE_LEAVE(QUIC_EV_CONN_INIT, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3671 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3672 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3673 | |
| 3674 | err: |
Amaury Denoyelle | e770ce3 | 2021-12-21 14:51:56 +0100 | [diff] [blame] | 3675 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL); |
Frédéric Lécaille | c0b481f | 2022-02-02 15:39:55 +0100 | [diff] [blame] | 3676 | pool_free(pool_head_quic_conn_rxbuf, buf_area); |
| 3677 | if (qc) |
| 3678 | qc->rx.buf.area = NULL; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 3679 | quic_conn_drop(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3680 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | /* Initialize the timer task of <qc> QUIC connection. |
| 3684 | * Returns 1 if succeeded, 0 if not. |
| 3685 | */ |
| 3686 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 3687 | { |
Frédéric Lécaille | f57c333 | 2021-12-09 10:06:21 +0100 | [diff] [blame] | 3688 | /* Attach this task to the same thread ID used for the connection */ |
| 3689 | qc->timer_task = task_new(1UL << qc->tid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3690 | if (!qc->timer_task) |
| 3691 | return 0; |
| 3692 | |
| 3693 | qc->timer = TICK_ETERNITY; |
| 3694 | qc->timer_task->process = process_timer; |
Frédéric Lécaille | 7fbb94d | 2022-01-31 10:37:07 +0100 | [diff] [blame] | 3695 | qc->timer_task->context = qc->xprt_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3696 | |
| 3697 | return 1; |
| 3698 | } |
| 3699 | |
| 3700 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 3701 | * past one byte of this buffer. |
| 3702 | */ |
| 3703 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 3704 | struct quic_rx_packet *pkt) |
| 3705 | { |
| 3706 | unsigned char dcid_len, scid_len; |
| 3707 | |
| 3708 | /* Version */ |
| 3709 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 3710 | return 0; |
| 3711 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3712 | /* Destination Connection ID Length */ |
| 3713 | dcid_len = *(*buf)++; |
| 3714 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 3715 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 3716 | /* XXX MUST BE DROPPED */ |
| 3717 | return 0; |
| 3718 | |
| 3719 | if (dcid_len) { |
| 3720 | /* Check that the length of this received DCID matches the CID lengths |
| 3721 | * of our implementation for non Initials packets only. |
| 3722 | */ |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 3723 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && |
| 3724 | pkt->type != QUIC_PACKET_TYPE_0RTT && |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 3725 | dcid_len != QUIC_HAP_CID_LEN) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3726 | return 0; |
| 3727 | |
| 3728 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 3729 | } |
| 3730 | |
| 3731 | pkt->dcid.len = dcid_len; |
| 3732 | *buf += dcid_len; |
| 3733 | |
| 3734 | /* Source Connection ID Length */ |
| 3735 | scid_len = *(*buf)++; |
| 3736 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 3737 | /* XXX MUST BE DROPPED */ |
| 3738 | return 0; |
| 3739 | |
| 3740 | if (scid_len) |
| 3741 | memcpy(pkt->scid.data, *buf, scid_len); |
| 3742 | pkt->scid.len = scid_len; |
| 3743 | *buf += scid_len; |
| 3744 | |
| 3745 | return 1; |
| 3746 | } |
| 3747 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3748 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 3749 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 3750 | { |
| 3751 | pkt->pn_node.key = pkt->pn; |
Frédéric Lécaille | 2ce5acf | 2021-12-20 14:41:19 +0100 | [diff] [blame] | 3752 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3753 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3754 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 3755 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | /* 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] | 3759 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 3760 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 3761 | * the packet first byte. |
| 3762 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 3763 | * Returns 1 if succeeded, 0 if not. |
| 3764 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3765 | static inline int qc_try_rm_hp(struct quic_conn *qc, |
| 3766 | struct quic_rx_packet *pkt, |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3767 | unsigned char *buf, unsigned char *beg, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3768 | const unsigned char *end, |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3769 | struct quic_enc_level **el) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3770 | { |
| 3771 | unsigned char *pn = NULL; /* Packet number field */ |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3772 | enum quic_tls_enc_level tel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3773 | struct quic_enc_level *qel; |
| 3774 | /* Only for traces. */ |
| 3775 | struct quic_rx_packet *qpkt_trace; |
| 3776 | |
| 3777 | qpkt_trace = NULL; |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3778 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3779 | /* The packet number is here. This is also the start minus |
| 3780 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 3781 | * protection. |
| 3782 | */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 3783 | pn = buf; |
Amaury Denoyelle | 8ae2807 | 2022-01-24 18:34:52 +0100 | [diff] [blame] | 3784 | |
| 3785 | tel = quic_packet_type_enc_level(pkt->type); |
| 3786 | qel = &qc->els[tel]; |
| 3787 | |
| 3788 | if (qc_qel_may_rm_hp(qc, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3789 | /* Note that the following function enables us to unprotect the packet |
| 3790 | * number and its length subsequently used to decrypt the entire |
| 3791 | * packets. |
| 3792 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3793 | if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx, |
| 3794 | qel->pktns->rx.largest_pn, pn, beg, end)) { |
| 3795 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3796 | goto err; |
| 3797 | } |
| 3798 | |
| 3799 | /* The AAD includes the packet number field found at <pn>. */ |
| 3800 | pkt->aad_len = pn - beg + pkt->pnl; |
| 3801 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3802 | } |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3803 | else if (qel) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3804 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 3805 | /* If the packet number space has been discarded, this packet |
| 3806 | * will be not parsed. |
| 3807 | */ |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3808 | 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] | 3809 | goto out; |
| 3810 | } |
| 3811 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3812 | 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] | 3813 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3814 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 3815 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3816 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3817 | else { |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3818 | 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] | 3819 | goto err; |
| 3820 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3821 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3822 | *el = qel; |
| 3823 | /* No reference counter incrementation here!!! */ |
| 3824 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 3825 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 3826 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 3827 | b_add(&qc->rx.buf, pkt->len); |
| 3828 | out: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3829 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3830 | return 1; |
| 3831 | |
| 3832 | err: |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 3833 | 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] | 3834 | return 0; |
| 3835 | } |
| 3836 | |
| 3837 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 3838 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 3839 | */ |
| 3840 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 3841 | unsigned char byte0, int *long_header) |
| 3842 | { |
| 3843 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 3844 | pkt->type = |
| 3845 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 3846 | *long_header = 1; |
| 3847 | } |
| 3848 | else { |
| 3849 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 3850 | *long_header = 0; |
| 3851 | } |
| 3852 | } |
| 3853 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3854 | /* |
| 3855 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 3856 | */ |
| 3857 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 3858 | { |
| 3859 | int j = 0, version; |
| 3860 | |
| 3861 | do { |
| 3862 | version = quic_supported_version[j]; |
| 3863 | if (version == pkt->version) |
| 3864 | return 1; |
| 3865 | |
| 3866 | version = quic_supported_version[++j]; |
| 3867 | } while(version); |
| 3868 | |
| 3869 | return 0; |
| 3870 | } |
| 3871 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3872 | /* |
| 3873 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 3874 | * address <addr>. |
| 3875 | * Implementation of RFC9000 6. Version Negotiation |
| 3876 | * |
| 3877 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 3878 | * |
| 3879 | * Returns 0 on success else non-zero |
| 3880 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 3881 | static int send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 3882 | struct quic_rx_packet *pkt) |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3883 | { |
| 3884 | char buf[256]; |
| 3885 | int i = 0, j, version; |
| 3886 | const socklen_t addrlen = get_addr_len(addr); |
| 3887 | |
| 3888 | /* |
| 3889 | * header form |
| 3890 | * long header, fixed bit to 0 for Version Negotiation |
| 3891 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3892 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 3893 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3894 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3895 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3896 | /* null version for Version Negotiation */ |
| 3897 | buf[i++] = '\x00'; |
| 3898 | buf[i++] = '\x00'; |
| 3899 | buf[i++] = '\x00'; |
| 3900 | buf[i++] = '\x00'; |
| 3901 | |
| 3902 | /* source connection id */ |
| 3903 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3904 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3905 | i += pkt->scid.len; |
| 3906 | |
| 3907 | /* destination connection id */ |
| 3908 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3909 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3910 | i += pkt->dcid.len; |
| 3911 | |
| 3912 | /* supported version */ |
| 3913 | j = 0; |
| 3914 | do { |
| 3915 | version = htonl(quic_supported_version[j]); |
| 3916 | memcpy(&buf[i], &version, sizeof(version)); |
| 3917 | i += sizeof(version); |
| 3918 | |
| 3919 | version = quic_supported_version[++j]; |
| 3920 | } while (version); |
| 3921 | |
| 3922 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 3923 | return 1; |
| 3924 | |
| 3925 | return 0; |
| 3926 | } |
| 3927 | |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 3928 | /* Generate the token to be used in Retry packets. The token is written to |
| 3929 | * <buf> which is expected to be <len> bytes. |
| 3930 | * |
| 3931 | * Various parameters are expected to be encoded in the token. For now, only |
| 3932 | * the DCID from <pkt> is stored. This is useful to implement a stateless Retry |
| 3933 | * as this CID must be repeated by the server in the transport parameters. |
| 3934 | * |
| 3935 | * TODO add the client address to validate the token origin. |
| 3936 | * |
| 3937 | * Returns the length of the encoded token or 0 on error. |
| 3938 | */ |
| 3939 | static int generate_retry_token(unsigned char *buf, unsigned char len, |
| 3940 | struct quic_rx_packet *pkt) |
| 3941 | { |
| 3942 | const size_t token_len = 1 + pkt->dcid.len; |
| 3943 | unsigned char i = 0; |
| 3944 | |
| 3945 | if (token_len > len) |
| 3946 | return 0; |
| 3947 | |
| 3948 | buf[i++] = pkt->dcid.len; |
| 3949 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
| 3950 | i += pkt->dcid.len; |
| 3951 | |
| 3952 | return i; |
| 3953 | } |
| 3954 | |
| 3955 | /* Generate a Retry packet and send it on <fd> socket to <addr> in response to |
| 3956 | * the Initial <pkt> packet. |
| 3957 | * |
| 3958 | * Returns 0 on success else non-zero. |
| 3959 | */ |
| 3960 | static int send_retry(int fd, struct sockaddr_storage *addr, |
| 3961 | struct quic_rx_packet *pkt) |
| 3962 | { |
| 3963 | unsigned char buf[128]; |
| 3964 | int i = 0, token_len; |
| 3965 | const socklen_t addrlen = get_addr_len(addr); |
| 3966 | struct quic_cid scid; |
| 3967 | |
| 3968 | /* long header + fixed bit + packet type 0x3 */ |
| 3969 | buf[i++] = 0xf0; |
| 3970 | /* version */ |
| 3971 | buf[i++] = 0x00; |
| 3972 | buf[i++] = 0x00; |
| 3973 | buf[i++] = 0x00; |
| 3974 | buf[i++] = 0x01; |
| 3975 | |
| 3976 | /* Use the SCID from <pkt> for Retry DCID. */ |
| 3977 | buf[i++] = pkt->scid.len; |
| 3978 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
| 3979 | i += pkt->scid.len; |
| 3980 | |
| 3981 | /* Generate a new CID to be used as SCID for the Retry packet. */ |
| 3982 | scid.len = QUIC_HAP_CID_LEN; |
| 3983 | if (RAND_bytes(scid.data, scid.len) != 1) |
| 3984 | return 1; |
| 3985 | |
| 3986 | buf[i++] = scid.len; |
| 3987 | memcpy(&buf[i], scid.data, scid.len); |
| 3988 | i += scid.len; |
| 3989 | |
| 3990 | /* token */ |
| 3991 | if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt))) |
| 3992 | return 1; |
| 3993 | i += token_len; |
| 3994 | |
| 3995 | /* token integrity tag */ |
| 3996 | if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) || |
| 3997 | !quic_tls_generate_retry_integrity_tag(pkt->dcid.data, |
| 3998 | pkt->dcid.len, buf, i)) { |
| 3999 | return 1; |
| 4000 | } |
| 4001 | |
| 4002 | i += QUIC_TLS_TAG_LEN; |
| 4003 | |
| 4004 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 4005 | return 1; |
| 4006 | |
| 4007 | return 0; |
| 4008 | } |
| 4009 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4010 | /* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of |
| 4011 | * type INITIAL, the ODCID tree is first used. In this case, <saddr> is |
| 4012 | * concatenated to the <pkt> DCID field. |
| 4013 | * |
| 4014 | * Returns the instance or NULL if not found. |
| 4015 | */ |
Amaury Denoyelle | d6b1667 | 2021-12-23 10:37:19 +0100 | [diff] [blame] | 4016 | 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] | 4017 | struct listener *l, |
| 4018 | struct sockaddr_storage *saddr) |
| 4019 | { |
| 4020 | struct quic_conn *qc = NULL; |
| 4021 | struct ebmb_node *node; |
| 4022 | struct quic_connection_id *id; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4023 | /* set if the quic_conn is found in the second DCID tree */ |
| 4024 | int found_in_dcid = 0; |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4025 | |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4026 | /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */ |
| 4027 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL || |
| 4028 | pkt->type == QUIC_PACKET_TYPE_0RTT) { |
| 4029 | /* DCIDs of first packets coming from multiple clients may have |
| 4030 | * the same values. Let's distinguish them by concatenating the |
| 4031 | * socket addresses. |
| 4032 | */ |
| 4033 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4034 | node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data, |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4035 | pkt->dcid.len + pkt->dcid.addrlen); |
| 4036 | if (node) { |
| 4037 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
| 4038 | goto end; |
| 4039 | } |
| 4040 | } |
| 4041 | |
| 4042 | /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used |
| 4043 | * also for INITIAL/0-RTT non-first packets with the final DCID in |
| 4044 | * used. |
| 4045 | */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4046 | node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4047 | if (!node) |
| 4048 | goto end; |
| 4049 | |
| 4050 | id = ebmb_entry(node, struct quic_connection_id, node); |
| 4051 | qc = id->qc; |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4052 | found_in_dcid = 1; |
| 4053 | |
| 4054 | end: |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4055 | if (qc) |
| 4056 | quic_conn_take(qc); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4057 | |
| 4058 | /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree. |
| 4059 | * If already done, this is a noop. |
| 4060 | */ |
Frédéric Lécaille | 74904a4 | 2022-01-27 15:35:56 +0100 | [diff] [blame] | 4061 | if (qc && found_in_dcid) |
Amaury Denoyelle | 250ac42 | 2021-12-22 11:29:05 +0100 | [diff] [blame] | 4062 | ebmb_delete(&qc->odcid_node); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4063 | |
| 4064 | return qc; |
| 4065 | } |
| 4066 | |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4067 | /* Parse the Retry token from buffer <token> whose size is <token_len>. This |
| 4068 | * will extract the parameters stored in the token : <odcid>. |
| 4069 | * |
| 4070 | * Returns 0 on success else non-zero. |
| 4071 | */ |
| 4072 | static int parse_retry_token(const unsigned char *token, uint64_t token_len, |
| 4073 | struct quic_cid *odcid) |
| 4074 | { |
| 4075 | uint64_t odcid_len; |
| 4076 | |
| 4077 | if (!quic_dec_int(&odcid_len, &token, token + token_len)) |
| 4078 | return 1; |
| 4079 | |
| 4080 | memcpy(odcid->data, token, odcid_len); |
| 4081 | odcid->len = odcid_len; |
| 4082 | |
| 4083 | return 0; |
| 4084 | } |
| 4085 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4086 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 4087 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 4088 | * parameters of this session. |
| 4089 | * This is the responsibility of the caller to check the validity of all the |
| 4090 | * pointers passed as parameter to this function. |
| 4091 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 4092 | * CO_ER_SSL_NO_MEM. |
| 4093 | */ |
| 4094 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 4095 | unsigned char *params, size_t params_len) |
| 4096 | { |
| 4097 | int retry; |
| 4098 | |
| 4099 | retry = 1; |
| 4100 | retry: |
| 4101 | *ssl = SSL_new(ssl_ctx); |
| 4102 | if (!*ssl) { |
| 4103 | if (!retry--) |
| 4104 | goto err; |
| 4105 | |
| 4106 | pool_gc(NULL); |
| 4107 | goto retry; |
| 4108 | } |
| 4109 | |
| 4110 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 4111 | !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) || |
| 4112 | !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) { |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4113 | SSL_free(*ssl); |
| 4114 | *ssl = NULL; |
| 4115 | if (!retry--) |
| 4116 | goto err; |
| 4117 | |
| 4118 | pool_gc(NULL); |
| 4119 | goto retry; |
| 4120 | } |
| 4121 | |
| 4122 | return 0; |
| 4123 | |
| 4124 | err: |
| 4125 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 4126 | return -1; |
| 4127 | } |
| 4128 | |
| 4129 | /* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet |
| 4130 | * used to process <qc> received packets. The allocated context is stored in |
| 4131 | * <qc.xprt_ctx>. |
| 4132 | * |
| 4133 | * Returns 0 on success else non-zero. |
| 4134 | */ |
| 4135 | int qc_conn_alloc_ssl_ctx(struct quic_conn *qc) |
| 4136 | { |
| 4137 | struct bind_conf *bc = qc->li->bind_conf; |
| 4138 | struct ssl_sock_ctx *ctx = NULL; |
| 4139 | |
| 4140 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
| 4141 | if (!ctx) |
| 4142 | goto err; |
| 4143 | |
| 4144 | ctx->wait_event.tasklet = tasklet_new(); |
| 4145 | if (!ctx->wait_event.tasklet) |
| 4146 | goto err; |
| 4147 | |
| 4148 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 4149 | ctx->wait_event.tasklet->context = ctx; |
| 4150 | ctx->wait_event.events = 0; |
| 4151 | ctx->subs = NULL; |
| 4152 | ctx->xprt_ctx = NULL; |
| 4153 | ctx->qc = qc; |
| 4154 | |
| 4155 | /* Set tasklet tid based on the SCID selected by us for this |
| 4156 | * connection. The upper layer will also be binded on the same thread. |
| 4157 | */ |
Frédéric Lécaille | 220894a | 2022-01-26 18:04:50 +0100 | [diff] [blame] | 4158 | qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data); |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4159 | |
| 4160 | if (qc_is_listener(qc)) { |
| 4161 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 4162 | qc->enc_params, qc->enc_params_len) == -1) { |
| 4163 | goto err; |
| 4164 | } |
| 4165 | |
| 4166 | /* Enabling 0-RTT */ |
| 4167 | if (bc->ssl_conf.early_data) |
| 4168 | SSL_set_quic_early_data_enabled(ctx->ssl, 1); |
| 4169 | |
| 4170 | SSL_set_accept_state(ctx->ssl); |
| 4171 | } |
| 4172 | |
| 4173 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 4174 | |
| 4175 | /* Store the allocated context in <qc>. */ |
| 4176 | HA_ATOMIC_STORE(&qc->xprt_ctx, ctx); |
| 4177 | |
| 4178 | return 0; |
| 4179 | |
| 4180 | err: |
| 4181 | if (ctx && ctx->wait_event.tasklet) |
| 4182 | tasklet_free(ctx->wait_event.tasklet); |
| 4183 | pool_free(pool_head_quic_conn_ctx, ctx); |
| 4184 | |
| 4185 | return 1; |
| 4186 | } |
| 4187 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4188 | static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end, |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4189 | struct quic_rx_packet *pkt, int first_pkt, |
| 4190 | struct quic_dgram *dgram) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4191 | { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4192 | unsigned char *beg, *payload; |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4193 | struct quic_conn *qc, *qc_to_purge = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4194 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4195 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4196 | int long_header = 0, io_cb_wakeup = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4197 | size_t b_cspace; |
| 4198 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4199 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4200 | beg = buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4201 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4202 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 4203 | qel = NULL; |
Frédéric Lécaille | 8678eb0 | 2021-12-16 18:03:52 +0100 | [diff] [blame] | 4204 | TRACE_ENTER(QUIC_EV_CONN_LPKT); |
| 4205 | /* This ist only to please to traces and distinguish the |
| 4206 | * packet with parsed packet number from others. |
| 4207 | */ |
| 4208 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4209 | if (end <= buf) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4210 | goto err; |
| 4211 | |
| 4212 | /* Fixed bit */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4213 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4214 | /* XXX TO BE DISCARDED */ |
| 4215 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4216 | goto err; |
| 4217 | } |
| 4218 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4219 | l = dgram->owner; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4220 | /* Header form */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4221 | qc_parse_hd_form(pkt, *buf++, &long_header); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4222 | if (long_header) { |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4223 | uint64_t len; |
| 4224 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4225 | if (!quic_packet_read_long_header(&buf, end, pkt)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4226 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4227 | goto err; |
| 4228 | } |
| 4229 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4230 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4231 | * they must have the same DCID. |
| 4232 | */ |
| 4233 | if (!first_pkt && |
| 4234 | (pkt->dcid.len != dgram->dcid_len || |
| 4235 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 4236 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 4237 | goto err; |
| 4238 | } |
| 4239 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4240 | /* Retry of Version Negotiation packets are only sent by servers */ |
| 4241 | if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) { |
| 4242 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4243 | goto err; |
| 4244 | } |
| 4245 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4246 | /* RFC9000 6. Version Negotiation */ |
| 4247 | if (!qc_pkt_is_supported_version(pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4248 | /* unsupported version, send Negotiation packet */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4249 | if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4250 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 4251 | goto err; |
| 4252 | } |
| 4253 | |
| 4254 | 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] | 4255 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 4256 | } |
| 4257 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4258 | /* For Initial packets, and for servers (QUIC clients connections), |
| 4259 | * there is no Initial connection IDs storage. |
| 4260 | */ |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4261 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4262 | uint64_t token_len; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4263 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4264 | if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) || |
| 4265 | end - buf < token_len) { |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4266 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4267 | goto err; |
Frédéric Lécaille | a5da31d | 2021-12-14 19:44:14 +0100 | [diff] [blame] | 4268 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4269 | |
Frédéric Lécaille | 055ee6c | 2022-01-25 21:21:56 +0100 | [diff] [blame] | 4270 | /* The token may be provided in a Retry packet or NEW_TOKEN frame |
| 4271 | * only by the QUIC server. |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4272 | */ |
| 4273 | pkt->token_len = token_len; |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4274 | |
| 4275 | /* TODO Retry should be automatically activated if |
| 4276 | * suspect network usage is detected. |
| 4277 | */ |
| 4278 | if (!token_len && l->bind_conf->quic_force_retry) { |
| 4279 | TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4280 | if (send_retry(l->rx.fd, &dgram->saddr, pkt)) { |
Amaury Denoyelle | b76ae69 | 2022-01-11 14:16:37 +0100 | [diff] [blame] | 4281 | TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT); |
| 4282 | goto err; |
| 4283 | } |
| 4284 | |
| 4285 | goto err; |
| 4286 | } |
| 4287 | else { |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4288 | pkt->token = buf; |
| 4289 | buf += pkt->token_len; |
| 4290 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4291 | } |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4292 | else if (pkt->type != QUIC_PACKET_TYPE_0RTT) { |
Amaury Denoyelle | d496251 | 2021-12-14 17:17:28 +0100 | [diff] [blame] | 4293 | if (pkt->dcid.len != QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4294 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4295 | goto err; |
| 4296 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4297 | } |
| 4298 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4299 | if (!quic_dec_int(&len, (const unsigned char **)&buf, end) || |
| 4300 | end - buf < len) { |
| 4301 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4302 | goto err; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4303 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4304 | |
Frédéric Lécaille | 2c15a66 | 2021-12-22 20:39:12 +0100 | [diff] [blame] | 4305 | payload = buf; |
| 4306 | pkt->len = len + payload - beg; |
| 4307 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4308 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Amaury Denoyelle | 8efe032 | 2021-12-15 16:32:56 +0100 | [diff] [blame] | 4309 | if (!qc) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4310 | int ipv4; |
| 4311 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4312 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4313 | const unsigned char *salt = initial_salt_v1; |
| 4314 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4315 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4316 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
Amaury Denoyelle | 47e1f6d | 2021-12-17 10:58:05 +0100 | [diff] [blame] | 4317 | TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4318 | goto err; |
| 4319 | } |
| 4320 | |
Frédéric Lécaille | dc36404 | 2022-01-27 16:51:54 +0100 | [diff] [blame] | 4321 | if (pkt->dcid.len < QUIC_ODCID_MINLEN) { |
| 4322 | TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT); |
| 4323 | goto err; |
| 4324 | } |
| 4325 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4326 | pkt->saddr = dgram->saddr; |
| 4327 | ipv4 = dgram->saddr.ss_family == AF_INET; |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4328 | qc = qc_new_conn(pkt->version, ipv4, |
| 4329 | pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4330 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4331 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4332 | goto err; |
| 4333 | |
Amaury Denoyelle | 9fa15e5 | 2022-01-19 15:54:23 +0100 | [diff] [blame] | 4334 | memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr)); |
| 4335 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4336 | odcid = &qc->rx.params.original_destination_connection_id; |
| 4337 | /* Copy the transport parameters. */ |
| 4338 | qc->rx.params = l->bind_conf->quic_params; |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4339 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4340 | /* Copy original_destination_connection_id transport parameter. */ |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4341 | if (pkt->token_len) { |
| 4342 | if (parse_retry_token(pkt->token, pkt->token_len, odcid)) { |
| 4343 | TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc); |
| 4344 | goto err; |
| 4345 | } |
Amaury Denoyelle | c3b6f4d | 2022-01-11 12:03:09 +0100 | [diff] [blame] | 4346 | /* Copy retry_source_connection_id transport parameter. */ |
| 4347 | quic_cid_cpy(&qc->rx.params.retry_source_connection_id, |
| 4348 | &pkt->dcid); |
Amaury Denoyelle | 5ff1c97 | 2022-01-11 14:11:32 +0100 | [diff] [blame] | 4349 | } |
| 4350 | else { |
| 4351 | memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len); |
| 4352 | odcid->len = pkt->dcid.len; |
| 4353 | } |
| 4354 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4355 | /* Copy the initial source connection ID. */ |
| 4356 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4357 | qc->enc_params_len = |
| 4358 | quic_transport_params_encode(qc->enc_params, |
| 4359 | qc->enc_params + sizeof qc->enc_params, |
| 4360 | &qc->rx.params, 1); |
| 4361 | if (!qc->enc_params_len) |
| 4362 | goto err; |
| 4363 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 4364 | if (qc_conn_alloc_ssl_ctx(qc)) |
| 4365 | goto err; |
| 4366 | |
Frédéric Lécaille | 789413c | 2022-01-31 10:16:18 +0100 | [diff] [blame] | 4367 | if (!quic_conn_init_timer(qc)) |
| 4368 | goto err; |
| 4369 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4370 | /* NOTE: the socket address has been concatenated to the destination ID |
| 4371 | * chosen by the client for Initial packets. |
| 4372 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4373 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 4374 | salt = initial_salt_draft_29; |
| 4375 | salt_len = sizeof initial_salt_draft_29; |
| 4376 | } |
| 4377 | if (!qc_new_isecs(qc, salt, salt_len, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4378 | pkt->dcid.data, pkt->dcid.len, 1)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4379 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 4380 | goto err; |
| 4381 | } |
| 4382 | |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 4383 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 4384 | n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node, |
Amaury Denoyelle | c92cbfc | 2021-12-14 17:20:59 +0100 | [diff] [blame] | 4385 | qc->odcid.len + qc->odcid.addrlen); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4386 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4387 | /* If the insertion failed, it means that another |
| 4388 | * thread has already allocated a QUIC connection for |
| 4389 | * the same CID. Liberate our allocated connection. |
| 4390 | */ |
| 4391 | if (unlikely(n != &qc->odcid_node)) { |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4392 | qc_to_purge = qc; |
| 4393 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 4394 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 4395 | pkt->qc = qc; |
| 4396 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4397 | |
| 4398 | quic_conn_take(qc); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4399 | |
| 4400 | if (likely(!qc_to_purge)) { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4401 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 4402 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4403 | } |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4404 | else { |
| 4405 | quic_conn_drop(qc_to_purge); |
| 4406 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4407 | } |
| 4408 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4409 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4410 | } |
| 4411 | } |
| 4412 | else { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4413 | if (end - buf < QUIC_HAP_CID_LEN) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4414 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 4415 | goto err; |
| 4416 | } |
| 4417 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4418 | memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN); |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4419 | pkt->dcid.len = QUIC_HAP_CID_LEN; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4420 | |
| 4421 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4422 | * they must have the same DCID. |
| 4423 | */ |
| 4424 | if (!first_pkt && |
| 4425 | (pkt->dcid.len != dgram->dcid_len || |
| 4426 | memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) { |
| 4427 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
| 4428 | goto err; |
| 4429 | } |
| 4430 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4431 | buf += QUIC_HAP_CID_LEN; |
| 4432 | |
| 4433 | /* A short packet is the last one of a UDP datagram. */ |
| 4434 | payload = buf; |
| 4435 | pkt->len = end - beg; |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4436 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4437 | qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr); |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4438 | if (!qc) { |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4439 | size_t pktlen = end - buf; |
Frédéric Lécaille | 4d118d6 | 2021-12-21 14:48:58 +0100 | [diff] [blame] | 4440 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen); |
| 4441 | goto err; |
| 4442 | } |
| 4443 | |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 4444 | pkt->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4445 | } |
| 4446 | |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4447 | |
| 4448 | /* When multiple QUIC packets are coalesced on the same UDP datagram, |
| 4449 | * they must have the same DCID. |
| 4450 | * |
| 4451 | * This check must be done after the final update to pkt.len to |
| 4452 | * properly drop the packet on failure. |
| 4453 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4454 | if (first_pkt && !quic_peer_validated_addr(qc) && |
| 4455 | HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
| 4456 | TRACE_PROTO("PTO timer must be armed after anti-amplication was reached", |
| 4457 | QUIC_EV_CONN_LPKT, qc); |
| 4458 | /* Reset the anti-amplification bit. It will be set again |
| 4459 | * when sending the next packet if reached again. |
| 4460 | */ |
| 4461 | HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT); |
| 4462 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT); |
| 4463 | io_cb_wakeup = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4464 | } |
Amaury Denoyelle | adb2276 | 2021-12-14 15:04:14 +0100 | [diff] [blame] | 4465 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 4466 | dgram->qc = qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4467 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4468 | if (HA_ATOMIC_LOAD(&qc->err_code)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4469 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4470 | goto out; |
| 4471 | } |
| 4472 | |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4473 | pkt->raw_len = pkt->len; |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4474 | quic_rx_pkts_del(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4475 | b_cspace = b_contig_space(&qc->rx.buf); |
| 4476 | if (b_cspace < pkt->len) { |
| 4477 | /* Let us consume the remaining contiguous space. */ |
Frédéric Lécaille | d61bc8d | 2021-12-02 14:46:19 +0100 | [diff] [blame] | 4478 | if (b_cspace) { |
| 4479 | b_putchr(&qc->rx.buf, 0x00); |
| 4480 | b_cspace--; |
| 4481 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4482 | b_add(&qc->rx.buf, b_cspace); |
| 4483 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4484 | 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] | 4485 | qc_list_all_rx_pkts(qc); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 4486 | goto err; |
| 4487 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4488 | } |
| 4489 | |
Amaury Denoyelle | e81fed9 | 2021-12-22 11:06:34 +0100 | [diff] [blame] | 4490 | if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4491 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 4492 | goto err; |
| 4493 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4494 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4495 | 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] | 4496 | if (pkt->aad_len) |
| 4497 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4498 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 4499 | /* Wake up the connection packet handler task from here only if all |
| 4500 | * the contexts have been initialized, especially the mux context |
| 4501 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 4502 | * will start it if these contexts for the connection are not already |
| 4503 | * initialized. |
| 4504 | */ |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 4505 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 4506 | if (conn_ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4507 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 4508 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4509 | 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] | 4510 | |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4511 | if (qc) |
| 4512 | quic_conn_drop(qc); |
| 4513 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4514 | return pkt->len; |
| 4515 | |
| 4516 | err: |
Frédéric Lécaille | 6b66315 | 2022-01-04 17:03:11 +0100 | [diff] [blame] | 4517 | /* Wakeup the I/O handler callback if the PTO timer must be armed. |
| 4518 | * This cannot be done by this thread. |
| 4519 | */ |
| 4520 | if (io_cb_wakeup) { |
| 4521 | conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx); |
| 4522 | if (conn_ctx && conn_ctx->wait_event.tasklet) |
| 4523 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 4524 | } |
Frédéric Lécaille | f7ef976 | 2021-12-31 16:37:58 +0100 | [diff] [blame] | 4525 | /* If length not found, consume the entire datagram */ |
Frédéric Lécaille | 01cfec7 | 2021-12-22 10:17:01 +0100 | [diff] [blame] | 4526 | if (!pkt->len) |
| 4527 | pkt->len = end - beg; |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 4528 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4529 | qc ? qc : NULL, pkt); |
Amaury Denoyelle | 76f47ca | 2021-12-23 10:02:50 +0100 | [diff] [blame] | 4530 | |
| 4531 | if (qc) |
| 4532 | quic_conn_drop(qc); |
| 4533 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4534 | return -1; |
| 4535 | } |
| 4536 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4537 | /* This function builds into <buf> buffer a QUIC long packet header. |
| 4538 | * 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] | 4539 | */ |
| 4540 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 4541 | int type, size_t pn_len, struct quic_conn *conn) |
| 4542 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4543 | 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] | 4544 | return 0; |
| 4545 | |
| 4546 | /* #0 byte flags */ |
| 4547 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 4548 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 4549 | /* Version */ |
| 4550 | quic_write_uint32(buf, end, conn->version); |
| 4551 | *(*buf)++ = conn->dcid.len; |
| 4552 | /* Destination connection ID */ |
| 4553 | if (conn->dcid.len) { |
| 4554 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4555 | *buf += conn->dcid.len; |
| 4556 | } |
| 4557 | /* Source connection ID */ |
| 4558 | *(*buf)++ = conn->scid.len; |
| 4559 | if (conn->scid.len) { |
| 4560 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 4561 | *buf += conn->scid.len; |
| 4562 | } |
| 4563 | |
| 4564 | return 1; |
| 4565 | } |
| 4566 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4567 | /* This function builds into <buf> buffer a QUIC short packet header. |
| 4568 | * 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] | 4569 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4570 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 4571 | size_t pn_len, struct quic_conn *conn, |
| 4572 | unsigned char tls_flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4573 | { |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4574 | if (end - *buf < 1 + conn->dcid.len) |
| 4575 | return 0; |
| 4576 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4577 | /* #0 byte flags */ |
Frédéric Lécaille | a7d2c09 | 2021-11-30 11:18:18 +0100 | [diff] [blame] | 4578 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | |
| 4579 | ((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] | 4580 | /* Destination connection ID */ |
| 4581 | if (conn->dcid.len) { |
| 4582 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4583 | *buf += conn->dcid.len; |
| 4584 | } |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4585 | |
| 4586 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 4590 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 4591 | * with <aead> as AEAD cipher and <key> as secret key. |
| 4592 | * Returns 1 if succeeded or 0 if failed. |
| 4593 | */ |
| 4594 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 4595 | const EVP_CIPHER *aead, const unsigned char *key) |
| 4596 | { |
| 4597 | int i, ret, outlen; |
| 4598 | EVP_CIPHER_CTX *ctx; |
| 4599 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 4600 | * and at most 4 bytes for the packet number |
| 4601 | */ |
| 4602 | unsigned char mask[5] = {0}; |
| 4603 | |
| 4604 | ret = 0; |
| 4605 | ctx = EVP_CIPHER_CTX_new(); |
| 4606 | if (!ctx) |
| 4607 | return 0; |
| 4608 | |
| 4609 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 4610 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 4611 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 4612 | goto out; |
| 4613 | |
| 4614 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 4615 | for (i = 0; i < pnlen; i++) |
| 4616 | pn[i] ^= mask[i + 1]; |
| 4617 | |
| 4618 | ret = 1; |
| 4619 | |
| 4620 | out: |
| 4621 | EVP_CIPHER_CTX_free(ctx); |
| 4622 | |
| 4623 | return ret; |
| 4624 | } |
| 4625 | |
| 4626 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 4627 | * ACK ranges if needed to a value below <limit> in bytes. |
| 4628 | * Return 1 if succeeded, 0 if not. |
| 4629 | */ |
| 4630 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 4631 | { |
| 4632 | size_t room, ack_delay_sz; |
| 4633 | |
| 4634 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 4635 | /* A frame is made of 1 byte for the frame type. */ |
| 4636 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4637 | 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] | 4638 | return 0; |
| 4639 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4640 | 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] | 4641 | } |
| 4642 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4643 | /* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames |
| 4644 | * 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] | 4645 | * and <*len> the packet Length field initialized with the number of bytes already present |
| 4646 | * 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] | 4647 | * <headlen> is the number of bytes already present in this packet before building frames. |
| 4648 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4649 | * 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] | 4650 | * 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] | 4651 | * Return 1 if succeeded, 0 if not. |
| 4652 | */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4653 | static inline int qc_build_frms(struct list *l, |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4654 | size_t room, size_t *len, size_t headlen, |
| 4655 | struct quic_enc_level *qel, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4656 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4657 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4658 | int ret; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4659 | struct quic_frame *cf, *cfbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4660 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4661 | ret = 0; |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 4662 | if (*len > room) |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4663 | return 0; |
| 4664 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4665 | /* If we are not probing we must take into an account the congestion |
| 4666 | * control window. |
| 4667 | */ |
Frédéric Lécaille | f4e5a7c | 2022-01-17 17:56:20 +0100 | [diff] [blame] | 4668 | if (!qel->pktns->tx.pto_probe) { |
| 4669 | size_t remain = quic_path_prep_data(qc->path); |
| 4670 | |
| 4671 | if (headlen > remain) |
| 4672 | return 0; |
| 4673 | |
| 4674 | room = QUIC_MIN(room, remain - headlen); |
| 4675 | } |
| 4676 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4677 | TRACE_PROTO("************** frames build (headlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4678 | QUIC_EV_CONN_BCFRMS, qc, &headlen); |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4679 | 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] | 4680 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4681 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4682 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4683 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4684 | break; |
| 4685 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4686 | switch (cf->type) { |
| 4687 | case QUIC_FT_CRYPTO: |
| 4688 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4689 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4690 | /* Compute the length of this CRYPTO frame header */ |
| 4691 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 4692 | /* Compute the data length of this CRyPTO frame. */ |
| 4693 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 4694 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4695 | 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] | 4696 | if (!dlen) |
| 4697 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4698 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4699 | /* CRYPTO frame length. */ |
| 4700 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 4701 | TRACE_PROTO(" CRYPTO frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4702 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4703 | /* Add the CRYPTO data length and its encoded length to the packet |
| 4704 | * length and the length of this length. |
| 4705 | */ |
| 4706 | *len += flen; |
| 4707 | room -= flen; |
| 4708 | if (dlen == cf->crypto.len) { |
| 4709 | /* <cf> CRYPTO data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4710 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4711 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4712 | } |
| 4713 | else { |
| 4714 | struct quic_frame *new_cf; |
| 4715 | |
| 4716 | new_cf = pool_alloc(pool_head_quic_frame); |
| 4717 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4718 | 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] | 4719 | return 0; |
| 4720 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4721 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4722 | new_cf->type = QUIC_FT_CRYPTO; |
| 4723 | new_cf->crypto.len = dlen; |
| 4724 | new_cf->crypto.offset = cf->crypto.offset; |
| 4725 | new_cf->crypto.qel = qel; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4726 | LIST_APPEND(l, &new_cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4727 | /* Consume <dlen> bytes of the current frame. */ |
| 4728 | cf->crypto.len -= dlen; |
| 4729 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4730 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4731 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4732 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4733 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4734 | /* Note that these frames are accepted in short packets only without |
| 4735 | * "Length" packet field. Here, <*len> is used only to compute the |
| 4736 | * 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] | 4737 | * |
| 4738 | * 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] | 4739 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 4740 | */ |
| 4741 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4742 | ((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] | 4743 | /* Compute the data length of this STREAM frame. */ |
| 4744 | avail_room = room - hlen - *len; |
| 4745 | if ((ssize_t)avail_room <= 0) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4746 | break; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4747 | |
Frédéric Lécaille | d8b8443 | 2021-12-10 15:18:36 +0100 | [diff] [blame] | 4748 | TRACE_PROTO(" New STREAM frame build (room, len)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4749 | QUIC_EV_CONN_BCFRMS, qc, &room, len); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4750 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 4751 | dlen = max_available_room(avail_room, &dlen_sz); |
| 4752 | if (dlen > cf->stream.len) { |
| 4753 | dlen = cf->stream.len; |
| 4754 | } |
| 4755 | dlen_sz = quic_int_getsize(dlen); |
| 4756 | flen = hlen + dlen_sz + dlen; |
| 4757 | } |
| 4758 | else { |
| 4759 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 4760 | flen = hlen + dlen; |
| 4761 | } |
| 4762 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4763 | 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] | 4764 | TRACE_PROTO(" STREAM frame length (flen)", |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4765 | QUIC_EV_CONN_BCFRMS, qc, &flen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4766 | /* Add the STREAM data length and its encoded length to the packet |
| 4767 | * length and the length of this length. |
| 4768 | */ |
| 4769 | *len += flen; |
| 4770 | room -= flen; |
| 4771 | if (dlen == cf->stream.len) { |
| 4772 | /* <cf> STREAM data have been consumed. */ |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4773 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4774 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4775 | } |
| 4776 | else { |
| 4777 | struct quic_frame *new_cf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame^] | 4778 | struct buffer cf_buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4779 | |
| 4780 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 4781 | if (!new_cf) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4782 | 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] | 4783 | return 0; |
| 4784 | } |
| 4785 | |
| 4786 | new_cf->type = cf->type; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4787 | new_cf->stream.qcs = cf->stream.qcs; |
| 4788 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4789 | new_cf->stream.id = cf->stream.id; |
| 4790 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 4791 | new_cf->stream.offset = cf->stream.offset; |
| 4792 | new_cf->stream.len = dlen; |
| 4793 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 4794 | /* FIN bit reset */ |
| 4795 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 4796 | new_cf->stream.data = cf->stream.data; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4797 | LIST_APPEND(l, &new_cf->list); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4798 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 4799 | /* Consume <dlen> bytes of the current frame. */ |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame^] | 4800 | cf_buf = b_make(b_orig(cf->stream.buf), |
| 4801 | b_size(cf->stream.buf), |
| 4802 | (char *)cf->stream.data - b_orig(cf->stream.buf), 0); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4803 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4804 | cf->stream.offset.key += dlen; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame^] | 4805 | cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen); |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4806 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4807 | break; |
| 4808 | |
| 4809 | default: |
| 4810 | flen = qc_frm_len(cf); |
| 4811 | BUG_ON(!flen); |
| 4812 | if (flen > room) |
| 4813 | continue; |
| 4814 | |
| 4815 | *len += flen; |
| 4816 | room -= flen; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4817 | LIST_DELETE(&cf->list); |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4818 | LIST_APPEND(l, &cf->list); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4819 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4820 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4821 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4822 | } |
| 4823 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4824 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4825 | } |
| 4826 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4827 | /* 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] | 4828 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 4829 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 4830 | * filling the buffer with as much frames as possible. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4831 | * 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] | 4832 | * 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] | 4833 | * having returned from this function. |
| 4834 | * 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] | 4835 | * number field in this packet. <pn_len> will also have the packet number |
| 4836 | * length as value. |
| 4837 | * |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4838 | * 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] | 4839 | */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4840 | static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 4841 | size_t dglen, struct quic_tx_packet *pkt, |
| 4842 | 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] | 4843 | int ack, int padding, int cc, int probe, |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4844 | struct quic_enc_level *qel, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4845 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4846 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4847 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4848 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 4849 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4850 | 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] | 4851 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4852 | int64_t largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4853 | int add_ping_frm; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4854 | struct list frm_list = LIST_HEAD_INIT(frm_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4855 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4856 | /* Length field value with CRYPTO frames if present. */ |
| 4857 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4858 | beg = pos; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4859 | /* When not probing and not acking, and no immediate close is required, |
| 4860 | * reduce the size of this buffer to respect |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4861 | * the congestion controller window. So, we do not limit the size of this |
| 4862 | * packet if we have an ACK frame to send because an ACK frame is not |
| 4863 | * ack-eliciting. This size will be limited if we have ack-eliciting |
| 4864 | * frames to send from qel->pktns->tx.frms. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4865 | */ |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 4866 | if (!probe && !ack && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4867 | size_t path_room; |
| 4868 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4869 | path_room = quic_path_prep_data(qc->path); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4870 | if (end - beg > path_room) |
| 4871 | end = beg + path_room; |
| 4872 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4873 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4874 | /* Ensure there is enough room for the TLS encryption tag and a zero token |
| 4875 | * length field if any. |
| 4876 | */ |
| 4877 | if (end - pos < QUIC_TLS_TAG_LEN + |
| 4878 | (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0)) |
| 4879 | goto no_room; |
| 4880 | |
| 4881 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | e1aa0d3 | 2021-08-03 16:03:09 +0200 | [diff] [blame] | 4882 | 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] | 4883 | /* packet number length */ |
| 4884 | *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] | 4885 | /* Build the header */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4886 | if ((pkt->type == QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4887 | !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] | 4888 | (pkt->type != QUIC_PACKET_TYPE_SHORT && |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4889 | !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] | 4890 | goto no_room; |
| 4891 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4892 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 4893 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4894 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4895 | head_len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4896 | /* Build an ACK frame if required. */ |
| 4897 | ack_frm_len = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4898 | 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] | 4899 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4900 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4901 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 4902 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 4903 | * that from here, we do not know if we will have to send a PING frame. |
| 4904 | * This will be decided after having computed the ack-eliciting frames |
| 4905 | * to be added to this packet. |
| 4906 | */ |
| 4907 | 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] | 4908 | if (!ack_frm_len) |
| 4909 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4910 | } |
| 4911 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4912 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4913 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4914 | len_frms = 0; |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 4915 | if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4916 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4917 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4918 | /* Initialize the length of the frames built below to <len>. |
| 4919 | * If any frame could be successfully built by qc_build_frms(), |
| 4920 | * we will have len_frms > len. |
| 4921 | */ |
| 4922 | len_frms = len; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4923 | 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] | 4924 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4925 | qc, NULL, NULL, &room); |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4926 | } |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4927 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4928 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4929 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 4930 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 4931 | * for the encryption tag. It must be taken into an account for the length |
| 4932 | * of this packet. |
| 4933 | */ |
| 4934 | if (len_frms) |
| 4935 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 4936 | else |
| 4937 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4938 | /* CONNECTION_CLOSE frame */ |
| 4939 | if (cc) { |
| 4940 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4941 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4942 | cc->error_code = qc->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4943 | len += qc_frm_len(&cc_frm); |
| 4944 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4945 | add_ping_frm = 0; |
| 4946 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4947 | len_sz = quic_int_getsize(len); |
| 4948 | /* Add this packet size to <dglen> */ |
| 4949 | dglen += head_len + len_sz + len; |
| 4950 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 4951 | /* This is a maximum padding size */ |
| 4952 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 4953 | /* The length field value is of this packet is <len> + <padding_len> |
| 4954 | * the size of which may be greater than the initial computed size |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 4955 | * <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] | 4956 | * sizes from <padding_len>. |
| 4957 | */ |
| 4958 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 4959 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4960 | } |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4961 | else if (LIST_ISEMPTY(&frm_list) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4962 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4963 | /* 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] | 4964 | add_ping_frm = 1; |
| 4965 | len += 1; |
| 4966 | } |
| 4967 | /* 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] | 4968 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4969 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 4970 | } |
| 4971 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4972 | if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len)) |
| 4973 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4974 | |
| 4975 | /* Packet number field address. */ |
| 4976 | *buf_pn = pos; |
| 4977 | |
| 4978 | /* Packet number encoding. */ |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4979 | if (!quic_packet_number_encode(&pos, end, pn, *pn_len)) |
| 4980 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4981 | |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4982 | 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] | 4983 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4984 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4985 | /* Ack-eliciting frames */ |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4986 | if (!LIST_ISEMPTY(&frm_list)) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4987 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4988 | |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 4989 | list_for_each_entry(cf, &frm_list, list) { |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 4990 | if (!qc_build_frm(&pos, end, cf, pkt, qc)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4991 | ssize_t room = end - pos; |
| 4992 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 4993 | qc, NULL, NULL, &room); |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 4994 | break; |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4995 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4996 | } |
| 4997 | } |
| 4998 | |
| 4999 | /* Build a PING frame if needed. */ |
| 5000 | if (add_ping_frm) { |
| 5001 | frm.type = QUIC_FT_PING; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5002 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5003 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5004 | } |
| 5005 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5006 | /* Build a CONNECTION_CLOSE frame if needed. */ |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5007 | 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] | 5008 | goto no_room; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 5009 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5010 | /* Build a PADDING frame if needed. */ |
| 5011 | if (padding_len) { |
| 5012 | frm.type = QUIC_FT_PADDING; |
| 5013 | frm.padding.len = padding_len; |
Amaury Denoyelle | 17a7416 | 2021-12-21 14:45:39 +0100 | [diff] [blame] | 5014 | if (!qc_build_frm(&pos, end, &frm, pkt, qc)) |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5015 | goto no_room; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5016 | } |
| 5017 | |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5018 | /* If this packet is ack-eliciting and we are probing let's |
| 5019 | * decrement the PTO probe counter. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5020 | */ |
Frédéric Lécaille | 466e9da | 2021-12-29 12:04:13 +0100 | [diff] [blame] | 5021 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING && |
| 5022 | qel->pktns->tx.pto_probe) |
| 5023 | qel->pktns->tx.pto_probe--; |
| 5024 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5025 | pkt->len = pos - beg; |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5026 | LIST_SPLICE(&pkt->frms, &frm_list); |
| 5027 | 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] | 5028 | |
| 5029 | return 1; |
| 5030 | |
| 5031 | no_room: |
Frédéric Lécaille | cba4cd4 | 2022-01-14 20:39:18 +0100 | [diff] [blame] | 5032 | /* Replace the pre-built frames which could not be add to this packet */ |
| 5033 | LIST_SPLICE(&qel->pktns->tx.frms, &frm_list); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5034 | 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] | 5035 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5036 | } |
| 5037 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5038 | 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] | 5039 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5040 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5041 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5042 | pkt->in_flight_len = 0; |
Frédéric Lécaille | 0371cd5 | 2021-12-13 12:30:54 +0100 | [diff] [blame] | 5043 | pkt->pn_node.key = (uint64_t)-1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5044 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5045 | pkt->next = NULL; |
| 5046 | pkt->refcnt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5047 | } |
| 5048 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5049 | /* 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] | 5050 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 5051 | { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 5052 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5053 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5054 | if (!pkt) |
| 5055 | return; |
| 5056 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5057 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5058 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 5059 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5060 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5061 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5062 | } |
| 5063 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5064 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 5065 | * type for <qc> QUIC connection from <qel> encryption level. |
| 5066 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 5067 | * -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] | 5068 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 5069 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 5070 | const unsigned char *buf_end, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5071 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 5072 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | ce6602d | 2022-01-17 11:06:10 +0100 | [diff] [blame] | 5073 | 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] | 5074 | { |
| 5075 | /* The pointer to the packet number field. */ |
| 5076 | unsigned char *buf_pn; |
| 5077 | unsigned char *beg, *end, *payload; |
| 5078 | int64_t pn; |
| 5079 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5080 | struct quic_tls_ctx *tls_ctx; |
| 5081 | struct quic_tx_packet *pkt; |
| 5082 | |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5083 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5084 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5085 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 5086 | if (!pkt) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5087 | 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] | 5088 | *err = -2; |
| 5089 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5090 | } |
| 5091 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 5092 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5093 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5094 | pn_len = 0; |
| 5095 | buf_pn = NULL; |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5096 | |
| 5097 | pn = qel->pktns->tx.next_pn + 1; |
| 5098 | 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] | 5099 | ack, padding, cc, probe, qel, qc)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 5100 | *err = -1; |
| 5101 | goto err; |
| 5102 | } |
| 5103 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5104 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5105 | payload = buf_pn + pn_len; |
| 5106 | payload_len = end - payload; |
| 5107 | aad_len = payload - beg; |
| 5108 | |
| 5109 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | 5f7f118 | 2022-01-10 11:00:16 +0100 | [diff] [blame] | 5110 | 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] | 5111 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5112 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5113 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5114 | |
| 5115 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5116 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5117 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 5118 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5119 | 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] | 5120 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5121 | goto err; |
| 5122 | } |
| 5123 | |
Frédéric Lécaille | 73dcc6e | 2021-12-07 15:27:44 +0100 | [diff] [blame] | 5124 | /* Consume a packet number */ |
| 5125 | qel->pktns->tx.next_pn++; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 5126 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | 41a0760 | 2022-01-04 16:57:37 +0100 | [diff] [blame] | 5127 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes) |
| 5128 | 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] | 5129 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 5130 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5131 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 5132 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5133 | /* 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] | 5134 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5135 | pkt->in_flight_len = pkt->len; |
| 5136 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 5137 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5138 | pkt->pktns = qel->pktns; |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5139 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5140 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 5141 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5142 | |
| 5143 | err: |
| 5144 | free_quic_tx_packet(pkt); |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5145 | 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] | 5146 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5147 | } |
| 5148 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5149 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5150 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 5151 | * event subscriber <es> is not allowed to change from a previous call as long |
| 5152 | * as at least one event is still subscribed. The <event_type> must only be a |
| 5153 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 5154 | */ |
| 5155 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5156 | { |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 5157 | struct qcc *qcc = conn->qc->qcc; |
| 5158 | |
| 5159 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 5160 | BUG_ON(qcc->subs && qcc->subs != es); |
| 5161 | |
| 5162 | es->events |= event_type; |
| 5163 | qcc->subs = es; |
| 5164 | |
| 5165 | if (event_type & SUB_RETRY_RECV) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5166 | 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] | 5167 | |
| 5168 | if (event_type & SUB_RETRY_SEND) |
Amaury Denoyelle | 7aaeb5b | 2021-12-21 14:29:15 +0100 | [diff] [blame] | 5169 | 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] | 5170 | |
| 5171 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5172 | } |
| 5173 | |
| 5174 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5175 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5176 | * subscribe() call. It always returns zero. |
| 5177 | */ |
| 5178 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 5179 | { |
| 5180 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 5181 | } |
| 5182 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5183 | /* Store in <xprt_ctx> the context attached to <conn>. |
| 5184 | * Returns always 0. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5185 | */ |
| 5186 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 5187 | { |
Amaury Denoyelle | 7ca7c84 | 2021-12-22 18:20:38 +0100 | [diff] [blame] | 5188 | struct quic_conn *qc = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5189 | |
| 5190 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 5191 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5192 | /* do not store the context if already set */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5193 | if (*xprt_ctx) |
| 5194 | goto out; |
| 5195 | |
Amaury Denoyelle | 33ac346 | 2022-01-18 16:44:34 +0100 | [diff] [blame] | 5196 | HA_ATOMIC_STORE(xprt_ctx, conn->qc->xprt_ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5197 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5198 | out: |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 5199 | TRACE_LEAVE(QUIC_EV_CONN_NEW, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5200 | |
| 5201 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5202 | } |
| 5203 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5204 | /* Start the QUIC transport layer */ |
| 5205 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 5206 | { |
| 5207 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 5208 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5209 | |
| 5210 | qc = conn->qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 5211 | quic_mux_transport_params_update(qc->qcc); |
| 5212 | if (qcc_install_app_ops(qc->qcc, qc->app_ops)) { |
| 5213 | TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc); |
| 5214 | return 0; |
| 5215 | } |
| 5216 | |
| 5217 | /* mux-quic can now be considered ready. */ |
| 5218 | qc->mux_state = QC_MUX_READY; |
| 5219 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5220 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 5221 | return 1; |
| 5222 | } |
| 5223 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5224 | /* transport-layer operations for QUIC connections. */ |
| 5225 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 5226 | .close = quic_close, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5227 | .subscribe = quic_conn_subscribe, |
| 5228 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5229 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5230 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5231 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 5232 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 5233 | .get_alpn = ssl_sock_get_alpn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5234 | .name = "QUIC", |
| 5235 | }; |
| 5236 | |
| 5237 | __attribute__((constructor)) |
| 5238 | static void __quic_conn_init(void) |
| 5239 | { |
| 5240 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 5241 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 5242 | } |
| 5243 | |
| 5244 | __attribute__((destructor)) |
| 5245 | static void __quic_conn_deinit(void) |
| 5246 | { |
| 5247 | BIO_meth_free(ha_quic_meth); |
| 5248 | } |
| 5249 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5250 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 5251 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5252 | * 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] | 5253 | */ |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5254 | struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5255 | { |
| 5256 | unsigned char *pos; |
| 5257 | const unsigned char *end; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5258 | struct quic_dghdlr *dghdlr = ctx; |
| 5259 | struct quic_dgram *dgram; |
| 5260 | int first_pkt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5261 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5262 | while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) { |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5263 | pos = dgram->buf; |
| 5264 | end = pos + dgram->len; |
| 5265 | do { |
| 5266 | int ret; |
| 5267 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5268 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5269 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
| 5270 | if (!pkt) |
| 5271 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5272 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5273 | quic_rx_packet_refinc(pkt); |
| 5274 | ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram); |
| 5275 | first_pkt = 0; |
| 5276 | pos += pkt->len; |
| 5277 | quic_rx_packet_refdec(pkt); |
| 5278 | if (ret == -1) |
| 5279 | /* If the packet length could not be found, we cannot continue. */ |
| 5280 | break; |
| 5281 | } while (pos < end); |
| 5282 | |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5283 | /* Increasing the received bytes counter by the UDP datagram length |
| 5284 | * if this datagram could be associated to a connection. |
| 5285 | */ |
| 5286 | if (dgram->qc) |
| 5287 | dgram->qc->rx.bytes += dgram->len; |
Frédéric Lécaille | 841bf5e | 2022-02-02 09:41:27 +0100 | [diff] [blame] | 5288 | |
| 5289 | /* Mark this datagram as consumed */ |
| 5290 | HA_ATOMIC_STORE(&dgram->buf, NULL); |
Frédéric Lécaille | df1c7c7 | 2022-01-28 15:38:52 +0100 | [diff] [blame] | 5291 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5292 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5293 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5294 | |
| 5295 | err: |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5296 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5297 | } |
| 5298 | |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5299 | /* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet. |
| 5300 | * Returns 1 if succeeded, 0 if not. |
| 5301 | */ |
| 5302 | static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end, |
| 5303 | unsigned char **dcid, size_t *dcid_len) |
| 5304 | { |
| 5305 | int long_header; |
| 5306 | size_t minlen, skip; |
| 5307 | |
| 5308 | if (!(*buf & QUIC_PACKET_FIXED_BIT)) |
| 5309 | goto err; |
| 5310 | |
| 5311 | long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT; |
| 5312 | minlen = long_header ? |
| 5313 | QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN; |
| 5314 | skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF; |
Frédéric Lécaille | bfa3236 | 2022-02-02 10:44:36 +0100 | [diff] [blame] | 5315 | if (end - buf <= minlen) |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5316 | goto err; |
| 5317 | |
| 5318 | buf += skip; |
| 5319 | *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN; |
| 5320 | if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len) |
| 5321 | goto err; |
| 5322 | |
| 5323 | *dcid = buf; |
| 5324 | |
| 5325 | return 1; |
| 5326 | |
| 5327 | err: |
| 5328 | TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT); |
| 5329 | return 0; |
| 5330 | } |
| 5331 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 5332 | /* Retrieve the DCID from the datagram found in <buf> and deliver it to the |
| 5333 | * correct datagram handler. |
| 5334 | * Return 1 if a correct datagram could be found, 0 if not. |
| 5335 | */ |
| 5336 | int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner, |
| 5337 | struct sockaddr_storage *saddr, |
| 5338 | struct quic_dgram *new_dgram, struct list *dgrams) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5339 | { |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5340 | struct quic_dgram *dgram; |
| 5341 | unsigned char *dcid; |
| 5342 | size_t dcid_len; |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5343 | int cid_tid; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5344 | |
| 5345 | if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len)) |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5346 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5347 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 5348 | dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5349 | if (!dgram) |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5350 | goto err; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5351 | |
Amaury Denoyelle | f6dcbce | 2022-02-08 15:05:58 +0100 | [diff] [blame] | 5352 | cid_tid = quic_get_cid_tid(dcid); |
| 5353 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5354 | /* All the members must be initialized! */ |
| 5355 | dgram->owner = owner; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5356 | dgram->buf = buf; |
| 5357 | dgram->len = len; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5358 | dgram->dcid = dcid; |
| 5359 | dgram->dcid_len = dcid_len; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5360 | dgram->saddr = *saddr; |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5361 | dgram->qc = NULL; |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5362 | LIST_APPEND(dgrams, &dgram->list); |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5363 | MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5364 | |
Amaury Denoyelle | 8524f0f | 2022-02-08 15:03:40 +0100 | [diff] [blame] | 5365 | tasklet_wakeup(quic_dghdlrs[cid_tid].task); |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5366 | |
Frédéric Lécaille | 25bc887 | 2022-01-27 09:15:40 +0100 | [diff] [blame] | 5367 | return 1; |
| 5368 | |
| 5369 | err: |
Frédéric Lécaille | 3d4bfe7 | 2022-01-26 16:07:16 +0100 | [diff] [blame] | 5370 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5371 | } |
| 5372 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5373 | /* Function to automatically activate QUIC traces on stdout. |
| 5374 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 5375 | * Main use for now is in the docker image for QUIC interop testing. |
| 5376 | */ |
| 5377 | static void quic_init_stdout_traces(void) |
| 5378 | { |
| 5379 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 5380 | trace_quic.sink = sink_find("stdout"); |
| 5381 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5382 | trace_quic.state = TRACE_STATE_RUNNING; |
| 5383 | #endif |
| 5384 | } |
| 5385 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 5386 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5387 | /* |
| 5388 | * Local variables: |
| 5389 | * c-indent-level: 8 |
| 5390 | * c-basic-offset: 8 |
| 5391 | * End: |
| 5392 | */ |