Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QUIC transport layer over SOCK_DGRAM sockets. |
| 3 | * |
| 4 | * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define _GNU_SOURCE |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <netinet/tcp.h> |
| 24 | |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 25 | #include <import/ebmbtree.h> |
| 26 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 27 | #include <haproxy/buf-t.h> |
| 28 | #include <haproxy/compat.h> |
| 29 | #include <haproxy/api.h> |
| 30 | #include <haproxy/debug.h> |
| 31 | #include <haproxy/tools.h> |
| 32 | #include <haproxy/ticks.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 33 | |
| 34 | #include <haproxy/connection.h> |
| 35 | #include <haproxy/fd.h> |
| 36 | #include <haproxy/freq_ctr.h> |
| 37 | #include <haproxy/global.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 38 | #include <haproxy/h3.h> |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 39 | #include <haproxy/hq_interop.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 40 | #include <haproxy/log.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 41 | #include <haproxy/mux_quic.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 42 | #include <haproxy/pipe.h> |
| 43 | #include <haproxy/proxy.h> |
| 44 | #include <haproxy/quic_cc.h> |
| 45 | #include <haproxy/quic_frame.h> |
| 46 | #include <haproxy/quic_loss.h> |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 47 | #include <haproxy/cbuf.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 48 | #include <haproxy/quic_tls.h> |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 49 | #include <haproxy/sink.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 50 | #include <haproxy/ssl_sock.h> |
| 51 | #include <haproxy/stream_interface.h> |
| 52 | #include <haproxy/task.h> |
| 53 | #include <haproxy/trace.h> |
| 54 | #include <haproxy/xprt_quic.h> |
| 55 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 56 | /* list of supported QUIC versions by this implementation */ |
| 57 | static int quic_supported_version[] = { |
| 58 | 0x00000001, |
Frédéric Lécaille | 56d3e1b | 2021-11-19 14:32:52 +0100 | [diff] [blame] | 59 | 0xff00001d, /* draft-29 */ |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 60 | |
| 61 | /* placeholder, do not add entry after this */ |
| 62 | 0x0 |
| 63 | }; |
| 64 | |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 65 | /* This is the values of some QUIC transport parameters when absent. |
| 66 | * Should be used to initialize any transport parameters (local or remote) |
| 67 | * before updating them with customized values. |
| 68 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 69 | struct quic_transport_params quic_dflt_transport_params = { |
Frédéric Lécaille | 46be7e9 | 2021-10-22 15:04:27 +0200 | [diff] [blame] | 70 | .max_udp_payload_size = QUIC_PACKET_MAXLEN, |
Frédéric Lécaille | 785c9c9 | 2021-05-17 16:42:21 +0200 | [diff] [blame] | 71 | .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT, |
| 72 | .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY, |
Frédéric Lécaille | 48fc74a | 2021-09-03 16:42:19 +0200 | [diff] [blame] | 73 | .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /* trace source and events */ |
| 77 | static void quic_trace(enum trace_level level, uint64_t mask, \ |
| 78 | const struct trace_source *src, |
| 79 | const struct ist where, const struct ist func, |
| 80 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 81 | |
| 82 | static const struct trace_event quic_trace_events[] = { |
| 83 | { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" }, |
| 84 | { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" }, |
| 85 | { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" }, |
| 86 | { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" }, |
| 87 | { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" }, |
| 88 | { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" }, |
| 89 | { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" }, |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 90 | { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 91 | { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" }, |
| 92 | { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" }, |
| 93 | { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" }, |
| 94 | { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" }, |
| 95 | { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" }, |
| 96 | { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" }, |
| 97 | { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" }, |
| 98 | { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" }, |
| 99 | { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" }, |
| 100 | { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" }, |
| 101 | { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" }, |
| 102 | { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" }, |
| 103 | { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" }, |
| 104 | { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" }, |
| 105 | { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" }, |
| 106 | { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"}, |
| 107 | { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"}, |
| 108 | { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"}, |
| 109 | { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"}, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 110 | { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" }, |
| 111 | { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" }, |
| 112 | { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" }, |
| 113 | { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" }, |
| 114 | { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" }, |
| 115 | { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" }, |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 116 | { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" }, |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 117 | { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" }, |
| 118 | { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 119 | { /* end */ } |
| 120 | }; |
| 121 | |
| 122 | static const struct name_desc quic_trace_lockon_args[4] = { |
| 123 | /* arg1 */ { /* already used by the connection */ }, |
| 124 | /* arg2 */ { .name="quic", .desc="QUIC transport" }, |
| 125 | /* arg3 */ { }, |
| 126 | /* arg4 */ { } |
| 127 | }; |
| 128 | |
| 129 | static const struct name_desc quic_trace_decoding[] = { |
| 130 | #define QUIC_VERB_CLEAN 1 |
| 131 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 132 | { /* end */ } |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | struct trace_source trace_quic = { |
| 137 | .name = IST("quic"), |
| 138 | .desc = "QUIC xprt", |
| 139 | .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */ |
| 140 | .default_cb = quic_trace, |
| 141 | .known_events = quic_trace_events, |
| 142 | .lockon_args = quic_trace_lockon_args, |
| 143 | .decoding = quic_trace_decoding, |
| 144 | .report_events = ~0, /* report everything by default */ |
| 145 | }; |
| 146 | |
| 147 | #define TRACE_SOURCE &trace_quic |
| 148 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 149 | |
| 150 | static BIO_METHOD *ha_quic_meth; |
| 151 | |
Frédéric Lécaille | dbe25af | 2021-08-04 15:27:37 +0200 | [diff] [blame] | 152 | DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ); |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 153 | DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 154 | 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] | 155 | DECLARE_STATIC_POOL(pool_head_quic_conn_ctx, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 156 | "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 157 | 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] | 158 | DECLARE_POOL(pool_head_quic_connection_id, |
| 159 | "quic_connnection_id_pool", sizeof(struct quic_connection_id)); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 160 | 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] | 161 | 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] | 162 | 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] | 163 | 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] | 164 | 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] | 165 | 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] | 166 | 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] | 167 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 168 | 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] | 169 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 170 | struct quic_conn *qc, size_t dglen, int pkt_type, |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 171 | int padding, int ack, int nb_pto_dgrams, int cc, int *err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 172 | |
| 173 | /* Add traces to <buf> depending on <frm> TX frame type. */ |
| 174 | static inline void chunk_tx_frm_appendf(struct buffer *buf, |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 175 | const struct quic_frame *frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 176 | { |
| 177 | switch (frm->type) { |
| 178 | case QUIC_FT_CRYPTO: |
| 179 | chunk_appendf(buf, " cfoff=%llu cflen=%llu", |
| 180 | (unsigned long long)frm->crypto.offset, |
| 181 | (unsigned long long)frm->crypto.len); |
| 182 | break; |
| 183 | default: |
| 184 | chunk_appendf(buf, " %s", quic_frame_type_string(frm->type)); |
| 185 | } |
| 186 | } |
| 187 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 188 | /* Only for debug purpose */ |
| 189 | struct enc_debug_info { |
| 190 | unsigned char *payload; |
| 191 | size_t payload_len; |
| 192 | unsigned char *aad; |
| 193 | size_t aad_len; |
| 194 | uint64_t pn; |
| 195 | }; |
| 196 | |
| 197 | /* Initializes a enc_debug_info struct (only for debug purpose) */ |
| 198 | static inline void enc_debug_info_init(struct enc_debug_info *edi, |
| 199 | unsigned char *payload, size_t payload_len, |
| 200 | unsigned char *aad, size_t aad_len, uint64_t pn) |
| 201 | { |
| 202 | edi->payload = payload; |
| 203 | edi->payload_len = payload_len; |
| 204 | edi->aad = aad; |
| 205 | edi->aad_len = aad_len; |
| 206 | edi->pn = pn; |
| 207 | } |
| 208 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 209 | /* Trace callback for QUIC. |
| 210 | * These traces always expect that arg1, if non-null, is of type connection. |
| 211 | */ |
| 212 | static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 213 | const struct ist where, const struct ist func, |
| 214 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 215 | { |
| 216 | const struct connection *conn = a1; |
| 217 | |
| 218 | if (conn) { |
| 219 | struct quic_tls_secrets *secs; |
| 220 | struct quic_conn *qc; |
| 221 | |
| 222 | qc = conn->qc; |
| 223 | chunk_appendf(&trace_buf, " : conn@%p", conn); |
| 224 | if ((mask & QUIC_EV_CONN_INIT) && qc) { |
| 225 | chunk_appendf(&trace_buf, "\n odcid"); |
| 226 | quic_cid_dump(&trace_buf, &qc->odcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 227 | chunk_appendf(&trace_buf, "\n dcid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 228 | quic_cid_dump(&trace_buf, &qc->dcid); |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 229 | chunk_appendf(&trace_buf, "\n scid"); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 230 | quic_cid_dump(&trace_buf, &qc->scid); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | if (mask & QUIC_EV_CONN_ADDDATA) { |
| 234 | const enum ssl_encryption_level_t *level = a2; |
| 235 | const size_t *len = a3; |
| 236 | |
| 237 | if (level) { |
| 238 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 239 | |
| 240 | chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl); |
| 241 | } |
| 242 | if (len) |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 243 | 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] | 244 | } |
| 245 | if ((mask & QUIC_EV_CONN_ISEC) && qc) { |
| 246 | /* Initial read & write secrets. */ |
| 247 | enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL; |
| 248 | const unsigned char *rx_sec = a2; |
| 249 | const unsigned char *tx_sec = a3; |
| 250 | |
| 251 | secs = &qc->els[level].tls_ctx.rx; |
| 252 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 253 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level)); |
| 254 | if (rx_sec) |
| 255 | quic_tls_secret_hexdump(&trace_buf, rx_sec, 32); |
| 256 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 257 | } |
| 258 | secs = &qc->els[level].tls_ctx.tx; |
| 259 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) { |
| 260 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level)); |
| 261 | if (tx_sec) |
| 262 | quic_tls_secret_hexdump(&trace_buf, tx_sec, 32); |
| 263 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 264 | } |
| 265 | } |
| 266 | if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) { |
| 267 | const enum ssl_encryption_level_t *level = a2; |
| 268 | const unsigned char *secret = a3; |
| 269 | const size_t *secret_len = a4; |
| 270 | |
| 271 | if (level) { |
| 272 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 273 | |
| 274 | chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl)); |
| 275 | if (secret && secret_len) |
| 276 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 277 | secs = &qc->els[lvl].tls_ctx.rx; |
| 278 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 279 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) { |
| 284 | const enum ssl_encryption_level_t *level = a2; |
| 285 | const unsigned char *secret = a3; |
| 286 | const size_t *secret_len = a4; |
| 287 | |
| 288 | if (level) { |
| 289 | enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level); |
| 290 | |
| 291 | chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl)); |
| 292 | if (secret && secret_len) |
| 293 | quic_tls_secret_hexdump(&trace_buf, secret, *secret_len); |
| 294 | secs = &qc->els[lvl].tls_ctx.tx; |
| 295 | if (secs->flags & QUIC_FL_TLS_SECRETS_SET) |
| 296 | quic_tls_keys_hexdump(&trace_buf, secs); |
| 297 | } |
| 298 | |
| 299 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 300 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 301 | 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] | 302 | const struct quic_tx_packet *pkt = a2; |
| 303 | const struct quic_enc_level *qel = a3; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 304 | const ssize_t *room = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 305 | |
| 306 | if (qel) { |
| 307 | struct quic_pktns *pktns; |
| 308 | |
| 309 | pktns = qc->pktns; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 310 | chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu " |
| 311 | "if=%llu pp=%u pdg=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 312 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 313 | (unsigned long long)qc->path->cwnd, |
| 314 | (unsigned long long)qc->path->prep_in_flight, |
| 315 | (unsigned long long)qc->path->in_flight, |
| 316 | (unsigned long long)pktns->tx.in_flight, |
| 317 | pktns->tx.pto_probe, qc->tx.nb_pto_dgrams); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 318 | } |
| 319 | if (pkt) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 320 | const struct quic_frame *frm; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 321 | chunk_appendf(&trace_buf, " pn=%llu cdlen=%u", |
| 322 | (unsigned long long)pkt->pn_node.key, pkt->cdata_len); |
| 323 | list_for_each_entry(frm, &pkt->frms, list) |
| 324 | chunk_tx_frm_appendf(&trace_buf, frm); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 325 | chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes); |
| 326 | } |
| 327 | |
| 328 | if (room) { |
| 329 | chunk_appendf(&trace_buf, " room=%lld", (long long)*room); |
| 330 | chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu", |
| 331 | (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] | 332 | } |
| 333 | } |
| 334 | |
| 335 | if (mask & QUIC_EV_CONN_HDSHK) { |
| 336 | const enum quic_handshake_state *state = a2; |
| 337 | const int *err = a3; |
| 338 | |
| 339 | if (state) |
| 340 | chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state)); |
| 341 | if (err) |
| 342 | chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err)); |
| 343 | } |
| 344 | |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 345 | 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] | 346 | const struct quic_rx_packet *pkt = a2; |
| 347 | const unsigned long *pktlen = a3; |
| 348 | const SSL *ssl = a4; |
| 349 | |
| 350 | if (pkt) { |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 351 | chunk_appendf(&trace_buf, " pkt@%p el=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 352 | pkt, quic_packet_type_enc_level_char(pkt->type)); |
| 353 | if (pkt->pnl) |
| 354 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl, |
| 355 | (unsigned long long)pkt->pn); |
| 356 | if (pkt->token_len) |
| 357 | chunk_appendf(&trace_buf, " toklen=%llu", |
| 358 | (unsigned long long)pkt->token_len); |
| 359 | if (pkt->aad_len) |
| 360 | chunk_appendf(&trace_buf, " aadlen=%llu", |
| 361 | (unsigned long long)pkt->aad_len); |
| 362 | chunk_appendf(&trace_buf, " flags=0x%x len=%llu", |
| 363 | pkt->flags, (unsigned long long)pkt->len); |
| 364 | } |
| 365 | if (pktlen) |
| 366 | chunk_appendf(&trace_buf, " (%ld)", *pktlen); |
| 367 | if (ssl) { |
| 368 | enum ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 369 | chunk_appendf(&trace_buf, " el=%c", |
| 370 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) { |
| 375 | const struct quic_rx_packet *pkt = a2; |
| 376 | const struct quic_rx_crypto_frm *cf = a3; |
| 377 | const SSL *ssl = a4; |
| 378 | |
| 379 | if (pkt) |
| 380 | chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt, |
| 381 | quic_packet_type_enc_level_char(pkt->type), |
| 382 | (unsigned long long)pkt->pn); |
| 383 | if (cf) |
| 384 | chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu", |
| 385 | (unsigned long long)cf->offset_node.key, |
| 386 | (unsigned long long)cf->len); |
| 387 | if (ssl) { |
| 388 | 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] | 389 | chunk_appendf(&trace_buf, " rel=%c", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 390 | quic_enc_level_char(ssl_to_quic_enc_level(level))); |
| 391 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 392 | |
| 393 | if (qc->err_code) |
| 394 | chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)conn->qc->err_code); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) { |
| 398 | const struct quic_frame *frm = a2; |
| 399 | |
| 400 | if (frm) |
| 401 | chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type)); |
| 402 | } |
| 403 | |
| 404 | if (mask & QUIC_EV_CONN_PHPKTS) { |
| 405 | const struct quic_enc_level *qel = a2; |
| 406 | |
| 407 | if (qel) { |
| 408 | struct quic_pktns *pktns; |
| 409 | |
| 410 | pktns = qc->pktns; |
| 411 | chunk_appendf(&trace_buf, |
Frédéric Lécaille | 546186b | 2021-08-03 14:25:36 +0200 | [diff] [blame] | 412 | " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u pdg=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 413 | quic_enc_level_char_from_qel(qel, qc), |
Frédéric Lécaille | 546186b | 2021-08-03 14:25:36 +0200 | [diff] [blame] | 414 | quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)), |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 415 | !!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_PKTNS_ACK_REQUIRED), |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 416 | (unsigned long long)qc->path->cwnd, |
| 417 | (unsigned long long)qc->path->prep_in_flight, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 418 | (unsigned long long)qc->path->in_flight, |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 419 | (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe, |
| 420 | (unsigned long long)qc->tx.nb_pto_dgrams); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 424 | if (mask & QUIC_EV_CONN_ENCPKT) { |
| 425 | const struct enc_debug_info *edi = a2; |
| 426 | |
| 427 | if (edi) |
| 428 | chunk_appendf(&trace_buf, |
| 429 | " payload=@%p payload_len=%llu" |
| 430 | " aad=@%p aad_len=%llu pn=%llu", |
| 431 | edi->payload, (unsigned long long)edi->payload_len, |
| 432 | edi->aad, (unsigned long long)edi->aad_len, |
| 433 | (unsigned long long)edi->pn); |
| 434 | } |
| 435 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 436 | if (mask & QUIC_EV_CONN_RMHP) { |
| 437 | const struct quic_rx_packet *pkt = a2; |
| 438 | |
| 439 | if (pkt) { |
| 440 | const int *ret = a3; |
| 441 | |
| 442 | chunk_appendf(&trace_buf, " pkt@%p", pkt); |
| 443 | if (ret && *ret) |
| 444 | chunk_appendf(&trace_buf, " pnl=%u pn=%llu", |
| 445 | pkt->pnl, (unsigned long long)pkt->pn); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | if (mask & QUIC_EV_CONN_PRSAFRM) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 450 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 451 | const unsigned long *val1 = a3; |
| 452 | const unsigned long *val2 = a4; |
| 453 | |
| 454 | if (frm) |
| 455 | chunk_tx_frm_appendf(&trace_buf, frm); |
| 456 | if (val1) |
| 457 | chunk_appendf(&trace_buf, " %lu", *val1); |
| 458 | if (val2) |
| 459 | chunk_appendf(&trace_buf, "..%lu", *val2); |
| 460 | } |
| 461 | |
| 462 | if (mask & QUIC_EV_CONN_RTTUPDT) { |
| 463 | const unsigned int *rtt_sample = a2; |
| 464 | const unsigned int *ack_delay = a3; |
| 465 | const struct quic_loss *ql = a4; |
| 466 | |
| 467 | if (rtt_sample) |
| 468 | chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample); |
| 469 | if (ack_delay) |
| 470 | chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay); |
| 471 | if (ql) |
| 472 | chunk_appendf(&trace_buf, |
| 473 | " srtt=%ums rttvar=%ums min_rtt=%ums", |
| 474 | ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min); |
| 475 | } |
| 476 | if (mask & QUIC_EV_CONN_CC) { |
| 477 | const struct quic_cc_event *ev = a2; |
| 478 | const struct quic_cc *cc = a3; |
| 479 | |
| 480 | if (a2) |
| 481 | quic_cc_event_trace(&trace_buf, ev); |
| 482 | if (a3) |
| 483 | quic_cc_state_trace(&trace_buf, cc); |
| 484 | } |
| 485 | |
| 486 | if (mask & QUIC_EV_CONN_PKTLOSS) { |
| 487 | const struct quic_pktns *pktns = a2; |
| 488 | const struct list *lost_pkts = a3; |
| 489 | struct quic_conn *qc = conn->qc; |
| 490 | |
| 491 | if (pktns) { |
| 492 | chunk_appendf(&trace_buf, " pktns=%s", |
| 493 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 494 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H"); |
| 495 | if (pktns->tx.loss_time) |
| 496 | chunk_appendf(&trace_buf, " loss_time=%dms", |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 497 | 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] | 498 | } |
| 499 | if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) { |
| 500 | struct quic_tx_packet *pkt; |
| 501 | |
| 502 | chunk_appendf(&trace_buf, " lost_pkts:"); |
| 503 | list_for_each_entry(pkt, lost_pkts, list) |
| 504 | chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) { |
| 509 | struct quic_conn *qc = conn->qc; |
| 510 | const struct quic_pktns *pktns = a2; |
| 511 | const int *duration = a3; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 512 | const uint64_t *ifae_pkts = a4; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 513 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 514 | if (ifae_pkts) |
| 515 | chunk_appendf(&trace_buf, " ifae_pkts=%llu", |
| 516 | (unsigned long long)*ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 517 | if (pktns) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 518 | chunk_appendf(&trace_buf, " pktns=%s pp=%d", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 519 | pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 520 | pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
| 521 | pktns->tx.pto_probe); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 522 | if (mask & QUIC_EV_CONN_STIMER) { |
| 523 | if (pktns->tx.loss_time) |
| 524 | chunk_appendf(&trace_buf, " loss_time=%dms", |
| 525 | TICKS_TO_MS(pktns->tx.loss_time - now_ms)); |
| 526 | } |
| 527 | if (mask & QUIC_EV_CONN_SPTO) { |
| 528 | if (pktns->tx.time_of_last_eliciting) |
| 529 | chunk_appendf(&trace_buf, " tole=%dms", |
| 530 | TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms)); |
| 531 | if (duration) |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 532 | 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] | 533 | } |
| 534 | } |
| 535 | |
| 536 | if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) { |
| 537 | chunk_appendf(&trace_buf, |
| 538 | " expire=%dms", TICKS_TO_MS(qc->timer - now_ms)); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (mask & QUIC_EV_CONN_SPPKTS) { |
| 543 | const struct quic_tx_packet *pkt = a2; |
| 544 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 545 | chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu", |
| 546 | (unsigned long long)qc->path->cwnd, |
| 547 | (unsigned long long)qc->path->prep_in_flight, |
| 548 | (unsigned long long)qc->path->in_flight); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 549 | if (pkt) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 550 | chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu", |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 551 | (unsigned long)pkt->pn_node.key, |
| 552 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" : |
| 553 | pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H", |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 554 | (unsigned long long)pkt->in_flight_len, |
| 555 | (unsigned long long)pkt->cdata_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 556 | } |
| 557 | } |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 558 | |
| 559 | if (mask & QUIC_EV_CONN_SSLALERT) { |
| 560 | const uint8_t *alert = a2; |
| 561 | const enum ssl_encryption_level_t *level = a3; |
| 562 | |
| 563 | if (alert) |
| 564 | chunk_appendf(&trace_buf, " alert=0x%02x", *alert); |
| 565 | if (level) |
| 566 | chunk_appendf(&trace_buf, " el=%c", |
| 567 | quic_enc_level_char(ssl_to_quic_enc_level(*level))); |
| 568 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 569 | |
| 570 | if (mask & QUIC_EV_CONN_BCFRMS) { |
| 571 | const size_t *sz1 = a2; |
| 572 | const size_t *sz2 = a3; |
| 573 | const size_t *sz3 = a4; |
| 574 | |
| 575 | if (sz1) |
| 576 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1); |
| 577 | if (sz2) |
| 578 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2); |
| 579 | if (sz3) |
| 580 | chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3); |
| 581 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 582 | |
| 583 | if (mask & QUIC_EV_CONN_PSTRM) { |
| 584 | const struct quic_frame *frm = a2; |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 585 | |
| 586 | if (a2) { |
| 587 | const struct quic_stream *s = &frm->stream; |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 588 | |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 589 | chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu", |
| 590 | !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT), |
| 591 | !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT), |
| 592 | (unsigned long long)s->id, |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 593 | (unsigned long long)s->offset.key, |
Frédéric Lécaille | 577fe48 | 2021-01-11 15:10:06 +0100 | [diff] [blame] | 594 | (unsigned long long)s->len); |
| 595 | } |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 596 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 597 | } |
| 598 | if (mask & QUIC_EV_CONN_LPKT) { |
| 599 | const struct quic_rx_packet *pkt = a2; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 600 | const uint64_t *len = a3; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 601 | |
| 602 | if (conn) |
Frédéric Lécaille | 2e7ffc9 | 2021-06-10 08:18:45 +0200 | [diff] [blame] | 603 | chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 604 | if (pkt) |
Frédéric Lécaille | 2e7ffc9 | 2021-06-10 08:18:45 +0200 | [diff] [blame] | 605 | chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p", |
| 606 | pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc); |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 607 | if (len) |
| 608 | chunk_appendf(&trace_buf, " len=%llu", (ull)*len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | } |
| 612 | |
| 613 | /* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 614 | static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 615 | { |
| 616 | struct quic_conn *qc; |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 617 | struct quic_pktns *hdshk_pktns, *app_pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 618 | |
| 619 | qc = ctx->conn->qc; |
| 620 | if (objt_server(qc->conn->target)) |
| 621 | return 1; |
| 622 | |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 623 | hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns; |
| 624 | app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns; |
| 625 | if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) || |
| 626 | (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] | 627 | HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 628 | return 1; |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | /* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for |
| 634 | * both loss detection and PTO and schedule the task assiated to this timer if needed. |
| 635 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 636 | static inline void qc_set_timer(struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 637 | { |
| 638 | struct quic_conn *qc; |
| 639 | struct quic_pktns *pktns; |
| 640 | unsigned int pto; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 641 | int handshake_complete; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 642 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 643 | TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn, |
| 644 | NULL, NULL, &ctx->conn->qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 645 | qc = ctx->conn->qc; |
| 646 | pktns = quic_loss_pktns(qc); |
| 647 | if (tick_isset(pktns->tx.loss_time)) { |
| 648 | qc->timer = pktns->tx.loss_time; |
| 649 | goto out; |
| 650 | } |
| 651 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 652 | /* anti-amplification: the timer must be |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 653 | * cancelled for a server which reached the anti-amplification limit. |
| 654 | */ |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 655 | if (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) { |
| 656 | TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, ctx->conn); |
| 657 | qc->timer = TICK_ETERNITY; |
| 658 | goto out; |
| 659 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 660 | |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 661 | if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) { |
| 662 | TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 663 | /* Timer cancellation. */ |
| 664 | qc->timer = TICK_ETERNITY; |
| 665 | goto out; |
| 666 | } |
| 667 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 668 | handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE; |
| 669 | pktns = quic_pto_pktns(qc, handshake_complete, &pto); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 670 | if (tick_isset(pto)) |
| 671 | qc->timer = pto; |
| 672 | out: |
Amaury Denoyelle | 336f6fd | 2021-10-05 14:42:25 +0200 | [diff] [blame] | 673 | if (qc->timer != TICK_ETERNITY) |
| 674 | task_schedule(qc->timer_task, qc->timer); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 675 | TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns); |
| 676 | } |
| 677 | |
| 678 | #ifndef OPENSSL_IS_BORINGSSL |
| 679 | int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level, |
| 680 | const uint8_t *read_secret, |
| 681 | const uint8_t *write_secret, size_t secret_len) |
| 682 | { |
| 683 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 684 | struct quic_tls_ctx *tls_ctx = |
| 685 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 686 | const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 687 | struct quic_tls_secrets *rx, *tx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 688 | |
| 689 | TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 690 | BUG_ON(secret_len > QUIC_TLS_SECRET_LEN); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 691 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 692 | TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, conn); |
| 693 | goto out; |
| 694 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 695 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 696 | if (!quic_tls_ctx_keys_alloc(tls_ctx)) { |
| 697 | TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, conn); |
| 698 | goto err; |
| 699 | } |
| 700 | |
| 701 | rx = &tls_ctx->rx; |
| 702 | tx = &tls_ctx->tx; |
| 703 | |
| 704 | rx->aead = tx->aead = tls_aead(cipher); |
| 705 | rx->md = tx->md = tls_md(cipher); |
| 706 | rx->hp = tx->hp = tls_hp(cipher); |
| 707 | |
| 708 | if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen, |
| 709 | 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] | 710 | read_secret, secret_len)) { |
| 711 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 712 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 713 | } |
| 714 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 715 | rx->flags |= QUIC_FL_TLS_SECRETS_SET; |
| 716 | if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen, |
| 717 | 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] | 718 | write_secret, secret_len)) { |
| 719 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn); |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 720 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 721 | } |
| 722 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 723 | tx->flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 724 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 725 | const unsigned char *buf; |
| 726 | size_t buflen; |
| 727 | |
| 728 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 729 | if (!buflen) |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 730 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 731 | |
| 732 | if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen)) |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 733 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 734 | } |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 735 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 736 | TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 737 | return 1; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 738 | |
| 739 | err: |
| 740 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, conn); |
| 741 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 742 | } |
| 743 | #else |
| 744 | /* ->set_read_secret callback to derive the RX secrets at <level> encryption |
| 745 | * level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 746 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 747 | */ |
| 748 | int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 749 | const SSL_CIPHER *cipher, |
| 750 | const uint8_t *secret, size_t secret_len) |
| 751 | { |
| 752 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 753 | struct quic_tls_ctx *tls_ctx = |
| 754 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 755 | |
| 756 | TRACE_ENTER(QUIC_EV_CONN_RSEC, conn); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 757 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 758 | TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, conn); |
| 759 | goto out; |
| 760 | } |
| 761 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 762 | tls_ctx->rx.aead = tls_aead(cipher); |
| 763 | tls_ctx->rx.md = tls_md(cipher); |
| 764 | tls_ctx->rx.hp = tls_hp(cipher); |
| 765 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 766 | if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key))) |
| 767 | goto err; |
| 768 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 769 | 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] | 770 | tls_ctx->rx.key, tls_ctx->rx.keylen, |
| 771 | tls_ctx->rx.iv, tls_ctx->rx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 772 | tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key, |
| 773 | secret, secret_len)) { |
| 774 | TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn); |
| 775 | goto err; |
| 776 | } |
| 777 | |
| 778 | if (objt_server(conn->target) && level == ssl_encryption_application) { |
| 779 | const unsigned char *buf; |
| 780 | size_t buflen; |
| 781 | |
| 782 | SSL_get_peer_quic_transport_params(ssl, &buf, &buflen); |
| 783 | if (!buflen) |
| 784 | goto err; |
| 785 | |
| 786 | if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen)) |
| 787 | goto err; |
| 788 | } |
| 789 | |
| 790 | tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 791 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 792 | TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len); |
| 793 | |
| 794 | return 1; |
| 795 | |
| 796 | err: |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 797 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | /* ->set_write_secret callback to derive the TX secrets at <level> |
| 802 | * encryption level. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 803 | * Returns 1 if succeeded, 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 804 | */ |
| 805 | int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level, |
| 806 | const SSL_CIPHER *cipher, |
| 807 | const uint8_t *secret, size_t secret_len) |
| 808 | { |
| 809 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 810 | struct quic_tls_ctx *tls_ctx = |
| 811 | &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx; |
| 812 | |
| 813 | TRACE_ENTER(QUIC_EV_CONN_WSEC, conn); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 814 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 815 | TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, conn); |
| 816 | goto out; |
| 817 | } |
| 818 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 819 | if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key))) |
| 820 | goto err; |
| 821 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 822 | tls_ctx->tx.aead = tls_aead(cipher); |
| 823 | tls_ctx->tx.md = tls_md(cipher); |
| 824 | tls_ctx->tx.hp = tls_hp(cipher); |
| 825 | |
| 826 | 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] | 827 | tls_ctx->tx.key, tls_ctx->tx.keylen, |
| 828 | tls_ctx->tx.iv, tls_ctx->tx.ivlen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 829 | tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key, |
| 830 | secret, secret_len)) { |
| 831 | TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn); |
| 832 | goto err; |
| 833 | } |
| 834 | |
| 835 | tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET; |
| 836 | TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 837 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 838 | return 1; |
| 839 | |
| 840 | err: |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 841 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 842 | return 0; |
| 843 | } |
| 844 | #endif |
| 845 | |
| 846 | /* This function copies the CRYPTO data provided by the TLS stack found at <data> |
| 847 | * with <len> as size in CRYPTO buffers dedicated to store the information about |
| 848 | * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams. |
| 849 | * It fails only if it could not managed to allocate enough CRYPTO buffers to |
| 850 | * store all the data. |
| 851 | * Note that CRYPTO data may exist at any encryption level except at 0-RTT. |
| 852 | */ |
| 853 | static int quic_crypto_data_cpy(struct quic_enc_level *qel, |
| 854 | const unsigned char *data, size_t len) |
| 855 | { |
| 856 | struct quic_crypto_buf **qcb; |
| 857 | /* The remaining byte to store in CRYPTO buffers. */ |
| 858 | size_t cf_offset, cf_len, *nb_buf; |
| 859 | unsigned char *pos; |
| 860 | |
| 861 | nb_buf = &qel->tx.crypto.nb_buf; |
| 862 | qcb = &qel->tx.crypto.bufs[*nb_buf - 1]; |
| 863 | cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz; |
| 864 | cf_len = len; |
| 865 | |
| 866 | while (len) { |
| 867 | size_t to_copy, room; |
| 868 | |
| 869 | pos = (*qcb)->data + (*qcb)->sz; |
| 870 | room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz; |
| 871 | to_copy = len > room ? room : len; |
| 872 | if (to_copy) { |
| 873 | memcpy(pos, data, to_copy); |
| 874 | /* Increment the total size of this CRYPTO buffers by <to_copy>. */ |
| 875 | qel->tx.crypto.sz += to_copy; |
| 876 | (*qcb)->sz += to_copy; |
| 877 | pos += to_copy; |
| 878 | len -= to_copy; |
| 879 | data += to_copy; |
| 880 | } |
| 881 | else { |
| 882 | struct quic_crypto_buf **tmp; |
| 883 | |
| 884 | tmp = realloc(qel->tx.crypto.bufs, |
| 885 | (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs); |
| 886 | if (tmp) { |
| 887 | qel->tx.crypto.bufs = tmp; |
| 888 | qcb = &qel->tx.crypto.bufs[*nb_buf]; |
| 889 | *qcb = pool_alloc(pool_head_quic_crypto_buf); |
| 890 | if (!*qcb) |
| 891 | return 0; |
| 892 | |
| 893 | (*qcb)->sz = 0; |
| 894 | ++*nb_buf; |
| 895 | } |
| 896 | else { |
| 897 | break; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | /* Allocate a TX CRYPTO frame only if all the CRYPTO data |
| 903 | * have been buffered. |
| 904 | */ |
| 905 | if (!len) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 906 | struct quic_frame *frm; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 907 | |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 908 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 909 | if (!frm) |
| 910 | return 0; |
| 911 | |
| 912 | frm->type = QUIC_FT_CRYPTO; |
| 913 | frm->crypto.offset = cf_offset; |
| 914 | frm->crypto.len = cf_len; |
Frédéric Lécaille | f252adb | 2021-08-03 17:01:25 +0200 | [diff] [blame] | 915 | frm->crypto.qel = qel; |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 916 | MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | return len == 0; |
| 920 | } |
| 921 | |
| 922 | |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 923 | /* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */ |
| 924 | void quic_set_tls_alert(struct quic_conn *qc, int alert) |
| 925 | { |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 926 | 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] | 927 | HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
| 928 | TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc->conn); |
| 929 | } |
| 930 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 931 | /* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it |
| 932 | * wants to provide the QUIC layer with CRYPTO data. |
| 933 | * Returns 1 if succeeded, 0 if not. |
| 934 | */ |
| 935 | int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 936 | const uint8_t *data, size_t len) |
| 937 | { |
| 938 | struct connection *conn; |
| 939 | enum quic_tls_enc_level tel; |
| 940 | struct quic_enc_level *qel; |
| 941 | |
| 942 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 943 | TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 944 | if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) { |
| 945 | TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, conn); |
| 946 | goto out; |
| 947 | } |
| 948 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 949 | tel = ssl_to_quic_enc_level(level); |
| 950 | qel = &conn->qc->els[tel]; |
| 951 | |
| 952 | if (tel == -1) { |
| 953 | TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn); |
| 954 | goto err; |
| 955 | } |
| 956 | |
| 957 | if (!quic_crypto_data_cpy(qel, data, len)) { |
| 958 | TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn); |
| 959 | goto err; |
| 960 | } |
| 961 | |
| 962 | TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA, |
| 963 | conn, &level, &len); |
| 964 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 965 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 966 | TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn); |
| 967 | return 1; |
| 968 | |
| 969 | err: |
| 970 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn); |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | int ha_quic_flush_flight(SSL *ssl) |
| 975 | { |
| 976 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 977 | |
| 978 | TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn); |
| 979 | TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn); |
| 980 | |
| 981 | return 1; |
| 982 | } |
| 983 | |
| 984 | int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) |
| 985 | { |
| 986 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 987 | |
Frédéric Lécaille | 47c433f | 2020-12-10 17:03:11 +0100 | [diff] [blame] | 988 | TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 989 | quic_set_tls_alert(conn->qc, alert); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 990 | HA_ATOMIC_STORE(&conn->qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 991 | return 1; |
| 992 | } |
| 993 | |
| 994 | /* QUIC TLS methods */ |
| 995 | static SSL_QUIC_METHOD ha_quic_method = { |
| 996 | #ifdef OPENSSL_IS_BORINGSSL |
| 997 | .set_read_secret = ha_set_rsec, |
| 998 | .set_write_secret = ha_set_wsec, |
| 999 | #else |
| 1000 | .set_encryption_secrets = ha_quic_set_encryption_secrets, |
| 1001 | #endif |
| 1002 | .add_handshake_data = ha_quic_add_handshake_data, |
| 1003 | .flush_flight = ha_quic_flush_flight, |
| 1004 | .send_alert = ha_quic_send_alert, |
| 1005 | }; |
| 1006 | |
| 1007 | /* Initialize the TLS context of a listener with <bind_conf> as configuration. |
| 1008 | * Returns an error count. |
| 1009 | */ |
| 1010 | int ssl_quic_initial_ctx(struct bind_conf *bind_conf) |
| 1011 | { |
| 1012 | struct proxy *curproxy = bind_conf->frontend; |
| 1013 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
| 1014 | int cfgerr = 0; |
| 1015 | |
| 1016 | #if 0 |
| 1017 | /* XXX Did not manage to use this. */ |
| 1018 | const char *ciphers = |
| 1019 | "TLS_AES_128_GCM_SHA256:" |
| 1020 | "TLS_AES_256_GCM_SHA384:" |
| 1021 | "TLS_CHACHA20_POLY1305_SHA256:" |
| 1022 | "TLS_AES_128_CCM_SHA256"; |
| 1023 | #endif |
Frédéric Lécaille | 4b1fddc | 2021-07-01 17:09:05 +0200 | [diff] [blame] | 1024 | 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] | 1025 | long options = |
| 1026 | (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) | |
| 1027 | SSL_OP_SINGLE_ECDH_USE | |
| 1028 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 1029 | SSL_CTX *ctx; |
| 1030 | |
| 1031 | ctx = SSL_CTX_new(TLS_server_method()); |
| 1032 | bind_conf->initial_ctx = ctx; |
| 1033 | |
| 1034 | SSL_CTX_set_options(ctx, options); |
| 1035 | #if 0 |
| 1036 | if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) { |
| 1037 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' " |
| 1038 | "for bind '%s' at [%s:%d].\n", |
| 1039 | curproxy->id, ciphers, |
| 1040 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1041 | cfgerr++; |
| 1042 | } |
| 1043 | #endif |
| 1044 | |
| 1045 | if (SSL_CTX_set1_curves_list(ctx, groups) != 1) { |
| 1046 | ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' " |
| 1047 | "for bind '%s' at [%s:%d].\n", |
| 1048 | curproxy->id, groups, |
| 1049 | bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1050 | cfgerr++; |
| 1051 | } |
| 1052 | |
| 1053 | SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); |
| 1054 | SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1055 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 1056 | SSL_CTX_set_default_verify_paths(ctx); |
| 1057 | |
| 1058 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1059 | #ifdef OPENSSL_IS_BORINGSSL |
| 1060 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 1061 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1062 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 1063 | if (bind_conf->ssl_conf.early_data) { |
| 1064 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
| 1065 | SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite); |
| 1066 | } |
| 1067 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 1068 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 1069 | #else |
| 1070 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 1071 | #endif |
| 1072 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 1073 | #endif |
| 1074 | SSL_CTX_set_quic_method(ctx, &ha_quic_method); |
| 1075 | |
| 1076 | return cfgerr; |
| 1077 | } |
| 1078 | |
| 1079 | /* Decode an expected packet number from <truncated_on> its truncated value, |
| 1080 | * depending on <largest_pn> the largest received packet number, and <pn_nbits> |
| 1081 | * the number of bits used to encode this packet number (its length in bytes * 8). |
| 1082 | * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding |
| 1083 | */ |
| 1084 | static uint64_t decode_packet_number(uint64_t largest_pn, |
| 1085 | uint32_t truncated_pn, unsigned int pn_nbits) |
| 1086 | { |
| 1087 | uint64_t expected_pn = largest_pn + 1; |
| 1088 | uint64_t pn_win = (uint64_t)1 << pn_nbits; |
| 1089 | uint64_t pn_hwin = pn_win / 2; |
| 1090 | uint64_t pn_mask = pn_win - 1; |
| 1091 | uint64_t candidate_pn; |
| 1092 | |
| 1093 | |
| 1094 | candidate_pn = (expected_pn & ~pn_mask) | truncated_pn; |
| 1095 | /* Note that <pn_win> > <pn_hwin>. */ |
| 1096 | if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win && |
| 1097 | candidate_pn + pn_hwin <= expected_pn) |
| 1098 | return candidate_pn + pn_win; |
| 1099 | |
| 1100 | if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) |
| 1101 | return candidate_pn - pn_win; |
| 1102 | |
| 1103 | return candidate_pn; |
| 1104 | } |
| 1105 | |
| 1106 | /* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS |
| 1107 | * cryptographic context. |
| 1108 | * <largest_pn> is the largest received packet number and <pn> the address of |
| 1109 | * the packet number field for this packet with <byte0> address of its first byte. |
| 1110 | * <end> points to one byte past the end of this packet. |
| 1111 | * Returns 1 if succeeded, 0 if not. |
| 1112 | */ |
| 1113 | static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx, |
| 1114 | int64_t largest_pn, unsigned char *pn, |
| 1115 | unsigned char *byte0, const unsigned char *end, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1116 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1117 | { |
| 1118 | int ret, outlen, i, pnlen; |
| 1119 | uint64_t packet_number; |
| 1120 | uint32_t truncated_pn = 0; |
| 1121 | unsigned char mask[5] = {0}; |
| 1122 | unsigned char *sample; |
| 1123 | EVP_CIPHER_CTX *cctx; |
| 1124 | unsigned char *hp_key; |
| 1125 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1126 | /* Check there is enough data in this packet. */ |
| 1127 | if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) { |
| 1128 | TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt); |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | cctx = EVP_CIPHER_CTX_new(); |
| 1133 | if (!cctx) { |
| 1134 | TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt); |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | ret = 0; |
| 1139 | sample = pn + QUIC_PACKET_PN_MAXLEN; |
| 1140 | |
| 1141 | hp_key = tls_ctx->rx.hp_key; |
| 1142 | if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) || |
| 1143 | !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) || |
| 1144 | !EVP_DecryptFinal_ex(cctx, mask, &outlen)) { |
| 1145 | TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt); |
| 1146 | goto out; |
| 1147 | } |
| 1148 | |
| 1149 | *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 1150 | pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1; |
| 1151 | for (i = 0; i < pnlen; i++) { |
| 1152 | pn[i] ^= mask[i + 1]; |
| 1153 | truncated_pn = (truncated_pn << 8) | pn[i]; |
| 1154 | } |
| 1155 | |
| 1156 | packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8); |
| 1157 | /* Store remaining information for this unprotected header */ |
| 1158 | pkt->pn = packet_number; |
| 1159 | pkt->pnl = pnlen; |
| 1160 | |
| 1161 | ret = 1; |
| 1162 | |
| 1163 | out: |
| 1164 | EVP_CIPHER_CTX_free(cctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1165 | |
| 1166 | return ret; |
| 1167 | } |
| 1168 | |
| 1169 | /* Encrypt the payload of a QUIC packet with <pn> as number found at <payload> |
| 1170 | * address, with <payload_len> as payload length, <aad> as address of |
| 1171 | * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS |
| 1172 | * context. |
| 1173 | * Returns 1 if succeeded, 0 if not. |
| 1174 | */ |
| 1175 | static int quic_packet_encrypt(unsigned char *payload, size_t payload_len, |
| 1176 | unsigned char *aad, size_t aad_len, uint64_t pn, |
| 1177 | struct quic_tls_ctx *tls_ctx, struct connection *conn) |
| 1178 | { |
| 1179 | unsigned char iv[12]; |
| 1180 | unsigned char *tx_iv = tls_ctx->tx.iv; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 1181 | size_t tx_iv_sz = tls_ctx->tx.ivlen; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1182 | struct enc_debug_info edi; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1183 | |
| 1184 | if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) { |
| 1185 | TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1186 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | if (!quic_tls_encrypt(payload, payload_len, aad, aad_len, |
| 1190 | tls_ctx->tx.aead, tls_ctx->tx.key, iv)) { |
| 1191 | TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn); |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1192 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | return 1; |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 1196 | |
| 1197 | err: |
| 1198 | enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn); |
| 1199 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi); |
| 1200 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | /* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context. |
| 1204 | * Returns 1 if succeeded, 0 if not. |
| 1205 | */ |
| 1206 | static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx) |
| 1207 | { |
| 1208 | int ret; |
| 1209 | unsigned char iv[12]; |
| 1210 | unsigned char *rx_iv = tls_ctx->rx.iv; |
| 1211 | size_t rx_iv_sz = sizeof tls_ctx->rx.iv; |
| 1212 | |
| 1213 | if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) |
| 1214 | return 0; |
| 1215 | |
| 1216 | ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len, |
| 1217 | pkt->data, pkt->aad_len, |
| 1218 | tls_ctx->rx.aead, tls_ctx->rx.key, iv); |
| 1219 | if (!ret) |
| 1220 | return 0; |
| 1221 | |
| 1222 | /* Update the packet length (required to parse the frames). */ |
| 1223 | pkt->len = pkt->aad_len + ret; |
| 1224 | |
| 1225 | return 1; |
| 1226 | } |
| 1227 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1228 | /* Remove from <qcs> stream the acknowledged frames. |
| 1229 | * Never fails. |
| 1230 | */ |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1231 | static int qcs_try_to_consume(struct qcs *qcs) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1232 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1233 | int ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1234 | struct eb64_node *frm_node; |
| 1235 | |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1236 | ret = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1237 | frm_node = eb64_first(&qcs->tx.acked_frms); |
| 1238 | while (frm_node) { |
| 1239 | struct quic_stream *strm; |
| 1240 | |
| 1241 | strm = eb64_entry(&frm_node->node, struct quic_stream, offset); |
| 1242 | if (strm->offset.key != qcs->tx.ack_offset) |
| 1243 | break; |
| 1244 | |
| 1245 | b_del(strm->buf, strm->len); |
| 1246 | qcs->tx.ack_offset += strm->len; |
| 1247 | frm_node = eb64_next(frm_node); |
| 1248 | eb64_delete(&strm->offset); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1249 | ret = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1250 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1251 | |
| 1252 | return ret; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1253 | } |
| 1254 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1255 | /* Treat <frm> frame whose packet it is attached to has just been acknowledged. */ |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1256 | static inline void qc_treat_acked_tx_frm(struct quic_frame *frm, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1257 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1258 | { |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1259 | int stream_acked; |
| 1260 | struct quic_conn *qc = ctx->conn->qc; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1261 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1262 | TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1263 | stream_acked = 0; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1264 | switch (frm->type) { |
| 1265 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 1266 | { |
| 1267 | struct qcs *qcs = frm->stream.qcs; |
| 1268 | struct quic_stream *strm = &frm->stream; |
| 1269 | |
| 1270 | if (qcs->tx.ack_offset == strm->offset.key) { |
| 1271 | b_del(strm->buf, strm->len); |
| 1272 | qcs->tx.ack_offset += strm->len; |
| 1273 | LIST_DELETE(&frm->list); |
| 1274 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1275 | qc->qcc->flags &= ~QC_CF_MUX_MFULL; |
| 1276 | stream_acked = 1; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1277 | } |
| 1278 | else { |
| 1279 | eb64_insert(&qcs->tx.acked_frms, &strm->offset); |
| 1280 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1281 | stream_acked |= qcs_try_to_consume(qcs); |
Amaury Denoyelle | cae0791 | 2021-10-08 17:57:41 +0200 | [diff] [blame] | 1282 | |
| 1283 | if (qcs->flags & QC_SF_DETACH) |
| 1284 | tasklet_wakeup(qcs->shut_tl); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1285 | } |
| 1286 | break; |
| 1287 | default: |
| 1288 | LIST_DELETE(&frm->list); |
| 1289 | pool_free(pool_head_quic_frame, frm); |
| 1290 | } |
Frédéric Lécaille | 1c482c6 | 2021-09-20 16:59:51 +0200 | [diff] [blame] | 1291 | |
| 1292 | if (stream_acked) { |
| 1293 | struct qcc *qcc = qc->qcc; |
| 1294 | |
| 1295 | if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) { |
| 1296 | tasklet_wakeup(qcc->subs->tasklet); |
| 1297 | qcc->subs->events &= ~SUB_RETRY_SEND; |
| 1298 | if (!qcc->subs->events) |
| 1299 | qcc->subs = NULL; |
| 1300 | } |
| 1301 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet, |
| 1305 | * deallocating them, and their TX frames. |
| 1306 | * Returns the last node reached to be used for the next range. |
| 1307 | * May be NULL if <largest> node could not be found. |
| 1308 | */ |
| 1309 | static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags, |
| 1310 | struct list *newly_acked_pkts, |
| 1311 | struct eb64_node *largest_node, |
| 1312 | uint64_t largest, uint64_t smallest, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1313 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1314 | { |
| 1315 | struct eb64_node *node; |
| 1316 | struct quic_tx_packet *pkt; |
| 1317 | |
| 1318 | if (largest_node) |
| 1319 | node = largest_node; |
| 1320 | else { |
| 1321 | node = eb64_lookup(pkts, largest); |
| 1322 | while (!node && largest > smallest) { |
| 1323 | node = eb64_lookup(pkts, --largest); |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | while (node && node->key >= smallest) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1328 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1329 | |
| 1330 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 1331 | *pkt_flags |= pkt->flags; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1332 | LIST_INSERT(newly_acked_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1333 | TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key); |
| 1334 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1335 | qc_treat_acked_tx_frm(frm, ctx); |
| 1336 | node = eb64_prev(node); |
| 1337 | eb64_delete(&pkt->pn_node); |
| 1338 | } |
| 1339 | |
| 1340 | return node; |
| 1341 | } |
| 1342 | |
| 1343 | /* Treat <frm> frame whose packet it is attached to has just been detected as non |
| 1344 | * acknowledged. |
| 1345 | */ |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1346 | static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1347 | struct quic_pktns *pktns, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1348 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1349 | { |
| 1350 | TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1351 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 1352 | MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | /* Free the TX packets of <pkts> list */ |
| 1357 | static inline void free_quic_tx_pkts(struct list *pkts) |
| 1358 | { |
| 1359 | struct quic_tx_packet *pkt, *tmp; |
| 1360 | |
| 1361 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1362 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1363 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1364 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | /* Send a packet loss event nofification to the congestion controller |
| 1369 | * attached to <qc> connection with <lost_bytes> the number of lost bytes, |
| 1370 | * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet |
| 1371 | * at <now_us> current time. |
| 1372 | * Always succeeds. |
| 1373 | */ |
| 1374 | static inline void qc_cc_loss_event(struct quic_conn *qc, |
| 1375 | unsigned int lost_bytes, |
| 1376 | unsigned int newest_time_sent, |
| 1377 | unsigned int period, |
| 1378 | unsigned int now_us) |
| 1379 | { |
| 1380 | struct quic_cc_event ev = { |
| 1381 | .type = QUIC_CC_EVT_LOSS, |
| 1382 | .loss.now_ms = now_ms, |
| 1383 | .loss.max_ack_delay = qc->max_ack_delay, |
| 1384 | .loss.lost_bytes = lost_bytes, |
| 1385 | .loss.newest_time_sent = newest_time_sent, |
| 1386 | .loss.period = period, |
| 1387 | }; |
| 1388 | |
| 1389 | quic_cc_event(&qc->path->cc, &ev); |
| 1390 | } |
| 1391 | |
| 1392 | /* Send a packet ack event nofication for each newly acked packet of |
| 1393 | * <newly_acked_pkts> list and free them. |
| 1394 | * Always succeeds. |
| 1395 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1396 | static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1397 | struct list *newly_acked_pkts) |
| 1398 | { |
| 1399 | struct quic_conn *qc = ctx->conn->qc; |
| 1400 | struct quic_tx_packet *pkt, *tmp; |
| 1401 | struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, }; |
| 1402 | |
| 1403 | list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) { |
| 1404 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1405 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1406 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1407 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1408 | ev.ack.acked = pkt->in_flight_len; |
| 1409 | ev.ack.time_sent = pkt->time_sent; |
| 1410 | quic_cc_event(&qc->path->cc, &ev); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1411 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1412 | eb64_delete(&pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1413 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | } |
| 1417 | |
| 1418 | /* Handle <pkts> list of lost packets detected at <now_us> handling |
| 1419 | * their TX frames. |
| 1420 | * Send a packet loss event to the congestion controller if |
| 1421 | * in flight packet have been lost. |
| 1422 | * Also frees the packet in <pkts> list. |
| 1423 | * Never fails. |
| 1424 | */ |
| 1425 | static inline void qc_release_lost_pkts(struct quic_pktns *pktns, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1426 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1427 | struct list *pkts, |
| 1428 | uint64_t now_us) |
| 1429 | { |
| 1430 | struct quic_conn *qc = ctx->conn->qc; |
| 1431 | struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost; |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 1432 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1433 | uint64_t lost_bytes; |
| 1434 | |
| 1435 | lost_bytes = 0; |
| 1436 | oldest_lost = newest_lost = NULL; |
| 1437 | list_for_each_entry_safe(pkt, tmp, pkts, list) { |
| 1438 | lost_bytes += pkt->in_flight_len; |
| 1439 | pkt->pktns->tx.in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 1440 | qc->path->prep_in_flight -= pkt->in_flight_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1441 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 1442 | qc->path->ifae_pkts--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1443 | /* Treat the frames of this lost packet. */ |
| 1444 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) |
| 1445 | qc_treat_nacked_tx_frm(frm, pktns, ctx); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1446 | LIST_DELETE(&pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1447 | if (!oldest_lost) { |
| 1448 | oldest_lost = newest_lost = pkt; |
| 1449 | } |
| 1450 | else { |
| 1451 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1452 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1453 | newest_lost = pkt; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | if (lost_bytes) { |
| 1458 | /* Sent a packet loss event to the congestion controller. */ |
| 1459 | qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent, |
| 1460 | 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] | 1461 | quic_tx_packet_refdec(oldest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1462 | if (newest_lost != oldest_lost) |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 1463 | quic_tx_packet_refdec(newest_lost); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 1468 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 1469 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 1470 | * space if any not deemed lost. |
| 1471 | * Should be called after having received an ACK frame with newly acknowledged |
| 1472 | * packets or when the the loss detection timer has expired. |
| 1473 | * Always succeeds. |
| 1474 | */ |
| 1475 | static void qc_packet_loss_lookup(struct quic_pktns *pktns, |
| 1476 | struct quic_conn *qc, |
| 1477 | struct list *lost_pkts) |
| 1478 | { |
| 1479 | struct eb_root *pkts; |
| 1480 | struct eb64_node *node; |
| 1481 | struct quic_loss *ql; |
| 1482 | unsigned int loss_delay; |
| 1483 | |
| 1484 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns); |
| 1485 | pkts = &pktns->tx.pkts; |
| 1486 | pktns->tx.loss_time = TICK_ETERNITY; |
| 1487 | if (eb_is_empty(pkts)) |
| 1488 | goto out; |
| 1489 | |
| 1490 | ql = &qc->path->loss; |
| 1491 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
| 1492 | loss_delay += loss_delay >> 3; |
| 1493 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)); |
| 1494 | |
| 1495 | node = eb64_first(pkts); |
| 1496 | while (node) { |
| 1497 | struct quic_tx_packet *pkt; |
| 1498 | int64_t largest_acked_pn; |
| 1499 | unsigned int loss_time_limit, time_sent; |
| 1500 | |
| 1501 | 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] | 1502 | 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] | 1503 | node = eb64_next(node); |
| 1504 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 1505 | break; |
| 1506 | |
| 1507 | time_sent = pkt->time_sent; |
| 1508 | loss_time_limit = tick_add(time_sent, loss_delay); |
| 1509 | if (tick_is_le(time_sent, now_ms) || |
| 1510 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 1511 | eb64_delete(&pkt->pn_node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1512 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1513 | } |
| 1514 | else { |
| 1515 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | out: |
| 1520 | TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts); |
| 1521 | } |
| 1522 | |
| 1523 | /* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at |
| 1524 | * 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] | 1525 | * 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] | 1526 | * acked ack-eliciting packet. |
| 1527 | * Return 1, if succeeded, 0 if not. |
| 1528 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 1529 | static inline int qc_parse_ack_frm(struct quic_frame *frm, struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1530 | struct quic_enc_level *qel, |
| 1531 | unsigned int *rtt_sample, |
| 1532 | const unsigned char **pos, const unsigned char *end) |
| 1533 | { |
| 1534 | struct quic_ack *ack = &frm->ack; |
| 1535 | uint64_t smallest, largest; |
| 1536 | struct eb_root *pkts; |
| 1537 | struct eb64_node *largest_node; |
| 1538 | unsigned int time_sent, pkt_flags; |
| 1539 | struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts); |
| 1540 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 1541 | |
| 1542 | if (ack->largest_ack > qel->pktns->tx.next_pn) { |
| 1543 | TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM, |
| 1544 | ctx->conn,, &ack->largest_ack); |
| 1545 | goto err; |
| 1546 | } |
| 1547 | |
| 1548 | if (ack->first_ack_range > ack->largest_ack) { |
| 1549 | TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM, |
| 1550 | ctx->conn,, &ack->first_ack_range); |
| 1551 | goto err; |
| 1552 | } |
| 1553 | |
| 1554 | largest = ack->largest_ack; |
| 1555 | smallest = largest - ack->first_ack_range; |
| 1556 | pkts = &qel->pktns->tx.pkts; |
| 1557 | pkt_flags = 0; |
| 1558 | largest_node = NULL; |
| 1559 | time_sent = 0; |
| 1560 | |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1561 | 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] | 1562 | largest_node = eb64_lookup(pkts, largest); |
| 1563 | if (!largest_node) { |
| 1564 | TRACE_DEVEL("Largest acked packet not found", |
| 1565 | QUIC_EV_CONN_PRSAFRM, ctx->conn); |
Frédéric Lécaille | 83b7a5b | 2021-11-17 16:16:04 +0100 | [diff] [blame] | 1566 | } |
| 1567 | else { |
| 1568 | time_sent = eb64_entry(&largest_node->node, |
| 1569 | struct quic_tx_packet, pn_node)->time_sent; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1570 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
| 1574 | ctx->conn,, &largest, &smallest); |
| 1575 | do { |
| 1576 | uint64_t gap, ack_range; |
| 1577 | |
| 1578 | qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts, |
| 1579 | largest_node, largest, smallest, ctx); |
| 1580 | if (!ack->ack_range_num--) |
| 1581 | break; |
| 1582 | |
| 1583 | if (!quic_dec_int(&gap, pos, end)) |
| 1584 | goto err; |
| 1585 | |
| 1586 | if (smallest < gap + 2) { |
| 1587 | TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM, |
| 1588 | ctx->conn,, &gap, &smallest); |
| 1589 | goto err; |
| 1590 | } |
| 1591 | |
| 1592 | largest = smallest - gap - 2; |
| 1593 | if (!quic_dec_int(&ack_range, pos, end)) |
| 1594 | goto err; |
| 1595 | |
| 1596 | if (largest < ack_range) { |
| 1597 | TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM, |
| 1598 | ctx->conn,, &largest, &ack_range); |
| 1599 | goto err; |
| 1600 | } |
| 1601 | |
| 1602 | /* Do not use this node anymore. */ |
| 1603 | largest_node = NULL; |
| 1604 | /* Next range */ |
| 1605 | smallest = largest - ack_range; |
| 1606 | |
| 1607 | TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, |
| 1608 | ctx->conn,, &largest, &smallest); |
| 1609 | } while (1); |
| 1610 | |
| 1611 | /* 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] | 1612 | 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] | 1613 | |
| 1614 | if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 1615 | *rtt_sample = tick_remain(time_sent, now_ms); |
Frédéric Lécaille | 59b07c7 | 2021-08-03 16:06:01 +0200 | [diff] [blame] | 1616 | 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] | 1617 | } |
| 1618 | |
| 1619 | if (!LIST_ISEMPTY(&newly_acked_pkts)) { |
| 1620 | if (!eb_is_empty(&qel->pktns->tx.pkts)) { |
| 1621 | qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts); |
| 1622 | if (!LIST_ISEMPTY(&lost_pkts)) |
| 1623 | qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms); |
| 1624 | } |
| 1625 | qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts); |
| 1626 | if (quic_peer_validated_addr(ctx)) |
| 1627 | ctx->conn->qc->path->loss.pto_count = 0; |
| 1628 | qc_set_timer(ctx); |
| 1629 | } |
| 1630 | |
| 1631 | |
| 1632 | return 1; |
| 1633 | |
| 1634 | err: |
| 1635 | free_quic_tx_pkts(&newly_acked_pkts); |
| 1636 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn); |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
Frédéric Lécaille | 6f0fadb | 2021-09-28 09:04:12 +0200 | [diff] [blame] | 1640 | /* This function gives the detail of the SSL error. It is used only |
| 1641 | * if the debug mode and the verbose mode are activated. It dump all |
| 1642 | * the SSL error until the stack was empty. |
| 1643 | */ |
| 1644 | static forceinline void qc_ssl_dump_errors(struct connection *conn) |
| 1645 | { |
| 1646 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 1647 | while (1) { |
| 1648 | unsigned long ret; |
| 1649 | |
| 1650 | ret = ERR_get_error(); |
| 1651 | if (!ret) |
| 1652 | return; |
| 1653 | |
| 1654 | fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret, |
| 1655 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 1656 | } |
| 1657 | } |
| 1658 | } |
| 1659 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1660 | int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, |
| 1661 | const char **str, int *len); |
| 1662 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1663 | /* Provide CRYPTO data to the TLS stack found at <data> with <len> as length |
| 1664 | * from <qel> encryption level with <ctx> as QUIC connection context. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1665 | * Remaining parameter are there for debugging purposes. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1666 | * Return 1 if succeeded, 0 if not. |
| 1667 | */ |
| 1668 | 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] | 1669 | struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1670 | const unsigned char *data, size_t len, |
| 1671 | struct quic_rx_packet *pkt, |
| 1672 | struct quic_rx_crypto_frm *cf) |
| 1673 | { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1674 | int ssl_err, state; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 1675 | struct quic_conn *qc; |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1676 | const char *alpn; |
| 1677 | int alpn_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1678 | |
| 1679 | TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn); |
| 1680 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 1681 | qc = ctx->conn->qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1682 | if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) { |
| 1683 | TRACE_PROTO("SSL_provide_quic_data() error", |
| 1684 | QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl); |
| 1685 | goto err; |
| 1686 | } |
| 1687 | |
| 1688 | el->rx.crypto.offset += len; |
| 1689 | TRACE_PROTO("in order CRYPTO data", |
| 1690 | QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl); |
| 1691 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1692 | state = HA_ATOMIC_LOAD(&qc->state); |
| 1693 | if (state < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1694 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 1695 | if (ssl_err != 1) { |
| 1696 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1697 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1698 | TRACE_PROTO("SSL handshake", |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1699 | QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1700 | goto out; |
| 1701 | } |
| 1702 | |
| 1703 | TRACE_DEVEL("SSL handshake error", |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1704 | QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err); |
Frédéric Lécaille | 7c881bd | 2021-09-28 09:05:59 +0200 | [diff] [blame] | 1705 | qc_ssl_dump_errors(ctx->conn); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 1706 | ERR_clear_error(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1707 | goto err; |
| 1708 | } |
| 1709 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1710 | TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1711 | if (objt_listener(ctx->conn->target)) |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1712 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1713 | else |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1714 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1715 | } else { |
| 1716 | ssl_err = SSL_process_quic_post_handshake(ctx->ssl); |
| 1717 | if (ssl_err != 1) { |
| 1718 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 1719 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
| 1720 | TRACE_DEVEL("SSL post handshake", |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1721 | QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1722 | goto out; |
| 1723 | } |
| 1724 | |
| 1725 | TRACE_DEVEL("SSL post handshake error", |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1726 | QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1727 | goto err; |
| 1728 | } |
| 1729 | |
| 1730 | TRACE_PROTO("SSL post handshake succeeded", |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 1731 | QUIC_EV_CONN_HDSHK, ctx->conn, &state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1732 | } |
| 1733 | |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1734 | conn_get_alpn(ctx->conn, &alpn, &alpn_len); |
| 1735 | if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) { |
| 1736 | qc->qcc->app_ops = &h3_ops; |
| 1737 | if (!qc->qcc->app_ops->init(qc->qcc)) |
| 1738 | goto err; |
| 1739 | } |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 1740 | else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) { |
| 1741 | qc->qcc->app_ops = &hq_interop_ops; |
| 1742 | } |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1743 | else { |
| 1744 | /* TODO RFC9001 8.1. Protocol Negotiation |
| 1745 | * must return no_application_protocol TLS alert |
| 1746 | */ |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1747 | TRACE_PROTO("No matching ALPN", QUIC_EV_CONN_SSLDATA, ctx->conn); |
| 1748 | goto err; |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 1749 | } |
| 1750 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1751 | out: |
| 1752 | TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn); |
| 1753 | return 1; |
| 1754 | |
| 1755 | err: |
| 1756 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn); |
| 1757 | return 0; |
| 1758 | } |
| 1759 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1760 | /* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to |
| 1761 | * <pkt> RX packet. |
| 1762 | * Return it if succeeded, NULL if not. |
| 1763 | */ |
| 1764 | static inline |
| 1765 | struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm, |
| 1766 | struct quic_rx_packet *pkt) |
| 1767 | { |
| 1768 | struct quic_rx_strm_frm *frm; |
| 1769 | |
| 1770 | frm = pool_alloc(pool_head_quic_rx_strm_frm); |
| 1771 | if (frm) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1772 | frm->offset_node.key = stream_frm->offset.key; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1773 | frm->len = stream_frm->len; |
| 1774 | frm->data = stream_frm->data; |
| 1775 | frm->pkt = pkt; |
| 1776 | } |
| 1777 | |
| 1778 | return frm; |
| 1779 | } |
| 1780 | |
| 1781 | /* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 1782 | * several streams, depending on the already open ones. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1783 | * Return this node if succeeded, NULL if not. |
| 1784 | */ |
| 1785 | static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id) |
| 1786 | { |
| 1787 | unsigned int strm_type; |
| 1788 | int64_t sub_id; |
| 1789 | struct eb64_node *strm_node; |
| 1790 | |
| 1791 | TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn); |
| 1792 | |
| 1793 | strm_type = id & QCS_ID_TYPE_MASK; |
| 1794 | sub_id = id >> QCS_ID_TYPE_SHIFT; |
| 1795 | strm_node = NULL; |
| 1796 | if (qc_local_stream_id(qcc, id)) { |
| 1797 | /* Local streams: this stream must be already opened. */ |
| 1798 | strm_node = eb64_lookup(&qcc->streams_by_id, id); |
| 1799 | if (!strm_node) { |
| 1800 | TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn); |
| 1801 | goto out; |
| 1802 | } |
| 1803 | } |
| 1804 | else { |
| 1805 | /* Remote streams. */ |
| 1806 | struct eb_root *strms; |
| 1807 | uint64_t largest_id; |
| 1808 | enum qcs_type qcs_type; |
| 1809 | |
| 1810 | strms = &qcc->streams_by_id; |
| 1811 | qcs_type = qcs_id_type(id); |
| 1812 | if (sub_id + 1 > qcc->strms[qcs_type].max_streams) { |
| 1813 | TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn); |
| 1814 | goto out; |
| 1815 | } |
| 1816 | |
| 1817 | /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a |
| 1818 | * correct value. |
| 1819 | */ |
| 1820 | largest_id = qcc->strms[qcs_type].largest_id; |
| 1821 | if (sub_id > (int64_t)largest_id) { |
| 1822 | /* RFC: "A stream ID that is used out of order results in all streams |
| 1823 | * of that type with lower-numbered stream IDs also being opened". |
| 1824 | * So, let's "open" these streams. |
| 1825 | */ |
| 1826 | int64_t i; |
| 1827 | struct qcs *qcs; |
| 1828 | |
| 1829 | qcs = NULL; |
| 1830 | for (i = largest_id + 1; i <= sub_id; i++) { |
| 1831 | qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type); |
| 1832 | if (!qcs) { |
| 1833 | TRACE_PROTO("Could not allocate a new stream", |
| 1834 | QUIC_EV_CONN_PSTRM, qcc->conn); |
| 1835 | goto out; |
| 1836 | } |
| 1837 | |
| 1838 | qcc->strms[qcs_type].largest_id = i; |
| 1839 | } |
| 1840 | if (qcs) |
| 1841 | strm_node = &qcs->by_id; |
| 1842 | } |
| 1843 | else { |
| 1844 | strm_node = eb64_lookup(strms, id); |
| 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn); |
| 1849 | return strm_node; |
| 1850 | |
| 1851 | out: |
| 1852 | TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn); |
| 1853 | return NULL; |
| 1854 | } |
| 1855 | |
| 1856 | /* Copy as most as possible STREAM data from <strm_frm> into <strm> stream. |
| 1857 | * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame |
| 1858 | * to reflect the data which have been consumed. |
| 1859 | */ |
| 1860 | static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm) |
| 1861 | { |
| 1862 | size_t ret; |
| 1863 | |
| 1864 | ret = 0; |
| 1865 | while (strm_frm->len) { |
| 1866 | size_t try; |
| 1867 | |
| 1868 | try = b_contig_space(buf); |
| 1869 | if (!try) |
| 1870 | break; |
| 1871 | |
| 1872 | if (try > strm_frm->len) |
| 1873 | try = strm_frm->len; |
| 1874 | memcpy(b_tail(buf), strm_frm->data, try); |
| 1875 | strm_frm->len -= try; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1876 | strm_frm->offset.key += try; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1877 | b_add(buf, try); |
| 1878 | ret += try; |
| 1879 | } |
| 1880 | |
| 1881 | return ret; |
| 1882 | } |
| 1883 | |
| 1884 | /* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several |
| 1885 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 1886 | * If not, the STREAM frame is stored to be treated again later. |
| 1887 | * We rely on the flow control so that not to store too much STREAM frames. |
| 1888 | * Return 1 if succeeded, 0 if not. |
| 1889 | */ |
| 1890 | static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt, |
| 1891 | struct quic_stream *strm_frm, |
| 1892 | struct quic_conn *qc) |
| 1893 | { |
| 1894 | struct qcs *strm; |
| 1895 | struct eb64_node *strm_node, *frm_node; |
| 1896 | struct quic_rx_strm_frm *frm; |
| 1897 | |
| 1898 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 1899 | if (!strm_node) { |
| 1900 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn); |
| 1901 | return 0; |
| 1902 | } |
| 1903 | |
| 1904 | strm = eb64_entry(&strm_node->node, struct qcs, by_id); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1905 | frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1906 | /* FIXME: handle the case where this frame overlap others */ |
| 1907 | if (frm_node) { |
| 1908 | TRACE_PROTO("Already existing stream data", |
| 1909 | QUIC_EV_CONN_PSTRM, qc->conn); |
| 1910 | goto out; |
| 1911 | } |
| 1912 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1913 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1914 | int ret; |
| 1915 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 1916 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1917 | goto store_frm; |
| 1918 | |
| 1919 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
Amaury Denoyelle | b9ce14e | 2021-11-08 09:13:42 +0100 | [diff] [blame] | 1920 | if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) < 0) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1921 | TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM); |
| 1922 | return 0; |
| 1923 | } |
| 1924 | |
| 1925 | strm->rx.offset += ret; |
| 1926 | } |
| 1927 | |
| 1928 | if (!strm_frm->len) |
| 1929 | goto out; |
| 1930 | |
| 1931 | store_frm: |
| 1932 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 1933 | if (!frm) { |
| 1934 | TRACE_PROTO("Could not alloc RX STREAM frame", |
| 1935 | QUIC_EV_CONN_PSTRM, qc->conn); |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1939 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1940 | quic_rx_packet_refinc(pkt); |
| 1941 | |
| 1942 | out: |
| 1943 | return 1; |
| 1944 | } |
| 1945 | |
| 1946 | /* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several |
| 1947 | * streams may be open. The data are copied to the stream RX buffer if possible. |
| 1948 | * If not, the STREAM frame is stored to be treated again later. |
| 1949 | * We rely on the flow control so that not to store too much STREAM frames. |
| 1950 | * Return 1 if succeeded, 0 if not. |
| 1951 | */ |
| 1952 | static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt, |
| 1953 | struct quic_stream *strm_frm, |
| 1954 | struct quic_conn *qc) |
| 1955 | { |
| 1956 | struct qcs *strm; |
| 1957 | struct eb64_node *strm_node, *frm_node; |
| 1958 | struct quic_rx_strm_frm *frm; |
| 1959 | size_t strm_frm_len; |
| 1960 | |
| 1961 | strm_node = qcc_get_qcs(qc->qcc, strm_frm->id); |
| 1962 | if (!strm_node) { |
| 1963 | TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn); |
| 1964 | return 0; |
| 1965 | } |
| 1966 | |
| 1967 | strm = eb64_entry(&strm_node->node, struct qcs, by_id); |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1968 | frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1969 | /* FIXME: handle the case where this frame overlap others */ |
| 1970 | if (frm_node) { |
| 1971 | TRACE_PROTO("Already existing stream data", |
| 1972 | QUIC_EV_CONN_PSTRM, qc->conn); |
| 1973 | goto out; |
| 1974 | } |
| 1975 | |
| 1976 | strm_frm_len = strm_frm->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1977 | if (strm_frm->offset.key == strm->rx.offset) { |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1978 | int ret; |
| 1979 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 1980 | if (!qc_get_buf(strm, &strm->rx.buf)) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1981 | goto store_frm; |
| 1982 | |
| 1983 | /* qc_strm_cpy() will modify the offset, depending on the number |
| 1984 | * of bytes copied. |
| 1985 | */ |
| 1986 | ret = qc_strm_cpy(&strm->rx.buf, strm_frm); |
| 1987 | /* Inform the application of the arrival of this new stream */ |
| 1988 | if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) { |
| 1989 | TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn); |
| 1990 | return 0; |
| 1991 | } |
| 1992 | |
| 1993 | if (ret) |
| 1994 | ruqs_notify_recv(strm); |
| 1995 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 1996 | strm_frm->offset.key += ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 1997 | } |
| 1998 | /* Take this frame into an account for the stream flow control */ |
| 1999 | strm->rx.offset += strm_frm_len; |
| 2000 | /* 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] | 2001 | * store any more information for it. |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2002 | */ |
| 2003 | if (!strm_frm->len) |
| 2004 | goto out; |
| 2005 | |
| 2006 | store_frm: |
| 2007 | frm = new_quic_rx_strm_frm(strm_frm, pkt); |
| 2008 | if (!frm) { |
| 2009 | TRACE_PROTO("Could not alloc RX STREAM frame", |
| 2010 | QUIC_EV_CONN_PSTRM, qc->conn); |
| 2011 | return 0; |
| 2012 | } |
| 2013 | |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 2014 | eb64_insert(&strm->rx.frms, &frm->offset_node); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2015 | quic_rx_packet_refinc(pkt); |
| 2016 | |
| 2017 | out: |
| 2018 | return 1; |
| 2019 | } |
| 2020 | |
| 2021 | static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt, |
| 2022 | struct quic_stream *strm_frm, |
| 2023 | struct quic_conn *qc) |
| 2024 | { |
| 2025 | if (strm_frm->id & QCS_ID_DIR_BIT) |
| 2026 | return qc_handle_uni_strm_frm(pkt, strm_frm, qc); |
| 2027 | else |
| 2028 | return qc_handle_bidi_strm_frm(pkt, strm_frm, qc); |
| 2029 | } |
| 2030 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2031 | /* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx> |
| 2032 | * as I/O handler context and <qel> as encryption level. |
| 2033 | * Returns 1 if succeeded, 0 if failed. |
| 2034 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2035 | 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] | 2036 | struct quic_enc_level *qel) |
| 2037 | { |
| 2038 | struct quic_frame frm; |
| 2039 | const unsigned char *pos, *end; |
| 2040 | struct quic_conn *conn = ctx->conn->qc; |
| 2041 | |
| 2042 | TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn); |
| 2043 | /* Skip the AAD */ |
| 2044 | pos = pkt->data + pkt->aad_len; |
| 2045 | end = pkt->data + pkt->len; |
| 2046 | |
| 2047 | while (pos < end) { |
| 2048 | if (!qc_parse_frm(&frm, pkt, &pos, end, conn)) |
| 2049 | goto err; |
| 2050 | |
| 2051 | switch (frm.type) { |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2052 | case QUIC_FT_PADDING: |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2053 | break; |
| 2054 | case QUIC_FT_PING: |
| 2055 | break; |
| 2056 | case QUIC_FT_ACK: |
| 2057 | { |
| 2058 | unsigned int rtt_sample; |
| 2059 | |
| 2060 | rtt_sample = 0; |
| 2061 | if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end)) |
| 2062 | goto err; |
| 2063 | |
| 2064 | if (rtt_sample) { |
| 2065 | unsigned int ack_delay; |
| 2066 | |
| 2067 | ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 : |
| 2068 | MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay)); |
| 2069 | quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn); |
| 2070 | } |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2071 | break; |
| 2072 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2073 | case QUIC_FT_CRYPTO: |
| 2074 | if (frm.crypto.offset != qel->rx.crypto.offset) { |
| 2075 | struct quic_rx_crypto_frm *cf; |
| 2076 | |
| 2077 | cf = pool_alloc(pool_head_quic_rx_crypto_frm); |
| 2078 | if (!cf) { |
| 2079 | TRACE_DEVEL("CRYPTO frame allocation failed", |
| 2080 | QUIC_EV_CONN_PRSHPKT, ctx->conn); |
| 2081 | goto err; |
| 2082 | } |
| 2083 | |
| 2084 | cf->offset_node.key = frm.crypto.offset; |
| 2085 | cf->len = frm.crypto.len; |
| 2086 | cf->data = frm.crypto.data; |
| 2087 | cf->pkt = pkt; |
| 2088 | eb64_insert(&qel->rx.crypto.frms, &cf->offset_node); |
| 2089 | quic_rx_packet_refinc(pkt); |
| 2090 | } |
| 2091 | else { |
| 2092 | /* XXX TO DO: <cf> is used only for the traces. */ |
| 2093 | struct quic_rx_crypto_frm cf = { }; |
| 2094 | |
| 2095 | cf.offset_node.key = frm.crypto.offset; |
| 2096 | cf.len = frm.crypto.len; |
| 2097 | if (!qc_provide_cdata(qel, ctx, |
| 2098 | frm.crypto.data, frm.crypto.len, |
| 2099 | pkt, &cf)) |
| 2100 | goto err; |
| 2101 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2102 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2103 | case QUIC_FT_STREAM_8: |
| 2104 | case QUIC_FT_STREAM_9: |
| 2105 | case QUIC_FT_STREAM_A: |
| 2106 | case QUIC_FT_STREAM_B: |
| 2107 | case QUIC_FT_STREAM_C: |
| 2108 | case QUIC_FT_STREAM_D: |
| 2109 | case QUIC_FT_STREAM_E: |
| 2110 | case QUIC_FT_STREAM_F: |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2111 | { |
| 2112 | struct quic_stream *stream = &frm.stream; |
| 2113 | |
| 2114 | TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm); |
| 2115 | if (objt_listener(ctx->conn->target)) { |
| 2116 | if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT) |
| 2117 | goto err; |
| 2118 | } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)) |
| 2119 | goto err; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2120 | |
| 2121 | if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc)) |
| 2122 | goto err; |
| 2123 | |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 2124 | break; |
| 2125 | } |
Frédéric Lécaille | f366cb7 | 2021-11-18 10:57:18 +0100 | [diff] [blame] | 2126 | case QUIC_FT_MAX_DATA: |
| 2127 | case QUIC_FT_MAX_STREAM_DATA: |
| 2128 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 2129 | case QUIC_FT_MAX_STREAMS_UNI: |
| 2130 | case QUIC_FT_DATA_BLOCKED: |
| 2131 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 2132 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 2133 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 2134 | break; |
Frédéric Lécaille | 0c14020 | 2020-12-09 15:56:48 +0100 | [diff] [blame] | 2135 | case QUIC_FT_NEW_CONNECTION_ID: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2136 | break; |
| 2137 | case QUIC_FT_CONNECTION_CLOSE: |
| 2138 | case QUIC_FT_CONNECTION_CLOSE_APP: |
Amaury Denoyelle | 493bb1d | 2021-10-13 16:34:49 +0200 | [diff] [blame] | 2139 | conn->qcc->flags |= QC_CF_CC_RECV; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2140 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2141 | case QUIC_FT_HANDSHAKE_DONE: |
| 2142 | if (objt_listener(ctx->conn->target)) |
| 2143 | goto err; |
| 2144 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2145 | HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_CONFIRMED); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2146 | break; |
| 2147 | default: |
| 2148 | goto err; |
| 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | /* The server must switch from INITIAL to HANDSHAKE handshake state when it |
| 2153 | * has successfully parse a Handshake packet. The Initial encryption must also |
| 2154 | * be discarded. |
| 2155 | */ |
Frédéric Lécaille | 8c27de7 | 2021-09-20 11:00:46 +0200 | [diff] [blame] | 2156 | if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) { |
| 2157 | int state = HA_ATOMIC_LOAD(&conn->state); |
| 2158 | |
| 2159 | if (state >= QUIC_HS_ST_SERVER_INITIAL) { |
| 2160 | quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
| 2161 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn); |
| 2162 | quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn); |
| 2163 | qc_set_timer(ctx); |
| 2164 | if (state < QUIC_HS_ST_SERVER_HANDSHAKE) |
| 2165 | HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_SERVER_HANDSHAKE); |
| 2166 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn); |
| 2170 | return 1; |
| 2171 | |
| 2172 | err: |
| 2173 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn); |
| 2174 | return 0; |
| 2175 | } |
| 2176 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2177 | /* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2178 | * 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] | 2179 | * room in <cbuf>. Also increase the <cbuf> write index consequently. |
| 2180 | * This function must be called only after having built a correct datagram. |
| 2181 | * Always succeeds. |
| 2182 | */ |
| 2183 | static inline void qc_set_dg(struct cbuf *cbuf, |
| 2184 | uint16_t dglen, struct quic_tx_packet *pkt) |
| 2185 | { |
| 2186 | write_u16(cb_wr(cbuf), dglen); |
| 2187 | write_ptr(cb_wr(cbuf) + sizeof dglen, pkt); |
| 2188 | cb_add(cbuf, dglen + sizeof dglen + sizeof pkt); |
| 2189 | } |
| 2190 | |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2191 | /* 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] | 2192 | * the QUIC connection with <ctx> as I/O handler context, possibly concatenating |
| 2193 | * several packets in the same datagram. A header made of two fields is added |
| 2194 | * to each datagram: the datagram length followed by the address of the first |
| 2195 | * packet in this datagram. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2196 | * Returns 1 if succeeded, or 0 if something wrong happened. |
| 2197 | */ |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2198 | static int qc_prep_pkts(struct qring *qr, struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2199 | { |
| 2200 | struct quic_conn *qc; |
| 2201 | enum quic_tls_enc_level tel, next_tel; |
| 2202 | struct quic_enc_level *qel; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2203 | struct cbuf *cbuf; |
| 2204 | unsigned char *end_buf, *end, *pos, *spos; |
| 2205 | struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt; |
| 2206 | /* length of datagrams */ |
| 2207 | uint16_t dglen; |
| 2208 | size_t total; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2209 | int padding; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2210 | /* Each datagram is prepended with its length followed by the |
| 2211 | * address of the first packet in the datagram. |
| 2212 | */ |
| 2213 | size_t dg_headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2214 | |
| 2215 | TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn); |
| 2216 | qc = ctx->conn->qc; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2217 | if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state))) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2218 | TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2219 | goto err; |
| 2220 | } |
| 2221 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2222 | start: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2223 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2224 | total = 0; |
| 2225 | padding = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2226 | qel = &qc->els[tel]; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2227 | cbuf = qr->cbuf; |
| 2228 | spos = pos = cb_wr(cbuf); |
| 2229 | /* Leave at least <dglen> bytes at the end of this buffer |
| 2230 | * to ensure there is enough room to mark the end of prepared |
| 2231 | * contiguous data with a zero length. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2232 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2233 | end_buf = pos + cb_contig_space(cbuf) - sizeof dglen; |
| 2234 | first_pkt = prv_pkt = NULL; |
| 2235 | while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) { |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2236 | int err, nb_ptos, ack, cc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2237 | enum quic_pkt_type pkt_type; |
| 2238 | |
Frédéric Lécaille | c5e72b9 | 2020-12-02 16:11:40 +0100 | [diff] [blame] | 2239 | TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 2240 | nb_ptos = ack = 0; |
| 2241 | cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE; |
| 2242 | if (!cc) { |
| 2243 | if (!prv_pkt) { |
| 2244 | /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */ |
| 2245 | do { |
| 2246 | nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams); |
| 2247 | } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1)); |
| 2248 | } |
| 2249 | ack = HA_ATOMIC_BTR(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2250 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2251 | /* 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] | 2252 | * 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] | 2253 | * 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] | 2254 | * and if there is no more CRYPTO data available or in flight |
| 2255 | * congestion control limit is reached for prepared data |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2256 | */ |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2257 | if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) || |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2258 | (!cc && !ack && !nb_ptos && |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2259 | (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) || |
| 2260 | qc->path->prep_in_flight >= qc->path->cwnd))) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2261 | TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2262 | /* Set the current datagram as prepared into <cbuf> if |
| 2263 | * the was already a correct packet which was previously written. |
| 2264 | */ |
| 2265 | if (prv_pkt) |
| 2266 | qc_set_dg(cbuf, dglen, first_pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2267 | break; |
| 2268 | } |
| 2269 | |
| 2270 | pkt_type = quic_tls_level_pkt_type(tel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2271 | if (!prv_pkt) { |
| 2272 | /* Leave room for the datagram header */ |
| 2273 | pos += dg_headlen; |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 2274 | if (!quic_peer_validated_addr(ctx) && objt_listener(ctx->conn->target)) { |
| 2275 | if (qc->tx.prep_bytes >= 3 * qc->rx.bytes) |
| 2276 | qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED; |
| 2277 | end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes); |
| 2278 | } |
| 2279 | else { |
| 2280 | end = pos + qc->path->mtu; |
| 2281 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2282 | } |
| 2283 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2284 | cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding, |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 2285 | pkt_type, ack, nb_ptos, cc, &err); |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 2286 | /* 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] | 2287 | if (err < 0) { |
| 2288 | if (!prv_pkt && nb_ptos) |
| 2289 | HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2290 | if (ack) |
| 2291 | HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2292 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2293 | switch (err) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2294 | case -2: |
| 2295 | goto err; |
| 2296 | case -1: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2297 | /* If there was already a correct packet present, set the |
| 2298 | * current datagram as prepared into <cbuf>. |
| 2299 | */ |
| 2300 | if (prv_pkt) { |
| 2301 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2302 | goto stop_build; |
| 2303 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2304 | goto out; |
| 2305 | default: |
Frédéric Lécaille | 67f47d0 | 2021-08-19 15:19:09 +0200 | [diff] [blame] | 2306 | /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */ |
| 2307 | if (!cur_pkt) |
| 2308 | goto err; |
| 2309 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2310 | total += cur_pkt->len; |
| 2311 | /* keep trace of the first packet in the datagram */ |
| 2312 | if (!first_pkt) |
| 2313 | first_pkt = cur_pkt; |
| 2314 | /* Attach the current one to the previous one */ |
| 2315 | if (prv_pkt) |
| 2316 | prv_pkt->next = cur_pkt; |
| 2317 | /* Let's say we have to build a new dgram */ |
| 2318 | prv_pkt = NULL; |
| 2319 | dglen += cur_pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2320 | /* Discard the Initial encryption keys as soon as |
| 2321 | * a handshake packet could be built. |
| 2322 | */ |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2323 | if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL && |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2324 | pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) { |
| 2325 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]); |
Frédéric Lécaille | acd43a5 | 2021-09-20 11:18:24 +0200 | [diff] [blame] | 2326 | TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, ctx->conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2327 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc); |
| 2328 | qc_set_timer(ctx); |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2329 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2330 | } |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2331 | /* If the data for the current encryption level have all been sent, |
| 2332 | * select the next level. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2333 | */ |
Frédéric Lécaille | d067088 | 2021-08-19 07:53:27 +0200 | [diff] [blame] | 2334 | if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) && |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 2335 | (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) || |
| 2336 | (next_tel != QUIC_TLS_ENC_LEVEL_NONE && qc->els[next_tel].pktns->tx.in_flight))) { |
Frédéric Lécaille | 4bade77 | 2021-08-23 08:54:28 +0200 | [diff] [blame] | 2337 | /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */ |
| 2338 | if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) |
| 2339 | next_tel = QUIC_TLS_ENC_LEVEL_APP; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2340 | tel = next_tel; |
| 2341 | qel = &qc->els[tel]; |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 2342 | if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2343 | /* If there is data for the next level, do not |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2344 | * consume a datagram. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2345 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2346 | prv_pkt = cur_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2347 | } |
| 2348 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | /* If we have to build a new datagram, set the current datagram as |
| 2352 | * prepared into <cbuf>. |
| 2353 | */ |
| 2354 | if (!prv_pkt) { |
| 2355 | qc_set_dg(cbuf, dglen, first_pkt); |
| 2356 | first_pkt = NULL; |
| 2357 | dglen = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 2358 | padding = 0; |
| 2359 | } |
| 2360 | else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL && |
| 2361 | (objt_server(qc->conn->target) || |
| 2362 | prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) { |
| 2363 | padding = 1; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | stop_build: |
| 2368 | /* Reset <wr> writer index if in front of <rd> index */ |
| 2369 | if (end_buf - pos < (int)qc->path->mtu + dg_headlen) { |
| 2370 | int rd = HA_ATOMIC_LOAD(&cbuf->rd); |
| 2371 | |
| 2372 | TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn); |
| 2373 | if (cb_contig_space(cbuf) >= sizeof(uint16_t)) { |
| 2374 | if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) { |
| 2375 | /* Mark the end of contiguous data for the reader */ |
| 2376 | write_u16(cb_wr(cbuf), 0); |
| 2377 | cb_add(cbuf, sizeof(uint16_t)); |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | if (rd && rd <= cbuf->wr) { |
| 2382 | cb_wr_reset(cbuf); |
| 2383 | if (pos == spos) { |
| 2384 | /* Reuse the same buffer if nothing was built. */ |
| 2385 | goto start; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2386 | } |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | out: |
| 2391 | TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2392 | return total; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2393 | |
| 2394 | err: |
| 2395 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2396 | return -1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | /* 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] | 2400 | * 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] | 2401 | */ |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2402 | 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] | 2403 | { |
| 2404 | struct quic_conn *qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2405 | struct cbuf *cbuf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2406 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2407 | qc = ctx->conn->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2408 | cbuf = qr->cbuf; |
| 2409 | while (cb_contig_data(cbuf)) { |
| 2410 | unsigned char *pos; |
| 2411 | struct buffer tmpbuf = { }; |
| 2412 | struct quic_tx_packet *first_pkt, *pkt, *next_pkt; |
| 2413 | uint16_t dglen; |
| 2414 | size_t headlen = sizeof dglen + sizeof first_pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2415 | unsigned int time_sent; |
| 2416 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2417 | pos = cb_rd(cbuf); |
| 2418 | dglen = read_u16(pos); |
| 2419 | /* End of prepared datagrams. |
| 2420 | * Reset the reader index only if in front of the writer index. |
| 2421 | */ |
| 2422 | if (!dglen) { |
| 2423 | int wr = HA_ATOMIC_LOAD(&cbuf->wr); |
| 2424 | |
| 2425 | if (wr && wr < cbuf->rd) { |
| 2426 | cb_rd_reset(cbuf); |
| 2427 | continue; |
| 2428 | } |
| 2429 | break; |
| 2430 | } |
| 2431 | |
| 2432 | pos += sizeof dglen; |
| 2433 | first_pkt = read_ptr(pos); |
| 2434 | pos += sizeof first_pkt; |
| 2435 | tmpbuf.area = (char *)pos; |
| 2436 | tmpbuf.size = tmpbuf.data = dglen; |
| 2437 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 2438 | TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2439 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2440 | quic_tx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2441 | if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx, |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2442 | &tmpbuf, tmpbuf.data, 0) <= 0) { |
| 2443 | for (pkt = first_pkt; pkt; pkt = pkt->next) |
| 2444 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2445 | break; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2446 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2447 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2448 | cb_del(cbuf, dglen + headlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2449 | qc->tx.bytes += tmpbuf.data; |
| 2450 | time_sent = now_ms; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2451 | |
| 2452 | for (pkt = first_pkt; pkt; pkt = next_pkt) { |
| 2453 | pkt->time_sent = time_sent; |
| 2454 | if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) { |
| 2455 | pkt->pktns->tx.time_of_last_eliciting = time_sent; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 2456 | qc->path->ifae_pkts++; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2457 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2458 | qc->path->in_flight += pkt->in_flight_len; |
| 2459 | pkt->pktns->tx.in_flight += pkt->in_flight_len; |
| 2460 | if (pkt->in_flight_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2461 | qc_set_timer(ctx); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2462 | TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt); |
| 2463 | next_pkt = pkt->next; |
Frédéric Lécaille | 0eb60c5 | 2021-07-19 14:48:36 +0200 | [diff] [blame] | 2464 | eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2465 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2466 | } |
| 2467 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2468 | |
| 2469 | return 1; |
| 2470 | } |
| 2471 | |
| 2472 | /* Build all the frames which must be sent just after the handshake have succeeded. |
| 2473 | * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send |
| 2474 | * a HANDSHAKE_DONE frame. |
| 2475 | * Return 1 if succeeded, 0 if not. |
| 2476 | */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2477 | 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] | 2478 | { |
| 2479 | int i; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2480 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2481 | struct quic_frame *frm; |
| 2482 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2483 | qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2484 | /* Only servers must send a HANDSHAKE_DONE frame. */ |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2485 | if (!objt_server(qc->conn->target)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2486 | frm = pool_alloc(pool_head_quic_frame); |
Frédéric Lécaille | 153d4a8 | 2021-01-06 12:12:39 +0100 | [diff] [blame] | 2487 | if (!frm) |
| 2488 | return 0; |
| 2489 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2490 | frm->type = QUIC_FT_HANDSHAKE_DONE; |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2491 | MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2492 | } |
| 2493 | |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2494 | 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] | 2495 | struct quic_connection_id *cid; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2496 | struct listener *l = __objt_listener(qc->conn->target); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2497 | |
| 2498 | frm = pool_alloc(pool_head_quic_frame); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2499 | if (!frm) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2500 | goto err; |
| 2501 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 2502 | cid = new_quic_cid(&qc->cids, qc, i); |
| 2503 | if (!cid) |
| 2504 | goto err; |
| 2505 | |
| 2506 | /* insert the allocated CID in the receiver tree */ |
| 2507 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2508 | ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len); |
| 2509 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 2510 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2511 | quic_connection_id_to_frm_cpy(frm, cid); |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2512 | MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | return 1; |
| 2516 | |
| 2517 | err: |
Frédéric Lécaille | 522c65c | 2021-08-03 14:29:03 +0200 | [diff] [blame] | 2518 | free_quic_conn_cids(qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2519 | return 0; |
| 2520 | } |
| 2521 | |
| 2522 | /* Deallocate <l> list of ACK ranges. */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2523 | void free_quic_arngs(struct quic_arngs *arngs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2524 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2525 | struct eb64_node *n; |
| 2526 | struct quic_arng_node *ar; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2527 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2528 | n = eb64_first(&arngs->root); |
| 2529 | while (n) { |
| 2530 | struct eb64_node *next; |
| 2531 | |
| 2532 | ar = eb64_entry(&n->node, struct quic_arng_node, first); |
| 2533 | next = eb64_next(n); |
| 2534 | eb64_delete(n); |
| 2535 | free(ar); |
| 2536 | n = next; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2537 | } |
| 2538 | } |
| 2539 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2540 | /* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in |
| 2541 | * descending order. |
| 2542 | */ |
| 2543 | static inline size_t sack_gap(struct quic_arng_node *p, |
| 2544 | struct quic_arng_node *q) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2545 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2546 | return p->first.key - q->last - 2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | |
| 2550 | /* Remove the last elements of <ack_ranges> list of ack range updating its |
| 2551 | * encoded size until it goes below <limit>. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2552 | * 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] | 2553 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2554 | 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] | 2555 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2556 | struct eb64_node *last, *prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2557 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2558 | last = eb64_last(&arngs->root); |
| 2559 | while (last && arngs->enc_sz > limit) { |
| 2560 | struct quic_arng_node *last_node, *prev_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2561 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2562 | prev = eb64_prev(last); |
| 2563 | if (!prev) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2564 | return 0; |
| 2565 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2566 | last_node = eb64_entry(&last->node, struct quic_arng_node, first); |
| 2567 | prev_node = eb64_entry(&prev->node, struct quic_arng_node, first); |
| 2568 | arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key); |
| 2569 | arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node)); |
| 2570 | arngs->enc_sz -= quic_decint_size_diff(arngs->sz); |
| 2571 | --arngs->sz; |
| 2572 | eb64_delete(last); |
| 2573 | pool_free(pool_head_quic_arng, last); |
| 2574 | last = prev; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | return 1; |
| 2578 | } |
| 2579 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2580 | /* Set the encoded size of <arngs> QUIC ack ranges. */ |
| 2581 | static void quic_arngs_set_enc_sz(struct quic_arngs *arngs) |
| 2582 | { |
| 2583 | struct eb64_node *node, *next; |
| 2584 | struct quic_arng_node *ar, *ar_next; |
| 2585 | |
| 2586 | node = eb64_last(&arngs->root); |
| 2587 | if (!node) |
| 2588 | return; |
| 2589 | |
| 2590 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2591 | arngs->enc_sz = quic_int_getsize(ar->last) + |
| 2592 | quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1); |
| 2593 | |
| 2594 | while ((next = eb64_prev(node))) { |
| 2595 | ar_next = eb64_entry(&next->node, struct quic_arng_node, first); |
| 2596 | arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) + |
| 2597 | quic_int_getsize(ar_next->last - ar_next->first.key); |
| 2598 | node = next; |
| 2599 | ar = eb64_entry(&node->node, struct quic_arng_node, first); |
| 2600 | } |
| 2601 | } |
| 2602 | |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2603 | /* Insert <ar> ack range into <argns> tree of ack ranges. |
| 2604 | * 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] | 2605 | */ |
| 2606 | static inline |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2607 | struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs, |
| 2608 | struct quic_arng *ar) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2609 | { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2610 | struct quic_arng_node *new_ar; |
| 2611 | |
| 2612 | new_ar = pool_alloc(pool_head_quic_arng); |
| 2613 | if (new_ar) { |
| 2614 | new_ar->first.key = ar->first; |
| 2615 | new_ar->last = ar->last; |
| 2616 | eb64_insert(&arngs->root, &new_ar->first); |
| 2617 | arngs->sz++; |
| 2618 | } |
| 2619 | |
| 2620 | return new_ar; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | /* 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] | 2624 | * 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] | 2625 | * this tree of ACK ranges in descending order. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2626 | * |
| 2627 | * Descending order |
| 2628 | * -------------> |
| 2629 | * range1 range2 |
| 2630 | * ..........|--------|..............|--------| |
| 2631 | * ^ ^ ^ ^ |
| 2632 | * | | | | |
| 2633 | * last1 first1 last2 first2 |
| 2634 | * ..........+--------+--------------+--------+...... |
| 2635 | * diff1 gap12 diff2 |
| 2636 | * |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2637 | * To encode the previous list of ranges we must encode integers as follows in |
| 2638 | * descending order: |
| 2639 | * enc(last2),enc(diff2),enc(gap12),enc(diff1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2640 | * with diff1 = last1 - first1 |
| 2641 | * diff2 = last2 - first2 |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2642 | * gap12 = first1 - last2 - 2 (>= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2643 | * |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2644 | */ |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2645 | int quic_update_ack_ranges_list(struct quic_arngs *arngs, |
| 2646 | struct quic_arng *ar) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2647 | { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2648 | struct eb64_node *le; |
| 2649 | struct quic_arng_node *new_node; |
| 2650 | struct eb64_node *new; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2651 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2652 | new = NULL; |
| 2653 | if (eb_is_empty(&arngs->root)) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2654 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2655 | if (!new_node) |
| 2656 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2657 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2658 | goto out; |
| 2659 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2660 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2661 | le = eb64_lookup_le(&arngs->root, ar->first); |
| 2662 | if (!le) { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2663 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2664 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2665 | return 0; |
Frédéric Lécaille | 0e25783 | 2021-11-16 10:54:19 +0100 | [diff] [blame] | 2666 | |
| 2667 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2668 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2669 | else { |
| 2670 | struct quic_arng_node *le_ar = |
| 2671 | eb64_entry(&le->node, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2672 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2673 | /* Already existing range */ |
Frédéric Lécaille | d3f4dd8 | 2021-06-02 15:36:12 +0200 | [diff] [blame] | 2674 | if (le_ar->last >= ar->last) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2675 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2676 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2677 | if (le_ar->last + 1 >= ar->first) { |
| 2678 | le_ar->last = ar->last; |
| 2679 | new = le; |
| 2680 | new_node = le_ar; |
| 2681 | } |
| 2682 | else { |
Frédéric Lécaille | 9ef64cd | 2021-06-02 15:27:34 +0200 | [diff] [blame] | 2683 | new_node = quic_insert_new_range(arngs, ar); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2684 | if (!new_node) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2685 | return 0; |
Frédéric Lécaille | 8ba4276 | 2021-06-02 17:40:09 +0200 | [diff] [blame] | 2686 | |
| 2687 | new = &new_node->first; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2688 | } |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2689 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2690 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2691 | /* Verify that the new inserted node does not overlap the nodes |
| 2692 | * which follow it. |
| 2693 | */ |
| 2694 | if (new) { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2695 | struct eb64_node *next; |
| 2696 | struct quic_arng_node *next_node; |
| 2697 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2698 | while ((next = eb64_next(new))) { |
| 2699 | next_node = |
| 2700 | eb64_entry(&next->node, struct quic_arng_node, first); |
Frédéric Lécaille | c825eba | 2021-06-02 17:38:13 +0200 | [diff] [blame] | 2701 | if (new_node->last + 1 < next_node->first.key) |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2702 | break; |
| 2703 | |
| 2704 | if (next_node->last > new_node->last) |
| 2705 | new_node->last = next_node->last; |
| 2706 | eb64_delete(next); |
Frédéric Lécaille | baea284 | 2021-06-02 15:04:03 +0200 | [diff] [blame] | 2707 | pool_free(pool_head_quic_arng, next_node); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2708 | /* Decrement the size of these ranges. */ |
| 2709 | arngs->sz--; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2710 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2711 | } |
| 2712 | |
Frédéric Lécaille | 82b8652 | 2021-08-10 09:54:03 +0200 | [diff] [blame] | 2713 | out: |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2714 | quic_arngs_set_enc_sz(arngs); |
| 2715 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2716 | return 1; |
| 2717 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2718 | /* Remove the header protection of packets at <el> encryption level. |
| 2719 | * Always succeeds. |
| 2720 | */ |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 2721 | static inline void qc_rm_hp_pkts(struct quic_enc_level *el, struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2722 | { |
| 2723 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2724 | struct quic_rx_packet *pqpkt; |
| 2725 | struct mt_list *pkttmp1, pkttmp2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2726 | struct quic_enc_level *app_qel; |
| 2727 | |
| 2728 | TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn); |
| 2729 | app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
| 2730 | /* A server must not process incoming 1-RTT packets before the handshake is complete. */ |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2731 | if (el == app_qel && objt_listener(ctx->conn->target) && |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2732 | HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2733 | TRACE_PROTO("hp not removed (handshake not completed)", |
| 2734 | QUIC_EV_CONN_ELRMHP, ctx->conn); |
| 2735 | goto out; |
| 2736 | } |
| 2737 | tls_ctx = &el->tls_ctx; |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2738 | mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2739 | if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn, |
| 2740 | pqpkt->data + pqpkt->pn_offset, |
| 2741 | pqpkt->data, pqpkt->data + pqpkt->len, ctx)) { |
| 2742 | TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn); |
| 2743 | /* XXX TO DO XXX */ |
| 2744 | } |
| 2745 | else { |
| 2746 | /* The AAD includes the packet number field */ |
| 2747 | pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl; |
| 2748 | /* Store the packet into the tree of packets to decrypt. */ |
| 2749 | pqpkt->pn_node.key = pqpkt->pn; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 2750 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 2751 | eb64_insert(&el->rx.pkts, &pqpkt->pn_node); |
| 2752 | quic_rx_packet_refinc(pqpkt); |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 2753 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2754 | TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt); |
| 2755 | } |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2756 | MT_LIST_DELETE_SAFE(pkttmp1); |
| 2757 | quic_rx_packet_refdec(pqpkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | out: |
| 2761 | TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn); |
| 2762 | } |
| 2763 | |
| 2764 | /* Process all the CRYPTO frame at <el> encryption level. |
| 2765 | * Return 1 if succeeded, 0 if not. |
| 2766 | */ |
| 2767 | 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] | 2768 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2769 | { |
| 2770 | struct eb64_node *node; |
| 2771 | |
| 2772 | TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn); |
| 2773 | node = eb64_first(&el->rx.crypto.frms); |
| 2774 | while (node) { |
| 2775 | struct quic_rx_crypto_frm *cf; |
| 2776 | |
| 2777 | cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node); |
| 2778 | if (cf->offset_node.key != el->rx.crypto.offset) |
| 2779 | break; |
| 2780 | |
| 2781 | if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) |
| 2782 | goto err; |
| 2783 | |
| 2784 | node = eb64_next(node); |
| 2785 | quic_rx_packet_refdec(cf->pkt); |
| 2786 | eb64_delete(&cf->offset_node); |
| 2787 | pool_free(pool_head_quic_rx_crypto_frm, cf); |
| 2788 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2789 | TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn); |
| 2790 | return 1; |
| 2791 | |
| 2792 | err: |
| 2793 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn); |
| 2794 | return 0; |
| 2795 | } |
| 2796 | |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 2797 | /* Process all the packets at <el> and <next_el> encryption level. |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 2798 | * 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] | 2799 | * as pointer value. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2800 | * Return 1 if succeeded, 0 if not. |
| 2801 | */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2802 | int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el, |
| 2803 | struct ssl_sock_ctx *ctx, int force_ack) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2804 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2805 | struct eb64_node *node; |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 2806 | int64_t largest_pn = -1; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2807 | struct quic_conn *qc = ctx->conn->qc; |
| 2808 | struct quic_enc_level *qel = cur_el; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2809 | |
| 2810 | TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2811 | qel = cur_el; |
| 2812 | next_tel: |
| 2813 | if (!qel) |
| 2814 | goto out; |
| 2815 | |
| 2816 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 2817 | node = eb64_first(&qel->rx.pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2818 | while (node) { |
| 2819 | struct quic_rx_packet *pkt; |
| 2820 | |
| 2821 | 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] | 2822 | TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS, |
| 2823 | ctx->conn, pkt, NULL, ctx->ssl); |
| 2824 | if (!qc_pkt_decrypt(pkt, &qel->tls_ctx)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2825 | /* Drop the packet */ |
| 2826 | TRACE_PROTO("packet decryption failed -> dropped", |
| 2827 | QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt); |
| 2828 | } |
| 2829 | else { |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2830 | if (!qc_parse_pkt_frms(pkt, ctx, qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2831 | /* Drop the packet */ |
| 2832 | TRACE_PROTO("packet parsing failed -> dropped", |
| 2833 | QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt); |
| 2834 | } |
| 2835 | else { |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 2836 | struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn }; |
| 2837 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 2838 | if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING && |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2839 | (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack)) |
| 2840 | HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT); |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 2841 | if (pkt->pn > largest_pn) |
| 2842 | largest_pn = pkt->pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2843 | /* Update the list of ranges to acknowledge. */ |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2844 | 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] | 2845 | TRACE_DEVEL("Could not update ack range list", |
| 2846 | QUIC_EV_CONN_ELRXPKTS, ctx->conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2847 | } |
| 2848 | } |
| 2849 | node = eb64_next(node); |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 2850 | eb64_delete(&pkt->pn_node); |
| 2851 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2852 | } |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2853 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2854 | |
Frédéric Lécaille | 120ea6f | 2021-07-26 16:42:56 +0200 | [diff] [blame] | 2855 | /* Update the largest packet number. */ |
| 2856 | if (largest_pn != -1) |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2857 | HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn); |
| 2858 | if (!qc_treat_rx_crypto_frms(qel, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2859 | goto err; |
| 2860 | |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2861 | if (qel == cur_el) { |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 2862 | BUG_ON(qel == next_el); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2863 | qel = next_el; |
| 2864 | goto next_tel; |
| 2865 | } |
| 2866 | |
| 2867 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2868 | TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn); |
| 2869 | return 1; |
| 2870 | |
| 2871 | err: |
| 2872 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn); |
| 2873 | return 0; |
| 2874 | } |
| 2875 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 2876 | /* QUIC connection packet handler task. */ |
| 2877 | 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] | 2878 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2879 | int ret, ssl_err; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 2880 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2881 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2882 | enum quic_tls_enc_level tel, next_tel; |
| 2883 | struct quic_enc_level *qel, *next_qel; |
| 2884 | struct quic_tls_ctx *tls_ctx; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2885 | struct qring *qr; // Tx ring |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2886 | int prev_st, st, force_ack; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2887 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 2888 | ctx = context; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2889 | qc = ctx->conn->qc; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2890 | qr = NULL; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2891 | st = HA_ATOMIC_LOAD(&qc->state); |
| 2892 | TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2893 | ssl_err = SSL_ERROR_NONE; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2894 | start: |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2895 | if (!quic_get_tls_enc_levels(&tel, &next_tel, st)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2896 | goto err; |
| 2897 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 2898 | qel = &qc->els[tel]; |
Frédéric Lécaille | f798096 | 2021-08-19 17:35:21 +0200 | [diff] [blame] | 2899 | 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] | 2900 | |
| 2901 | next_level: |
| 2902 | tls_ctx = &qel->tls_ctx; |
| 2903 | |
| 2904 | /* If the header protection key for this level has been derived, |
| 2905 | * remove the packet header protections. |
| 2906 | */ |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2907 | if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2908 | (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET)) |
| 2909 | qc_rm_hp_pkts(qel, ctx); |
| 2910 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 2911 | prev_st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2912 | force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] || |
| 2913 | qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 2914 | 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] | 2915 | goto err; |
| 2916 | |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 2917 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | 754f99e | 2021-08-19 15:35:59 +0200 | [diff] [blame] | 2918 | if (st >= QUIC_HS_ST_COMPLETE && |
| 2919 | (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) { |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 2920 | /* Discard the Handshake keys. */ |
| 2921 | quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]); |
Frédéric Lécaille | acd43a5 | 2021-09-20 11:18:24 +0200 | [diff] [blame] | 2922 | TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, ctx->conn); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 2923 | quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc); |
| 2924 | qc_set_timer(ctx); |
| 2925 | if (!quic_build_post_handshake_frames(qc)) |
| 2926 | goto err; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 2927 | goto start; |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 2928 | } |
| 2929 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2930 | if (!qr) |
| 2931 | qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list); |
Frédéric Lécaille | e2660e6 | 2021-11-23 11:36:51 +0100 | [diff] [blame] | 2932 | ret = qc_prep_pkts(qr, ctx); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2933 | if (ret == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2934 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2935 | else if (ret == 0) |
| 2936 | goto skip_send; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2937 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2938 | if (!qc_send_ppkts(qr, ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2939 | goto err; |
| 2940 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2941 | skip_send: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2942 | /* Check if there is something to do for the next level. |
| 2943 | */ |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 2944 | if (next_qel && next_qel != qel && |
| 2945 | (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) && |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2946 | (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2947 | qel = next_qel; |
Frédéric Lécaille | 3230bcf | 2021-09-22 15:15:46 +0200 | [diff] [blame] | 2948 | next_qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2949 | goto next_level; |
| 2950 | } |
| 2951 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2952 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2953 | TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 2954 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2955 | |
| 2956 | err: |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 2957 | if (qr) |
| 2958 | MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list); |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 2959 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err); |
Frédéric Lécaille | 91ae7aa | 2021-08-03 16:45:39 +0200 | [diff] [blame] | 2960 | return t; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2961 | } |
| 2962 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2963 | /* Uninitialize <qel> QUIC encryption level. Never fails. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2964 | static void quic_conn_enc_level_uninit(struct quic_enc_level *qel) |
| 2965 | { |
| 2966 | int i; |
| 2967 | |
| 2968 | for (i = 0; i < qel->tx.crypto.nb_buf; i++) { |
| 2969 | if (qel->tx.crypto.bufs[i]) { |
| 2970 | pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]); |
| 2971 | qel->tx.crypto.bufs[i] = NULL; |
| 2972 | } |
| 2973 | } |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2974 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | /* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 2978 | * connection allocating everything needed. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2979 | * Returns 1 if succeeded, 0 if not. |
| 2980 | */ |
| 2981 | static int quic_conn_enc_level_init(struct quic_conn *qc, |
| 2982 | enum quic_tls_enc_level level) |
| 2983 | { |
| 2984 | struct quic_enc_level *qel; |
| 2985 | |
| 2986 | qel = &qc->els[level]; |
| 2987 | qel->level = quic_to_ssl_enc_level(level); |
| 2988 | qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL; |
| 2989 | qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL; |
| 2990 | qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL; |
| 2991 | qel->tls_ctx.rx.flags = 0; |
| 2992 | qel->tls_ctx.tx.flags = 0; |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 2993 | qel->tls_ctx.flags = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 2994 | |
| 2995 | qel->rx.pkts = EB_ROOT; |
Frédéric Lécaille | 98cdeb2 | 2021-07-26 16:38:14 +0200 | [diff] [blame] | 2996 | HA_RWLOCK_INIT(&qel->rx.pkts_rwlock); |
Frédéric Lécaille | a11d0e2 | 2021-06-07 14:38:18 +0200 | [diff] [blame] | 2997 | MT_LIST_INIT(&qel->rx.pqpkts); |
Frédéric Lécaille | 9054d1b | 2021-07-26 16:23:53 +0200 | [diff] [blame] | 2998 | qel->rx.crypto.offset = 0; |
| 2999 | qel->rx.crypto.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3000 | |
| 3001 | /* Allocate only one buffer. */ |
| 3002 | qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs); |
| 3003 | if (!qel->tx.crypto.bufs) |
| 3004 | goto err; |
| 3005 | |
| 3006 | qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf); |
| 3007 | if (!qel->tx.crypto.bufs[0]) |
| 3008 | goto err; |
| 3009 | |
| 3010 | qel->tx.crypto.bufs[0]->sz = 0; |
| 3011 | qel->tx.crypto.nb_buf = 1; |
| 3012 | |
| 3013 | qel->tx.crypto.sz = 0; |
| 3014 | qel->tx.crypto.offset = 0; |
| 3015 | |
| 3016 | return 1; |
| 3017 | |
| 3018 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3019 | ha_free(&qel->tx.crypto.bufs); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3020 | return 0; |
| 3021 | } |
| 3022 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3023 | /* Release all the memory allocated for <conn> QUIC connection. */ |
| 3024 | static void quic_conn_free(struct quic_conn *conn) |
| 3025 | { |
| 3026 | int i; |
| 3027 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3028 | if (!conn) |
| 3029 | return; |
| 3030 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3031 | free_quic_conn_cids(conn); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3032 | |
| 3033 | /* remove the connection from receiver cids trees */ |
Frédéric Lécaille | b0006ee | 2021-11-03 19:01:31 +0100 | [diff] [blame] | 3034 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &conn->li->rx.cids_lock); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3035 | ebmb_delete(&conn->odcid_node); |
| 3036 | ebmb_delete(&conn->scid_node); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3037 | |
Frédéric Lécaille | b0006ee | 2021-11-03 19:01:31 +0100 | [diff] [blame] | 3038 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &conn->li->rx.cids_lock); |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3039 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3040 | for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) |
| 3041 | quic_conn_enc_level_uninit(&conn->els[i]); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3042 | if (conn->timer_task) |
| 3043 | task_destroy(conn->timer_task); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3044 | pool_free(pool_head_quic_conn_rxbuf, conn->rx.buf.area); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3045 | pool_free(pool_head_quic_conn, conn); |
| 3046 | } |
| 3047 | |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 3048 | void quic_close(struct connection *conn, void *xprt_ctx) |
| 3049 | { |
| 3050 | struct ssl_sock_ctx *conn_ctx = xprt_ctx; |
| 3051 | struct quic_conn *qc = conn_ctx->conn->qc; |
| 3052 | quic_conn_free(qc); |
| 3053 | } |
| 3054 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3055 | /* Callback called upon loss detection and PTO timer expirations. */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3056 | 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] | 3057 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3058 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3059 | struct quic_conn *qc; |
| 3060 | struct quic_pktns *pktns; |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3061 | int st; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3062 | |
| 3063 | conn_ctx = task->context; |
| 3064 | qc = conn_ctx->conn->qc; |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 3065 | TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn, |
| 3066 | NULL, NULL, &qc->path->ifae_pkts); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3067 | task->expire = TICK_ETERNITY; |
| 3068 | pktns = quic_loss_pktns(qc); |
| 3069 | if (tick_isset(pktns->tx.loss_time)) { |
| 3070 | struct list lost_pkts = LIST_HEAD_INIT(lost_pkts); |
| 3071 | |
| 3072 | qc_packet_loss_lookup(pktns, qc, &lost_pkts); |
| 3073 | if (!LIST_ISEMPTY(&lost_pkts)) |
| 3074 | qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms); |
| 3075 | qc_set_timer(conn_ctx); |
| 3076 | goto out; |
| 3077 | } |
| 3078 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3079 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3080 | if (qc->path->in_flight) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3081 | pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3082 | pktns->tx.pto_probe = 1; |
| 3083 | } |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3084 | else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3085 | struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3086 | struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]; |
| 3087 | |
| 3088 | if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3089 | hel->pktns->tx.pto_probe = 1; |
| 3090 | if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET) |
| 3091 | iel->pktns->tx.pto_probe = 1; |
| 3092 | } |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 3093 | HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3094 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3095 | qc->path->loss.pto_count++; |
| 3096 | |
| 3097 | out: |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 3098 | TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3099 | |
| 3100 | return task; |
| 3101 | } |
| 3102 | |
| 3103 | /* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC |
| 3104 | * connections used to identify the first Initial packets of client connecting |
| 3105 | * to listeners. This parameter must be NULL for QUIC connections attached |
| 3106 | * to listeners. <dcid> is the destination connection ID with <dcid_len> as length. |
| 3107 | * <scid> is the source connection ID with <scid_len> as length. |
| 3108 | * Returns 1 if succeeded, 0 if not. |
| 3109 | */ |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3110 | static struct quic_conn *qc_new_conn(unsigned int version, int ipv4, |
| 3111 | unsigned char *dcid, size_t dcid_len, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3112 | 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] | 3113 | { |
| 3114 | int i; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3115 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3116 | /* Initial CID. */ |
| 3117 | struct quic_connection_id *icid; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3118 | char *buf_area; |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3119 | struct listener *l = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3120 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3121 | TRACE_ENTER(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3122 | qc = pool_zalloc(pool_head_quic_conn); |
| 3123 | if (!qc) { |
| 3124 | TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT); |
| 3125 | goto err; |
| 3126 | } |
| 3127 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3128 | buf_area = pool_alloc(pool_head_quic_conn_rxbuf); |
| 3129 | if (!buf_area) { |
| 3130 | TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT); |
| 3131 | goto err; |
| 3132 | } |
| 3133 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3134 | qc->cids = EB_ROOT; |
| 3135 | /* QUIC Server (or listener). */ |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3136 | if (server) { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3137 | l = owner; |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3138 | |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3139 | HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3140 | /* Copy the initial DCID. */ |
| 3141 | qc->odcid.len = dcid_len; |
| 3142 | if (qc->odcid.len) |
| 3143 | memcpy(qc->odcid.data, dcid, dcid_len); |
| 3144 | |
Amaury Denoyelle | 42b9f1c | 2021-11-24 15:29:53 +0100 | [diff] [blame] | 3145 | /* 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] | 3146 | if (scid_len) |
| 3147 | memcpy(qc->dcid.data, scid, scid_len); |
| 3148 | qc->dcid.len = scid_len; |
Frédéric Lécaille | c1029f6 | 2021-10-20 11:09:58 +0200 | [diff] [blame] | 3149 | qc->tx.qring_list = &l->rx.tx_qring_list; |
Amaury Denoyelle | 2af1985 | 2021-09-30 11:03:28 +0200 | [diff] [blame] | 3150 | qc->li = l; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3151 | } |
| 3152 | /* QUIC Client (outgoing connection to servers) */ |
| 3153 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3154 | 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] | 3155 | if (dcid_len) |
| 3156 | memcpy(qc->dcid.data, dcid, dcid_len); |
| 3157 | qc->dcid.len = dcid_len; |
| 3158 | } |
| 3159 | |
| 3160 | /* Initialize the output buffer */ |
| 3161 | qc->obuf.pos = qc->obuf.data; |
| 3162 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3163 | icid = new_quic_cid(&qc->cids, qc, 0); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3164 | if (!icid) { |
| 3165 | TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT); |
| 3166 | goto err; |
| 3167 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3168 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3169 | /* insert the allocated CID in the receiver tree */ |
| 3170 | if (server) { |
| 3171 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3172 | ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len); |
| 3173 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
| 3174 | } |
| 3175 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3176 | /* Select our SCID which is the first CID with 0 as sequence number. */ |
| 3177 | qc->scid = icid->cid; |
| 3178 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3179 | /* Packet number spaces initialization. */ |
| 3180 | for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) |
| 3181 | quic_pktns_init(&qc->pktns[i]); |
| 3182 | /* QUIC encryption level context initialization. */ |
| 3183 | 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] | 3184 | if (!quic_conn_enc_level_init(qc, i)) { |
| 3185 | TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3186 | goto err; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3187 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3188 | /* Initialize the packet number space. */ |
| 3189 | qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)]; |
| 3190 | } |
| 3191 | |
Frédéric Lécaille | c8d3f87 | 2021-07-06 17:19:44 +0200 | [diff] [blame] | 3192 | qc->version = version; |
Frédéric Lécaille | a956d15 | 2021-11-10 09:24:22 +0100 | [diff] [blame] | 3193 | qc->tps_tls_ext = qc->version & 0xff000000 ? |
| 3194 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT: |
| 3195 | TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3196 | /* TX part. */ |
| 3197 | LIST_INIT(&qc->tx.frms_to_send); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3198 | qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB; |
| 3199 | qc->tx.wbuf = qc->tx.rbuf = 0; |
| 3200 | qc->tx.bytes = 0; |
| 3201 | qc->tx.nb_pto_dgrams = 0; |
| 3202 | /* RX part. */ |
| 3203 | qc->rx.bytes = 0; |
Frédéric Lécaille | 2766e78 | 2021-08-30 17:16:07 +0200 | [diff] [blame] | 3204 | qc->rx.nb_ack_eliciting = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3205 | qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0); |
| 3206 | HA_RWLOCK_INIT(&qc->rx.buf_rwlock); |
| 3207 | LIST_INIT(&qc->rx.pkt_list); |
Frédéric Lécaille | 40df78f | 2021-11-30 10:59:37 +0100 | [diff] [blame^] | 3208 | if (!quic_tls_ku_init(qc)) { |
| 3209 | TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT); |
| 3210 | goto err; |
| 3211 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3212 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3213 | /* XXX TO DO: Only one path at this time. */ |
| 3214 | qc->path = &qc->paths[0]; |
| 3215 | quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc); |
| 3216 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3217 | TRACE_LEAVE(QUIC_EV_CONN_INIT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3218 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3219 | return qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3220 | |
| 3221 | err: |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3222 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3223 | quic_conn_free(qc); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3224 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | /* Initialize the timer task of <qc> QUIC connection. |
| 3228 | * Returns 1 if succeeded, 0 if not. |
| 3229 | */ |
| 3230 | static int quic_conn_init_timer(struct quic_conn *qc) |
| 3231 | { |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 3232 | qc->timer_task = task_new_anywhere(); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3233 | if (!qc->timer_task) |
| 3234 | return 0; |
| 3235 | |
| 3236 | qc->timer = TICK_ETERNITY; |
| 3237 | qc->timer_task->process = process_timer; |
| 3238 | qc->timer_task->context = qc->conn->xprt_ctx; |
| 3239 | |
| 3240 | return 1; |
| 3241 | } |
| 3242 | |
| 3243 | /* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end |
| 3244 | * past one byte of this buffer. |
| 3245 | */ |
| 3246 | static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end, |
| 3247 | struct quic_rx_packet *pkt) |
| 3248 | { |
| 3249 | unsigned char dcid_len, scid_len; |
| 3250 | |
| 3251 | /* Version */ |
| 3252 | if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end)) |
| 3253 | return 0; |
| 3254 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3255 | /* Destination Connection ID Length */ |
| 3256 | dcid_len = *(*buf)++; |
| 3257 | /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */ |
| 3258 | if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) |
| 3259 | /* XXX MUST BE DROPPED */ |
| 3260 | return 0; |
| 3261 | |
| 3262 | if (dcid_len) { |
| 3263 | /* Check that the length of this received DCID matches the CID lengths |
| 3264 | * of our implementation for non Initials packets only. |
| 3265 | */ |
| 3266 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN) |
| 3267 | return 0; |
| 3268 | |
| 3269 | memcpy(pkt->dcid.data, *buf, dcid_len); |
| 3270 | } |
| 3271 | |
| 3272 | pkt->dcid.len = dcid_len; |
| 3273 | *buf += dcid_len; |
| 3274 | |
| 3275 | /* Source Connection ID Length */ |
| 3276 | scid_len = *(*buf)++; |
| 3277 | if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) |
| 3278 | /* XXX MUST BE DROPPED */ |
| 3279 | return 0; |
| 3280 | |
| 3281 | if (scid_len) |
| 3282 | memcpy(pkt->scid.data, *buf, scid_len); |
| 3283 | pkt->scid.len = scid_len; |
| 3284 | *buf += scid_len; |
| 3285 | |
| 3286 | return 1; |
| 3287 | } |
| 3288 | |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3289 | /* If the header protection of <pkt> packet attached to <qc> connection with <ctx> |
| 3290 | * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated |
| 3291 | * encryption level matching with the packet type. <*qel> may be null if not found. |
| 3292 | * Note that <ctx> may be null (for Initial packets). |
| 3293 | */ |
| 3294 | static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt, |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3295 | struct quic_conn *qc, struct ssl_sock_ctx *ctx, |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3296 | struct quic_enc_level **qel) |
| 3297 | { |
| 3298 | enum quic_tls_enc_level tel; |
| 3299 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 3300 | /* Special case without connection context (first Initial packets) */ |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3301 | if (!ctx) { |
| 3302 | *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]; |
| 3303 | return 1; |
| 3304 | } |
| 3305 | |
| 3306 | tel = quic_packet_type_enc_level(pkt->type); |
| 3307 | if (tel == QUIC_TLS_ENC_LEVEL_NONE) { |
| 3308 | *qel = NULL; |
| 3309 | return 0; |
| 3310 | } |
| 3311 | |
| 3312 | *qel = &qc->els[tel]; |
| 3313 | if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 3314 | TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn); |
| 3315 | return 0; |
| 3316 | } |
| 3317 | |
| 3318 | if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) && |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 3319 | (tel != QUIC_TLS_ENC_LEVEL_APP || |
| 3320 | HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE)) |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3321 | return 1; |
| 3322 | |
| 3323 | return 0; |
| 3324 | } |
| 3325 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3326 | /* Insert <pkt> RX packet in its <qel> RX packets tree */ |
| 3327 | static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel) |
| 3328 | { |
| 3329 | pkt->pn_node.key = pkt->pn; |
| 3330 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3331 | eb64_insert(&qel->rx.pkts, &pkt->pn_node); |
| 3332 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock); |
| 3333 | quic_rx_packet_refinc(pkt); |
| 3334 | } |
| 3335 | |
| 3336 | /* 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] | 3337 | * QUIC connection with <buf> as packet number field address, <end> a pointer to one |
| 3338 | * byte past the end of the buffer containing this packet and <beg> the address of |
| 3339 | * the packet first byte. |
| 3340 | * If succeeded, this function updates <*buf> to point to the next packet in the buffer. |
| 3341 | * Returns 1 if succeeded, 0 if not. |
| 3342 | */ |
| 3343 | static inline int qc_try_rm_hp(struct quic_rx_packet *pkt, |
| 3344 | unsigned char **buf, unsigned char *beg, |
| 3345 | const unsigned char *end, |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3346 | struct quic_conn *qc, struct quic_enc_level **el, |
| 3347 | struct ssl_sock_ctx *ctx) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3348 | { |
| 3349 | unsigned char *pn = NULL; /* Packet number field */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3350 | struct quic_enc_level *qel; |
| 3351 | /* Only for traces. */ |
| 3352 | struct quic_rx_packet *qpkt_trace; |
| 3353 | |
| 3354 | qpkt_trace = NULL; |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3355 | TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3356 | /* The packet number is here. This is also the start minus |
| 3357 | * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header |
| 3358 | * protection. |
| 3359 | */ |
| 3360 | pn = *buf; |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3361 | if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3362 | /* Note that the following function enables us to unprotect the packet |
| 3363 | * number and its length subsequently used to decrypt the entire |
| 3364 | * packets. |
| 3365 | */ |
| 3366 | if (!qc_do_rm_hp(pkt, &qel->tls_ctx, |
| 3367 | qel->pktns->rx.largest_pn, pn, beg, end, ctx)) { |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3368 | TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3369 | goto err; |
| 3370 | } |
| 3371 | |
| 3372 | /* The AAD includes the packet number field found at <pn>. */ |
| 3373 | pkt->aad_len = pn - beg + pkt->pnl; |
| 3374 | qpkt_trace = pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3375 | } |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3376 | else if (qel) { |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3377 | if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) { |
| 3378 | /* If the packet number space has been discarded, this packet |
| 3379 | * will be not parsed. |
| 3380 | */ |
| 3381 | TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt); |
| 3382 | goto out; |
| 3383 | } |
| 3384 | |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3385 | TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3386 | pkt->pn_offset = pn - beg; |
Frédéric Lécaille | ebc3fc1 | 2021-09-22 08:34:21 +0200 | [diff] [blame] | 3387 | MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list); |
| 3388 | quic_rx_packet_refinc(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3389 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3390 | else { |
| 3391 | TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL); |
| 3392 | goto err; |
| 3393 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3394 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3395 | *el = qel; |
| 3396 | /* No reference counter incrementation here!!! */ |
| 3397 | LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list); |
| 3398 | memcpy(b_tail(&qc->rx.buf), beg, pkt->len); |
| 3399 | pkt->data = (unsigned char *)b_tail(&qc->rx.buf); |
| 3400 | b_add(&qc->rx.buf, pkt->len); |
| 3401 | out: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3402 | /* Updtate the offset of <*buf> for the next QUIC packet. */ |
| 3403 | *buf = beg + pkt->len; |
| 3404 | |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3405 | TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3406 | return 1; |
| 3407 | |
| 3408 | err: |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3409 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3410 | return 0; |
| 3411 | } |
| 3412 | |
| 3413 | /* Parse the header form from <byte0> first byte of <pkt> pacekt to set type. |
| 3414 | * Also set <*long_header> to 1 if this form is long, 0 if not. |
| 3415 | */ |
| 3416 | static inline void qc_parse_hd_form(struct quic_rx_packet *pkt, |
| 3417 | unsigned char byte0, int *long_header) |
| 3418 | { |
| 3419 | if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) { |
| 3420 | pkt->type = |
| 3421 | (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK; |
| 3422 | *long_header = 1; |
| 3423 | } |
| 3424 | else { |
| 3425 | pkt->type = QUIC_PACKET_TYPE_SHORT; |
| 3426 | *long_header = 0; |
| 3427 | } |
| 3428 | } |
| 3429 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3430 | /* |
| 3431 | * Check if the QUIC version in packet <pkt> is supported. Returns a boolean. |
| 3432 | */ |
| 3433 | static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt) |
| 3434 | { |
| 3435 | int j = 0, version; |
| 3436 | |
| 3437 | do { |
| 3438 | version = quic_supported_version[j]; |
| 3439 | if (version == pkt->version) |
| 3440 | return 1; |
| 3441 | |
| 3442 | version = quic_supported_version[++j]; |
| 3443 | } while(version); |
| 3444 | |
| 3445 | return 0; |
| 3446 | } |
| 3447 | |
Frédéric Lécaille | c5c69a0 | 2021-10-20 17:24:42 +0200 | [diff] [blame] | 3448 | __attribute__((unused)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3449 | static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end, |
| 3450 | struct quic_rx_packet *pkt, |
| 3451 | struct quic_dgram_ctx *dgram_ctx, |
| 3452 | struct sockaddr_storage *saddr) |
| 3453 | { |
| 3454 | unsigned char *beg; |
| 3455 | uint64_t len; |
| 3456 | struct quic_conn *qc; |
| 3457 | struct eb_root *cids; |
| 3458 | struct ebmb_node *node; |
| 3459 | struct connection *srv_conn; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3460 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3461 | int long_header; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3462 | size_t b_cspace; |
| 3463 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3464 | |
| 3465 | qc = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 3466 | qel = NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3467 | TRACE_ENTER(QUIC_EV_CONN_SPKT); |
| 3468 | if (end <= *buf) |
| 3469 | goto err; |
| 3470 | |
| 3471 | /* Fixed bit */ |
| 3472 | if (!(**buf & QUIC_PACKET_FIXED_BIT)) |
| 3473 | /* XXX TO BE DISCARDED */ |
| 3474 | goto err; |
| 3475 | |
| 3476 | srv_conn = dgram_ctx->owner; |
| 3477 | beg = *buf; |
| 3478 | /* Header form */ |
| 3479 | qc_parse_hd_form(pkt, *(*buf)++, &long_header); |
| 3480 | if (long_header) { |
| 3481 | size_t cid_lookup_len; |
| 3482 | |
| 3483 | if (!quic_packet_read_long_header(buf, end, pkt)) |
| 3484 | goto err; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3485 | |
| 3486 | /* unsupported QUIC version */ |
| 3487 | if (!qc_pkt_is_supported_version(pkt)) { |
| 3488 | TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT); |
| 3489 | goto err; |
| 3490 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3491 | |
| 3492 | /* For Initial packets, and for servers (QUIC clients connections), |
| 3493 | * there is no Initial connection IDs storage. |
| 3494 | */ |
| 3495 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3496 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
| 3497 | cid_lookup_len = pkt->dcid.len; |
| 3498 | } |
| 3499 | else { |
| 3500 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
| 3501 | cid_lookup_len = QUIC_CID_LEN; |
| 3502 | } |
| 3503 | |
| 3504 | node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len); |
| 3505 | if (!node) |
| 3506 | goto err; |
| 3507 | |
| 3508 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
| 3509 | |
| 3510 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3511 | qc->dcid.len = pkt->scid.len; |
| 3512 | if (pkt->scid.len) |
| 3513 | memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len); |
| 3514 | } |
| 3515 | |
| 3516 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
| 3517 | uint64_t token_len; |
| 3518 | |
| 3519 | if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len) |
| 3520 | goto err; |
| 3521 | |
| 3522 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 3523 | * A server which sends an Initial packet must not set the token. |
| 3524 | * So, a client which receives an Initial packet with a token |
| 3525 | * MUST discard the packet or generate a connection error with |
| 3526 | * PROTOCOL_VIOLATION as type. |
| 3527 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 3528 | */ |
| 3529 | pkt->token_len = token_len; |
| 3530 | } |
| 3531 | } |
| 3532 | else { |
| 3533 | /* XXX TO DO: Short header XXX */ |
| 3534 | if (end - *buf < QUIC_CID_LEN) |
| 3535 | goto err; |
| 3536 | |
| 3537 | cids = &((struct server *)__objt_server(srv_conn->target))->cids; |
| 3538 | node = ebmb_lookup(cids, *buf, QUIC_CID_LEN); |
| 3539 | if (!node) |
| 3540 | goto err; |
| 3541 | |
| 3542 | qc = ebmb_entry(node, struct quic_conn, scid_node); |
| 3543 | *buf += QUIC_CID_LEN; |
| 3544 | } |
| 3545 | /* Store the DCID used for this packet to check the packet which |
| 3546 | * come in this UDP datagram match with it. |
| 3547 | */ |
| 3548 | if (!dgram_ctx->dcid_node) |
| 3549 | dgram_ctx->dcid_node = node; |
| 3550 | /* Only packets packets with long headers and not RETRY or VERSION as type |
| 3551 | * have a length field. |
| 3552 | */ |
| 3553 | if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) { |
| 3554 | if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len) |
| 3555 | goto err; |
| 3556 | |
| 3557 | pkt->len = len; |
| 3558 | } |
| 3559 | else if (!long_header) { |
| 3560 | /* A short packet is the last one of an UDP datagram. */ |
| 3561 | pkt->len = end - *buf; |
| 3562 | } |
| 3563 | |
| 3564 | conn_ctx = qc->conn->xprt_ctx; |
| 3565 | |
| 3566 | /* Increase the total length of this packet by the header length. */ |
| 3567 | pkt->len += *buf - beg; |
| 3568 | /* Do not check the DCID node before the length. */ |
| 3569 | if (dgram_ctx->dcid_node != node) { |
| 3570 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn); |
| 3571 | goto err; |
| 3572 | } |
| 3573 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3574 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3575 | b_cspace = b_contig_space(&qc->rx.buf); |
| 3576 | if (b_cspace < pkt->len) { |
| 3577 | /* Let us consume the remaining contiguous space. */ |
| 3578 | b_add(&qc->rx.buf, b_cspace); |
| 3579 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 3580 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3581 | TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len); |
| 3582 | goto err; |
| 3583 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3584 | } |
| 3585 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3586 | if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) { |
| 3587 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3588 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3589 | goto err; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3590 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3591 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3592 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3593 | if (pkt->aad_len) |
| 3594 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3595 | /* Wake the tasklet of the QUIC connection packet handler. */ |
| 3596 | if (conn_ctx) |
| 3597 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
| 3598 | |
| 3599 | TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn); |
| 3600 | |
| 3601 | return pkt->len; |
| 3602 | |
| 3603 | err: |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 3604 | TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3605 | return -1; |
| 3606 | } |
| 3607 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3608 | /* |
| 3609 | * Send a Version Negotiation packet on response to <pkt> on socket <fd> to |
| 3610 | * address <addr>. |
| 3611 | * Implementation of RFC9000 6. Version Negotiation |
| 3612 | * |
| 3613 | * TODO implement a rate-limiting sending of Version Negotiation packets |
| 3614 | * |
| 3615 | * Returns 0 on success else non-zero |
| 3616 | */ |
| 3617 | static int qc_send_version_negotiation(int fd, struct sockaddr_storage *addr, |
| 3618 | struct quic_rx_packet *pkt) |
| 3619 | { |
| 3620 | char buf[256]; |
| 3621 | int i = 0, j, version; |
| 3622 | const socklen_t addrlen = get_addr_len(addr); |
| 3623 | |
| 3624 | /* |
| 3625 | * header form |
| 3626 | * long header, fixed bit to 0 for Version Negotiation |
| 3627 | */ |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3628 | if (RAND_bytes((unsigned char *)buf, 1) != 1) |
| 3629 | return 1; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3630 | |
Frédéric Lécaille | ea78ee1 | 2021-11-18 13:54:43 +0100 | [diff] [blame] | 3631 | buf[i++] |= '\x80'; |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3632 | /* null version for Version Negotiation */ |
| 3633 | buf[i++] = '\x00'; |
| 3634 | buf[i++] = '\x00'; |
| 3635 | buf[i++] = '\x00'; |
| 3636 | buf[i++] = '\x00'; |
| 3637 | |
| 3638 | /* source connection id */ |
| 3639 | buf[i++] = pkt->scid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3640 | memcpy(&buf[i], pkt->scid.data, pkt->scid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3641 | i += pkt->scid.len; |
| 3642 | |
| 3643 | /* destination connection id */ |
| 3644 | buf[i++] = pkt->dcid.len; |
Amaury Denoyelle | 10eed8e | 2021-11-18 13:48:57 +0100 | [diff] [blame] | 3645 | memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len); |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3646 | i += pkt->dcid.len; |
| 3647 | |
| 3648 | /* supported version */ |
| 3649 | j = 0; |
| 3650 | do { |
| 3651 | version = htonl(quic_supported_version[j]); |
| 3652 | memcpy(&buf[i], &version, sizeof(version)); |
| 3653 | i += sizeof(version); |
| 3654 | |
| 3655 | version = quic_supported_version[++j]; |
| 3656 | } while (version); |
| 3657 | |
| 3658 | if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) |
| 3659 | return 1; |
| 3660 | |
| 3661 | return 0; |
| 3662 | } |
| 3663 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3664 | static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end, |
| 3665 | struct quic_rx_packet *pkt, |
| 3666 | struct quic_dgram_ctx *dgram_ctx, |
| 3667 | struct sockaddr_storage *saddr) |
| 3668 | { |
| 3669 | unsigned char *beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3670 | struct quic_conn *qc; |
| 3671 | struct eb_root *cids; |
| 3672 | struct ebmb_node *node; |
| 3673 | struct listener *l; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 3674 | struct ssl_sock_ctx *conn_ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3675 | int long_header = 0; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3676 | size_t b_cspace; |
| 3677 | struct quic_enc_level *qel; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3678 | |
| 3679 | qc = NULL; |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 3680 | conn_ctx = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 3681 | qel = NULL; |
Frédéric Lécaille | 2e7ffc9 | 2021-06-10 08:18:45 +0200 | [diff] [blame] | 3682 | TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3683 | if (end <= *buf) |
| 3684 | goto err; |
| 3685 | |
| 3686 | /* Fixed bit */ |
| 3687 | if (!(**buf & QUIC_PACKET_FIXED_BIT)) { |
| 3688 | /* XXX TO BE DISCARDED */ |
| 3689 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3690 | goto err; |
| 3691 | } |
| 3692 | |
| 3693 | l = dgram_ctx->owner; |
| 3694 | beg = *buf; |
| 3695 | /* Header form */ |
| 3696 | qc_parse_hd_form(pkt, *(*buf)++, &long_header); |
| 3697 | if (long_header) { |
| 3698 | unsigned char dcid_len; |
| 3699 | |
| 3700 | if (!quic_packet_read_long_header(buf, end, pkt)) { |
| 3701 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3702 | goto err; |
| 3703 | } |
| 3704 | |
Amaury Denoyelle | a22d860 | 2021-11-10 15:17:56 +0100 | [diff] [blame] | 3705 | /* RFC9000 6. Version Negotiation */ |
| 3706 | if (!qc_pkt_is_supported_version(pkt)) { |
| 3707 | /* do not send Version Negotiation in response to a |
| 3708 | * Version Negotiation packet. |
| 3709 | */ |
| 3710 | if (!pkt->version) { |
| 3711 | TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT); |
| 3712 | goto err; |
| 3713 | } |
| 3714 | |
| 3715 | /* unsupported version, send Negotiation packet */ |
| 3716 | if (qc_send_version_negotiation(l->rx.fd, saddr, pkt)) { |
| 3717 | TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT); |
| 3718 | goto err; |
| 3719 | } |
| 3720 | |
| 3721 | TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT); |
| 3722 | goto out; |
| 3723 | } |
| 3724 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3725 | dcid_len = pkt->dcid.len; |
| 3726 | /* For Initial packets, and for servers (QUIC clients connections), |
| 3727 | * there is no Initial connection IDs storage. |
| 3728 | */ |
| 3729 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) { |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 3730 | uint64_t token_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3731 | /* DCIDs of first packets coming from clients may have the same values. |
| 3732 | * Let's distinguish them concatenating the socket addresses to the DCIDs. |
| 3733 | */ |
| 3734 | quic_cid_saddr_cat(&pkt->dcid, saddr); |
| 3735 | cids = &l->rx.odcids; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 3736 | |
| 3737 | if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || |
| 3738 | end - *buf < token_len) { |
| 3739 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3740 | goto err; |
| 3741 | } |
| 3742 | |
| 3743 | /* XXX TO DO XXX 0 value means "the token is not present". |
| 3744 | * A server which sends an Initial packet must not set the token. |
| 3745 | * So, a client which receives an Initial packet with a token |
| 3746 | * MUST discard the packet or generate a connection error with |
| 3747 | * PROTOCOL_VIOLATION as type. |
| 3748 | * The token must be provided in a Retry packet or NEW_TOKEN frame. |
| 3749 | */ |
| 3750 | pkt->token_len = token_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3751 | } |
| 3752 | else { |
| 3753 | if (pkt->dcid.len != QUIC_CID_LEN) { |
| 3754 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3755 | goto err; |
| 3756 | } |
| 3757 | |
| 3758 | cids = &l->rx.cids; |
| 3759 | } |
| 3760 | |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 3761 | /* Only packets packets with long headers and not RETRY or VERSION as type |
| 3762 | * have a length field. |
| 3763 | */ |
| 3764 | if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) { |
| 3765 | uint64_t len; |
| 3766 | |
| 3767 | if (!quic_dec_int(&len, (const unsigned char **)buf, end) || |
| 3768 | end - *buf < len) { |
| 3769 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3770 | goto err; |
| 3771 | } |
| 3772 | |
| 3773 | pkt->len = len; |
| 3774 | } |
| 3775 | |
| 3776 | |
| 3777 | HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3778 | node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len); |
| 3779 | if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN && |
| 3780 | cids == &l->rx.odcids) { |
| 3781 | /* Switch to the definitive tree ->cids containing the final CIDs. */ |
| 3782 | node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len); |
| 3783 | if (node) { |
| 3784 | /* If found, signal this with NULL as special value for <cids>. */ |
| 3785 | pkt->dcid.len = dcid_len; |
| 3786 | cids = NULL; |
| 3787 | } |
| 3788 | } |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 3789 | HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3790 | |
| 3791 | if (!node) { |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3792 | int ipv4; |
| 3793 | struct quic_cid *odcid; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 3794 | struct ebmb_node *n = NULL; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 3795 | const unsigned char *salt = initial_salt_v1; |
| 3796 | size_t salt_len = sizeof initial_salt_v1; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3797 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3798 | if (pkt->type != QUIC_PACKET_TYPE_INITIAL) { |
| 3799 | TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT); |
| 3800 | goto err; |
| 3801 | } |
| 3802 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3803 | pkt->saddr = *saddr; |
| 3804 | /* Note that here, odcid_len equals to pkt->dcid.len minus the length |
| 3805 | * of <saddr>. |
| 3806 | */ |
| 3807 | pkt->odcid_len = dcid_len; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3808 | ipv4 = saddr->ss_family == AF_INET; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3809 | qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 3810 | pkt->scid.data, pkt->scid.len, 1, l); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 3811 | if (qc == NULL) |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 3812 | goto err; |
| 3813 | |
| 3814 | odcid = &qc->rx.params.original_destination_connection_id; |
| 3815 | /* Copy the transport parameters. */ |
| 3816 | qc->rx.params = l->bind_conf->quic_params; |
| 3817 | /* Copy original_destination_connection_id transport parameter. */ |
| 3818 | memcpy(odcid->data, &pkt->dcid, pkt->odcid_len); |
| 3819 | odcid->len = pkt->odcid_len; |
| 3820 | /* Copy the initial source connection ID. */ |
| 3821 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 3822 | qc->enc_params_len = |
| 3823 | quic_transport_params_encode(qc->enc_params, |
| 3824 | qc->enc_params + sizeof qc->enc_params, |
| 3825 | &qc->rx.params, 1); |
| 3826 | if (!qc->enc_params_len) |
| 3827 | goto err; |
| 3828 | |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 3829 | /* NOTE: the socket address has been concatenated to the destination ID |
| 3830 | * chosen by the client for Initial packets. |
| 3831 | */ |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 3832 | if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) { |
| 3833 | salt = initial_salt_draft_29; |
| 3834 | salt_len = sizeof initial_salt_draft_29; |
| 3835 | } |
| 3836 | if (!qc_new_isecs(qc, salt, salt_len, |
| 3837 | pkt->dcid.data, pkt->odcid_len, 1)) { |
Frédéric Lécaille | 497fa78 | 2021-05-31 15:16:13 +0200 | [diff] [blame] | 3838 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn); |
| 3839 | goto err; |
| 3840 | } |
| 3841 | |
Frédéric Lécaille | b0006ee | 2021-11-03 19:01:31 +0100 | [diff] [blame] | 3842 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 3843 | /* Insert the DCID the QUIC client has chosen (only for listeners) */ |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 3844 | n = ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len); |
Frédéric Lécaille | b0006ee | 2021-11-03 19:01:31 +0100 | [diff] [blame] | 3845 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock); |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 3846 | |
Amaury Denoyelle | aff4ec8 | 2021-11-24 15:16:08 +0100 | [diff] [blame] | 3847 | /* If the insertion failed, it means that another |
| 3848 | * thread has already allocated a QUIC connection for |
| 3849 | * the same CID. Liberate our allocated connection. |
| 3850 | */ |
| 3851 | if (unlikely(n != &qc->odcid_node)) { |
| 3852 | quic_conn_free(qc); |
| 3853 | qc = ebmb_entry(n, struct quic_conn, odcid_node); |
| 3854 | pkt->qc = qc; |
| 3855 | } |
| 3856 | else { |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 3857 | /* Enqueue this packet. */ |
Frédéric Lécaille | f67b356 | 2021-11-15 16:21:40 +0100 | [diff] [blame] | 3858 | pkt->qc = qc; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 3859 | MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list); |
| 3860 | /* Try to accept a new connection. */ |
| 3861 | listener_accept(l); |
| 3862 | } |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 3863 | |
| 3864 | /* This is the DCID node sent in this packet by the client. */ |
| 3865 | node = &qc->odcid_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3866 | } |
| 3867 | else { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3868 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3869 | qc = ebmb_entry(node, struct quic_conn, odcid_node); |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3870 | } |
| 3871 | else { |
| 3872 | struct quic_connection_id *cid = ebmb_entry(node, struct quic_connection_id, node); |
| 3873 | qc = cid->qc; |
| 3874 | } |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 3875 | pkt->qc = qc; |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 3876 | if (HA_ATOMIC_LOAD(&qc->conn)) |
| 3877 | conn_ctx = HA_ATOMIC_LOAD(&qc->conn->xprt_ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3878 | } |
| 3879 | } |
| 3880 | else { |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3881 | struct quic_connection_id *cid; |
| 3882 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3883 | if (end - *buf < QUIC_CID_LEN) { |
| 3884 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3885 | goto err; |
| 3886 | } |
| 3887 | |
| 3888 | cids = &l->rx.cids; |
| 3889 | node = ebmb_lookup(cids, *buf, QUIC_CID_LEN); |
| 3890 | if (!node) { |
| 3891 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT); |
| 3892 | goto err; |
| 3893 | } |
| 3894 | |
Amaury Denoyelle | d6a352a | 2021-11-24 15:32:46 +0100 | [diff] [blame] | 3895 | cid = ebmb_entry(node, struct quic_connection_id, node); |
| 3896 | qc = cid->qc; |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 3897 | if (HA_ATOMIC_LOAD(&qc->conn)) |
| 3898 | conn_ctx = HA_ATOMIC_LOAD(&qc->conn->xprt_ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3899 | *buf += QUIC_CID_LEN; |
Frédéric Lécaille | 8370c93 | 2021-11-08 17:01:46 +0100 | [diff] [blame] | 3900 | pkt->qc = qc; |
Frédéric Lécaille | f3d078d | 2021-06-14 14:18:10 +0200 | [diff] [blame] | 3901 | /* A short packet is the last one of an UDP datagram. */ |
| 3902 | pkt->len = end - *buf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | /* Store the DCID used for this packet to check the packet which |
| 3906 | * come in this UDP datagram match with it. |
| 3907 | */ |
| 3908 | if (!dgram_ctx->dcid_node) { |
| 3909 | dgram_ctx->dcid_node = node; |
| 3910 | dgram_ctx->qc = qc; |
| 3911 | } |
| 3912 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3913 | /* Increase the total length of this packet by the header length. */ |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3914 | pkt->raw_len = pkt->len += *buf - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3915 | /* Do not check the DCID node before the length. */ |
| 3916 | if (dgram_ctx->dcid_node != node) { |
| 3917 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn); |
| 3918 | goto err; |
| 3919 | } |
| 3920 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 3921 | if (HA_ATOMIC_LOAD(&qc->err_code)) { |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 3922 | TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc->conn); |
| 3923 | goto out; |
| 3924 | } |
| 3925 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3926 | HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3927 | b_cspace = b_contig_space(&qc->rx.buf); |
| 3928 | if (b_cspace < pkt->len) { |
| 3929 | /* Let us consume the remaining contiguous space. */ |
| 3930 | b_add(&qc->rx.buf, b_cspace); |
| 3931 | if (b_contig_space(&qc->rx.buf) < pkt->len) { |
| 3932 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
| 3933 | TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len); |
| 3934 | goto err; |
| 3935 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3936 | } |
| 3937 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3938 | if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) { |
| 3939 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Frédéric Lécaille | 1a5e88c | 2021-05-31 18:04:07 +0200 | [diff] [blame] | 3940 | TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn); |
| 3941 | goto err; |
| 3942 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3943 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3944 | HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock); |
Frédéric Lécaille | 2e7ffc9 | 2021-06-10 08:18:45 +0200 | [diff] [blame] | 3945 | TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 3946 | if (pkt->aad_len) |
| 3947 | qc_pkt_insert(pkt, qel); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 3948 | out: |
Frédéric Lécaille | 01abc46 | 2021-07-21 09:34:27 +0200 | [diff] [blame] | 3949 | /* Wake up the connection packet handler task from here only if all |
| 3950 | * the contexts have been initialized, especially the mux context |
| 3951 | * conn_ctx->conn->ctx. Note that this is ->start xprt callback which |
| 3952 | * will start it if these contexts for the connection are not already |
| 3953 | * initialized. |
| 3954 | */ |
| 3955 | if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3956 | tasklet_wakeup(conn_ctx->wait_event.tasklet); |
Frédéric Lécaille | d24c2ec | 2021-05-31 10:24:49 +0200 | [diff] [blame] | 3957 | |
Amaury Denoyelle | ed66b0f | 2021-11-18 14:38:00 +0100 | [diff] [blame] | 3958 | TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc->conn : NULL, pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3959 | |
| 3960 | return pkt->len; |
| 3961 | |
| 3962 | err: |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 3963 | TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3964 | qc ? qc->conn : NULL, pkt); |
| 3965 | return -1; |
| 3966 | } |
| 3967 | |
| 3968 | /* This function builds into <buf> buffer a QUIC long packet header whose size may be computed |
| 3969 | * in advance. This is the reponsability of the caller to check there is enough room in this |
| 3970 | * buffer to build a long header. |
| 3971 | * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded. |
| 3972 | */ |
| 3973 | static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end, |
| 3974 | int type, size_t pn_len, struct quic_conn *conn) |
| 3975 | { |
| 3976 | if (type > QUIC_PACKET_TYPE_RETRY) |
| 3977 | return 0; |
| 3978 | |
| 3979 | /* #0 byte flags */ |
| 3980 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT | |
| 3981 | (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1); |
| 3982 | /* Version */ |
| 3983 | quic_write_uint32(buf, end, conn->version); |
| 3984 | *(*buf)++ = conn->dcid.len; |
| 3985 | /* Destination connection ID */ |
| 3986 | if (conn->dcid.len) { |
| 3987 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 3988 | *buf += conn->dcid.len; |
| 3989 | } |
| 3990 | /* Source connection ID */ |
| 3991 | *(*buf)++ = conn->scid.len; |
| 3992 | if (conn->scid.len) { |
| 3993 | memcpy(*buf, conn->scid.data, conn->scid.len); |
| 3994 | *buf += conn->scid.len; |
| 3995 | } |
| 3996 | |
| 3997 | return 1; |
| 3998 | } |
| 3999 | |
| 4000 | /* This function builds into <buf> buffer a QUIC long packet header whose size may be computed |
| 4001 | * in advance. This is the reponsability of the caller to check there is enough room in this |
| 4002 | * buffer to build a long header. |
| 4003 | * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded. |
| 4004 | */ |
| 4005 | static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end, |
| 4006 | size_t pn_len, struct quic_conn *conn) |
| 4007 | { |
| 4008 | /* #0 byte flags */ |
| 4009 | *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1); |
| 4010 | /* Destination connection ID */ |
| 4011 | if (conn->dcid.len) { |
| 4012 | memcpy(*buf, conn->dcid.data, conn->dcid.len); |
| 4013 | *buf += conn->dcid.len; |
| 4014 | } |
| 4015 | |
| 4016 | return 1; |
| 4017 | } |
| 4018 | |
| 4019 | /* Apply QUIC header protection to the packet with <buf> as first byte address, |
| 4020 | * <pn> as address of the Packet number field, <pnlen> being this field length |
| 4021 | * with <aead> as AEAD cipher and <key> as secret key. |
| 4022 | * Returns 1 if succeeded or 0 if failed. |
| 4023 | */ |
| 4024 | static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen, |
| 4025 | const EVP_CIPHER *aead, const unsigned char *key) |
| 4026 | { |
| 4027 | int i, ret, outlen; |
| 4028 | EVP_CIPHER_CTX *ctx; |
| 4029 | /* We need an IV of at least 5 bytes: one byte for bytes #0 |
| 4030 | * and at most 4 bytes for the packet number |
| 4031 | */ |
| 4032 | unsigned char mask[5] = {0}; |
| 4033 | |
| 4034 | ret = 0; |
| 4035 | ctx = EVP_CIPHER_CTX_new(); |
| 4036 | if (!ctx) |
| 4037 | return 0; |
| 4038 | |
| 4039 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) || |
| 4040 | !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) || |
| 4041 | !EVP_EncryptFinal_ex(ctx, mask, &outlen)) |
| 4042 | goto out; |
| 4043 | |
| 4044 | *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f); |
| 4045 | for (i = 0; i < pnlen; i++) |
| 4046 | pn[i] ^= mask[i + 1]; |
| 4047 | |
| 4048 | ret = 1; |
| 4049 | |
| 4050 | out: |
| 4051 | EVP_CIPHER_CTX_free(ctx); |
| 4052 | |
| 4053 | return ret; |
| 4054 | } |
| 4055 | |
| 4056 | /* Reduce the encoded size of <ack_frm> ACK frame removing the last |
| 4057 | * ACK ranges if needed to a value below <limit> in bytes. |
| 4058 | * Return 1 if succeeded, 0 if not. |
| 4059 | */ |
| 4060 | static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit) |
| 4061 | { |
| 4062 | size_t room, ack_delay_sz; |
| 4063 | |
| 4064 | ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay); |
| 4065 | /* A frame is made of 1 byte for the frame type. */ |
| 4066 | room = limit - ack_delay_sz - 1; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4067 | 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] | 4068 | return 0; |
| 4069 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4070 | 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] | 4071 | } |
| 4072 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4073 | /* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames |
| 4074 | * 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] | 4075 | * and <*len> the packet Length field initialized with the number of bytes already present |
| 4076 | * 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] | 4077 | * <headlen> is the number of bytes already present in this packet before building frames. |
| 4078 | * |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4079 | * Update consequently <*len> to reflect the size of these frames built |
| 4080 | * by this function. Also attach these frames to <pkt> QUIC packet. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4081 | * Return 1 if succeeded, 0 if not. |
| 4082 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4083 | static inline int qc_build_frms(struct quic_tx_packet *pkt, |
| 4084 | size_t room, size_t *len, size_t headlen, |
| 4085 | struct quic_enc_level *qel, |
| 4086 | struct quic_conn *conn) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4087 | { |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4088 | int ret; |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4089 | struct quic_frame *cf; |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 4090 | struct mt_list *tmp1, tmp2; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4091 | size_t remain = quic_path_prep_data(conn->path); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4092 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4093 | ret = 0; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4094 | if (*len > room || headlen > remain) |
| 4095 | return 0; |
| 4096 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4097 | /* If we are not probing we must take into an account the congestion |
| 4098 | * control window. |
| 4099 | */ |
| 4100 | if (!conn->tx.nb_pto_dgrams) |
| 4101 | room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen); |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4102 | TRACE_PROTO("************** frames build (headlen)", |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4103 | QUIC_EV_CONN_BCFRMS, conn->conn, &headlen); |
Frédéric Lécaille | c88df07 | 2021-07-27 11:43:11 +0200 | [diff] [blame] | 4104 | mt_list_for_each_entry_safe(cf, &qel->pktns->tx.frms, mt_list, tmp1, tmp2) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4105 | /* header length, data length, frame length. */ |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4106 | size_t hlen, dlen, dlen_sz, avail_room, flen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4107 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4108 | if (!room) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4109 | break; |
| 4110 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4111 | switch (cf->type) { |
| 4112 | case QUIC_FT_CRYPTO: |
| 4113 | TRACE_PROTO(" New CRYPTO frame build (room, len)", |
| 4114 | QUIC_EV_CONN_BCFRMS, conn->conn, &room, len); |
| 4115 | /* Compute the length of this CRYPTO frame header */ |
| 4116 | hlen = 1 + quic_int_getsize(cf->crypto.offset); |
| 4117 | /* Compute the data length of this CRyPTO frame. */ |
| 4118 | dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len); |
| 4119 | TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)", |
| 4120 | QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen); |
| 4121 | if (!dlen) |
| 4122 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4123 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4124 | pkt->cdata_len += dlen; |
| 4125 | /* CRYPTO frame length. */ |
| 4126 | flen = hlen + quic_int_getsize(dlen) + dlen; |
| 4127 | TRACE_PROTO(" CRYPTO frame length (flen)", |
| 4128 | QUIC_EV_CONN_BCFRMS, conn->conn, &flen); |
| 4129 | /* Add the CRYPTO data length and its encoded length to the packet |
| 4130 | * length and the length of this length. |
| 4131 | */ |
| 4132 | *len += flen; |
| 4133 | room -= flen; |
| 4134 | if (dlen == cf->crypto.len) { |
| 4135 | /* <cf> CRYPTO data have been consumed. */ |
| 4136 | MT_LIST_DELETE_SAFE(tmp1); |
| 4137 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4138 | } |
| 4139 | else { |
| 4140 | struct quic_frame *new_cf; |
| 4141 | |
| 4142 | new_cf = pool_alloc(pool_head_quic_frame); |
| 4143 | if (!new_cf) { |
| 4144 | TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn); |
| 4145 | return 0; |
| 4146 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4147 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4148 | new_cf->type = QUIC_FT_CRYPTO; |
| 4149 | new_cf->crypto.len = dlen; |
| 4150 | new_cf->crypto.offset = cf->crypto.offset; |
| 4151 | new_cf->crypto.qel = qel; |
| 4152 | LIST_APPEND(&pkt->frms, &new_cf->list); |
| 4153 | /* Consume <dlen> bytes of the current frame. */ |
| 4154 | cf->crypto.len -= dlen; |
| 4155 | cf->crypto.offset += dlen; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4156 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4157 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4158 | |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4159 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4160 | /* Note that these frames are accepted in short packets only without |
| 4161 | * "Length" packet field. Here, <*len> is used only to compute the |
| 4162 | * sum of the lengths of the already built frames for this packet. |
| 4163 | */ |
| 4164 | TRACE_PROTO(" New STREAM frame build (room, len)", |
| 4165 | QUIC_EV_CONN_BCFRMS, conn->conn, &room, len); |
| 4166 | /* Compute the length of this STREAM frame "header" made a all the field |
| 4167 | * excepting the variable ones. Note that +1 is for the type of this frame. |
| 4168 | */ |
| 4169 | hlen = 1 + quic_int_getsize(cf->stream.id) + |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4170 | ((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] | 4171 | /* Compute the data length of this STREAM frame. */ |
| 4172 | avail_room = room - hlen - *len; |
| 4173 | if ((ssize_t)avail_room <= 0) |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4174 | break; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4175 | |
| 4176 | if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) { |
| 4177 | dlen = max_available_room(avail_room, &dlen_sz); |
| 4178 | if (dlen > cf->stream.len) { |
| 4179 | dlen = cf->stream.len; |
| 4180 | } |
| 4181 | dlen_sz = quic_int_getsize(dlen); |
| 4182 | flen = hlen + dlen_sz + dlen; |
| 4183 | } |
| 4184 | else { |
| 4185 | dlen = QUIC_MIN(avail_room, cf->stream.len); |
| 4186 | flen = hlen + dlen; |
| 4187 | } |
| 4188 | TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)", |
| 4189 | QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen); |
| 4190 | TRACE_PROTO(" STREAM frame length (flen)", |
| 4191 | QUIC_EV_CONN_BCFRMS, conn->conn, &flen); |
| 4192 | /* Add the STREAM data length and its encoded length to the packet |
| 4193 | * length and the length of this length. |
| 4194 | */ |
| 4195 | *len += flen; |
| 4196 | room -= flen; |
| 4197 | if (dlen == cf->stream.len) { |
| 4198 | /* <cf> STREAM data have been consumed. */ |
| 4199 | MT_LIST_DELETE_SAFE(tmp1); |
| 4200 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4201 | } |
| 4202 | else { |
| 4203 | struct quic_frame *new_cf; |
| 4204 | |
| 4205 | new_cf = pool_zalloc(pool_head_quic_frame); |
| 4206 | if (!new_cf) { |
| 4207 | TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn); |
| 4208 | return 0; |
| 4209 | } |
| 4210 | |
| 4211 | new_cf->type = cf->type; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4212 | new_cf->stream.qcs = cf->stream.qcs; |
| 4213 | new_cf->stream.buf = cf->stream.buf; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4214 | new_cf->stream.id = cf->stream.id; |
| 4215 | if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) |
| 4216 | new_cf->stream.offset = cf->stream.offset; |
| 4217 | new_cf->stream.len = dlen; |
| 4218 | new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 4219 | /* FIN bit reset */ |
| 4220 | new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 4221 | new_cf->stream.data = cf->stream.data; |
| 4222 | LIST_APPEND(&pkt->frms, &new_cf->list); |
| 4223 | cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 4224 | /* Consume <dlen> bytes of the current frame. */ |
| 4225 | cf->stream.len -= dlen; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 4226 | cf->stream.offset.key += dlen; |
Frédéric Lécaille | a5b1b89 | 2021-08-25 17:56:22 +0200 | [diff] [blame] | 4227 | cf->stream.data += dlen; |
| 4228 | } |
Frédéric Lécaille | 0ac3851 | 2021-08-03 16:38:49 +0200 | [diff] [blame] | 4229 | break; |
| 4230 | |
| 4231 | default: |
| 4232 | flen = qc_frm_len(cf); |
| 4233 | BUG_ON(!flen); |
| 4234 | if (flen > room) |
| 4235 | continue; |
| 4236 | |
| 4237 | *len += flen; |
| 4238 | room -= flen; |
| 4239 | MT_LIST_DELETE_SAFE(tmp1); |
| 4240 | LIST_APPEND(&pkt->frms, &cf->list); |
| 4241 | break; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4242 | } |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4243 | ret = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4244 | } |
| 4245 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4246 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4247 | } |
| 4248 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4249 | /* This function evaluates if <pkt> packet may be built into a buffer with |
| 4250 | * <room> as available room. A valid packet should at least contain a valid |
| 4251 | * header and at least a frame. |
| 4252 | * To estimate the minimal space to build a packet, we consider the worst case: |
| 4253 | - there is not enough space to build ack-eliciting frames from |
| 4254 | qel->pktns->tx.frms. This is safe to consider this because when we build |
| 4255 | a packet we first build the ACK frames, then the ack-eliciting frames |
| 4256 | from qel->pktns->tx.frms only if there is enough space for these |
| 4257 | ack-eliciting frames, finally PING and PADDING frames if needed, |
| 4258 | - we have to ensure there is enough space to build an ACK frame if required, |
| 4259 | and a PING frame, even if we do not have to probe, |
| 4260 | - we also have to verify there is enough space to build a PADDING frame |
| 4261 | if needed, especially if there is no need to send an ACK frame. |
| 4262 | * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build |
| 4263 | * a valid packet). |
| 4264 | */ |
| 4265 | static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt, |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4266 | int ack, int nb_pto_dgrams, int cc, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4267 | struct quic_enc_level *qel, struct quic_conn *conn) |
| 4268 | { |
| 4269 | size_t minlen, token_fields_len; |
| 4270 | /* XXX FIXME XXX : ack delay not supported */ |
| 4271 | uint64_t ack_delay = 0; |
| 4272 | size_t ack_frm_len = 0; |
| 4273 | |
| 4274 | TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT, |
| 4275 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4276 | /* When we do not have to probe nor send acks either, and no immediate close |
| 4277 | * is required, we must take into |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4278 | * an account the data which have already been prepared and limit |
| 4279 | * the size of this packet. We will potentially build an ack-eliciting |
| 4280 | * packet. |
| 4281 | */ |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4282 | if (!nb_pto_dgrams && !ack && !cc) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4283 | size_t path_room; |
| 4284 | |
| 4285 | path_room = quic_path_prep_data(conn->path); |
| 4286 | if (room > path_room) |
| 4287 | room = path_room; |
| 4288 | } |
| 4289 | |
| 4290 | if (ack) |
| 4291 | /* A frame is made of 1 byte for the frame type. */ |
| 4292 | ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz; |
| 4293 | |
| 4294 | /* XXX FIXME XXX : token not supported */ |
| 4295 | token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0; |
| 4296 | /* Check there is enough room to build the header followed by a token, |
| 4297 | * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes |
| 4298 | * encryption tag is also taken into an account. Note that we have no |
| 4299 | * knowledge of the packet number for this packet. It must be atomically |
| 4300 | * incremented each time a packet is built. But before building a packet |
| 4301 | * we must estimate if it may be built if we do not want to consume a packet |
| 4302 | * number for nothing! Note that we add 1 byte more to |
| 4303 | * <minlen> to be able to build an ack-eliciting packet when probing without |
| 4304 | * ack-eliciting frames to send. In this case we need to add a 1-byte length |
| 4305 | * PING frame. |
| 4306 | */ |
| 4307 | minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1; |
| 4308 | if (pkt->type != QUIC_PACKET_TYPE_SHORT) |
| 4309 | minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len |
| 4310 | + token_fields_len; |
| 4311 | else |
| 4312 | minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len; |
| 4313 | |
| 4314 | /* Consider any PADDING frame to add */ |
| 4315 | if (objt_server(conn->conn->target) && |
| 4316 | pkt->type == QUIC_PACKET_TYPE_INITIAL && |
| 4317 | minlen < QUIC_INITIAL_PACKET_MINLEN) { |
| 4318 | /* Pad too short client Initial packet */ |
| 4319 | minlen += QUIC_INITIAL_PACKET_MINLEN - minlen; |
| 4320 | } |
| 4321 | else if (!ack) { |
| 4322 | /* Consider we will have to add the longest short PADDING frame to |
| 4323 | * protect a 1-byte length packet number. |
| 4324 | */ |
| 4325 | minlen += QUIC_PACKET_PN_MAXLEN - 1; |
| 4326 | } |
| 4327 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4328 | if (cc) { |
| 4329 | struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, }; |
| 4330 | struct quic_connection_close *cc = &cc_frm.connection_close; |
| 4331 | |
| 4332 | cc->error_code = conn->err_code; |
| 4333 | minlen += qc_frm_len(&cc_frm); |
| 4334 | } |
| 4335 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4336 | if (room < minlen) { |
| 4337 | TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT, |
| 4338 | conn->conn, NULL, NULL, &room); |
| 4339 | return 0; |
| 4340 | } |
| 4341 | |
| 4342 | return 1; |
| 4343 | } |
| 4344 | |
| 4345 | /* 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] | 4346 | * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption |
| 4347 | * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level, |
| 4348 | * filling the buffer with as much frames as possible. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4349 | * 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] | 4350 | * 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] | 4351 | * having returned from this function. |
| 4352 | * 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] | 4353 | * number field in this packet. <pn_len> will also have the packet number |
| 4354 | * length as value. |
| 4355 | * |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 4356 | * Always succeeds: this is the responsibility of the caller to ensure there is |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4357 | * enough room to build a packet. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4358 | */ |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4359 | static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end, |
| 4360 | size_t dglen, struct quic_tx_packet *pkt, |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4361 | int64_t pn, size_t *pn_len, unsigned char **buf_pn, |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4362 | int ack, int padding, int cc, int nb_pto_dgrams, |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4363 | struct quic_enc_level *qel, struct quic_conn *conn) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4364 | { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4365 | unsigned char *beg; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4366 | size_t len, len_sz, len_frms, padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4367 | struct quic_frame frm = { .type = QUIC_FT_CRYPTO, }; |
| 4368 | struct quic_frame ack_frm = { .type = QUIC_FT_ACK, }; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4369 | 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] | 4370 | size_t ack_frm_len, head_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4371 | int64_t largest_acked_pn; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4372 | int add_ping_frm; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4373 | |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4374 | /* Length field value with CRYPTO frames if present. */ |
| 4375 | len_frms = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4376 | beg = pos; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4377 | /* When not probing and not acking, and no immediate close is required, |
| 4378 | * reduce the size of this buffer to respect |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4379 | * the congestion controller window. So, we do not limit the size of this |
| 4380 | * packet if we have an ACK frame to send because an ACK frame is not |
| 4381 | * ack-eliciting. This size will be limited if we have ack-eliciting |
| 4382 | * frames to send from qel->pktns->tx.frms. |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4383 | */ |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4384 | if (!nb_pto_dgrams && !ack && !cc) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4385 | size_t path_room; |
| 4386 | |
| 4387 | path_room = quic_path_prep_data(conn->path); |
| 4388 | if (end - beg > path_room) |
| 4389 | end = beg + path_room; |
| 4390 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4391 | |
Frédéric Lécaille | e1aa0d3 | 2021-08-03 16:03:09 +0200 | [diff] [blame] | 4392 | 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] | 4393 | /* packet number length */ |
| 4394 | *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] | 4395 | /* Build the header */ |
| 4396 | if (pkt->type == QUIC_PACKET_TYPE_SHORT) |
Frédéric Lécaille | e1aa0d3 | 2021-08-03 16:03:09 +0200 | [diff] [blame] | 4397 | quic_build_packet_short_header(&pos, end, *pn_len, conn); |
| 4398 | else |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4399 | quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn); |
| 4400 | /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */ |
| 4401 | if (pkt->type == QUIC_PACKET_TYPE_INITIAL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4402 | *pos++ = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4403 | head_len = pos - beg; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4404 | /* Ensure there is enough room for the TLS encryption tag */ |
| 4405 | end -= QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4406 | /* Build an ACK frame if required. */ |
| 4407 | ack_frm_len = 0; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4408 | 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] | 4409 | ack_frm.tx_ack.ack_delay = 0; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 4410 | ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs; |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4411 | /* XXX BE CAREFUL XXX : here we reserved at least one byte for the |
| 4412 | * smallest frame (PING) and <*pn_len> more for the packet number. Note |
| 4413 | * that from here, we do not know if we will have to send a PING frame. |
| 4414 | * This will be decided after having computed the ack-eliciting frames |
| 4415 | * to be added to this packet. |
| 4416 | */ |
| 4417 | ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - 1 - *pn_len - pos); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4418 | if (!ack_frm_len) { |
| 4419 | ssize_t room = end - pos; |
| 4420 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 4421 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4422 | BUG_ON(1); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4423 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4424 | } |
| 4425 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4426 | /* Length field value without the ack-eliciting frames. */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4427 | len = ack_frm_len + *pn_len; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4428 | len_frms = 0; |
| 4429 | if (!cc && !MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) { |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4430 | ssize_t room = end - pos; |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4431 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4432 | /* Initialize the length of the frames built below to <len>. |
| 4433 | * If any frame could be successfully built by qc_build_frms(), |
| 4434 | * we will have len_frms > len. |
| 4435 | */ |
| 4436 | len_frms = len; |
| 4437 | if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn)) |
Frédéric Lécaille | ea60499 | 2020-12-24 13:01:37 +0100 | [diff] [blame] | 4438 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 4439 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4440 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4441 | |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4442 | /* Length (of the remaining data). Must not fail because, the buffer size |
| 4443 | * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes |
| 4444 | * for the encryption tag. It must be taken into an account for the length |
| 4445 | * of this packet. |
| 4446 | */ |
| 4447 | if (len_frms) |
| 4448 | len = len_frms + QUIC_TLS_TAG_LEN; |
| 4449 | else |
| 4450 | len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4451 | /* CONNECTION_CLOSE frame */ |
| 4452 | if (cc) { |
| 4453 | struct quic_connection_close *cc = &cc_frm.connection_close; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4454 | |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4455 | cc->error_code = conn->err_code; |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4456 | len += qc_frm_len(&cc_frm); |
| 4457 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4458 | add_ping_frm = 0; |
| 4459 | padding_len = 0; |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4460 | len_sz = quic_int_getsize(len); |
| 4461 | /* Add this packet size to <dglen> */ |
| 4462 | dglen += head_len + len_sz + len; |
| 4463 | if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) { |
| 4464 | /* This is a maximum padding size */ |
| 4465 | padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen; |
| 4466 | /* The length field value is of this packet is <len> + <padding_len> |
| 4467 | * the size of which may be greater than the initial computed size |
| 4468 | * <len_sz>. So, let's deduce the difference betwen these to packet |
| 4469 | * sizes from <padding_len>. |
| 4470 | */ |
| 4471 | padding_len -= quic_int_getsize(len + padding_len) - len_sz; |
| 4472 | len += padding_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4473 | } |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4474 | else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4475 | if (qel->pktns->tx.pto_probe) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4476 | /* 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] | 4477 | add_ping_frm = 1; |
| 4478 | len += 1; |
| 4479 | } |
| 4480 | /* 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] | 4481 | if (!ack_frm_len && !cc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4482 | len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len; |
| 4483 | } |
| 4484 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4485 | if (pkt->type != QUIC_PACKET_TYPE_SHORT) |
Frédéric Lécaille | e1aa0d3 | 2021-08-03 16:03:09 +0200 | [diff] [blame] | 4486 | quic_enc_int(&pos, end, len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4487 | |
| 4488 | /* Packet number field address. */ |
| 4489 | *buf_pn = pos; |
| 4490 | |
| 4491 | /* Packet number encoding. */ |
| 4492 | quic_packet_number_encode(&pos, end, pn, *pn_len); |
| 4493 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4494 | if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) { |
| 4495 | ssize_t room = end - pos; |
| 4496 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 4497 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4498 | BUG_ON(1); |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4499 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4500 | |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4501 | /* Ack-eliciting frames */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4502 | if (!LIST_ISEMPTY(&pkt->frms)) { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4503 | struct quic_frame *cf; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4504 | |
| 4505 | list_for_each_entry(cf, &pkt->frms, list) { |
Frédéric Lécaille | f252adb | 2021-08-03 17:01:25 +0200 | [diff] [blame] | 4506 | if (!qc_build_frm(&pos, end, cf, pkt, conn)) { |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4507 | ssize_t room = end - pos; |
| 4508 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 4509 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4510 | BUG_ON(1); |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4511 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | /* Build a PING frame if needed. */ |
| 4516 | if (add_ping_frm) { |
| 4517 | frm.type = QUIC_FT_PING; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4518 | if (!qc_build_frm(&pos, end, &frm, pkt, conn)) { |
| 4519 | ssize_t room = end - pos; |
| 4520 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 4521 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4522 | BUG_ON(1); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4523 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4524 | } |
| 4525 | |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4526 | /* Build a CONNECTION_CLOSE frame if needed. */ |
| 4527 | if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, conn)) { |
| 4528 | ssize_t room = end - pos; |
| 4529 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 4530 | conn->conn, NULL, NULL, &room); |
| 4531 | } |
| 4532 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4533 | /* Build a PADDING frame if needed. */ |
| 4534 | if (padding_len) { |
| 4535 | frm.type = QUIC_FT_PADDING; |
| 4536 | frm.padding.len = padding_len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4537 | if (!qc_build_frm(&pos, end, &frm, pkt, conn)) { |
| 4538 | ssize_t room = end - pos; |
| 4539 | TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, |
| 4540 | conn->conn, NULL, NULL, &room); |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4541 | BUG_ON(1); |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4542 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4543 | } |
| 4544 | |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4545 | /* Always reset this variable as this function has no idea |
| 4546 | * if it was set. It is handle by the loss detection timer. |
| 4547 | */ |
| 4548 | qel->pktns->tx.pto_probe = 0; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4549 | pkt->len = pos - beg; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4550 | } |
| 4551 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4552 | 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] | 4553 | { |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4554 | pkt->type = type; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4555 | pkt->len = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4556 | pkt->cdata_len = 0; |
| 4557 | pkt->in_flight_len = 0; |
| 4558 | LIST_INIT(&pkt->frms); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4559 | pkt->next = NULL; |
| 4560 | pkt->refcnt = 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4561 | } |
| 4562 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4563 | /* 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] | 4564 | static inline void free_quic_tx_packet(struct quic_tx_packet *pkt) |
| 4565 | { |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4566 | struct quic_frame *frm, *frmbak; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4567 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4568 | if (!pkt) |
| 4569 | return; |
| 4570 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4571 | list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4572 | LIST_DELETE(&frm->list); |
Frédéric Lécaille | 0ad0458 | 2021-07-27 14:51:54 +0200 | [diff] [blame] | 4573 | pool_free(pool_head_quic_frame, frm); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4574 | } |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4575 | quic_tx_packet_refdec(pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4576 | } |
| 4577 | |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4578 | /* Build a packet into <buf> packet buffer with <pkt_type> as packet |
| 4579 | * type for <qc> QUIC connection from <qel> encryption level. |
| 4580 | * Return -2 if the packet could not be allocated or encrypted for any reason, |
| 4581 | * -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] | 4582 | */ |
Frédéric Lécaille | 9445abc | 2021-08-04 10:49:51 +0200 | [diff] [blame] | 4583 | static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, |
| 4584 | const unsigned char *buf_end, |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4585 | struct quic_enc_level *qel, |
Frédéric Lécaille | 28f51fa | 2021-11-09 14:12:12 +0100 | [diff] [blame] | 4586 | struct quic_conn *qc, size_t dglen, int padding, |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4587 | int pkt_type, int ack, int nb_pto_dgrams, int cc, int *err) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4588 | { |
| 4589 | /* The pointer to the packet number field. */ |
| 4590 | unsigned char *buf_pn; |
| 4591 | unsigned char *beg, *end, *payload; |
| 4592 | int64_t pn; |
| 4593 | size_t pn_len, payload_len, aad_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4594 | struct quic_tls_ctx *tls_ctx; |
| 4595 | struct quic_tx_packet *pkt; |
| 4596 | |
Frédéric Lécaille | 133e8a7 | 2020-12-18 09:33:27 +0100 | [diff] [blame] | 4597 | TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4598 | *err = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4599 | pkt = pool_alloc(pool_head_quic_tx_packet); |
| 4600 | if (!pkt) { |
| 4601 | TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc->conn); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4602 | *err = -2; |
| 4603 | goto err; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4604 | } |
| 4605 | |
Frédéric Lécaille | 0e50e1b | 2021-08-03 15:03:35 +0200 | [diff] [blame] | 4606 | quic_tx_packet_init(pkt, pkt_type); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4607 | beg = *pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4608 | pn_len = 0; |
| 4609 | buf_pn = NULL; |
Frédéric Lécaille | 1fc5e16 | 2021-11-22 14:25:57 +0100 | [diff] [blame] | 4610 | if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, cc, qel, qc)) { |
Frédéric Lécaille | 4436cb6 | 2021-08-16 12:06:46 +0200 | [diff] [blame] | 4611 | *err = -1; |
| 4612 | goto err; |
| 4613 | } |
| 4614 | |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 4615 | /* Consume a packet number. */ |
| 4616 | pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1); |
Frédéric Lécaille | 66cbb82 | 2021-11-17 11:56:21 +0100 | [diff] [blame] | 4617 | qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn, |
| 4618 | ack, padding, cc, nb_pto_dgrams, qel, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4619 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4620 | end = beg + pkt->len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4621 | payload = buf_pn + pn_len; |
| 4622 | payload_len = end - payload; |
| 4623 | aad_len = payload - beg; |
| 4624 | |
| 4625 | tls_ctx = &qel->tls_ctx; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4626 | if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) { |
| 4627 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4628 | goto err; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4629 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4630 | |
| 4631 | end += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4632 | pkt->len += QUIC_TLS_TAG_LEN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4633 | if (!quic_apply_header_protection(beg, buf_pn, pn_len, |
| 4634 | tls_ctx->tx.hp, tls_ctx->tx.hp_key)) { |
| 4635 | TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4636 | *err = -2; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4637 | goto err; |
| 4638 | } |
| 4639 | |
Frédéric Lécaille | ca98a7f | 2021-11-10 17:30:15 +0100 | [diff] [blame] | 4640 | qc->tx.prep_bytes += pkt->len; |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4641 | /* Now that a correct packet is built, let us consume <*pos> buffer. */ |
| 4642 | *pos = end; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4643 | /* Attach the built packet to its tree. */ |
Frédéric Lécaille | a7348f6 | 2021-08-03 16:50:14 +0200 | [diff] [blame] | 4644 | pkt->pn_node.key = pn; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4645 | /* 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] | 4646 | if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) { |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4647 | pkt->in_flight_len = pkt->len; |
| 4648 | qc->path->prep_in_flight += pkt->len; |
Frédéric Lécaille | 04ffb66 | 2020-12-08 15:58:39 +0100 | [diff] [blame] | 4649 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4650 | pkt->pktns = qel->pktns; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4651 | TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt); |
| 4652 | |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4653 | return pkt; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4654 | |
| 4655 | err: |
| 4656 | free_quic_tx_packet(pkt); |
| 4657 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn); |
Frédéric Lécaille | c5b0c93 | 2021-07-06 16:35:52 +0200 | [diff] [blame] | 4658 | return NULL; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4659 | } |
| 4660 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4661 | /* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>. |
| 4662 | * Return the number of bytes which have been copied. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4663 | */ |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4664 | static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx, |
| 4665 | struct buffer *buf, size_t count, int flags) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4666 | { |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4667 | size_t try, done = 0; |
| 4668 | |
| 4669 | if (!conn_ctrl_ready(conn)) |
| 4670 | return 0; |
| 4671 | |
| 4672 | if (!fd_recv_ready(conn->handle.fd)) |
| 4673 | return 0; |
| 4674 | |
| 4675 | conn->flags &= ~CO_FL_WAIT_ROOM; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4676 | |
| 4677 | /* read the largest possible block. For this, we perform only one call |
| 4678 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4679 | * in which case we accept to do it once again. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4680 | */ |
| 4681 | while (count > 0) { |
| 4682 | try = b_contig_space(buf); |
| 4683 | if (!try) |
| 4684 | break; |
| 4685 | |
| 4686 | if (try > count) |
| 4687 | try = count; |
| 4688 | |
Frédéric Lécaille | fbe3b77 | 2021-03-03 16:23:44 +0100 | [diff] [blame] | 4689 | b_add(buf, try); |
| 4690 | done += try; |
| 4691 | count -= try; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 4695 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4696 | |
| 4697 | leave: |
| 4698 | return done; |
| 4699 | |
| 4700 | read0: |
| 4701 | conn_sock_read0(conn); |
| 4702 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4703 | |
| 4704 | /* Now a final check for a possible asynchronous low-level error |
| 4705 | * report. This can happen when a connection receives a reset |
| 4706 | * after a shutdown, both POLL_HUP and POLL_ERR are queued, and |
| 4707 | * we might have come from there by just checking POLL_HUP instead |
| 4708 | * of recv()'s return value 0, so we have no way to tell there was |
| 4709 | * an error without checking. |
| 4710 | */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 4711 | if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4712 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 4713 | goto leave; |
| 4714 | } |
| 4715 | |
| 4716 | |
| 4717 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 4718 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 4719 | * other pending data for example, but this flag is ignored at the moment. |
| 4720 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 4721 | * a second call may be performed. The connection's flags are updated with |
| 4722 | * whatever special event is detected (error, empty). The caller is responsible |
| 4723 | * for taking care of those events and avoiding the call if inappropriate. The |
| 4724 | * function does not call the connection's polling update function, so the caller |
| 4725 | * is responsible for this. It's up to the caller to update the buffer's contents |
| 4726 | * based on the return value. |
| 4727 | */ |
| 4728 | static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags) |
| 4729 | { |
| 4730 | ssize_t ret; |
| 4731 | size_t try, done; |
| 4732 | int send_flag; |
| 4733 | |
| 4734 | if (!conn_ctrl_ready(conn)) |
| 4735 | return 0; |
| 4736 | |
| 4737 | if (!fd_send_ready(conn->handle.fd)) |
| 4738 | return 0; |
| 4739 | |
| 4740 | done = 0; |
| 4741 | /* send the largest possible block. For this we perform only one call |
| 4742 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 4743 | * in which case we accept to do it once again. |
| 4744 | */ |
| 4745 | while (count) { |
| 4746 | try = b_contig_data(buf, done); |
| 4747 | if (try > count) |
| 4748 | try = count; |
| 4749 | |
| 4750 | send_flag = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 4751 | if (try < count || flags & CO_SFL_MSG_MORE) |
| 4752 | send_flag |= MSG_MORE; |
| 4753 | |
| 4754 | ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag, |
| 4755 | (struct sockaddr *)conn->dst, get_addr_len(conn->dst)); |
| 4756 | if (ret > 0) { |
| 4757 | count -= ret; |
| 4758 | done += ret; |
| 4759 | |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 4760 | /* A send succeeded, so we can consider ourself connected */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4761 | conn->flags |= CO_FL_WAIT_L4L6; |
| 4762 | /* if the system buffer is full, don't insist */ |
| 4763 | if (ret < try) |
| 4764 | break; |
| 4765 | } |
| 4766 | else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) { |
| 4767 | /* nothing written, we need to poll for write first */ |
| 4768 | fd_cant_send(conn->handle.fd); |
| 4769 | break; |
| 4770 | } |
| 4771 | else if (errno != EINTR) { |
| 4772 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 4773 | break; |
| 4774 | } |
| 4775 | } |
| 4776 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done) |
| 4777 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4778 | |
| 4779 | if (done > 0) { |
| 4780 | /* we count the total bytes sent, and the send rate for 32-byte |
| 4781 | * blocks. The reason for the latter is that freq_ctr are |
| 4782 | * limited to 4GB and that it's not enough per second. |
| 4783 | */ |
| 4784 | _HA_ATOMIC_ADD(&global.out_bytes, done); |
| 4785 | update_freq_ctr(&global.out_32bps, (done + 16) / 32); |
| 4786 | } |
| 4787 | return done; |
| 4788 | } |
| 4789 | |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 4790 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 4791 | * event subscriber <es> is not allowed to change from a previous call as long |
| 4792 | * as at least one event is still subscribed. The <event_type> must only be a |
| 4793 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 4794 | */ |
| 4795 | static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 4796 | { |
Frédéric Lécaille | 513b4f2 | 2021-09-20 15:23:17 +0200 | [diff] [blame] | 4797 | struct qcc *qcc = conn->qc->qcc; |
| 4798 | |
| 4799 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 4800 | BUG_ON(qcc->subs && qcc->subs != es); |
| 4801 | |
| 4802 | es->events |= event_type; |
| 4803 | qcc->subs = es; |
| 4804 | |
| 4805 | if (event_type & SUB_RETRY_RECV) |
| 4806 | TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn, qcc); |
| 4807 | |
| 4808 | if (event_type & SUB_RETRY_SEND) |
| 4809 | TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn, qcc); |
| 4810 | |
| 4811 | return 0; |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 4815 | * The <es> pointer is not allowed to differ from the one passed to the |
| 4816 | * subscribe() call. It always returns zero. |
| 4817 | */ |
| 4818 | static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
| 4819 | { |
| 4820 | return conn_unsubscribe(conn, xprt_ctx, event_type, es); |
| 4821 | } |
| 4822 | |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 4823 | /* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection |
| 4824 | * with <ssl_ctx> as SSL context inherited settings. Also set the transport |
| 4825 | * parameters of this session. |
| 4826 | * This is the responsibility of the caller to check the validity of all the |
| 4827 | * pointers passed as parameter to this function. |
| 4828 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to |
| 4829 | * CO_ER_SSL_NO_MEM. |
| 4830 | */ |
| 4831 | static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl, |
| 4832 | unsigned char *params, size_t params_len) |
| 4833 | { |
| 4834 | int retry; |
| 4835 | |
| 4836 | retry = 1; |
| 4837 | retry: |
| 4838 | *ssl = SSL_new(ssl_ctx); |
| 4839 | if (!*ssl) { |
| 4840 | if (!retry--) |
| 4841 | goto err; |
| 4842 | |
| 4843 | pool_gc(NULL); |
| 4844 | goto retry; |
| 4845 | } |
| 4846 | |
| 4847 | if (!SSL_set_quic_method(*ssl, &ha_quic_method) || |
| 4848 | !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) || |
| 4849 | !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) { |
| 4850 | goto err; |
| 4851 | |
| 4852 | SSL_free(*ssl); |
| 4853 | *ssl = NULL; |
| 4854 | if (!retry--) |
| 4855 | goto err; |
| 4856 | |
| 4857 | pool_gc(NULL); |
| 4858 | goto retry; |
| 4859 | } |
| 4860 | |
| 4861 | return 0; |
| 4862 | |
| 4863 | err: |
| 4864 | qc->conn->err_code = CO_ER_SSL_NO_MEM; |
| 4865 | return -1; |
| 4866 | } |
| 4867 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4868 | /* Initialize a QUIC connection (quic_conn struct) to be attached to <conn> |
| 4869 | * connection with <xprt_ctx> as address of the xprt context. |
| 4870 | * Returns 1 if succeeded, 0 if not. |
| 4871 | */ |
| 4872 | static int qc_conn_init(struct connection *conn, void **xprt_ctx) |
| 4873 | { |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4874 | struct ssl_sock_ctx *ctx; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4875 | |
| 4876 | TRACE_ENTER(QUIC_EV_CONN_NEW, conn); |
| 4877 | |
| 4878 | if (*xprt_ctx) |
| 4879 | goto out; |
| 4880 | |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 4881 | ctx = pool_zalloc(pool_head_quic_conn_ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4882 | if (!ctx) { |
| 4883 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 4884 | goto err; |
| 4885 | } |
| 4886 | |
| 4887 | ctx->wait_event.tasklet = tasklet_new(); |
| 4888 | if (!ctx->wait_event.tasklet) { |
| 4889 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 4890 | goto err; |
| 4891 | } |
| 4892 | |
| 4893 | ctx->wait_event.tasklet->process = quic_conn_io_cb; |
| 4894 | ctx->wait_event.tasklet->context = ctx; |
| 4895 | ctx->wait_event.events = 0; |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 4896 | HA_ATOMIC_STORE(&ctx->conn, conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4897 | ctx->subs = NULL; |
| 4898 | ctx->xprt_ctx = NULL; |
| 4899 | |
| 4900 | ctx->xprt = xprt_get(XPRT_QUIC); |
| 4901 | if (objt_server(conn->target)) { |
| 4902 | /* Server */ |
| 4903 | struct server *srv = __objt_server(conn->target); |
| 4904 | unsigned char dcid[QUIC_CID_LEN]; |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 4905 | struct quic_conn *qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4906 | int ssl_err, ipv4; |
| 4907 | |
| 4908 | ssl_err = SSL_ERROR_NONE; |
| 4909 | if (RAND_bytes(dcid, sizeof dcid) != 1) |
| 4910 | goto err; |
| 4911 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4912 | ipv4 = conn->dst->ss_family == AF_INET; |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4913 | qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4, |
Frédéric Lécaille | 6b19764 | 2021-07-06 16:25:08 +0200 | [diff] [blame] | 4914 | dcid, sizeof dcid, NULL, 0, 0, srv); |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4915 | if (qc == NULL) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4916 | goto err; |
| 4917 | |
Frédéric Lécaille | 6de7287 | 2021-06-11 15:44:24 +0200 | [diff] [blame] | 4918 | /* Insert our SCID, the connection ID for the QUIC client. */ |
| 4919 | ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len); |
| 4920 | |
| 4921 | conn->qc = qc; |
| 4922 | qc->conn = conn; |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 4923 | if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1, |
| 4924 | dcid, sizeof dcid, 0)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4925 | goto err; |
| 4926 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 4927 | qc->rx.params = srv->quic_params; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4928 | /* Copy the initial source connection ID. */ |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 4929 | quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid); |
| 4930 | qc->enc_params_len = |
| 4931 | quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params, |
| 4932 | &qc->rx.params, 0); |
| 4933 | if (!qc->enc_params_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4934 | goto err; |
| 4935 | |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 4936 | if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl, |
| 4937 | qc->enc_params, qc->enc_params_len) == -1) |
| 4938 | goto err; |
| 4939 | |
Frédéric Lécaille | a5fe49f | 2021-06-04 11:52:35 +0200 | [diff] [blame] | 4940 | SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4941 | SSL_set_connect_state(ctx->ssl); |
| 4942 | ssl_err = SSL_do_handshake(ctx->ssl); |
| 4943 | if (ssl_err != 1) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 4944 | int st; |
| 4945 | |
| 4946 | st = HA_ATOMIC_LOAD(&qc->state); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4947 | ssl_err = SSL_get_error(ctx->ssl, ssl_err); |
| 4948 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 4949 | TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4950 | } |
| 4951 | else { |
Frédéric Lécaille | eed7a7d | 2021-08-18 09:16:01 +0200 | [diff] [blame] | 4952 | TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4953 | goto err; |
| 4954 | } |
| 4955 | } |
| 4956 | } |
| 4957 | else if (objt_listener(conn->target)) { |
| 4958 | /* Listener */ |
| 4959 | struct bind_conf *bc = __objt_listener(conn->target)->bind_conf; |
Frédéric Lécaille | 1e1aad4 | 2021-05-27 14:57:09 +0200 | [diff] [blame] | 4960 | struct quic_conn *qc = ctx->conn->qc; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4961 | |
Frédéric Lécaille | d4d6aa7 | 2021-09-03 15:56:18 +0200 | [diff] [blame] | 4962 | ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid); |
Frédéric Lécaille | 75dd2b7 | 2021-09-28 09:51:23 +0200 | [diff] [blame] | 4963 | if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl, |
| 4964 | qc->enc_params, qc->enc_params_len) == -1) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4965 | goto err; |
| 4966 | |
| 4967 | SSL_set_accept_state(ctx->ssl); |
| 4968 | } |
| 4969 | |
Frédéric Lécaille | d77c50b | 2021-11-23 15:59:59 +0100 | [diff] [blame] | 4970 | HA_ATOMIC_STORE(xprt_ctx, ctx); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4971 | |
| 4972 | /* Leave init state and start handshake */ |
| 4973 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4974 | |
| 4975 | out: |
| 4976 | TRACE_LEAVE(QUIC_EV_CONN_NEW, conn); |
| 4977 | |
| 4978 | return 0; |
| 4979 | |
| 4980 | err: |
Willy Tarreau | 7deb28c | 2021-05-10 07:40:27 +0200 | [diff] [blame] | 4981 | if (ctx && ctx->wait_event.tasklet) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4982 | tasklet_free(ctx->wait_event.tasklet); |
| 4983 | pool_free(pool_head_quic_conn_ctx, ctx); |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 4984 | TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 4985 | return -1; |
| 4986 | } |
| 4987 | |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4988 | /* Start the QUIC transport layer */ |
| 4989 | static int qc_xprt_start(struct connection *conn, void *ctx) |
| 4990 | { |
| 4991 | struct quic_conn *qc; |
Frédéric Lécaille | 1eaec33 | 2021-06-04 14:59:59 +0200 | [diff] [blame] | 4992 | struct ssl_sock_ctx *qctx = ctx; |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 4993 | |
| 4994 | qc = conn->qc; |
| 4995 | if (!quic_conn_init_timer(qc)) { |
| 4996 | TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn); |
| 4997 | return 0; |
| 4998 | } |
| 4999 | |
| 5000 | tasklet_wakeup(qctx->wait_event.tasklet); |
| 5001 | return 1; |
| 5002 | } |
| 5003 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5004 | /* transport-layer operations for QUIC connections. */ |
| 5005 | static struct xprt_ops ssl_quic = { |
Amaury Denoyelle | 414cac5 | 2021-09-22 11:14:37 +0200 | [diff] [blame] | 5006 | .close = quic_close, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5007 | .snd_buf = quic_conn_from_buf, |
| 5008 | .rcv_buf = quic_conn_to_buf, |
Frédéric Lécaille | 422a39c | 2021-03-03 17:28:34 +0100 | [diff] [blame] | 5009 | .subscribe = quic_conn_subscribe, |
| 5010 | .unsubscribe = quic_conn_unsubscribe, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5011 | .init = qc_conn_init, |
Frédéric Lécaille | 3d77fa7 | 2021-05-31 09:30:14 +0200 | [diff] [blame] | 5012 | .start = qc_xprt_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5013 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
| 5014 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 5015 | .get_alpn = ssl_sock_get_alpn, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5016 | .name = "QUIC", |
| 5017 | }; |
| 5018 | |
| 5019 | __attribute__((constructor)) |
| 5020 | static void __quic_conn_init(void) |
| 5021 | { |
| 5022 | ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods"); |
| 5023 | xprt_register(XPRT_QUIC, &ssl_quic); |
| 5024 | } |
| 5025 | |
| 5026 | __attribute__((destructor)) |
| 5027 | static void __quic_conn_deinit(void) |
| 5028 | { |
| 5029 | BIO_meth_free(ha_quic_meth); |
| 5030 | } |
| 5031 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5032 | /* Read all the QUIC packets found in <buf> from QUIC connection with <owner> |
| 5033 | * as owner calling <func> function. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5034 | * 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] | 5035 | */ |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5036 | static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5037 | struct sockaddr_storage *saddr, qpkt_read_func *func) |
| 5038 | { |
| 5039 | unsigned char *pos; |
| 5040 | const unsigned char *end; |
| 5041 | struct quic_dgram_ctx dgram_ctx = { |
| 5042 | .dcid_node = NULL, |
| 5043 | .owner = owner, |
| 5044 | }; |
| 5045 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5046 | pos = (unsigned char *)b_head(buf); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5047 | end = pos + len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5048 | do { |
| 5049 | int ret; |
| 5050 | struct quic_rx_packet *pkt; |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 5051 | size_t pkt_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5052 | |
Willy Tarreau | e449893 | 2021-03-22 21:13:05 +0100 | [diff] [blame] | 5053 | pkt = pool_zalloc(pool_head_quic_rx_packet); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5054 | if (!pkt) |
| 5055 | goto err; |
| 5056 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5057 | quic_rx_packet_refinc(pkt); |
| 5058 | ret = func(&pos, end, pkt, &dgram_ctx, saddr); |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 5059 | pkt_len = pkt->len; |
Frédéric Lécaille | 310d1bd | 2021-09-22 15:10:49 +0200 | [diff] [blame] | 5060 | quic_rx_packet_refdec(pkt); |
Frédéric Lécaille | 865b078 | 2021-09-23 07:33:20 +0200 | [diff] [blame] | 5061 | if (ret == -1 && !pkt_len) |
| 5062 | /* If the packet length could not be found, we cannot continue. */ |
| 5063 | break; |
| 5064 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5065 | } while (pos < end); |
| 5066 | |
| 5067 | /* Increasing the received bytes counter by the UDP datagram length |
| 5068 | * if this datagram could be associated to a connection. |
| 5069 | */ |
| 5070 | if (dgram_ctx.qc) |
| 5071 | dgram_ctx.qc->rx.bytes += len; |
| 5072 | |
| 5073 | return pos - (unsigned char *)buf; |
| 5074 | |
| 5075 | err: |
| 5076 | return -1; |
| 5077 | } |
| 5078 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 5079 | ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5080 | struct sockaddr_storage *saddr) |
| 5081 | { |
| 5082 | return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv); |
| 5083 | } |
| 5084 | |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5085 | /* Function to automatically activate QUIC traces on stdout. |
| 5086 | * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES. |
| 5087 | * Main use for now is in the docker image for QUIC interop testing. |
| 5088 | */ |
| 5089 | static void quic_init_stdout_traces(void) |
| 5090 | { |
| 5091 | #ifdef ENABLE_QUIC_STDOUT_TRACES |
| 5092 | trace_quic.sink = sink_find("stdout"); |
| 5093 | trace_quic.level = TRACE_LEVEL_DEVELOPER; |
Amaury Denoyelle | 118b2cb | 2021-11-25 16:05:16 +0100 | [diff] [blame] | 5094 | trace_quic.state = TRACE_STATE_RUNNING; |
| 5095 | #endif |
| 5096 | } |
| 5097 | INITCALL0(STG_INIT, quic_init_stdout_traces); |
| 5098 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5099 | /* |
| 5100 | * Local variables: |
| 5101 | * c-indent-level: 8 |
| 5102 | * c-basic-offset: 8 |
| 5103 | * End: |
| 5104 | */ |