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